Rev 1609 | Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1608 | 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 "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 | |||
| 68 | void Console::SlotOnNewData(net::ClientId client_id, net::ServerId server_id, type::Byteset data) |
||
| 69 | { |
||
| 70 | if (server_id != this->server_id_) |
||
| 71 | { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | |||
| 75 | std::string str(data.ToCharString()); |
||
| 76 | |||
| 77 | boost::algorithm::trim_right_if(str, boost::is_any_of("\r\n")); |
||
| 78 | |||
| 79 | type::StringList parts; |
||
| 80 | |||
| 81 | boost::algorithm::split(parts, str, boost::is_any_of(";"), boost::algorithm::token_compress_off); |
||
| 82 | |||
| 83 | if (parts[0] == "A") // Autocomplete |
||
| 84 | { |
||
| 85 | unsigned int arg_index = boost::lexical_cast<unsigned int>(parts[1]); |
||
| 86 | |||
| 87 | std::string result; |
||
| 88 | type::StringList arguments; |
||
| 89 | |||
| 90 | boost::algorithm::split(arguments, parts[2], boost::is_any_of(" "), boost::algorithm::token_compress_on); |
||
| 91 | |||
| 92 | if (arg_index == 0) |
||
| 93 | { |
||
| 94 | for (unsigned int n = 0; n < this->commands_.size(); n++) |
||
| 95 | { |
||
| 96 | LOG.Debug(this->commands_[n]); |
||
| 97 | result += this->commands_[n] + "\n"; |
||
| 98 | } |
||
| 99 | |||
| 100 | net::Manager::Instance()->SendTo(client_id, result); |
||
| 101 | } |
||
| 102 | else |
||
| 103 | { |
||
| 104 | net::Manager::Instance()->SendTo(client_id, result); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | else if (parts[0] == "E") |
||
| 108 | { |
||
| 109 | ArgumentListPointer call_arguments = ArgumentListPointer(new ArgumentList); |
||
| 110 | type::StringList arguments; |
||
| 111 | |||
| 112 | boost::algorithm::split(arguments, parts[1], boost::is_any_of(" "), boost::algorithm::token_compress_on); |
||
| 113 | |||
| 114 | std::string command = arguments[0]; |
||
| 115 | |||
| 116 | LOG.Debug("Command:" + command); |
||
| 117 | |||
| 118 | for (unsigned int n = 1; n < arguments.size(); n++) |
||
| 119 | { |
||
| 120 | LOG.Debug("arg:" + arguments[n]); |
||
| 121 | call_arguments->push_back(v8::String::New(arguments[n].data())); |
||
| 122 | } |
||
| 123 | |||
| 124 | this->Call(client_id, command, call_arguments); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | void Console::SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state) |
||
| 129 | { |
||
| 130 | if (server_id != this->server_id_) |
||
| 131 | { |
||
| 132 | return; |
||
| 133 | } |
||
| 134 | |||
| 135 | if (client_state == net::CLIENT_STATE_DISCONNECTED) |
||
| 136 | { |
||
| 137 | LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has disconnected."); |
||
| 138 | } |
||
| 139 | else if (client_state == net::CLIENT_STATE_CONNECTED) |
||
| 140 | { |
||
| 141 | LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has connected."); |
||
| 142 | } |
||
| 143 | else |
||
| 144 | { |
||
| 145 | LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state)); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | void Console::ExecutionResult(std::string response, unsigned int request_id) |
||
| 150 | { |
||
| 151 | LOG.Debug("ExecutionResult: response=" + response + ", request_id=" + boost::lexical_cast<std::string>(request_id)); |
||
| 152 | net::Manager::Instance()->SendTo(request_id, response); |
||
| 153 | } |
||
| 154 | |||
| 155 | Value Console::Export_RegisterConsoleCommand(const v8::Arguments& args) |
||
| 156 | { |
||
| 157 | if (args.Length() < 1) |
||
| 158 | { |
||
| 159 | LOG.Error("To few arguments."); |
||
| 160 | } |
||
| 161 | |||
| 162 | v8::String::AsciiValue command(args[0]); |
||
| 163 | |||
| 164 | if (Console::ImportFunction(std::string(*command))) |
||
| 165 | { |
||
| 166 | LOG.Debug(std::string(*command) + " registered correctly"); |
||
| 167 | Console::commands_.push_back(std::string(*command)); |
||
| 168 | return v8::Boolean::New(true); |
||
| 169 | } |
||
| 170 | |||
| 171 | LOG.Debug("Failed to register command!"); |
||
| 172 | |||
| 173 | return v8::Boolean::New(false); |
||
| 174 | } |
||
| 175 | |||
| 176 | }; // namespace plugin |
||
| 177 | }; // namespace vm |
||
| 178 | }; // namespace atom |