Subversion Repositories HomeAutomation

Rev

Rev 1740 | 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 "Console.h"
  22.  
  23. #include <boost/lexical_cast.hpp>
  24. #include <boost/algorithm/string/trim.hpp>
  25. #include <boost/algorithm/string/split.hpp>
  26.  
  27. #include "net/Manager.h"
  28. #include "vm/Manager.h"
  29. #include "common/common.h"
  30.  
  31. namespace atom {
  32. namespace vm {
  33. namespace plugin {
  34.    
  35. logging::Logger Console::LOG("vm::plugin::Console");
  36. net::ClientId Console::current_client_id_;
  37.    
  38. Console::Console(boost::asio::io_service& io_service, unsigned int port) : Plugin(io_service)
  39. {
  40.     this->name_ = "console";
  41.     Console::current_client_id_ = 0;
  42.    
  43.     net::Manager::Instance()->ConnectSlots(net::Manager::SignalOnNewState::slot_type(&Console::SlotOnNewState, this, _1, _2, _3).track(this->tracker_),
  44.                                            net::Manager::SignalOnNewData::slot_type(&Console::SlotOnNewData, this, _1, _2, _3).track(this->tracker_));
  45.    
  46.     this->ExportFunction("ConsoleExport_PromptRequest",        Console::Export_PromptRequest);
  47.     this->ExportFunction("ConsoleExport_AutoCompleteResponse", Console::Export_AutoCompleteResponse);
  48.     this->ExportFunction("ConsoleExport_LogToClient",          Console::Export_LogToClient);
  49.     this->ExportFunction("ConsoleExport_DisconnectClient",     Console::Export_DisconnectClient);
  50.    
  51.     try
  52.     {
  53.         this->server_id_ = net::Manager::Instance()->StartServer(net::PROTOCOL_TCP, port);
  54.         LOG.Info("Started TCP server on port " + boost::lexical_cast<std::string>(port) + ".");
  55.         LOG.Debug("Server id is " + boost::lexical_cast<std::string>(this->server_id_) + ".");
  56.     }
  57.     catch (std::runtime_error& e)
  58.     {
  59.         LOG.Error(e.what());
  60.     }
  61. }
  62.  
  63. Console::~Console()
  64. {
  65.     net::Manager::Instance()->StopServer(this->server_id_);
  66.     this->server_id_ = 0;
  67. }
  68.  
  69. void Console::InitializeDone()
  70. {
  71.     Plugin::InitializeDone();
  72.    
  73.     this->ImportFunction("Console_AutocompleteRequest");
  74.     this->ImportFunction("Console_PromptResponse");
  75.     this->ImportFunction("Console_NewConnection");
  76. }
  77.  
  78. void Console::CallOutput(unsigned int request_id, std::string output)
  79. {
  80.     std::string packet = "TEXT";
  81.     packet += common::PadNumber(output.length() + 1, 4);
  82.     packet += output;
  83.    
  84.     net::Manager::Instance()->SendTo(request_id, packet);
  85. }
  86.  
  87. void Console::SlotOnNewData(net::ClientId client_id, net::ServerId server_id, common::Byteset data)
  88. {
  89.     if (server_id != this->server_id_)
  90.     {
  91.         return;
  92.     }
  93.    
  94.     this->io_service_.post(boost::bind(&Console::SlotOnNewDataHandler, this, client_id, server_id, data));
  95. }
  96.  
  97. void Console::SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
  98. {
  99.     if (server_id != this->server_id_)
  100.     {
  101.         return;
  102.     }
  103.    
  104.     this->io_service_.post(boost::bind(&Console::SlotOnNewStateHandler, this, client_id, server_id, client_state));
  105. }
  106.  
  107. void Console::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, common::Byteset data)
  108. {
  109.     v8::Locker lock;
  110.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  111.     v8::HandleScope handle_scope;
  112.  
  113.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  114.  
  115.     if (data.GetSize() == 0)
  116.     {
  117.         LOG.Error(std::string(__FUNCTION__) + " got empty data!");
  118.         return;
  119.     }
  120.    
  121.     std::string s(data.ToCharString());
  122.    
  123.     if (s.length() < 8)
  124.     {
  125.         LOG.Error(std::string(__FUNCTION__) + " got a packet which is to short, less then 8 bytes: \"" + s + "\"");
  126.         return;
  127.     }
  128.    
  129.     std::string command = s.substr(0, 4);
  130.    
  131.     LOG.Debug(std::string(__FUNCTION__) + " parsed command: \"" + command + "\"");
  132.    
  133.     // TODO: Check cast exception!
  134.     unsigned int payload_length = boost::lexical_cast<unsigned int>(s.substr(4, 4));
  135.  
  136.     LOG.Debug(std::string(__FUNCTION__) + " parsed payload_length: \"" + boost::lexical_cast<std::string>(payload_length) + "\"");
  137.  
  138.    
  139.     Console::current_client_id_ = client_id;
  140.    
  141.     if (command == "COMP")
  142.     {
  143.         ArgumentListPointer call_arguments = ArgumentListPointer(new ArgumentList);
  144.        
  145.         call_arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(client_id)));
  146.         call_arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(s.substr(8, 4))));
  147.         call_arguments->push_back(v8::String::New(s.substr(12).data()));
  148.        
  149.         if (!this->Call(client_id, "Console_AutocompleteRequest", call_arguments))
  150.         {
  151.             LOG.Error("Console_AutocompleteRequest failed, we are now in an undefined state!");
  152.         }
  153.     }
  154.     else if (command == "RESP")
  155.     {
  156.         LOG.Debug(std::string(__FUNCTION__) + " parsed data: \"" + s.substr(8).data() + "\"");
  157.        
  158.         ArgumentListPointer call_arguments = ArgumentListPointer(new ArgumentList);
  159.        
  160.         call_arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(client_id)));
  161.         call_arguments->push_back(v8::String::New(s.substr(8).data()));
  162.        
  163.         if (!this->Call(client_id, "Console_PromptResponse", call_arguments))
  164.         {
  165.             LOG.Error("Console_PromptResponse failed, we are now in an undefined state!");
  166.         }
  167.     }
  168.     else
  169.     {
  170.         LOG.Error("Unknown packat received, we are now in an undefined state!");
  171.     }
  172.    
  173.     Console::current_client_id_ = 0;
  174. }
  175.  
  176. void Console::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
  177. {
  178.     v8::Locker lock;
  179.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  180.     v8::HandleScope handle_scope;
  181.  
  182.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  183.    
  184.     if (client_state == net::CLIENT_STATE_DISCONNECTED)
  185.     {
  186.         LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has disconnected.");
  187.     }
  188.     else if (client_state == net::CLIENT_STATE_CONNECTED)
  189.     {
  190.         LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has connected.");
  191.      
  192.         Console::current_client_id_ = client_id;
  193.        
  194.         ArgumentListPointer call_arguments = ArgumentListPointer(new ArgumentList);
  195.        
  196.         call_arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(client_id)));
  197.        
  198.         if (!this->Call(client_id, "Console_NewConnection", call_arguments))
  199.         {
  200.             LOG.Error("Console_NewConnection failed, we are now in an undefined state!");
  201.         }
  202.        
  203.         Console::current_client_id_ = 0;
  204.     }
  205.     else
  206.     {
  207.         LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state));
  208.     }
  209. }
  210.  
  211. Value Console::Export_PromptRequest(const v8::Arguments& args)
  212. {
  213.     v8::Locker lock;
  214.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  215.     v8::HandleScope handle_scope;
  216.    
  217.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  218.    
  219.     if (args.Length() < 2)
  220.     {
  221.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  222.         return handle_scope.Close(v8::Boolean::New(false));
  223.     }
  224.    
  225.     v8::String::AsciiValue prompt(args[1]);
  226.    
  227.     std::string packet = "PROM";
  228.     packet += common::PadNumber(prompt.length() + 1, 4);
  229.     packet += *prompt;
  230.    
  231.     net::Manager::Instance()->SendTo(args[0]->Uint32Value(), packet);
  232.     return handle_scope.Close(v8::Boolean::New(true));
  233. }
  234.  
  235. Value Console::Export_AutoCompleteResponse(const v8::Arguments& args)
  236. {
  237.     v8::Locker lock;
  238.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  239.     v8::HandleScope handle_scope;
  240.    
  241.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  242.    
  243.     if (args.Length() < 2)
  244.     {
  245.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  246.         return handle_scope.Close(v8::Boolean::New(false));
  247.     }
  248.    
  249.     v8::String::AsciiValue result(args[1]);
  250.    
  251.     std::string packet = "COMP";
  252.     packet += common::PadNumber(result.length() + 1, 4);
  253.     packet += *result;
  254.    
  255.     net::Manager::Instance()->SendTo(args[0]->Uint32Value(), packet);
  256.     return handle_scope.Close(v8::Boolean::New(true));
  257. }
  258.  
  259. Value Console::Export_LogToClient(const v8::Arguments& args)
  260. {
  261.     v8::Locker lock;
  262.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  263.     v8::HandleScope handle_scope;
  264.    
  265.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  266.    
  267.     if (args.Length() < 2)
  268.     {
  269.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  270.         return handle_scope.Close(v8::Boolean::New(false));
  271.     }
  272.    
  273.     v8::String::AsciiValue text(args[1]);
  274.    
  275.     std::string line = *text;
  276.    
  277.     std::string packet = "TEXT";
  278.     packet += common::PadNumber(line.length() + 1, 4);
  279.     packet += line;
  280.    
  281.     net::Manager::Instance()->SendTo(args[0]->Uint32Value(), packet);
  282.     return handle_scope.Close(v8::Boolean::New(true));
  283. }
  284.  
  285. Value Console::Export_DisconnectClient(const v8::Arguments& args)
  286. {
  287.     v8::Locker lock;
  288.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  289.     v8::HandleScope handle_scope;
  290.    
  291.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  292.    
  293.     if (args.Length() < 1)
  294.     {
  295.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  296.         return handle_scope.Close(v8::Boolean::New(false));
  297.     }
  298.     net::Manager::Instance()->Disconnect(args[0]->Uint32Value());
  299.     return handle_scope.Close(v8::Boolean::New(true));
  300. }
  301.  
  302. }; // namespace plugin
  303. }; // namespace vm
  304. }; // namespace atom
  305.