Subversion Repositories HomeAutomation

Rev

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