Subversion Repositories HomeAutomation

Rev

Rev 1954 | Rev 1962 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1651 runge 1
/*
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 */
20
 
21
#include "Socket.h"
22
 
23
#include <v8-debug.h>
24
#include <stdio.h>
25
 
26
#include <boost/lexical_cast.hpp>
27
 
28
#include "vm/Manager.h"
29
#include "net/Manager.h"
1959 runge 30
#include "common/common.h"
31
#include "common/log.h"
32
#include "common/exception.h"
1651 runge 33
 
34
namespace atom {
35
namespace vm {
36
namespace plugin {
37
 
1959 runge 38
static const std::string log_module_ = "vm::plugin::socket";
1651 runge 39
 
40
Socket::Socket(boost::asio::io_service& io_service) : Plugin(io_service)
41
{
1959 runge 42
  LOG_DEBUG_ENTER;
43
 
44
  this->name_ = "socket";
45
 
46
  this->ExportFunction("SocketExport_StartServer", Socket::Export_StartServer);
47
  this->ExportFunction("SocketExport_Connect",     Socket::Export_Connect);
48
  this->ExportFunction("SocketExport_StopServer",  Socket::Export_StopServer);
49
  this->ExportFunction("SocketExport_Disconnect",  Socket::Export_Disconnect);
50
  this->ExportFunction("SocketExport_Send",        Socket::Export_Send);
51
 
52
  LOG_DEBUG_EXIT;
1651 runge 53
}
54
 
55
Socket::~Socket()
56
{
1959 runge 57
  LOG_DEBUG_ENTER;
58
  LOG_DEBUG_EXIT;
1651 runge 59
}
60
 
61
void Socket::InitializeDone()
62
{
1959 runge 63
  LOG_DEBUG_ENTER;
64
 
65
  Plugin::InitializeDone();
66
 
67
  this->ImportFunction("Socket_OnNewData");
68
  this->ImportFunction("Socket_OnNewState");
69
 
70
  net::Manager::Instance()->ConnectSlots(net::Manager::SignalOnNewState::slot_type(&Socket::SlotOnNewState, this, _1, _2, _3).track(this->tracker_), net::Manager::SignalOnNewData::slot_type(&Socket::SlotOnNewData, this, _1, _2, _3).track(this->tracker_));
71
 
72
  LOG_DEBUG_EXIT;
1651 runge 73
}
74
 
75
void Socket::CallOutput(unsigned int request_id, std::string output)
76
{
1959 runge 77
  LOG_DEBUG_ENTER;
78
 
79
  log::Info(log_module_, output);
80
 
81
  LOG_DEBUG_EXIT;
1651 runge 82
}
83
 
1959 runge 84
void Socket::SlotOnNewData(net::SocketId socket_id, net::SocketId server_id, common::Byteset data)
1651 runge 85
{
1959 runge 86
  LOG_DEBUG_ENTER;
87
 
88
  log::Debug(log_module_, data.ToDebugString());
89
 
90
  this->io_service_.post(boost::bind(&Socket::SlotOnNewDataHandler, this, socket_id, server_id, data));
91
 
92
  LOG_DEBUG_EXIT;
1651 runge 93
}
94
 
1959 runge 95
void Socket::SlotOnNewState(net::SocketId socket_id, net::SocketId server_id, net::ClientState client_state)
1651 runge 96
{
1959 runge 97
  LOG_DEBUG_ENTER;
98
 
99
  this->io_service_.post(boost::bind(&Socket::SlotOnNewStateHandler, this, socket_id, server_id, client_state));
100
 
101
  LOG_DEBUG_EXIT;
1651 runge 102
}
103
 
1959 runge 104
void Socket::SlotOnNewDataHandler(net::SocketId socket_id, net::SocketId server_id, common::Byteset data)
1651 runge 105
{
1959 runge 106
  LOG_DEBUG_ENTER;
107
  ATOM_VM_PLUGIN_SCOPE;
108
 
109
  try
110
  {
1651 runge 111
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
112
 
1959 runge 113
    if (data.GetSize() == 0)
114
    {
115
        atom::log::Error(log_module_, "Got empty data!");
116
        return;
117
    }
118
 
119
    arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(socket_id)));
120
    arguments->push_back(v8::String::New(data.ToCharString().c_str()));
121
 
122
    if (!this->Call(socket_id, "Socket_OnNewData", arguments))
123
    {
124
      atom::log::Error(log_module_, "%s failed!", __FUNCTION__);
125
    }
126
  }
127
  catch (std::exception& exception)
128
  {
129
    atom::log::Exception(log_module_, exception);
130
  }
131
 
132
  LOG_DEBUG_EXIT;
1651 runge 133
}
134
 
1959 runge 135
void Socket::SlotOnNewStateHandler(net::SocketId socket_id, net::SocketId server_id, net::ClientState client_state)
1651 runge 136
{
1959 runge 137
  LOG_DEBUG_ENTER;
138
  ATOM_VM_PLUGIN_SCOPE;
139
 
140
  try
141
  {
142
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
1651 runge 143
 
1959 runge 144
    arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(socket_id)));
145
    arguments->push_back(v8::Uint32::New((unsigned int)client_state));
1651 runge 146
 
1959 runge 147
    if (!this->Call(socket_id, "Socket_OnNewState", arguments))
148
    {
149
      atom::log::Error(log_module_, "%s failed!", __FUNCTION__);
150
    }
151
  }
152
  catch (std::exception& exception)
153
  {
154
    atom::log::Exception(log_module_, exception);
155
  }
156
 
157
  LOG_DEBUG_EXIT;
158
}
159
 
160
Value Socket::Export_StartServer(const v8::Arguments& args)
161
{
162
  LOG_DEBUG_ENTER
163
  ATOM_VM_PLUGIN_SCOPE;
164
 
165
  net::SocketId socket_id = 0;
166
 
167
  try
168
  {
1651 runge 169
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
170
 
1959 runge 171
    if (args.Length() < 1)
172
    {
173
      throw atom::exception::missing_in_param();
174
    }
175
 
176
    socket_id = net::Manager::Instance()->StartServer(net::PROTOCOL_TCP, args[0]->Uint32Value());
177
  }
178
  catch (std::exception& exception)
179
  {
180
    atom::log::Exception(log_module_, exception);
181
    return handle_scope.Close(v8::Boolean::New(false));
182
  }
183
 
184
  LOG_DEBUG_EXIT;
185
  return handle_scope.Close(v8::Integer::New(socket_id));
1651 runge 186
}
187
 
188
Value Socket::Export_Connect(const v8::Arguments& args)
189
{
1959 runge 190
  LOG_DEBUG_ENTER;
191
  ATOM_VM_PLUGIN_SCOPE;
192
 
193
  net::SocketId socket_id = 0;
194
 
195
  try
196
  {
197
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
1651 runge 198
 
199
    if (args.Length() < 2)
200
    {
1959 runge 201
      throw atom::exception::missing_in_param();
1651 runge 202
    }
203
 
204
    v8::String::AsciiValue address(args[0]);
205
 
1959 runge 206
    socket_id = net::Manager::Instance()->Connect(net::PROTOCOL_TCP, std::string(*address), args[1]->Uint32Value());
207
  }
208
  catch (std::exception& exception)
209
  {
210
    atom::log::Exception(log_module_, exception);
211
    return handle_scope.Close(v8::Boolean::New(false));
212
  }
213
 
214
  LOG_DEBUG_EXIT;
215
  return handle_scope.Close(v8::Integer::New(socket_id));
216
}
217
 
218
Value Socket::Export_StopServer(const v8::Arguments& args)
219
{
220
  LOG_DEBUG_EXIT;
221
  ATOM_VM_PLUGIN_SCOPE;
222
 
223
  try
224
  {
225
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
226
 
227
    if (args.Length() < 1)
1940 runge 228
    {
1959 runge 229
      throw atom::exception::missing_in_param();
1940 runge 230
    }
1959 runge 231
 
232
    net::Manager::Instance()->StopServer(args[0]->Uint32Value());
233
  }
234
  catch (std::exception& exception)
235
  {
236
    atom::log::Exception(log_module_, exception);
237
    return handle_scope.Close(v8::Boolean::New(false));
238
  }
1940 runge 239
 
1959 runge 240
  LOG_DEBUG_EXIT;
241
  return handle_scope.Close(v8::Boolean::New(true));
1651 runge 242
}
243
 
244
Value Socket::Export_Disconnect(const v8::Arguments& args)
245
{
1959 runge 246
  LOG_DEBUG_EXIT;
247
  ATOM_VM_PLUGIN_SCOPE;
248
 
249
  try
250
  {
251
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
1651 runge 252
 
253
    if (args.Length() < 1)
254
    {
1959 runge 255
      throw atom::exception::missing_in_param();
1651 runge 256
    }
257
 
258
    net::Manager::Instance()->Disconnect(args[0]->Uint32Value());
1959 runge 259
  }
260
  catch (std::exception& exception)
261
  {
262
    atom::log::Exception(log_module_, exception);
263
    return handle_scope.Close(v8::Boolean::New(false));
264
  }
265
 
266
  LOG_DEBUG_EXIT;
267
  return handle_scope.Close(v8::Boolean::New(true));
1651 runge 268
}
269
 
270
Value Socket::Export_Send(const v8::Arguments& args)
271
{
1959 runge 272
  LOG_DEBUG_EXIT;
273
  ATOM_VM_PLUGIN_SCOPE;
274
 
275
  try
276
  {
277
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
1651 runge 278
 
279
    if (args.Length() < 2)
280
    {
1959 runge 281
      throw atom::exception::missing_in_param();
1651 runge 282
    }
283
 
284
    v8::String::AsciiValue data(args[1]);
285
 
286
    net::Manager::Instance()->SendTo(args[0]->Uint32Value(), std::string(*data));
1959 runge 287
  }
288
  catch (std::exception& exception)
289
  {
290
    atom::log::Exception(log_module_, exception);
291
    return handle_scope.Close(v8::Boolean::New(false));
292
  }
293
 
294
  LOG_DEBUG_EXIT;
295
  return handle_scope.Close(v8::Boolean::New(true));
1651 runge 296
}
297
 
298
}; // namespace plugin
299
}; // namespace vm
300
}; // namespace atom