Subversion Repositories HomeAutomation

Rev

Rev 1959 | Rev 1987 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1959 Rev 1986
Line 38... Line 38...
38
Console::Console(boost::asio::io_service& io_service, unsigned int port) : Plugin(io_service)
38
Console::Console(boost::asio::io_service& io_service, unsigned int port) : Plugin(io_service)
39
{
39
{
40
    this->name_ = "console";
40
    this->name_ = "console";
41
    Console::current_client_id_ = 0;
41
    Console::current_client_id_ = 0;
42
   
42
   
43
    net::Manager::Instance()->ConnectSlots(net::Manager::SignalOnNewState::slot_type(&Console::SlotOnNewState, this, _1, _2, _3).track(this->tracker_),
43
    net::Manager::Instance()->ConnectSlots(net::Manager::SignalOnNewState::slot_type(&Console::SlotOnNewState, this, _1, _2).track(this->tracker_),
-
 
44
                                           net::Manager::SignalOnNewClient::slot_type(&Console::SlotOnNewClient, this, _1, _2).track(this->tracker_),
44
                                           net::Manager::SignalOnNewData::slot_type(&Console::SlotOnNewData, this, _1, _2, _3).track(this->tracker_));
45
                                           net::Manager::SignalOnNewData::slot_type(&Console::SlotOnNewData, this, _1, _2).track(this->tracker_));
45
   
46
   
46
    this->ExportFunction("ConsoleExport_PromptRequest",        Console::Export_PromptRequest);
47
    this->ExportFunction("ConsoleExport_PromptRequest",        Console::Export_PromptRequest);
47
    this->ExportFunction("ConsoleExport_AutoCompleteResponse", Console::Export_AutoCompleteResponse);
48
    this->ExportFunction("ConsoleExport_AutoCompleteResponse", Console::Export_AutoCompleteResponse);
48
    this->ExportFunction("ConsoleExport_LogToClient",          Console::Export_LogToClient);
49
    this->ExportFunction("ConsoleExport_LogToClient",          Console::Export_LogToClient);
49
    this->ExportFunction("ConsoleExport_DisconnectClient",     Console::Export_DisconnectClient);
50
    this->ExportFunction("ConsoleExport_DisconnectClient",     Console::Export_DisconnectClient);
Line 82... Line 83...
82
    packet += output;
83
    packet += output;
83
   
84
   
84
    net::Manager::Instance()->SendTo(request_id, packet);
85
    net::Manager::Instance()->SendTo(request_id, packet);
85
}
86
}
86
 
87
 
-
 
88
void Console::SlotOnNewData(net::SocketId id, common::Byteset data)
-
 
89
{
-
 
90
    this->io_service_.post(boost::bind(&Console::SlotOnNewDataHandler, this, id, data));
-
 
91
}
-
 
92
 
-
 
93
void Console::SlotOnNewClient(net::SocketId id, net::SocketId server_id)
-
 
94
{
-
 
95
  this->io_service_.post(boost::bind(&Console::SlotOnNewClientHandler, this, id, server_id));
-
 
96
}
-
 
97
 
-
 
98
void Console::SlotOnNewState(net::SocketId id, net::ClientState client_state)
-
 
99
{
-
 
100
    this->io_service_.post(boost::bind(&Console::SlotOnNewStateHandler, this, id, client_state));
-
 
101
}
-
 
102
 
87
void Console::SlotOnNewData(net::SocketId client_id, net::SocketId server_id, common::Byteset data)
103
void Console::SlotOnNewDataHandler(net::SocketId id, common::Byteset data)
-
 
104
{
-
 
105
    v8::Locker lock;
-
 
106
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
-
 
107
    v8::HandleScope handle_scope;
-
 
108
 
-
 
109
    if (std::find(this->clients_.begin(), this->clients_.end(), id) == this->clients_.end())
88
{
110
    {
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::SocketId client_id, net::SocketId 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::SocketId client_id, net::SocketId 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;
111
      return;
119
    }
112
    }
120
   
113
     
-
 
114
   
-
 
115
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
-
 
116
 
-
 
117
    if (data.GetSize() == 0)
-
 
118
    {
-
 
119
        LOG.Error(std::string(__FUNCTION__) + " got empty data!");
-
 
120
        return;
-
 
121
    }
-
 
122
   
121
    std::string s(data.ToCharString());
123
    std::string s(data.ToCharString());
122
   
124
   
123
    if (s.length() < 8)
125
    if (s.length() < 8)
124
    {
126
    {
125
        LOG.Error(std::string(__FUNCTION__) + " got a packet which is to short, less then 8 bytes: \"" + s + "\"");
127
        LOG.Error(std::string(__FUNCTION__) + " got a packet which is to short, less then 8 bytes: \"" + s + "\"");
126
        return;
128
        return;
127
    }
129
    }
128
   
130
   
129
    std::string command = s.substr(0, 4);
131
    std::string command = s.substr(0, 4);
130
   
132
   
131
    LOG.Debug(std::string(__FUNCTION__) + " parsed command: \"" + command + "\"");
133
    LOG.Debug(std::string(__FUNCTION__) + " parsed command: \"" + command + "\"");
132
   
134
   
133
    // TODO: Check cast exception!
135
    // TODO: Check cast exception!
134
    unsigned int payload_length = boost::lexical_cast<unsigned int>(s.substr(4, 4));
136
    unsigned int payload_length = boost::lexical_cast<unsigned int>(s.substr(4, 4));
135
 
137
 
136
    LOG.Debug(std::string(__FUNCTION__) + " parsed payload_length: \"" + boost::lexical_cast<std::string>(payload_length) + "\"");
138
    LOG.Debug(std::string(__FUNCTION__) + " parsed payload_length: \"" + boost::lexical_cast<std::string>(payload_length) + "\"");
137
 
139
 
138
   
140
   
139
    Console::current_client_id_ = client_id;
141
    Console::current_client_id_ = id;
140
   
142
   
141
    if (command == "COMP")
143
    if (command == "COMP")
142
    {
144
    {
143
        ArgumentListPointer call_arguments = ArgumentListPointer(new ArgumentList);
145
        ArgumentListPointer call_arguments = ArgumentListPointer(new ArgumentList);
144
       
146
       
145
        call_arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(client_id)));
147
        call_arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(id)));
146
        call_arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(s.substr(8, 4))));
148
        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()));
149
        call_arguments->push_back(v8::String::New(s.substr(12).data()));
148
       
150
       
149
        if (!this->Call(client_id, "Console_AutocompleteRequest", call_arguments))
151
        if (!this->Call(id, "Console_AutocompleteRequest", call_arguments))
150
        {
152
        {
151
            LOG.Error("Console_AutocompleteRequest failed, we are now in an undefined state!");
153
            LOG.Error("Console_AutocompleteRequest failed, we are now in an undefined state!");
152
        }
154
        }
153
    }
155
    }
154
    else if (command == "RESP")
156
    else if (command == "RESP")
155
    {
157
    {
156
        LOG.Debug(std::string(__FUNCTION__) + " parsed data: \"" + s.substr(8).data() + "\"");
158
        LOG.Debug(std::string(__FUNCTION__) + " parsed data: \"" + s.substr(8).data() + "\"");
157
       
159
       
158
        ArgumentListPointer call_arguments = ArgumentListPointer(new ArgumentList);
160
        ArgumentListPointer call_arguments = ArgumentListPointer(new ArgumentList);
159
       
161
       
160
        call_arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(client_id)));
162
        call_arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(id)));
161
        call_arguments->push_back(v8::String::New(s.substr(8).data()));
163
        call_arguments->push_back(v8::String::New(s.substr(8).data()));
162
       
164
       
163
        if (!this->Call(client_id, "Console_PromptResponse", call_arguments))
165
        if (!this->Call(id, "Console_PromptResponse", call_arguments))
164
        {
166
        {
165
            LOG.Error("Console_PromptResponse failed, we are now in an undefined state!");
167
            LOG.Error("Console_PromptResponse failed, we are now in an undefined state!");
166
        }
168
        }
167
    }
169
    }
168
    else
170
    else
169
    {
171
    {
170
        LOG.Error("Unknown packat received, we are now in an undefined state!");
172
        LOG.Error("Unknown packat received, we are now in an undefined state!");
171
    }
173
    }
172
   
174
   
173
    Console::current_client_id_ = 0;
175
    Console::current_client_id_ = 0;
174
}
176
}
-
 
177
 
-
 
178
void Console::SlotOnNewClientHandler(net::SocketId id, net::SocketId server_id)
-
 
179
{
-
 
180
  if (server_id == this->server_id_)
-
 
181
  {
-
 
182
    this->clients_.insert(id);
-
 
183
  }
-
 
184
}
175
 
185
 
176
void Console::SlotOnNewStateHandler(net::SocketId client_id, net::SocketId server_id, net::ClientState client_state)
186
void Console::SlotOnNewStateHandler(net::SocketId id, net::ClientState client_state)
177
{
187
{
178
    v8::Locker lock;
188
    v8::Locker lock;
179
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
189
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
180
    v8::HandleScope handle_scope;
190
    v8::HandleScope handle_scope;
-
 
191
 
-
 
192
    if (std::find(this->clients_.begin(), this->clients_.end(), id) == this->clients_.end())
-
 
193
    {
-
 
194
      return;
-
 
195
    }
181
 
196
   
182
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
197
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
183
   
198
   
184
    if (client_state == net::CLIENT_STATE_DISCONNECTED)
199
    if (client_state == net::CLIENT_STATE_DISCONNECTED)
185
    {
200
    {
186
        LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has disconnected.");
201
        LOG.Info("Client " + boost::lexical_cast<std::string>(id) + " has disconnected.");
-
 
202
        this->clients_.erase(id);
187
    }
203
    }
188
    else if (client_state == net::CLIENT_STATE_CONNECTED)
204
    else if (client_state == net::CLIENT_STATE_CONNECTED)
189
    {
205
    {
190
        LOG.Info("Client " + boost::lexical_cast<std::string>(client_id) + " has connected.");
206
        LOG.Info("Client " + boost::lexical_cast<std::string>(id) + " has connected.");
191
     
207
     
192
        Console::current_client_id_ = client_id;
208
        Console::current_client_id_ = id;
193
       
209
       
194
        ArgumentListPointer call_arguments = ArgumentListPointer(new ArgumentList);
210
        ArgumentListPointer call_arguments = ArgumentListPointer(new ArgumentList);
195
       
211
       
196
        call_arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(client_id)));
212
        call_arguments->push_back(v8::Integer::New(boost::lexical_cast<unsigned int>(id)));
197
       
213
       
198
        if (!this->Call(client_id, "Console_NewConnection", call_arguments))
214
        if (!this->Call(id, "Console_NewConnection", call_arguments))
199
        {
215
        {
200
            LOG.Error("Console_NewConnection failed, we are now in an undefined state!");
216
            LOG.Error("Console_NewConnection failed, we are now in an undefined state!");
201
        }
217
        }
202
       
218
       
203
        Console::current_client_id_ = 0;
219
        Console::current_client_id_ = 0;