Subversion Repositories HomeAutomation

Rev

Rev 1986 | 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. #include "common/common.h"
  31. #include "common/log.h"
  32. #include "common/exception.h"
  33.  
  34. namespace atom {
  35. namespace vm {
  36. namespace plugin {
  37.  
  38. static const std::string log_module_ = "vm::plugin::socket";
  39.  
  40. Socket::Socket(boost::asio::io_service& io_service) : Plugin(io_service)
  41. {
  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;
  53. }
  54.  
  55. Socket::~Socket()
  56. {
  57.   LOG_DEBUG_ENTER;
  58.   LOG_DEBUG_EXIT;
  59. }
  60.  
  61. void Socket::InitializeDone()
  62. {
  63.   LOG_DEBUG_ENTER;
  64.  
  65.   Plugin::InitializeDone();
  66.  
  67.   this->ImportFunction("Socket_OnNewData");
  68.   this->ImportFunction("Socket_OnNewClient");
  69.   this->ImportFunction("Socket_OnNewState");
  70.  
  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_));
  72.  
  73.   LOG_DEBUG_EXIT;
  74. }
  75.  
  76. void Socket::CallOutput(unsigned int request_id, std::string output)
  77. {
  78.   //LOG_DEBUG_ENTER;
  79.  
  80.   log::Info(log_module_, output);
  81.  
  82.   //LOG_DEBUG_EXIT;
  83. }
  84.  
  85. void Socket::SlotOnNewData(net::SocketId id, common::Byteset data)
  86. {
  87.   LOG_DEBUG_ENTER;
  88.  
  89.   this->io_service_.post(boost::bind(&Socket::SlotOnNewDataHandler, this, id, data));
  90.  
  91.   LOG_DEBUG_EXIT;
  92. }
  93.  
  94. void Socket::SlotOnNewClient(net::SocketId id, net::SocketId server_id)
  95. {
  96.   LOG_DEBUG_ENTER;
  97.  
  98.   this->io_service_.post(boost::bind(&Socket::SlotOnNewClientHandler, this, id, server_id));
  99.  
  100.   LOG_DEBUG_EXIT;
  101. }
  102.  
  103. void Socket::SlotOnNewState(net::SocketId id, net::ClientState client_state)
  104. {
  105.   LOG_DEBUG_ENTER;
  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;
  115.   ATOM_VM_PLUGIN_SCOPE;
  116.  
  117.   try
  118.   {
  119.     ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
  120.    
  121.     if (data.size() == 0)
  122.     {
  123.         atom::log::Error(log_module_, "Got empty data!");
  124.         return;
  125.     }
  126.    
  127.     arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(id)));
  128.     arguments->push_back(v8::String::New(std::string(data.begin(), data.end()).data()));
  129.        
  130.     if (!this->Call(id, "Socket_OnNewData", arguments))
  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;
  141. }
  142.  
  143. void Socket::SlotOnNewClientHandler(net::SocketId id, net::SocketId server_id)
  144. {
  145.   LOG_DEBUG_ENTER;
  146.   ATOM_VM_PLUGIN_SCOPE;
  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. }
  167.  
  168.  
  169. void Socket::SlotOnNewStateHandler(net::SocketId id, net::ClientState client_state)
  170. {
  171.   LOG_DEBUG_ENTER;
  172.   ATOM_VM_PLUGIN_SCOPE;
  173.  
  174.   try
  175.   {
  176.     ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
  177.    
  178.     arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(id)));
  179.     arguments->push_back(v8::Uint32::New((unsigned int)client_state));
  180.    
  181.     if (!this->Call(id, "Socket_OnNewState", arguments))
  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.   {
  203.     ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
  204.    
  205.     if (args.Length() < 1)
  206.     {
  207.       throw atom::exception::missing_in_param();
  208.     }
  209.    
  210.     socket_id = net::Manager::Instance()->StartServer(net::PROTOCOL_TCP, args[0]->Uint32Value());
  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));
  220. }
  221.  
  222. Value Socket::Export_Connect(const v8::Arguments& args)
  223. {
  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);
  232.    
  233.     if (args.Length() < 2)
  234.     {
  235.       throw atom::exception::missing_in_param();
  236.     }
  237.    
  238.     v8::String::AsciiValue address(args[0]);
  239.    
  240.     socket_id = net::Manager::Instance()->Connect(net::PROTOCOL_TCP, std::string(*address), args[1]->Uint32Value());
  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)
  262.     {
  263.       throw atom::exception::missing_in_param();
  264.     }
  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.   }
  273.  
  274.   LOG_DEBUG_EXIT;
  275.   return handle_scope.Close(v8::Boolean::New(true));
  276. }
  277.  
  278. Value Socket::Export_Disconnect(const v8::Arguments& args)
  279. {
  280.   LOG_DEBUG_EXIT;
  281.   ATOM_VM_PLUGIN_SCOPE;
  282.  
  283.   try
  284.   {
  285.     ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
  286.    
  287.     if (args.Length() < 1)
  288.     {
  289.       throw atom::exception::missing_in_param();
  290.     }
  291.    
  292.     net::Manager::Instance()->Disconnect(args[0]->Uint32Value());
  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));
  302. }
  303.  
  304. Value Socket::Export_Send(const v8::Arguments& args)
  305. {
  306.   LOG_DEBUG_EXIT;
  307.   ATOM_VM_PLUGIN_SCOPE;
  308.  
  309.   try
  310.   {
  311.     ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
  312.    
  313.     if (args.Length() < 2)
  314.     {
  315.       throw atom::exception::missing_in_param();
  316.     }
  317.    
  318.     v8::String::AsciiValue data(args[1]);
  319.  
  320.     atom::log::Debug(log_module_, "Socket %u send %s", args[0]->Uint32Value(), *data);
  321.    
  322.     std::string data_string(*data);
  323.    
  324.     net::Manager::Instance()->SendTo(args[0]->Uint32Value(), common::Byteset(data_string.begin(), data_string.end()));
  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));
  334. }
  335.  
  336. }; // namespace plugin
  337. }; // namespace vm
  338. }; // namespace atom
  339.