Rev 999 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 999 | Rev 1007 | ||
|---|---|---|---|
| 1 | /*************************************************************************** |
1 | /*************************************************************************** |
| 2 | * Copyright (C) December 10, 2008 by Mattias Runge * |
2 | * Copyright (C) December 10, 2008 by Mattias Runge * |
| 3 | * mattias@runge.se * |
3 | * mattias@runge.se * |
| 4 | * virtualmachine.cpp * |
4 | * virtualmachine.cpp * |
| 5 | * * |
5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
9 | * (at your option) any later version. * |
| 10 | * * |
10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
14 | * GNU General Public License for more details. * |
| 15 | * * |
15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the * |
17 | * along with this program; if not, write to the * |
| 18 | * Free Software Foundation, Inc., * |
18 | * Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
20 | ***************************************************************************/ |
| 21 | 21 | ||
| 22 | #include "virtualmachine.h" |
22 | #include "virtualmachine.h" |
| 23 | 23 | ||
| 24 | VirtualMachine* VirtualMachine::myInstance = NULL; |
24 | VirtualMachine* VirtualMachine::myInstance = NULL; |
| 25 | 25 | ||
| 26 | VirtualMachine& VirtualMachine::getInstance() |
26 | VirtualMachine& VirtualMachine::getInstance() |
| 27 | { |
27 | { |
| 28 | if (myInstance == NULL) |
28 | if (myInstance == NULL) |
| 29 | { |
29 | { |
| 30 | myInstance = new VirtualMachine(); |
30 | myInstance = new VirtualMachine(); |
| 31 | } |
31 | } |
| 32 | 32 | ||
| 33 | return *myInstance; |
33 | return *myInstance; |
| 34 | } |
34 | } |
| 35 | 35 | ||
| 36 | void VirtualMachine::deleteInstance() |
36 | void VirtualMachine::deleteInstance() |
| 37 | { |
37 | { |
| 38 | if (myInstance != NULL) |
38 | if (myInstance != NULL) |
| 39 | { |
39 | { |
| 40 | delete myInstance; |
40 | delete myInstance; |
| 41 | myInstance = NULL; |
41 | myInstance = NULL; |
| 42 | } |
42 | } |
| 43 | } |
43 | } |
| 44 | 44 | ||
| 45 | VirtualMachine::VirtualMachine() |
45 | VirtualMachine::VirtualMachine() |
| 46 | { |
46 | { |
| 47 | } |
47 | } |
| 48 | 48 | ||
| 49 | VirtualMachine::~VirtualMachine() |
49 | VirtualMachine::~VirtualMachine() |
| 50 | { |
50 | { |
| 51 | stop(); |
51 | stop(); |
| 52 | 52 | ||
| 53 | myCommandThread.stop(); |
53 | myCommandThread.stop(); |
| 54 | 54 | ||
| 55 | ///FIXME: Verify that this works and does not cause segfaults |
55 | ///FIXME: Verify that this works and does not cause segfaults |
| 56 | for (map<int, SocketThread*>::iterator iter = mySocketThreads.begin(); iter != mySocketThreads.end(); iter++) |
56 | for (map<int, SocketThread*>::iterator iter = mySocketThreads.begin(); iter != mySocketThreads.end(); iter++) |
| 57 | { |
57 | { |
| 58 | iter->second->stop(); |
58 | iter->second->stop(); |
| 59 | delete iter->second; |
59 | delete iter->second; |
| 60 | //mySocketThreads.erase(iter); |
60 | //mySocketThreads.erase(iter); |
| 61 | } |
61 | } |
| 62 | 62 | ||
| 63 | mySocketThreads.clear(); |
63 | mySocketThreads.clear(); |
| 64 | 64 | ||
| 65 | for (map<int, IntervalThread*>::iterator iter = myIntervalThreads.begin(); iter != myIntervalThreads.end(); iter++) |
65 | for (map<int, IntervalThread*>::iterator iter = myIntervalThreads.begin(); iter != myIntervalThreads.end(); iter++) |
| 66 | { |
66 | { |
| 67 | iter->second->stop(); |
67 | iter->second->stop(); |
| 68 | delete iter->second; |
68 | delete iter->second; |
| 69 | //myIntervalThreads.erase(iter); |
69 | //myIntervalThreads.erase(iter); |
| 70 | } |
70 | } |
| 71 | 71 | ||
| 72 | myIntervalThreads.clear(); |
72 | myIntervalThreads.clear(); |
| 73 | } |
73 | } |
| 74 | 74 | ||
| 75 | void VirtualMachine::run() |
75 | void VirtualMachine::run() |
| 76 | { |
76 | { |
| 77 | myGlobal = ObjectTemplate::New(); |
77 | myGlobal = ObjectTemplate::New(); |
| 78 | myGlobal->Set(String::New("_quit"), FunctionTemplate::New(VirtualMachine::_quit)); |
78 | myGlobal->Set(String::New("_quit"), FunctionTemplate::New(VirtualMachine::_quit)); |
| 79 | myGlobal->Set(String::New("_print"), FunctionTemplate::New(VirtualMachine::_print)); |
79 | myGlobal->Set(String::New("_print"), FunctionTemplate::New(VirtualMachine::_print)); |
| 80 | myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine::_log)); |
80 | myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine::_log)); |
| 81 | myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine::_sendCanMessage)); |
81 | myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine::_sendCanMessage)); |
| 82 | myGlobal->Set(String::New("sendCanNMTMessage"), FunctionTemplate::New(VirtualMachine::_sendCanNMTMessage)); |
82 | myGlobal->Set(String::New("sendCanNMTMessage"), FunctionTemplate::New(VirtualMachine::_sendCanNMTMessage)); |
| 83 | myGlobal->Set(String::New("loadScript"), FunctionTemplate::New(VirtualMachine::_loadScript)); |
83 | myGlobal->Set(String::New("loadScript"), FunctionTemplate::New(VirtualMachine::_loadScript)); |
| 84 | myGlobal->Set(String::New("startIntervalThread"), FunctionTemplate::New(VirtualMachine::_startIntervalThread)); |
84 | myGlobal->Set(String::New("startIntervalThread"), FunctionTemplate::New(VirtualMachine::_startIntervalThread)); |
| 85 | myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine::_stopIntervalThread)); |
85 | myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine::_stopIntervalThread)); |
| 86 | myGlobal->Set(String::New("startSocketThread"), FunctionTemplate::New(VirtualMachine::_startSocketThread)); |
86 | myGlobal->Set(String::New("startSocketThread"), FunctionTemplate::New(VirtualMachine::_startSocketThread)); |
| 87 | myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine::_stopSocketThread)); |
87 | myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine::_stopSocketThread)); |
| 88 | myGlobal->Set(String::New("sendToSocketThread"), FunctionTemplate::New(VirtualMachine::_sendToSocketThread)); |
88 | myGlobal->Set(String::New("sendToSocketThread"), FunctionTemplate::New(VirtualMachine::_sendToSocketThread)); |
| 89 | myGlobal->Set(String::New("uint2hex"), FunctionTemplate::New(VirtualMachine::_uint2hex)); |
89 | myGlobal->Set(String::New("uint2hex"), FunctionTemplate::New(VirtualMachine::_uint2hex)); |
| - | 90 | myGlobal->Set(String::New("loadDataStore"), FunctionTemplate::New(VirtualMachine::_loadDataStore)); |
|
| 90 | myContext = Context::New(NULL, myGlobal); |
91 | myContext = Context::New(NULL, myGlobal); |
| 91 | 92 | ||
| 92 | 93 | ||
| 93 | if (loadScript("System/Base.js")) |
94 | if (loadScript("System/Base.js")) |
| 94 | { |
95 | { |
| 95 | loadScript("System/Startup.js"); |
96 | loadScript("System/Startup.js"); |
| 96 | 97 | ||
| 97 | loadScript("Autostart.js"); |
98 | loadScript("Autostart.js"); |
| 98 | 99 | ||
| 99 | Context::Scope contextScope(myContext); |
100 | Context::Scope contextScope(myContext); |
| 100 | 101 | ||
| 101 | myFunctionStartup = Handle<Function>::Cast(myContext->Global()->Get(String::New("startup"))); |
102 | myFunctionStartup = Handle<Function>::Cast(myContext->Global()->Get(String::New("startup"))); |
| 102 | } |
103 | } |
| 103 | else |
104 | else |
| 104 | { |
105 | { |
| 105 | ///FIXME: We should probably exit the application here |
106 | ///FIXME: We should probably exit the application here |
| 106 | return; |
107 | return; |
| 107 | } |
108 | } |
| 108 | 109 | ||
| 109 | const int argc = 0; |
110 | const int argc = 0; |
| 110 | Handle<Value> argv[argc] = { }; |
111 | Handle<Value> argv[argc] = { }; |
| 111 | Handle<Value> result = myFunctionStartup->Call(myFunctionStartup, argc, argv); |
112 | Handle<Value> result = myFunctionStartup->Call(myFunctionStartup, argc, argv); |
| 112 | 113 | ||
| 113 | 114 | ||
| 114 | string commandPort = Settings::get("CommandPort"); |
115 | string commandPort = Settings::get("CommandPort"); |
| 115 | if (commandPort != "") |
116 | if (commandPort != "") |
| 116 | { |
117 | { |
| 117 | myCommandThread.setSettings(stoi(commandPort), "Command console"); |
118 | myCommandThread.setSettings(stoi(commandPort), "Command console"); |
| 118 | myCommandThread.start(); |
119 | myCommandThread.start(); |
| 119 | } |
120 | } |
| 120 | else |
121 | else |
| 121 | { |
122 | { |
| 122 | SyslogStream &slog = SyslogStream::getInstance(); |
123 | SyslogStream &slog = SyslogStream::getInstance(); |
| 123 | slog << "CommandPort is not defined, can not start Command console socket\n"; |
124 | slog << "CommandPort is not defined, can not start Command console socket\n"; |
| 124 | } |
125 | } |
| 125 | 126 | ||
| 126 | 127 | ||
| 127 | mySemaphore.lock(); |
128 | mySemaphore.lock(); |
| 128 | 129 | ||
| 129 | while (true) |
130 | while (true) |
| 130 | { |
131 | { |
| 131 | while (myExpressions.size() > 0) |
132 | while (myExpressions.size() > 0) |
| 132 | { |
133 | { |
| 133 | runExpression(myExpressions.pop()); |
134 | runExpression(myExpressions.pop()); |
| 134 | } |
135 | } |
| 135 | 136 | ||
| 136 | mySemaphore.wait(); |
137 | mySemaphore.wait(); |
| 137 | } |
138 | } |
| 138 | 139 | ||
| 139 | mySemaphore.unlock(); |
140 | mySemaphore.unlock(); |
| 140 | } |
141 | } |
| 141 | 142 | ||
| 142 | void VirtualMachine::queueExpression(Expression expression) |
143 | void VirtualMachine::queueExpression(Expression expression) |
| 143 | { |
144 | { |
| 144 | myExpressions.push(expression); |
145 | myExpressions.push(expression); |
| 145 | mySemaphore.broadcast(); |
146 | mySemaphore.broadcast(); |
| 146 | } |
147 | } |
| 147 | 148 | ||
| 148 | string VirtualMachine::formatException(TryCatch* tryCatch) |
149 | string VirtualMachine::formatException(TryCatch* tryCatch) |
| 149 | { |
150 | { |
| 150 | string result; |
151 | string result; |
| 151 | 152 | ||
| 152 | HandleScope handleScope; |
153 | HandleScope handleScope; |
| 153 | String::Utf8Value exception(tryCatch->Exception()); |
154 | String::Utf8Value exception(tryCatch->Exception()); |
| 154 | Handle<v8::Message> message = tryCatch->Message(); |
155 | Handle<v8::Message> message = tryCatch->Message(); |
| 155 | 156 | ||
| 156 | string strException = *exception; |
157 | string strException = *exception; |
| 157 | 158 | ||
| 158 | if (message.IsEmpty()) |
159 | if (message.IsEmpty()) |
| 159 | { |
160 | { |
| 160 | result += strException + "\n"; |
161 | result += strException + "\n"; |
| 161 | } |
162 | } |
| 162 | else |
163 | else |
| 163 | { |
164 | { |
| 164 | String::Utf8Value filename(message->GetScriptResourceName()); |
165 | String::Utf8Value filename(message->GetScriptResourceName()); |
| 165 | string strFilename = *filename; |
166 | string strFilename = *filename; |
| 166 | 167 | ||
| 167 | result += strFilename + ":" + itos(message->GetLineNumber()) + ": " + strException + "\n"; |
168 | result += strFilename + ":" + itos(message->GetLineNumber()) + ": " + strException + "\n"; |
| 168 | 169 | ||
| 169 | String::Utf8Value sourceline(message->GetSourceLine()); |
170 | String::Utf8Value sourceline(message->GetSourceLine()); |
| 170 | string strSourceline = *sourceline; |
171 | string strSourceline = *sourceline; |
| 171 | 172 | ||
| 172 | result += strSourceline + "\n"; |
173 | result += strSourceline + "\n"; |
| 173 | 174 | ||
| 174 | string underline = rpad("", message->GetStartColumn(), ' '); |
175 | string underline = rpad("", message->GetStartColumn(), ' '); |
| 175 | underline = rpad(underline, message->GetEndColumn(), '^'); |
176 | underline = rpad(underline, message->GetEndColumn(), '^'); |
| 176 | 177 | ||
| 177 | result += underline + "\n"; |
178 | result += underline + "\n"; |
| 178 | } |
179 | } |
| 179 | 180 | ||
| 180 | return result; |
181 | return result; |
| - | 182 | } |
|
| - | 183 | ||
| - | 184 | bool VirtualMachine::loadDataStore(string storeName) |
|
| - | 185 | { |
|
| - | 186 | SyslogStream &slog = SyslogStream::getInstance(); |
|
| - | 187 | ||
| - | 188 | string basePath = Settings::get("BasePath") + "Services/DataStores/"; |
|
| - | 189 | string scriptFileName = basePath + storeName + ".js"; |
|
| - | 190 | ||
| - | 191 | if (!file_exists(scriptFileName)) |
|
| - | 192 | { |
|
| - | 193 | slog << "Failed to load datastore " + scriptFileName + ", file does not exist.\n"; |
|
| - | 194 | return false; |
|
| - | 195 | } |
|
| - | 196 | ||
| - | 197 | string scriptSource = file_get_contents(scriptFileName); |
|
| - | 198 | ||
| - | 199 | scriptSource = "DataStore.addStore('" + storeName + "', " + str_replace("\n", "", scriptSource) + ");"; |
|
| - | 200 | ||
| - | 201 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
|
| - | 202 | Handle<String> name = String::New(scriptFileName.c_str(), scriptFileName.length()); |
|
| - | 203 | ||
| - | 204 | Context::Scope contextScope(myContext); |
|
| - | 205 | ||
| - | 206 | TryCatch tryCatch; |
|
| - | 207 | ||
| - | 208 | Handle<Script> script = Script::Compile(source, name); |
|
| - | 209 | ||
| - | 210 | if (script.IsEmpty()) |
|
| - | 211 | { |
|
| - | 212 | slog << formatException(&tryCatch); |
|
| - | 213 | return false; |
|
| - | 214 | } |
|
| - | 215 | else |
|
| - | 216 | { |
|
| - | 217 | Handle<Value> result = script->Run(); |
|
| - | 218 | ||
| - | 219 | if (result.IsEmpty()) |
|
| - | 220 | { |
|
| - | 221 | slog << formatException(&tryCatch); |
|
| - | 222 | return false; |
|
| - | 223 | } |
|
| - | 224 | ||
| - | 225 | slog << "Loaded datastore " + storeName + "\n"; |
|
| - | 226 | } |
|
| - | 227 | ||
| - | 228 | return true; |
|
| 181 | } |
229 | } |
| 182 | 230 | ||
| 183 | bool VirtualMachine::loadScript(string scriptName) |
231 | bool VirtualMachine::loadScript(string scriptName) |
| 184 | { |
232 | { |
| 185 | SyslogStream &slog = SyslogStream::getInstance(); |
233 | SyslogStream &slog = SyslogStream::getInstance(); |
| 186 | 234 | ||
| 187 | string basePath = Settings::get("BasePath") + "Services/"; |
235 | string basePath = Settings::get("BasePath") + "Services/"; |
| 188 | string scriptFileName = basePath + scriptName; |
236 | string scriptFileName = basePath + scriptName; |
| 189 | 237 | ||
| 190 | if (!file_exists(scriptFileName)) |
238 | if (!file_exists(scriptFileName)) |
| 191 | { |
239 | { |
| 192 | slog << "Failed to load " + scriptFileName + ", file does not exist.\n"; |
240 | slog << "Failed to load " + scriptFileName + ", file does not exist.\n"; |
| 193 | return false; |
241 | return false; |
| 194 | } |
242 | } |
| 195 | 243 | ||
| 196 | string scriptSource = file_get_contents(scriptFileName); |
244 | string scriptSource = file_get_contents(scriptFileName); |
| 197 | 245 | ||
| 198 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
246 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
| 199 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
247 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 200 | 248 | ||
| 201 | Context::Scope contextScope(myContext); |
249 | Context::Scope contextScope(myContext); |
| 202 | 250 | ||
| 203 | TryCatch tryCatch; |
251 | TryCatch tryCatch; |
| 204 | 252 | ||
| 205 | Handle<Script> script = Script::Compile(source, name); |
253 | Handle<Script> script = Script::Compile(source, name); |
| 206 | 254 | ||
| 207 | if (script.IsEmpty()) |
255 | if (script.IsEmpty()) |
| 208 | { |
256 | { |
| 209 | slog << formatException(&tryCatch); |
257 | slog << formatException(&tryCatch); |
| 210 | return false; |
258 | return false; |
| 211 | } |
259 | } |
| 212 | else |
260 | else |
| 213 | { |
261 | { |
| 214 | Handle<Value> result = script->Run(); |
262 | Handle<Value> result = script->Run(); |
| 215 | 263 | ||
| 216 | if (result.IsEmpty()) |
264 | if (result.IsEmpty()) |
| 217 | { |
265 | { |
| 218 | slog << formatException(&tryCatch); |
266 | slog << formatException(&tryCatch); |
| 219 | return false; |
267 | return false; |
| 220 | } |
268 | } |
| 221 | 269 | ||
| 222 | slog << "Loaded " + scriptName + "\n"; |
270 | slog << "Loaded " + scriptName + "\n"; |
| 223 | } |
271 | } |
| 224 | 272 | ||
| 225 | return true; |
273 | return true; |
| 226 | } |
274 | } |
| 227 | 275 | ||
| 228 | unsigned int VirtualMachine::startIntervalThread(unsigned int timeout) |
276 | unsigned int VirtualMachine::startIntervalThread(unsigned int timeout) |
| 229 | { |
277 | { |
| 230 | IntervalThread *intervalThread = new IntervalThread(timeout); |
278 | IntervalThread *intervalThread = new IntervalThread(timeout); |
| 231 | 279 | ||
| 232 | myIntervalThreads[intervalThread->getId()] = intervalThread; |
280 | myIntervalThreads[intervalThread->getId()] = intervalThread; |
| 233 | myIntervalThreads[intervalThread->getId()]->start(); |
281 | myIntervalThreads[intervalThread->getId()]->start(); |
| 234 | 282 | ||
| 235 | return intervalThread->getId(); |
283 | return intervalThread->getId(); |
| 236 | } |
284 | } |
| 237 | 285 | ||
| 238 | bool VirtualMachine::stopIntervalThread(unsigned int id) |
286 | bool VirtualMachine::stopIntervalThread(unsigned int id) |
| 239 | { |
287 | { |
| 240 | if (myIntervalThreads.find(id) != myIntervalThreads.end()) |
288 | if (myIntervalThreads.find(id) != myIntervalThreads.end()) |
| 241 | { |
289 | { |
| 242 | delete myIntervalThreads[id]; |
290 | delete myIntervalThreads[id]; |
| 243 | myIntervalThreads.erase(id); |
291 | myIntervalThreads.erase(id); |
| 244 | return true; |
292 | return true; |
| 245 | } |
293 | } |
| 246 | 294 | ||
| 247 | return false; |
295 | return false; |
| 248 | } |
296 | } |
| 249 | 297 | ||
| 250 | unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout) |
298 | unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout) |
| 251 | { |
299 | { |
| 252 | SocketThread *socketThread = new SocketThread(address, port, reconnectTimeout); |
300 | SocketThread *socketThread = new SocketThread(address, port, reconnectTimeout); |
| 253 | mySocketThreads[socketThread->getId()] = socketThread; |
301 | mySocketThreads[socketThread->getId()] = socketThread; |
| 254 | mySocketThreads[socketThread->getId()]->start(); |
302 | mySocketThreads[socketThread->getId()]->start(); |
| 255 | 303 | ||
| 256 | return socketThread->getId(); |
304 | return socketThread->getId(); |
| 257 | } |
305 | } |
| 258 | 306 | ||
| 259 | bool VirtualMachine::stopSocketThread(unsigned int id) |
307 | bool VirtualMachine::stopSocketThread(unsigned int id) |
| 260 | { |
308 | { |
| 261 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
309 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
| 262 | { |
310 | { |
| 263 | delete mySocketThreads[id]; |
311 | delete mySocketThreads[id]; |
| 264 | mySocketThreads.erase(id); |
312 | mySocketThreads.erase(id); |
| 265 | return true; |
313 | return true; |
| 266 | } |
314 | } |
| 267 | 315 | ||
| 268 | return false; |
316 | return false; |
| 269 | } |
317 | } |
| 270 | 318 | ||
| 271 | void VirtualMachine::sendToSocketThread(unsigned int id, string data) |
319 | void VirtualMachine::sendToSocketThread(unsigned int id, string data) |
| 272 | { |
320 | { |
| 273 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
321 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
| 274 | { |
322 | { |
| 275 | mySocketThreads[id]->send(data); |
323 | mySocketThreads[id]->send(data); |
| 276 | } |
324 | } |
| 277 | } |
325 | } |
| 278 | 326 | ||
| 279 | void VirtualMachine::disconnectCommandClient(unsigned int id) |
327 | void VirtualMachine::disconnectCommandClient(unsigned int id) |
| 280 | { |
328 | { |
| 281 | myCommandThread.disconnectClient(id); |
329 | myCommandThread.disconnectClient(id); |
| 282 | } |
330 | } |
| 283 | 331 | ||
| 284 | void VirtualMachine::sendToCommandClient(unsigned int id, string data) |
332 | void VirtualMachine::sendToCommandClient(unsigned int id, string data) |
| 285 | { |
333 | { |
| 286 | myCommandThread.sendTo(id, data); |
334 | myCommandThread.sendTo(id, data); |
| 287 | } |
335 | } |
| 288 | 336 | ||
| 289 | void VirtualMachine::print(unsigned int id, string data) |
337 | void VirtualMachine::print(unsigned int id, string data) |
| 290 | { |
338 | { |
| 291 | if (id == 0) |
339 | if (id == 0) |
| 292 | { |
340 | { |
| 293 | SyslogStream &slog = SyslogStream::getInstance(); |
341 | SyslogStream &slog = SyslogStream::getInstance(); |
| 294 | slog << data; |
342 | slog << data; |
| 295 | } |
343 | } |
| 296 | else |
344 | else |
| 297 | { |
345 | { |
| 298 | VirtualMachine &vm = VirtualMachine::getInstance(); |
346 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 299 | vm.sendToCommandClient(id, data); |
347 | vm.sendToCommandClient(id, data); |
| 300 | } |
348 | } |
| 301 | } |
349 | } |
| 302 | 350 | ||
| 303 | bool VirtualMachine::runExpression(Expression expression) |
351 | bool VirtualMachine::runExpression(Expression expression) |
| 304 | { |
352 | { |
| 305 | string scriptName = "runExpression"; |
353 | string scriptName = "runExpression"; |
| 306 | 354 | ||
| 307 | string scriptData = expression.getScript(); |
355 | string scriptData = expression.getScript(); |
| 308 | 356 | ||
| 309 | scriptData = "setClientId(" + itos(expression.getTargetId()) + ");\n" + scriptData; |
357 | scriptData = "setClientId(" + itos(expression.getTargetId()) + ");\n" + scriptData; |
| 310 | 358 | ||
| 311 | Handle<String> source = String::New(scriptData.c_str(), scriptData.length()); |
359 | Handle<String> source = String::New(scriptData.c_str(), scriptData.length()); |
| 312 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
360 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 313 | 361 | ||
| 314 | Context::Scope contextScope(myContext); |
362 | Context::Scope contextScope(myContext); |
| 315 | 363 | ||
| 316 | TryCatch tryCatch; |
364 | TryCatch tryCatch; |
| 317 | 365 | ||
| 318 | Handle<Script> script = Script::Compile(source, name); |
366 | Handle<Script> script = Script::Compile(source, name); |
| 319 | 367 | ||
| 320 | if (script.IsEmpty()) |
368 | if (script.IsEmpty()) |
| 321 | { |
369 | { |
| 322 | print(expression.getTargetId(), formatException(&tryCatch)); |
370 | print(expression.getTargetId(), formatException(&tryCatch)); |
| 323 | return false; |
371 | return false; |
| 324 | } |
372 | } |
| 325 | else |
373 | else |
| 326 | { |
374 | { |
| 327 | Handle<Value> result = script->Run(); |
375 | Handle<Value> result = script->Run(); |
| 328 | 376 | ||
| 329 | if (result.IsEmpty()) |
377 | if (result.IsEmpty()) |
| 330 | { |
378 | { |
| 331 | print(expression.getTargetId(), formatException(&tryCatch)); |
379 | print(expression.getTargetId(), formatException(&tryCatch)); |
| 332 | return false; |
380 | return false; |
| 333 | } |
381 | } |
| 334 | else if (expression.getTargetId() != 0) |
382 | else if (expression.getTargetId() != 0) |
| 335 | { |
383 | { |
| 336 | String::AsciiValue ascii(result); |
384 | String::AsciiValue ascii(result); |
| 337 | string resultString = *ascii; |
385 | string resultString = *ascii; |
| 338 | 386 | ||
| 339 | if (resultString != "undefined") |
387 | if (resultString != "undefined") |
| 340 | { |
388 | { |
| 341 | print(expression.getTargetId(), resultString + "\n"); |
389 | print(expression.getTargetId(), resultString + "\n"); |
| 342 | } |
390 | } |
| 343 | } |
391 | } |
| 344 | } |
392 | } |
| 345 | 393 | ||
| 346 | return true; |
394 | return true; |
| 347 | } |
395 | } |
| 348 | 396 | ||
| 349 | 397 | ||
| 350 | Handle<Value> VirtualMachine::_quit(const Arguments& args) |
398 | Handle<Value> VirtualMachine::_quit(const Arguments& args) |
| 351 | { |
399 | { |
| 352 | VirtualMachine &vm = VirtualMachine::getInstance(); |
400 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 353 | 401 | ||
| 354 | vm.disconnectCommandClient(args[0]->Uint32Value()); |
402 | vm.disconnectCommandClient(args[0]->Uint32Value()); |
| 355 | 403 | ||
| 356 | return Undefined(); |
404 | return Undefined(); |
| 357 | } |
405 | } |
| 358 | 406 | ||
| 359 | Handle<Value> VirtualMachine::_log(const Arguments& args) |
407 | Handle<Value> VirtualMachine::_log(const Arguments& args) |
| 360 | { |
408 | { |
| 361 | SyslogStream &slog = SyslogStream::getInstance(); |
409 | SyslogStream &slog = SyslogStream::getInstance(); |
| 362 | 410 | ||
| 363 | String::AsciiValue str(args[0]); |
411 | String::AsciiValue str(args[0]); |
| 364 | 412 | ||
| 365 | slog << *str; |
413 | slog << *str; |
| 366 | 414 | ||
| 367 | return Undefined(); |
415 | return Undefined(); |
| 368 | } |
416 | } |
| 369 | 417 | ||
| 370 | Handle<Value> VirtualMachine::_print(const Arguments& args) |
418 | Handle<Value> VirtualMachine::_print(const Arguments& args) |
| 371 | { |
419 | { |
| 372 | VirtualMachine &vm = VirtualMachine::getInstance(); |
420 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 373 | 421 | ||
| 374 | String::AsciiValue str(args[1]); |
422 | String::AsciiValue str(args[1]); |
| 375 | 423 | ||
| 376 | vm.print(args[0]->Uint32Value(), *str); |
424 | vm.print(args[0]->Uint32Value(), *str); |
| 377 | 425 | ||
| 378 | return Undefined(); |
426 | return Undefined(); |
| 379 | } |
427 | } |
| 380 | 428 | ||
| 381 | Handle<Value> VirtualMachine::_sendCanMessage(const Arguments& args) |
429 | Handle<Value> VirtualMachine::_sendCanMessage(const Arguments& args) |
| 382 | { |
430 | { |
| 383 | CanNetManager &canMan = CanNetManager::getInstance(); |
431 | CanNetManager &canMan = CanNetManager::getInstance(); |
| 384 | 432 | ||
| 385 | CanMessage canMessage; |
433 | CanMessage canMessage; |
| 386 | 434 | ||
| 387 | String::AsciiValue className(args[0]); |
435 | String::AsciiValue className(args[0]); |
| 388 | canMessage.setClassName(*className); |
436 | canMessage.setClassName(*className); |
| 389 | String::AsciiValue directionFlag(args[1]); |
437 | String::AsciiValue directionFlag(args[1]); |
| 390 | canMessage.setDirectionFlag(*directionFlag); |
438 | canMessage.setDirectionFlag(*directionFlag); |
| 391 | String::AsciiValue moduleName(args[2]); |
439 | String::AsciiValue moduleName(args[2]); |
| 392 | canMessage.setModuleName(*moduleName); |
440 | canMessage.setModuleName(*moduleName); |
| 393 | canMessage.setModuleId(args[3]->Uint32Value()); |
441 | canMessage.setModuleId(args[3]->Uint32Value()); |
| 394 | String::AsciiValue commandName(args[4]); |
442 | String::AsciiValue commandName(args[4]); |
| 395 | canMessage.setCommandName(*commandName); |
443 | canMessage.setCommandName(*commandName); |
| 396 | 444 | ||
| 397 | String::AsciiValue dataString(args[5]); |
445 | String::AsciiValue dataString(args[5]); |
| 398 | 446 | ||
| 399 | vector<string> parts = explode(",", trim(*dataString, ',')); |
447 | vector<string> parts = explode(",", trim(*dataString, ',')); |
| 400 | map<string, CanVariable> data; |
448 | map<string, CanVariable> data; |
| 401 | 449 | ||
| 402 | for (int n = 0; n < parts.size(); n++) |
450 | for (int n = 0; n < parts.size(); n++) |
| 403 | { |
451 | { |
| 404 | vector<string> keyAndValue = explode(":", parts[n]); |
452 | vector<string> keyAndValue = explode(":", parts[n]); |
| 405 | 453 | ||
| 406 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
454 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
| 407 | } |
455 | } |
| 408 | 456 | ||
| 409 | canMessage.setData(data); |
457 | canMessage.setData(data); |
| 410 | 458 | ||
| 411 | canMan.sendMessage(canMessage); |
459 | canMan.sendMessage(canMessage); |
| 412 | 460 | ||
| 413 | return Undefined(); |
461 | return Undefined(); |
| 414 | } |
462 | } |
| 415 | 463 | ||
| 416 | Handle<Value> VirtualMachine::_sendCanNMTMessage(const Arguments& args) |
464 | Handle<Value> VirtualMachine::_sendCanNMTMessage(const Arguments& args) |
| 417 | { |
465 | { |
| 418 | CanNetManager &canMan = CanNetManager::getInstance(); |
466 | CanNetManager &canMan = CanNetManager::getInstance(); |
| 419 | 467 | ||
| 420 | CanMessage canMessage; |
468 | CanMessage canMessage; |
| 421 | 469 | ||
| 422 | String::AsciiValue className(args[0]); |
470 | String::AsciiValue className(args[0]); |
| 423 | canMessage.setClassName(*className); |
471 | canMessage.setClassName(*className); |
| 424 | String::AsciiValue commandName(args[1]); |
472 | String::AsciiValue commandName(args[1]); |
| 425 | canMessage.setCommandName(*commandName); |
473 | canMessage.setCommandName(*commandName); |
| 426 | 474 | ||
| 427 | String::AsciiValue dataString(args[2]); |
475 | String::AsciiValue dataString(args[2]); |
| 428 | 476 | ||
| 429 | vector<string> parts = explode(",", trim(*dataString, ',')); |
477 | vector<string> parts = explode(",", trim(*dataString, ',')); |
| 430 | map<string, CanVariable> data; |
478 | map<string, CanVariable> data; |
| 431 | 479 | ||
| 432 | for (int n = 0; n < parts.size(); n++) |
480 | for (int n = 0; n < parts.size(); n++) |
| 433 | { |
481 | { |
| 434 | vector<string> keyAndValue = explode(":", parts[n]); |
482 | vector<string> keyAndValue = explode(":", parts[n]); |
| 435 | 483 | ||
| 436 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
484 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
| 437 | } |
485 | } |
| 438 | 486 | ||
| 439 | canMessage.setData(data); |
487 | canMessage.setData(data); |
| 440 | 488 | ||
| 441 | canMan.sendMessage(canMessage); |
489 | canMan.sendMessage(canMessage); |
| 442 | 490 | ||
| 443 | return Undefined(); |
491 | return Undefined(); |
| 444 | } |
492 | } |
| 445 | 493 | ||
| 446 | Handle<Value> VirtualMachine::_loadScript(const Arguments& args) |
494 | Handle<Value> VirtualMachine::_loadScript(const Arguments& args) |
| 447 | { |
495 | { |
| 448 | VirtualMachine &vm = VirtualMachine::getInstance(); |
496 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 449 | 497 | ||
| 450 | String::AsciiValue str(args[0]); |
498 | String::AsciiValue str(args[0]); |
| 451 | 499 | ||
| 452 | return Boolean::New(vm.loadScript(*str)); |
500 | return Boolean::New(vm.loadScript(*str)); |
| - | 501 | } |
|
| - | 502 | ||
| - | 503 | Handle<Value> VirtualMachine::_loadDataStore(const Arguments& args) |
|
| - | 504 | { |
|
| - | 505 | VirtualMachine &vm = VirtualMachine::getInstance(); |
|
| - | 506 | ||
| - | 507 | String::AsciiValue str(args[0]); |
|
| - | 508 | ||
| - | 509 | return Boolean::New(vm.loadDataStore(*str)); |
|
| 453 | } |
510 | } |
| 454 | 511 | ||
| 455 | Handle<Value> VirtualMachine::_startIntervalThread(const Arguments& args) |
512 | Handle<Value> VirtualMachine::_startIntervalThread(const Arguments& args) |
| 456 | { |
513 | { |
| 457 | VirtualMachine &vm = VirtualMachine::getInstance(); |
514 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 458 | return Integer::New(vm.startIntervalThread(args[0]->Uint32Value())); |
515 | return Integer::New(vm.startIntervalThread(args[0]->Uint32Value())); |
| 459 | } |
516 | } |
| 460 | 517 | ||
| 461 | Handle<Value> VirtualMachine::_stopIntervalThread(const Arguments& args) |
518 | Handle<Value> VirtualMachine::_stopIntervalThread(const Arguments& args) |
| 462 | { |
519 | { |
| 463 | VirtualMachine &vm = VirtualMachine::getInstance(); |
520 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 464 | return Boolean::New(vm.stopIntervalThread(args[0]->Uint32Value())); |
521 | return Boolean::New(vm.stopIntervalThread(args[0]->Uint32Value())); |
| 465 | } |
522 | } |
| 466 | 523 | ||
| 467 | Handle<Value> VirtualMachine::_startSocketThread(const Arguments& args) |
524 | Handle<Value> VirtualMachine::_startSocketThread(const Arguments& args) |
| 468 | { |
525 | { |
| 469 | VirtualMachine &vm = VirtualMachine::getInstance(); |
526 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 470 | 527 | ||
| 471 | String::AsciiValue address(args[0]); |
528 | String::AsciiValue address(args[0]); |
| 472 | 529 | ||
| 473 | return Integer::New(vm.startSocketThread(*address, args[1]->Uint32Value(), args[2]->Uint32Value())); |
530 | return Integer::New(vm.startSocketThread(*address, args[1]->Uint32Value(), args[2]->Uint32Value())); |
| 474 | } |
531 | } |
| 475 | 532 | ||
| 476 | Handle<Value> VirtualMachine::_stopSocketThread(const Arguments& args) |
533 | Handle<Value> VirtualMachine::_stopSocketThread(const Arguments& args) |
| 477 | { |
534 | { |
| 478 | VirtualMachine &vm = VirtualMachine::getInstance(); |
535 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 479 | 536 | ||
| 480 | return Boolean::New(vm.stopSocketThread(args[0]->Uint32Value())); |
537 | return Boolean::New(vm.stopSocketThread(args[0]->Uint32Value())); |
| 481 | } |
538 | } |
| 482 | 539 | ||
| 483 | Handle<Value> VirtualMachine::_sendToSocketThread(const Arguments& args) |
540 | Handle<Value> VirtualMachine::_sendToSocketThread(const Arguments& args) |
| 484 | { |
541 | { |
| 485 | VirtualMachine &vm = VirtualMachine::getInstance(); |
542 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 486 | 543 | ||
| 487 | String::AsciiValue data(args[1]); |
544 | String::AsciiValue data(args[1]); |
| 488 | 545 | ||
| 489 | vm.sendToSocketThread(args[0]->Uint32Value(), *data); |
546 | vm.sendToSocketThread(args[0]->Uint32Value(), *data); |
| 490 | 547 | ||
| 491 | return Undefined(); |
548 | return Undefined(); |
| 492 | } |
549 | } |
| 493 | 550 | ||
| 494 | Handle<Value> VirtualMachine::_uint2hex(const Arguments& args) |
551 | Handle<Value> VirtualMachine::_uint2hex(const Arguments& args) |
| 495 | { |
552 | { |
| 496 | VirtualMachine &vm = VirtualMachine::getInstance(); |
553 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 497 | 554 | ||
| 498 | string hexId = uint2hex(args[0]->Uint32Value(), args[1]->Uint32Value()); |
555 | string hexId = uint2hex(args[0]->Uint32Value(), args[1]->Uint32Value()); |
| 499 | 556 | ||
| 500 | return String::New(hexId.c_str()); |
557 | return String::New(hexId.c_str()); |
| 501 | } |
558 | } |
| 502 | - | ||
| 503 | 559 | ||