Subversion Repositories HomeAutomation

Rev

Rev 1987 | 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");
1986 runge 68
  this->ImportFunction("Socket_OnNewClient");
1959 runge 69
  this->ImportFunction("Socket_OnNewState");
70
 
1986 runge 71
  net::Manager::Instance()->ConnectSlots(net::Manager::SignalOnNewState::slot_type(&Socket::SlotOnNewState, this, _1, _2).track(this->tracker_), net::Manager::SignalOnNewClient::slot_type(&Socket::SlotOnNewClient, this, _1, _2).track(this->tracker_), net::Manager::SignalOnNewData::slot_type(&Socket::SlotOnNewData, this, _1, _2).track(this->tracker_));
1959 runge 72
 
73
  LOG_DEBUG_EXIT;
1651 runge 74
}
75
 
76
void Socket::CallOutput(unsigned int request_id, std::string output)
77
{
1962 runge 78
  //LOG_DEBUG_ENTER;
1959 runge 79
 
80
  log::Info(log_module_, output);
81
 
1962 runge 82
  //LOG_DEBUG_EXIT;
1651 runge 83
}
84
 
1986 runge 85
void Socket::SlotOnNewData(net::SocketId id, common::Byteset data)
1651 runge 86
{
1959 runge 87
  LOG_DEBUG_ENTER;
88
 
1986 runge 89
  this->io_service_.post(boost::bind(&Socket::SlotOnNewDataHandler, this, id, data));
1959 runge 90
 
91
  LOG_DEBUG_EXIT;
1651 runge 92
}
93
 
1986 runge 94
void Socket::SlotOnNewClient(net::SocketId id, net::SocketId server_id)
1651 runge 95
{
1959 runge 96
  LOG_DEBUG_ENTER;
97
 
1986 runge 98
  this->io_service_.post(boost::bind(&Socket::SlotOnNewClientHandler, this, id, server_id));
1959 runge 99
 
100
  LOG_DEBUG_EXIT;
1651 runge 101
}
102
 
1986 runge 103
void Socket::SlotOnNewState(net::SocketId id, net::ClientState client_state)
1651 runge 104
{
1959 runge 105
  LOG_DEBUG_ENTER;
1986 runge 106
 
107
  this->io_service_.post(boost::bind(&Socket::SlotOnNewStateHandler, this, id, client_state));
108
 
109
  LOG_DEBUG_EXIT;
110
}
111
 
112
void Socket::SlotOnNewDataHandler(net::SocketId id, common::Byteset data)
113
{
114
  LOG_DEBUG_ENTER;
1959 runge 115
  ATOM_VM_PLUGIN_SCOPE;
116
 
117
  try
118
  {
1651 runge 119
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
120
 
1987 runge 121
    if (data.size() == 0)
1959 runge 122
    {
123
        atom::log::Error(log_module_, "Got empty data!");
124
        return;
125
    }
126
 
1986 runge 127
    arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(id)));
1987 runge 128
    arguments->push_back(v8::String::New(std::string(data.begin(), data.end()).data()));
1959 runge 129
 
1986 runge 130
    if (!this->Call(id, "Socket_OnNewData", arguments))
1959 runge 131
    {
132
      atom::log::Error(log_module_, "%s failed!", __FUNCTION__);
133
    }
134
  }
135
  catch (std::exception& exception)
136
  {
137
    atom::log::Exception(log_module_, exception);
138
  }
139
 
140
  LOG_DEBUG_EXIT;
1651 runge 141
}
142
 
1986 runge 143
void Socket::SlotOnNewClientHandler(net::SocketId id, net::SocketId server_id)
1651 runge 144
{
1959 runge 145
  LOG_DEBUG_ENTER;
146
  ATOM_VM_PLUGIN_SCOPE;
1986 runge 147
 
148
  try
149
  {
150
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
151
 
152
    arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(id)));
153
    arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(server_id)));
154
 
155
    if (!this->Call(id, "Socket_OnNewClient", arguments))
156
    {
157
      atom::log::Error(log_module_, "%s failed!", __FUNCTION__);
158
    }
159
  }
160
  catch (std::exception& exception)
161
  {
162
    atom::log::Exception(log_module_, exception);
163
  }
164
 
165
  LOG_DEBUG_EXIT;
166
}
1959 runge 167
 
1986 runge 168
 
169
void Socket::SlotOnNewStateHandler(net::SocketId id, net::ClientState client_state)
170
{
171
  LOG_DEBUG_ENTER;
172
  ATOM_VM_PLUGIN_SCOPE;
173
 
1959 runge 174
  try
175
  {
176
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
1651 runge 177
 
1986 runge 178
    arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(id)));
1959 runge 179
    arguments->push_back(v8::Uint32::New((unsigned int)client_state));
1651 runge 180
 
1986 runge 181
    if (!this->Call(id, "Socket_OnNewState", arguments))
1959 runge 182
    {
183
      atom::log::Error(log_module_, "%s failed!", __FUNCTION__);
184
    }
185
  }
186
  catch (std::exception& exception)
187
  {
188
    atom::log::Exception(log_module_, exception);
189
  }
190
 
191
  LOG_DEBUG_EXIT;
192
}
193
 
194
Value Socket::Export_StartServer(const v8::Arguments& args)
195
{
196
  LOG_DEBUG_ENTER
197
  ATOM_VM_PLUGIN_SCOPE;
198
 
199
  net::SocketId socket_id = 0;
200
 
201
  try
202
  {
1651 runge 203
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
204
 
1959 runge 205
    if (args.Length() < 1)
206
    {
207
      throw atom::exception::missing_in_param();
208
    }
209
 
1989 runge 210
    socket_id = net::Manager::Instance()->StartServer(net::TRANSPORT_PROTOCOL_TCP, args[0]->Uint32Value());
1959 runge 211
  }
212
  catch (std::exception& exception)
213
  {
214
    atom::log::Exception(log_module_, exception);
215
    return handle_scope.Close(v8::Boolean::New(false));
216
  }
217
 
218
  LOG_DEBUG_EXIT;
219
  return handle_scope.Close(v8::Integer::New(socket_id));
1651 runge 220
}
221
 
222
Value Socket::Export_Connect(const v8::Arguments& args)
223
{
1959 runge 224
  LOG_DEBUG_ENTER;
225
  ATOM_VM_PLUGIN_SCOPE;
226
 
227
  net::SocketId socket_id = 0;
228
 
229
  try
230
  {
231
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
1651 runge 232
 
233
    if (args.Length() < 2)
234
    {
1959 runge 235
      throw atom::exception::missing_in_param();
1651 runge 236
    }
237
 
238
    v8::String::AsciiValue address(args[0]);
239
 
1989 runge 240
    socket_id = net::Manager::Instance()->Connect(net::TRANSPORT_PROTOCOL_TCP, std::string(*address), args[1]->Uint32Value());
1959 runge 241
  }
242
  catch (std::exception& exception)
243
  {
244
    atom::log::Exception(log_module_, exception);
245
    return handle_scope.Close(v8::Boolean::New(false));
246
  }
247
 
248
  LOG_DEBUG_EXIT;
249
  return handle_scope.Close(v8::Integer::New(socket_id));
250
}
251
 
252
Value Socket::Export_StopServer(const v8::Arguments& args)
253
{
254
  LOG_DEBUG_EXIT;
255
  ATOM_VM_PLUGIN_SCOPE;
256
 
257
  try
258
  {
259
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
260
 
261
    if (args.Length() < 1)
1940 runge 262
    {
1959 runge 263
      throw atom::exception::missing_in_param();
1940 runge 264
    }
1959 runge 265
 
266
    net::Manager::Instance()->StopServer(args[0]->Uint32Value());
267
  }
268
  catch (std::exception& exception)
269
  {
270
    atom::log::Exception(log_module_, exception);
271
    return handle_scope.Close(v8::Boolean::New(false));
272
  }
1940 runge 273
 
1959 runge 274
  LOG_DEBUG_EXIT;
275
  return handle_scope.Close(v8::Boolean::New(true));
1651 runge 276
}
277
 
278
Value Socket::Export_Disconnect(const v8::Arguments& args)
279
{
1959 runge 280
  LOG_DEBUG_EXIT;
281
  ATOM_VM_PLUGIN_SCOPE;
282
 
283
  try
284
  {
285
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
1651 runge 286
 
287
    if (args.Length() < 1)
288
    {
1959 runge 289
      throw atom::exception::missing_in_param();
1651 runge 290
    }
291
 
292
    net::Manager::Instance()->Disconnect(args[0]->Uint32Value());
1959 runge 293
  }
294
  catch (std::exception& exception)
295
  {
296
    atom::log::Exception(log_module_, exception);
297
    return handle_scope.Close(v8::Boolean::New(false));
298
  }
299
 
300
  LOG_DEBUG_EXIT;
301
  return handle_scope.Close(v8::Boolean::New(true));
1651 runge 302
}
303
 
304
Value Socket::Export_Send(const v8::Arguments& args)
305
{
1959 runge 306
  LOG_DEBUG_EXIT;
307
  ATOM_VM_PLUGIN_SCOPE;
308
 
309
  try
310
  {
311
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
1651 runge 312
 
313
    if (args.Length() < 2)
314
    {
1959 runge 315
      throw atom::exception::missing_in_param();
1651 runge 316
    }
317
 
318
    v8::String::AsciiValue data(args[1]);
1986 runge 319
 
320
    atom::log::Debug(log_module_, "Socket %u send %s", args[0]->Uint32Value(), *data);
1651 runge 321
 
1987 runge 322
    std::string data_string(*data);
323
 
324
    net::Manager::Instance()->SendTo(args[0]->Uint32Value(), common::Byteset(data_string.begin(), data_string.end()));
1959 runge 325
  }
326
  catch (std::exception& exception)
327
  {
328
    atom::log::Exception(log_module_, exception);
329
    return handle_scope.Close(v8::Boolean::New(false));
330
  }
331
 
332
  LOG_DEBUG_EXIT;
333
  return handle_scope.Close(v8::Boolean::New(true));
1651 runge 334
}
335
 
336
}; // namespace plugin
337
}; // namespace vm
338
}; // namespace atom