Subversion Repositories HomeAutomation

Rev

Rev 1608 | Rev 1620 | 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.  
  29. namespace atom {
  30. namespace vm {
  31. namespace plugin {
  32.    
  33. logging::Logger Console::LOG("vm::plugin::Console");
  34. type::StringList Console::commands_;
  35.    
  36. Console::Console(unsigned int port)
  37. {
  38.     this->name_ = "console";
  39.    
  40.     net::Manager::Instance()->ConnectSlots(net::Manager::SignalOnNewState::slot_type(&Console::SlotOnNewState, this, _1, _2, _3).track(this->tracker_),
  41.                                            net::Manager::SignalOnNewData::slot_type(&Console::SlotOnNewData, this, _1, _2, _3).track(this->tracker_));
  42.    
  43.     this->ExportFunction("Console_RegisterConsoleCommand",      Console::Export_RegisterConsoleCommand);
  44.    
  45.     try
  46.     {
  47.         this->server_id_ = net::Manager::Instance()->StartServer(net::PROTOCOL_TCP, port);
  48.         LOG.Info("Started TCP server on port " + boost::lexical_cast<std::string>(port) + ".");
  49.         LOG.Debug("Server id is " + boost::lexical_cast<std::string>(this->server_id_) + ".");
  50.     }
  51.     catch (std::runtime_error& e)
  52.     {
  53.         LOG.Error(e.what());
  54.     }
  55. }
  56.  
  57. Console::~Console()
  58. {
  59.     net::Manager::Instance()->StopServer(this->server_id_);
  60.     this->server_id_ = 0;
  61. }
  62.  
  63. void Console::InitializeDone()
  64. {
  65.     Plugin::InitializeDone();
  66.    
  67.     this->ImportFunction("Console_DoAutocomplete");
  68. }
  69.  
  70. void Console::SlotOnNewData(net::ClientId client_id, net::ServerId server_id, type::Byteset data)
  71. {
  72.     if (server_id != this->server_id_)
  73.     {
  74.         return;
  75.     }
  76.    
  77.     std::string str(data.ToCharString());
  78.    
  79.     boost::algorithm::trim_right_if(str, boost::is_any_of("\r\n"));
  80.    
  81.     type::StringList parts;
  82.    
  83.     boost::algorithm::split(parts, str, boost::is_any_of(";"), boost::algorithm::token_compress_off);
  84.    
  85.     if (parts[0] == "A") // Autocomplete
  86.     {
  87.         unsigned int arg_index = boost::lexical_cast<unsigned int>(parts[1]);
  88.        
  89.         std::string result;
  90.         type::StringList arguments;
  91.        
  92.         boost::algorithm::split(arguments, parts[2], boost::is_any_of(" "), boost::algorithm::token_compress_on);
  93.        
  94.         if (arg_index == 0)
  95.         {
  96.             for (unsigned int n = 0; n < this->commands_.size(); n++)
  97.             {
  98.                 //LOG.Debug(this->commands_[n]);
  99.                 result += this->commands_[n] + "\n";
  100.             }
  101.            
  102.             net::Manager::Instance()->SendTo(client_id, result);
  103.         }
  104.         else
  105.         {
  106.             ArgumentListPointer call_arguments = ArgumentListPointer(new ArgumentList);
  107.             type::StringList arguments;
  108.            
  109.             boost::algorithm::split(arguments, parts[2], boost::is_any_of(" "), boost::algorithm::token_compress_on);
  110.            
  111.             for (unsigned int n = 0; n < arguments.size() && n <= arg_index; n++)
  112.             {
  113.                 call_arguments->push_back(v8::String::New(arguments[n].data()));
  114.             }
  115.            
  116.             this->Call(client_id, "Console_DoAutocomplete", call_arguments);
  117.         }
  118.     }
  119.     else if (parts[0] == "E")
  120.     {
  121.         ArgumentListPointer call_arguments = ArgumentListPointer(new ArgumentList);
  122.         type::StringList arguments;
  123.        
  124.         boost::algorithm::split(arguments, parts[1], boost::is_any_of(" "), boost::algorithm::token_compress_on);
  125.        
  126.         std::string command = arguments[0];
  127.        
  128.         for (unsigned int n = 1; n < arguments.size(); n++)
  129.         {
  130.             call_arguments->push_back(v8::String::New(arguments[n].data()));
  131.         }
  132.        
  133.         this->Call(client_id, command, call_arguments);
  134.     }
  135. }
  136.  
  137. void Console::SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
  138. {
  139.     if (server_id != this->server_id_)
  140.     {
  141.         return;
  142.     }
  143.    
  144.     if (client_state == net::CLIENT_STATE_DISCONNECTED)
  145.     {
  146.         LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has disconnected.");
  147.     }
  148.     else if (client_state == net::CLIENT_STATE_CONNECTED)
  149.     {
  150.         LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has connected.");
  151.     }
  152.     else
  153.     {
  154.         LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state));
  155.     }
  156. }
  157.  
  158. void Console::ExecutionResult(std::string response, unsigned int request_id)
  159. {
  160.     //LOG.Debug("ExecutionResult: response=" + response + ", request_id=" + boost::lexical_cast<std::string>(request_id));
  161.     net::Manager::Instance()->SendTo(request_id, response);
  162. }
  163.  
  164. Value Console::Export_RegisterConsoleCommand(const v8::Arguments& args)
  165. {
  166.     if (args.Length() < 1)
  167.     {
  168.         LOG.Error("To few arguments.");
  169.     }
  170.    
  171.     v8::String::AsciiValue command(args[0]);
  172.    
  173.     if (Console::ImportFunction(std::string(*command)))
  174.     {
  175.         LOG.Debug(std::string(*command) + " registered successfully.");
  176.         Console::commands_.push_back(std::string(*command));
  177.         return v8::Boolean::New(true);
  178.     }
  179.    
  180.     LOG.Debug("Failed to register command!");
  181.    
  182.     return v8::Boolean::New(false);
  183. }
  184.  
  185. }; // namespace plugin
  186. }; // namespace vm
  187. }; // namespace atom
  188.