Subversion Repositories HomeAutomation

Rev

Rev 1898 | Rev 1959 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  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"
  30.  
  31. namespace atom {
  32. namespace vm {
  33. namespace plugin {
  34.  
  35. logging::Logger Socket::LOG("vm::plugin::Socket");
  36.  
  37. Socket::Socket(boost::asio::io_service& io_service) : Plugin(io_service)
  38. {
  39.     this->name_ = "socket";
  40.    
  41.     this->ExportFunction("SocketExport_Connect",     Socket::Export_Connect);
  42.     this->ExportFunction("SocketExport_Disconnect",  Socket::Export_Disconnect);
  43.     this->ExportFunction("SocketExport_Send",        Socket::Export_Send);
  44. }
  45.  
  46. Socket::~Socket()
  47. {
  48.    
  49. }
  50.  
  51. void Socket::InitializeDone()
  52. {
  53.     Plugin::InitializeDone();
  54.    
  55.     this->ImportFunction("Socket_OnNewData");
  56.     this->ImportFunction("Socket_OnNewState");
  57.    
  58.     net::Manager::Instance()->ConnectSlots(net::Manager::SignalOnNewState::slot_type(&Socket::SlotOnNewState, this, _1, _2, _3).track(this->tracker_),
  59.                                            net::Manager::SignalOnNewData::slot_type(&Socket::SlotOnNewData, this, _1, _2, _3).track(this->tracker_));
  60. }
  61.  
  62. void Socket::CallOutput(unsigned int request_id, std::string output)
  63. {
  64.     LOG.Info(output);
  65. }
  66.  
  67. void Socket::SlotOnNewData(net::ClientId client_id, net::ServerId server_id, common::Byteset data)
  68. {
  69.     common::Byteset temp_buffer(data);
  70.     this->io_service_.post(boost::bind(&Socket::SlotOnNewDataHandler, this, client_id, server_id, temp_buffer));
  71. }
  72.  
  73. void Socket::SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
  74. {
  75.     this->io_service_.post(boost::bind(&Socket::SlotOnNewStateHandler, this, client_id, server_id, client_state));
  76. }
  77.  
  78. void Socket::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, common::Byteset data)
  79. {
  80.     v8::Locker lock;
  81.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  82.     v8::HandleScope handle_scope;
  83.    
  84.     LOG.Debug(std::string(__FUNCTION__) + " called!");
  85.      
  86.     ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
  87.     arguments->push_back(v8::Integer::New(client_id));
  88.     arguments->push_back(v8::String::New(data.ToCharString().data()));
  89.    
  90.     this->Call(client_id, "Socket_OnNewData", arguments);
  91. }
  92.  
  93. void Socket::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
  94. {
  95.     v8::Locker lock;
  96.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  97.     v8::HandleScope handle_scope;
  98.    
  99.     LOG.Debug(std::string(__FUNCTION__) + " called!");
  100.    
  101.     ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
  102.     arguments->push_back(v8::Integer::New(client_id));
  103.     arguments->push_back(v8::Uint32::New((unsigned int)client_state));
  104.    
  105.     this->Call(client_id, "Socket_OnNewState", arguments);
  106. }
  107.  
  108. Value Socket::Export_Connect(const v8::Arguments& args)
  109. {
  110.     net::ClientId client_id = 0;
  111.     v8::Locker lock;
  112.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  113.     v8::HandleScope handle_scope;
  114.    
  115.     LOG.Debug(std::string(__FUNCTION__) + " called!");
  116.    
  117.     if (args.Length() < 2)
  118.     {
  119.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  120.         return handle_scope.Close(v8::Boolean::New(false));
  121.     }
  122.    
  123.     v8::String::AsciiValue address(args[0]);
  124.    
  125.     try
  126.     {
  127.         client_id = net::Manager::Instance()->Connect(net::PROTOCOL_TCP, std::string(*address), args[1]->Uint32Value());
  128.     }
  129.     catch (std::runtime_error &e)
  130.     {
  131.     }
  132.  
  133.     return handle_scope.Close(v8::Integer::New(client_id));
  134. }
  135.  
  136. Value Socket::Export_Disconnect(const v8::Arguments& args)
  137. {
  138.     v8::Locker lock;
  139.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  140.     v8::HandleScope handle_scope;
  141.    
  142.     LOG.Debug(std::string(__FUNCTION__) + " called!");
  143.    
  144.     if (args.Length() < 1)
  145.     {
  146.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  147.         return handle_scope.Close(v8::Boolean::New(false));
  148.     }
  149.    
  150.     net::Manager::Instance()->Disconnect(args[0]->Uint32Value());
  151.    
  152.     return handle_scope.Close(v8::Undefined());
  153. }
  154.  
  155. Value Socket::Export_Send(const v8::Arguments& args)
  156. {
  157.     v8::Locker lock;
  158.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  159.     v8::HandleScope handle_scope;
  160.    
  161.     LOG.Debug(std::string(__FUNCTION__) + " called!");
  162.    
  163.     if (args.Length() < 2)
  164.     {
  165.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  166.         return handle_scope.Close(v8::Boolean::New(false));
  167.     }
  168.    
  169.     v8::String::AsciiValue data(args[1]);
  170.    
  171.     net::Manager::Instance()->SendTo(args[0]->Uint32Value(), std::string(*data));
  172.    
  173.     return handle_scope.Close(v8::Undefined());
  174. }
  175.  
  176. }; // namespace plugin
  177. }; // namespace vm
  178. }; // namespace atom
  179.  
  180.