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