Rev 977 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 977 | Rev 981 | ||
|---|---|---|---|
| Line 16... | Line 16... | ||
| 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 | - | ||
| 22 | #include <map> |
- | |
| 23 | - | ||
| 24 | 21 | ||
| 25 | #include "virtualmachine.h" |
22 | #include "virtualmachine.h" |
| 26 | 23 | ||
| 27 | VirtualMachine* VirtualMachine::myInstance = NULL; |
24 | VirtualMachine* VirtualMachine::myInstance = NULL; |
| 28 | 25 | ||
| Line 32... | Line 29... | ||
| 32 | { |
29 | { |
| 33 | myInstance = new VirtualMachine(); |
30 | myInstance = new VirtualMachine(); |
| 34 | } |
31 | } |
| 35 | 32 | ||
| 36 | return *myInstance; |
33 | return *myInstance; |
| 37 | } |
34 | } |
| 38 | 35 | ||
| 39 | void VirtualMachine::deleteInstance() |
36 | void VirtualMachine::deleteInstance() |
| 40 | { |
37 | { |
| 41 | if (myInstance != NULL) |
38 | if (myInstance != NULL) |
| 42 | { |
39 | { |
| Line 44... | Line 41... | ||
| 44 | myInstance = NULL; |
41 | myInstance = NULL; |
| 45 | } |
42 | } |
| 46 | } |
43 | } |
| 47 | 44 | ||
| 48 | VirtualMachine::VirtualMachine() |
45 | VirtualMachine::VirtualMachine() |
| 49 | { |
46 | { |
| 50 | - | ||
| - | 47 | Thread<VirtualMachine>(); |
|
| 51 | } |
48 | } |
| 52 | 49 | ||
| 53 | VirtualMachine::~VirtualMachine() |
50 | VirtualMachine::~VirtualMachine() |
| 54 | { |
51 | { |
| 55 | 52 | stop(); |
|
| 56 | } |
53 | } |
| 57 | 54 | ||
| 58 | void VirtualMachine::run() |
55 | void VirtualMachine::run() |
| 59 | { |
56 | { |
| 60 | SyslogStream &slog = SyslogStream::getInstance(); |
57 | SyslogStream &slog = SyslogStream::getInstance(); |
| 61 | 58 | ||
| 62 | string basePath = Settings::get("BasePath") + "Services/"; |
59 | string basePath = Settings::get("BasePath") + "Services/"; |
| 63 | string scriptName = "Base.js"; |
60 | string scriptName = "Base.js"; |
| 64 | string scriptFileName = basePath + "System/" + scriptName; |
61 | string scriptFileName = basePath + "System/" + scriptName; |
| 65 | 62 | ||
| 66 | if (!file_exists(scriptFileName)) |
63 | if (!file_exists(scriptFileName)) |
| 67 | { |
64 | { |
| 68 | slog << "Failed to load :" + scriptFileName + "\n"; |
65 | slog << "Failed to load :" + scriptFileName + "\n"; |
| 69 | } |
66 | } |
| 70 | 67 | ||
| Line 73... | Line 70... | ||
| 73 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
70 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
| 74 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
71 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
| 75 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
72 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 76 | 73 | ||
| 77 | // Create a template for the global object. |
74 | // Create a template for the global object. |
| 78 | myGlobal = ObjectTemplate::New(); |
75 | myGlobal = ObjectTemplate::New(); |
| 79 | 76 | ||
| 80 | //associates "print" on script to the Print function |
77 | //associates "print" on script to the Print function |
| 81 | myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine_log)); |
78 | myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine_log)); |
| 82 | myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine_sendCanMessage)); |
79 | myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine_sendCanMessage)); |
| 83 | myGlobal->Set(String::New("loadScript"), FunctionTemplate::New(VirtualMachine_loadScript)); |
80 | myGlobal->Set(String::New("loadScript"), FunctionTemplate::New(VirtualMachine_loadScript)); |
| 84 | myGlobal->Set(String::New("startIntervalThread"), FunctionTemplate::New(VirtualMachine_startIntervalThread)); |
81 | myGlobal->Set(String::New("startIntervalThread"), FunctionTemplate::New(VirtualMachine_startIntervalThread)); |
| 85 | myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine_stopIntervalThread)); |
82 | myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine_stopIntervalThread)); |
| 86 | myGlobal->Set(String::New("startSocketThread"), FunctionTemplate::New(VirtualMachine_startSocketThread)); |
83 | myGlobal->Set(String::New("startSocketThread"), FunctionTemplate::New(VirtualMachine_startSocketThread)); |
| 87 | myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine_stopSocketThread)); |
84 | myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine_stopSocketThread)); |
| 88 | myGlobal->Set(String::New("sendToSocketThread"), FunctionTemplate::New(VirtualMachine_sendToSocketThread)); |
85 | myGlobal->Set(String::New("sendToSocketThread"), FunctionTemplate::New(VirtualMachine_sendToSocketThread)); |
| - | 86 | myGlobal->Set(String::New("uint2hex"), FunctionTemplate::New(VirtualMachine_uint2hex)); |
|
| 89 | 87 | ||
| 90 | //create context for the script |
88 | //create context for the script |
| 91 | myContext = Context::New(NULL, myGlobal); |
89 | myContext = Context::New(NULL, myGlobal); |
| 92 | 90 | ||
| 93 | 91 | ||
| Line 184... | Line 182... | ||
| 184 | 182 | ||
| 185 | void VirtualMachine::queueExpression(string expression) |
183 | void VirtualMachine::queueExpression(string expression) |
| 186 | { |
184 | { |
| 187 | myExpressions.push(expression); |
185 | myExpressions.push(expression); |
| 188 | mySemaphore.broadcast(); |
186 | mySemaphore.broadcast(); |
| 189 | } |
187 | } |
| 190 | 188 | ||
| 191 | bool VirtualMachine::loadScript(string scriptName) |
189 | bool VirtualMachine::loadScript(string scriptName) |
| 192 | { |
190 | { |
| 193 | SyslogStream &slog = SyslogStream::getInstance(); |
191 | SyslogStream &slog = SyslogStream::getInstance(); |
| 194 | 192 | ||
| 195 | string basePath = Settings::get("BasePath") + " |
193 | string basePath = Settings::get("BasePath") + "Services/"; |
| 196 | string scriptFileName = basePath + scriptName; |
194 | string scriptFileName = basePath + scriptName; |
| 197 | 195 | ||
| 198 | if (!file_exists(scriptFileName)) |
196 | if (!file_exists(scriptFileName)) |
| 199 | { |
197 | { |
| 200 | slog << "Failed to load |
198 | slog << "Failed to load " + scriptFileName + "\n"; |
| 201 | return false; |
199 | return false; |
| 202 | } |
200 | } |
| 203 | 201 | ||
| 204 | string scriptSource = file_get_contents(scriptFileName); |
202 | string scriptSource = file_get_contents(scriptFileName); |
| 205 | 203 | ||
| 206 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
204 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
| Line 214... | Line 212... | ||
| 214 | //compile script to binary code - JIT |
212 | //compile script to binary code - JIT |
| 215 | Handle<Script> script = Script::Compile(source, name); |
213 | Handle<Script> script = Script::Compile(source, name); |
| 216 | 214 | ||
| 217 | //check if we got problems on compilation |
215 | //check if we got problems on compilation |
| 218 | if (script.IsEmpty()) |
216 | if (script.IsEmpty()) |
| 219 | { |
217 | { |
| 220 | // Print errors that happened during compilation. |
218 | // Print errors that happened during compilation. |
| 221 | String::AsciiValue error(try_catch.Exception()); |
219 | String::AsciiValue error(try_catch.Exception()); |
| 222 | string sError = *error; |
220 | string sError = *error; |
| 223 | slog << "loadScript: Failed to compile script: " + sError + "\n"; |
221 | slog << "loadScript: Failed to compile script: " + sError + "\n"; |
| 224 | return false; |
222 | return false; |
| 225 | } |
223 | } |
| 226 | else |
224 | else |
| 227 | { |
225 | { |
| 228 | //no errors , let's continue |
226 | //no errors , let's continue |
| 229 | Handle<Value> result = script->Run(); |
227 | Handle<Value> result = script->Run(); |
| 230 | 228 | ||
| Line 233... | Line 231... | ||
| 233 | { |
231 | { |
| 234 | // Print errors that happened during execution. |
232 | // Print errors that happened during execution. |
| 235 | String::AsciiValue error(try_catch.Exception()); |
233 | String::AsciiValue error(try_catch.Exception()); |
| 236 | string sError = *error; |
234 | string sError = *error; |
| 237 | slog << "loadScript: Failed to run script: " + sError + "\n"; |
235 | slog << "loadScript: Failed to run script: " + sError + "\n"; |
| 238 | return false; |
236 | return false; |
| 239 | } |
237 | } |
| 240 | 238 | ||
| 241 | slog << "Loaded " + scriptName + "\n"; |
239 | slog << "Loaded " + scriptName + "\n"; |
| 242 | } |
240 | } |
| 243 | 241 | ||
| 244 | return true; |
242 | return true; |
| 245 | } |
243 | } |
| 246 | 244 | ||
| 247 | void VirtualMachine::callHandleHeartbeat(int hardwareId) |
245 | void VirtualMachine::callHandleHeartbeat(int hardwareId) |
| 248 | { |
246 | { |
| 249 | const int argc = 1; |
247 | const int argc = 1; |
| 250 | Handle<Value> argv[argc] = { Integer::New(hardwareId) }; |
248 | Handle<Value> argv[argc] = { Integer::New(hardwareId) }; |
| 251 | 249 | ||
| 252 | Handle<Value> result = myFunctionHandleHeartbeat->Call(myFunctionHandleHeartbeat, argc, argv); // argc and argv are your standard arguments to a function |
250 | Handle<Value> result = myFunctionHandleHeartbeat->Call(myFunctionHandleHeartbeat, argc, argv); // argc and argv are your standard arguments to a function |
| 253 | } |
251 | } |
| 254 | 252 | ||
| 255 | void VirtualMachine::callHandleMessage(CanMessage canMessage) |
253 | void VirtualMachine::callHandleMessage(CanMessage canMessage) |
| 256 | { |
254 | { |
| 257 | const int argc = 6; |
255 | const int argc = 6; |
| - | 256 | ||
| - | 257 | string jsonData = canMessage.getJSONData(); |
|
| - | 258 | ||
| 258 | Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()), |
259 | Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()), |
| 259 | String::New(canMessage.getDirectionFlag().c_str()), |
260 | String::New(canMessage.getDirectionFlag().c_str()), |
| 260 | String::New(canMessage.getModuleName().c_str()), |
261 | String::New(canMessage.getModuleName().c_str()), |
| 261 | Integer::New(canMessage.getModuleId()), |
262 | Integer::New(canMessage.getModuleId()), |
| 262 | String::New(canMessage.getCommandName().c_str()), |
263 | String::New(canMessage.getCommandName().c_str()), |
| 263 | String::New( |
264 | String::New(jsonData.c_str()) }; |
| 264 | 265 | ||
| 265 | Handle<Value> result = myFunctionHandleMessage->Call(myFunctionHandleMessage, argc, argv); // argc and argv are your standard arguments to a function |
266 | Handle<Value> result = myFunctionHandleMessage->Call(myFunctionHandleMessage, argc, argv); // argc and argv are your standard arguments to a function |
| 266 | } |
267 | } |
| 267 | 268 | ||
| 268 | unsigned int VirtualMachine::startIntervalThread(unsigned int timeout) |
269 | unsigned int VirtualMachine::startIntervalThread(unsigned int timeout) |
| Line 353... | Line 354... | ||
| 353 | String::AsciiValue error(try_catch.Exception()); |
354 | String::AsciiValue error(try_catch.Exception()); |
| 354 | string sError = *error; |
355 | string sError = *error; |
| 355 | slog << "runExpression: Failed to run script: " + sError + "\n"; |
356 | slog << "runExpression: Failed to run script: " + sError + "\n"; |
| 356 | return false; |
357 | return false; |
| 357 | } |
358 | } |
| 358 | } |
359 | } |
| 359 | 360 | ||
| 360 | return true; |
361 | return true; |
| 361 | } |
362 | } |
| 362 | 363 | ||
| 363 | Handle<Value> VirtualMachine_log(const Arguments& args) |
364 | Handle<Value> VirtualMachine_log(const Arguments& args) |
| Line 366... | Line 367... | ||
| 366 | 367 | ||
| 367 | String::AsciiValue str(args[0]); |
368 | String::AsciiValue str(args[0]); |
| 368 | 369 | ||
| 369 | slog << *str; |
370 | slog << *str; |
| 370 | 371 | ||
| 371 | return Undefined(); |
372 | return Undefined(); |
| 372 | } |
373 | } |
| 373 | 374 | ||
| 374 | Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args) |
375 | Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args) |
| 375 | { |
376 | { |
| 376 | //SyslogStream &slog = SyslogStream::getInstance(); |
377 | //SyslogStream &slog = SyslogStream::getInstance(); |
| 377 | CanNetManager &canMan = CanNetManager::getInstance(); |
378 | CanNetManager &canMan = CanNetManager::getInstance(); |
| 378 | 379 | ||
| Line 408... | Line 409... | ||
| 408 | 409 | ||
| 409 | return Undefined(); |
410 | return Undefined(); |
| 410 | } |
411 | } |
| 411 | 412 | ||
| 412 | Handle<Value> VirtualMachine_loadScript(const Arguments& args) |
413 | Handle<Value> VirtualMachine_loadScript(const Arguments& args) |
| 413 | { |
414 | { |
| 414 | VirtualMachine &vm = VirtualMachine::getInstance(); |
415 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 415 | 416 | ||
| 416 | String::AsciiValue str(args[0]); |
417 | String::AsciiValue str(args[0]); |
| 417 | 418 | ||
| 418 | return Boolean::New(vm.loadScript(*str)); |
419 | return Boolean::New(vm.loadScript(*str)); |
| 419 | } |
420 | } |
| 420 | 421 | ||
| 421 | Handle<Value> VirtualMachine_startIntervalThread(const Arguments& args) |
422 | Handle<Value> VirtualMachine_startIntervalThread(const Arguments& args) |
| 422 | { |
423 | { |
| 423 | VirtualMachine &vm = VirtualMachine::getInstance(); |
424 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 424 | 425 | ||
| 425 | unsigned int timeout = args[0]->Uint32Value(); |
426 | unsigned int timeout = args[0]->Uint32Value(); |
| 426 | 427 | ||
| 427 | return Integer::New(vm.startIntervalThread(timeout)); |
428 | return Integer::New(vm.startIntervalThread(timeout)); |
| 428 | } |
429 | } |
| 429 | 430 | ||
| 430 | Handle<Value> VirtualMachine_stopIntervalThread(const Arguments& args) |
431 | Handle<Value> VirtualMachine_stopIntervalThread(const Arguments& args) |
| 431 | { |
432 | { |
| 432 | VirtualMachine &vm = VirtualMachine::getInstance(); |
433 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 433 | 434 | ||
| 434 | unsigned int id = args[0]->Uint32Value(); |
435 | unsigned int id = args[0]->Uint32Value(); |
| 435 | 436 | ||
| 436 | return Boolean::New(vm.stopIntervalThread(id)); |
437 | return Boolean::New(vm.stopIntervalThread(id)); |
| 437 | } |
438 | } |
| 438 | 439 | ||
| 439 | Handle<Value> VirtualMachine_startSocketThread(const Arguments& args) |
440 | Handle<Value> VirtualMachine_startSocketThread(const Arguments& args) |
| 440 | { |
441 | { |
| 441 | VirtualMachine &vm = VirtualMachine::getInstance(); |
442 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 442 | 443 | ||
| 443 | String::AsciiValue address(args[0]); |
444 | String::AsciiValue address(args[0]); |
| 444 | int port = args[1]->Uint32Value(); |
445 | int port = args[1]->Uint32Value(); |
| 445 | unsigned int reconnectTimeout = args[2]->Uint32Value(); |
446 | unsigned int reconnectTimeout = args[2]->Uint32Value(); |
| 446 | 447 | ||
| 447 | return Integer::New(vm.startSocketThread(*address, port, reconnectTimeout)); |
448 | return Integer::New(vm.startSocketThread(*address, port, reconnectTimeout)); |
| 448 | } |
449 | } |
| 449 | 450 | ||
| 450 | Handle<Value> VirtualMachine_stopSocketThread(const Arguments& args) |
451 | Handle<Value> VirtualMachine_stopSocketThread(const Arguments& args) |
| 451 | { |
452 | { |
| 452 | VirtualMachine &vm = VirtualMachine::getInstance(); |
453 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 453 | 454 | ||
| 454 | unsigned int id = args[0]->Uint32Value(); |
455 | unsigned int id = args[0]->Uint32Value(); |
| 455 | 456 | ||
| 456 | return Boolean::New(vm.stopSocketThread(id)); |
457 | return Boolean::New(vm.stopSocketThread(id)); |
| 457 | } |
458 | } |
| 458 | 459 | ||
| 459 | Handle<Value> VirtualMachine_sendToSocketThread(const Arguments& args) |
460 | Handle<Value> VirtualMachine_sendToSocketThread(const Arguments& args) |
| - | 461 | { |
|
| - | 462 | VirtualMachine &vm = VirtualMachine::getInstance(); |
|
| - | 463 | ||
| - | 464 | unsigned int id = args[0]->Uint32Value(); |
|
| - | 465 | String::AsciiValue data(args[1]); |
|
| - | 466 | ||
| - | 467 | vm.sendToSocketThread(id, *data); |
|
| - | 468 | ||
| - | 469 | return Undefined(); |
|
| - | 470 | } |
|
| - | 471 | ||
| - | 472 | Handle<Value> VirtualMachine_uint2hex(const Arguments& args) |
|
| 460 | { |
473 | { |
| 461 | VirtualMachine &vm = VirtualMachine::getInstance(); |
474 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 462 | 475 | ||
| 463 | unsigned int id = args[0]->Uint32Value(); |
476 | unsigned int id = args[0]->Uint32Value(); |
| 464 |
|
477 | unsigned int length = args[1]->Uint32Value(); |
| 465 | - | ||
| 466 |
|
478 | string hexId = uint2hex(id, length); |
| 467 | 479 | ||
| 468 | return |
480 | return String::New(hexId.c_str()); |
| 469 | } |
481 | } |
| 470 | 482 | ||