Rev 1010 | Rev 1012 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1010 | Rev 1011 | ||
|---|---|---|---|
| Line 84... | Line 84... | ||
| 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)); |
- | |
| 90 | myGlobal->Set(String::New("loadDataStore"), FunctionTemplate::New(VirtualMachine::_loadDataStore)); |
89 | myGlobal->Set(String::New("loadDataStore"), FunctionTemplate::New(VirtualMachine::_loadDataStore)); |
| 91 | myGlobal->Set(String::New("getFileContents"), FunctionTemplate::New(VirtualMachine::_getFileContents)); |
90 | myGlobal->Set(String::New("getFileContents"), FunctionTemplate::New(VirtualMachine::_getFileContents)); |
| - | 91 | myGlobal->Set(String::New("uint2hex"), FunctionTemplate::New(VirtualMachine::_uint2hex)); |
|
| - | 92 | myGlobal->Set(String::New("hex2bin"), FunctionTemplate::New(VirtualMachine::_hex2bin)); |
|
| - | 93 | myGlobal->Set(String::New("bin2hex"), FunctionTemplate::New(VirtualMachine::_bin2hex)); |
|
| - | 94 | myGlobal->Set(String::New("bin2float"), FunctionTemplate::New(VirtualMachine::_bin2float)); |
|
| - | 95 | myGlobal->Set(String::New("float2bin"), FunctionTemplate::New(VirtualMachine::_float2bin)); |
|
| - | 96 | myGlobal->Set(String::New("bin2uint"), FunctionTemplate::New(VirtualMachine::_bin2uint)); |
|
| - | 97 | myGlobal->Set(String::New("uint2bin"), FunctionTemplate::New(VirtualMachine::_uint2bin)); |
|
| - | 98 | myGlobal->Set(String::New("hex2uint"), FunctionTemplate::New(VirtualMachine::_hex2uint)); |
|
| 92 | myContext = Context::New(NULL, myGlobal); |
99 | myContext = Context::New(NULL, myGlobal); |
| 93 | 100 | ||
| 94 | 101 | ||
| 95 | if (loadScript("System/Base.js")) |
102 | if (loadScript("System/Base.js")) |
| 96 | { |
103 | { |
| Line 199... | Line 206... | ||
| 199 | 206 | ||
| 200 | scriptSource = "DataStore.addStore('" + storeName + "', " + str_replace("\n", "", scriptSource) + ");"; |
207 | scriptSource = "DataStore.addStore('" + storeName + "', " + str_replace("\n", "", scriptSource) + ");"; |
| 201 | 208 | ||
| 202 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
209 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
| 203 | Handle<String> name = String::New(scriptFileName.c_str(), scriptFileName.length()); |
210 | Handle<String> name = String::New(scriptFileName.c_str(), scriptFileName.length()); |
| 204 | 211 | ||
| 205 | Context::Scope contextScope(myContext); |
212 | Context::Scope contextScope(myContext); |
| 206 | 213 | ||
| 207 | TryCatch tryCatch; |
214 | TryCatch tryCatch; |
| 208 | 215 | ||
| 209 | Handle<Script> script = Script::Compile(source, name); |
216 | Handle<Script> script = Script::Compile(source, name); |
| 210 | 217 | ||
| 211 | if (script.IsEmpty()) |
218 | if (script.IsEmpty()) |
| 212 | { |
219 | { |
| 213 | slog << formatException(&tryCatch); |
220 | slog << formatException(&tryCatch); |
| 214 | return false; |
221 | return false; |
| 215 | } |
222 | } |
| 216 | else |
223 | else |
| 217 | { |
224 | { |
| 218 | Handle<Value> result = script->Run(); |
225 | Handle<Value> result = script->Run(); |
| 219 | 226 | ||
| 220 | if (result.IsEmpty()) |
227 | if (result.IsEmpty()) |
| 221 | { |
228 | { |
| 222 | slog << formatException(&tryCatch); |
229 | slog << formatException(&tryCatch); |
| 223 | return false; |
230 | return false; |
| 224 | } |
231 | } |
| 225 | 232 | ||
| 226 | slog << "Loaded datastore " + storeName + "\n"; |
233 | slog << "Loaded datastore " + storeName + "\n"; |
| 227 | } |
234 | } |
| 228 | 235 | ||
| 229 | return true; |
236 | return true; |
| 230 | } |
237 | } |
| 231 | 238 | ||
| 232 | bool VirtualMachine::loadScript(string scriptName) |
239 | bool VirtualMachine::loadScript(string scriptName) |
| 233 | { |
240 | { |
| Line 237... | Line 244... | ||
| 237 | string scriptFileName = basePath + scriptName; |
244 | string scriptFileName = basePath + scriptName; |
| 238 | 245 | ||
| 239 | if (!file_exists(scriptFileName)) |
246 | if (!file_exists(scriptFileName)) |
| 240 | { |
247 | { |
| 241 | slog << "Failed to load " + scriptFileName + ", file does not exist.\n"; |
248 | slog << "Failed to load " + scriptFileName + ", file does not exist.\n"; |
| 242 | return false; |
249 | return false; |
| 243 | } |
250 | } |
| 244 | 251 | ||
| 245 | string scriptSource = file_get_contents(scriptFileName); |
252 | string scriptSource = file_get_contents(scriptFileName); |
| 246 | 253 | ||
| 247 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
254 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
| 248 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
255 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 249 | 256 | ||
| Line 264... | Line 271... | ||
| 264 | 271 | ||
| 265 | if (result.IsEmpty()) |
272 | if (result.IsEmpty()) |
| 266 | { |
273 | { |
| 267 | slog << formatException(&tryCatch); |
274 | slog << formatException(&tryCatch); |
| 268 | return false; |
275 | return false; |
| 269 | } |
276 | } |
| 270 | 277 | ||
| 271 | slog << "Loaded " + scriptName + "\n"; |
278 | slog << "Loaded " + scriptName + "\n"; |
| 272 | } |
279 | } |
| 273 | 280 | ||
| 274 | return true; |
281 | return true; |
| Line 280... | Line 287... | ||
| 280 | 287 | ||
| 281 | myIntervalThreads[intervalThread->getId()] = intervalThread; |
288 | myIntervalThreads[intervalThread->getId()] = intervalThread; |
| 282 | myIntervalThreads[intervalThread->getId()]->start(); |
289 | myIntervalThreads[intervalThread->getId()]->start(); |
| 283 | 290 | ||
| 284 | return intervalThread->getId(); |
291 | return intervalThread->getId(); |
| 285 | } |
292 | } |
| 286 | 293 | ||
| 287 | bool VirtualMachine::stopIntervalThread(unsigned int id) |
294 | bool VirtualMachine::stopIntervalThread(unsigned int id) |
| 288 | { |
295 | { |
| 289 | if (myIntervalThreads.find(id) != myIntervalThreads.end()) |
296 | if (myIntervalThreads.find(id) != myIntervalThreads.end()) |
| 290 | { |
297 | { |
| Line 295... | Line 302... | ||
| 295 | 302 | ||
| 296 | return false; |
303 | return false; |
| 297 | } |
304 | } |
| 298 | 305 | ||
| 299 | unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout) |
306 | unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout) |
| 300 | { |
307 | { |
| 301 | SocketThread *socketThread = new SocketThread(address, port, reconnectTimeout); |
308 | SocketThread *socketThread = new SocketThread(address, port, reconnectTimeout); |
| 302 | mySocketThreads[socketThread->getId()] = socketThread; |
309 | mySocketThreads[socketThread->getId()] = socketThread; |
| 303 | mySocketThreads[socketThread->getId()]->start(); |
310 | mySocketThreads[socketThread->getId()]->start(); |
| 304 | 311 | ||
| 305 | return socketThread->getId(); |
312 | return socketThread->getId(); |
| Line 310... | Line 317... | ||
| 310 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
317 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
| 311 | { |
318 | { |
| 312 | delete mySocketThreads[id]; |
319 | delete mySocketThreads[id]; |
| 313 | mySocketThreads.erase(id); |
320 | mySocketThreads.erase(id); |
| 314 | return true; |
321 | return true; |
| 315 | } |
322 | } |
| 316 | 323 | ||
| 317 | return false; |
324 | return false; |
| 318 | } |
325 | } |
| 319 | 326 | ||
| 320 | void VirtualMachine::sendToSocketThread(unsigned int id, string data) |
327 | void VirtualMachine::sendToSocketThread(unsigned int id, string data) |
| 321 | { |
328 | { |
| Line 326... | Line 333... | ||
| 326 | } |
333 | } |
| 327 | 334 | ||
| 328 | void VirtualMachine::disconnectCommandClient(unsigned int id) |
335 | void VirtualMachine::disconnectCommandClient(unsigned int id) |
| 329 | { |
336 | { |
| 330 | myCommandThread.disconnectClient(id); |
337 | myCommandThread.disconnectClient(id); |
| 331 | } |
338 | } |
| 332 | 339 | ||
| 333 | void VirtualMachine::sendToCommandClient(unsigned int id, string data) |
340 | void VirtualMachine::sendToCommandClient(unsigned int id, string data) |
| 334 | { |
341 | { |
| 335 | myCommandThread.sendTo(id, data); |
342 | myCommandThread.sendTo(id, data); |
| 336 | } |
343 | } |
| 337 | 344 | ||
| Line 344... | Line 351... | ||
| 344 | } |
351 | } |
| 345 | else |
352 | else |
| 346 | { |
353 | { |
| 347 | VirtualMachine &vm = VirtualMachine::getInstance(); |
354 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 348 | vm.sendToCommandClient(id, data); |
355 | vm.sendToCommandClient(id, data); |
| 349 | } |
356 | } |
| 350 | } |
357 | } |
| 351 | 358 | ||
| 352 | bool VirtualMachine::runExpression(Expression expression) |
359 | bool VirtualMachine::runExpression(Expression expression) |
| 353 | { |
360 | { |
| 354 | string scriptName = "runExpression"; |
361 | string scriptName = "runExpression"; |
| 355 | 362 | ||
| Line 359... | Line 366... | ||
| 359 | 366 | ||
| 360 | Handle<String> source = String::New(scriptData.c_str(), scriptData.length()); |
367 | Handle<String> source = String::New(scriptData.c_str(), scriptData.length()); |
| 361 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
368 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 362 | 369 | ||
| 363 | Context::Scope contextScope(myContext); |
370 | Context::Scope contextScope(myContext); |
| 364 | 371 | ||
| 365 | TryCatch tryCatch; |
372 | TryCatch tryCatch; |
| 366 | 373 | ||
| 367 | Handle<Script> script = Script::Compile(source, name); |
374 | Handle<Script> script = Script::Compile(source, name); |
| 368 | 375 | ||
| 369 | if (script.IsEmpty()) |
376 | if (script.IsEmpty()) |
| 370 | { |
377 | { |
| 371 | print(expression.getTargetId(), formatException(&tryCatch)); |
378 | print(expression.getTargetId(), formatException(&tryCatch)); |
| 372 | return false; |
379 | return false; |
| 373 | } |
380 | } |
| 374 | else |
381 | else |
| 375 | { |
382 | { |
| 376 | Handle<Value> result = script->Run(); |
383 | Handle<Value> result = script->Run(); |
| 377 | 384 | ||
| 378 | if (result.IsEmpty()) |
385 | if (result.IsEmpty()) |
| 379 | { |
386 | { |
| 380 | print(expression.getTargetId(), formatException(&tryCatch)); |
387 | print(expression.getTargetId(), formatException(&tryCatch)); |
| 381 | return false; |
388 | return false; |
| 382 | } |
389 | } |
| 383 | else if (expression.getTargetId() != 0) |
390 | else if (expression.getTargetId() != 0) |
| 384 | { |
391 | { |
| 385 | String::AsciiValue ascii(result); |
392 | String::AsciiValue ascii(result); |
| 386 | string resultString = *ascii; |
393 | string resultString = *ascii; |
| 387 | 394 | ||
| 388 | if (resultString != "undefined") |
395 | if (resultString != "undefined") |
| 389 | { |
396 | { |
| 390 | print(expression.getTargetId(), resultString + "\n"); |
397 | print(expression.getTargetId(), resultString + "\n"); |
| 391 | } |
398 | } |
| 392 | } |
399 | } |
| 393 | } |
400 | } |
| 394 | 401 | ||
| 395 | return true; |
402 | return true; |
| 396 | } |
403 | } |
| 397 | 404 | ||
| 398 | 405 | ||
| 399 | Handle<Value> VirtualMachine::_quit(const Arguments& args) |
406 | Handle<Value> VirtualMachine::_quit(const Arguments& args) |
| 400 | { |
407 | { |
| 401 | VirtualMachine &vm = VirtualMachine::getInstance(); |
408 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 402 | 409 | ||
| 403 | vm.disconnectCommandClient(args[0]->Uint32Value()); |
410 | vm.disconnectCommandClient(args[0]->Uint32Value()); |
| 404 | 411 | ||
| 405 | return Undefined(); |
412 | return Undefined(); |
| 406 | } |
413 | } |
| 407 | 414 | ||
| 408 | Handle<Value> VirtualMachine::_log(const Arguments& args) |
415 | Handle<Value> VirtualMachine::_log(const Arguments& args) |
| 409 | { |
416 | { |
| 410 | SyslogStream &slog = SyslogStream::getInstance(); |
417 | SyslogStream &slog = SyslogStream::getInstance(); |
| 411 | 418 | ||
| 412 | String::AsciiValue str(args[0]); |
419 | String::AsciiValue str(args[0]); |
| 413 | 420 | ||
| 414 | slog << *str; |
421 | slog << *str; |
| 415 | 422 | ||
| 416 | return Undefined(); |
423 | return Undefined(); |
| 417 | } |
424 | } |
| 418 | 425 | ||
| 419 | Handle<Value> VirtualMachine::_print(const Arguments& args) |
426 | Handle<Value> VirtualMachine::_print(const Arguments& args) |
| 420 | { |
427 | { |
| 421 | VirtualMachine &vm = VirtualMachine::getInstance(); |
428 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| Line 607... | Line 614... | ||
| 607 | { |
614 | { |
| 608 | string bin = uint2bin(args[0]->Uint32Value(), args[1]->Uint32Value()); |
615 | string bin = uint2bin(args[0]->Uint32Value(), args[1]->Uint32Value()); |
| 609 | return String::New(bin.c_str()); |
616 | return String::New(bin.c_str()); |
| 610 | } |
617 | } |
| 611 | 618 | ||
| - | 619 | Handle<Value> VirtualMachine::_hex2uint(const Arguments& args) |
|
| 612 | 620 | { |
|
| - | 621 | String::AsciiValue hex(args[0]); |
|
| - | 622 | unsigned int num = hex2uint(*hex); |
|
| - | 623 | return Uint32::New(num); |
|
| - | 624 | } |
|