Rev 986 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 986 | Rev 995 | ||
|---|---|---|---|
| Line 91... | Line 91... | ||
| 91 | 91 | ||
| 92 | 92 | ||
| 93 | //access global context within this scope |
93 | //access global context within this scope |
| 94 | Context::Scope context_scope(myContext); |
94 | Context::Scope context_scope(myContext); |
| 95 | //exception handler |
95 | //exception handler |
| 96 | TryCatch |
96 | TryCatch tryCatch; |
| 97 | //compile script to binary code - JIT |
97 | //compile script to binary code - JIT |
| 98 | Handle<Script> script = Script::Compile(source, name); |
98 | Handle<Script> script = Script::Compile(source, name); |
| 99 | 99 | ||
| 100 | //check if we got problems on compilation |
100 | //check if we got problems on compilation |
| 101 | if (script.IsEmpty()) |
101 | if (script.IsEmpty()) |
| 102 | { |
102 | { |
| 103 | - | ||
| 104 | // Print errors that happened during compilation. |
- | |
| 105 |
|
103 | printException(&tryCatch); |
| 106 |
|
104 | slog << "\n"; |
| 107 |
|
105 | return; |
| 108 | } |
106 | } |
| 109 | else |
107 | else |
| 110 | { |
108 | { |
| 111 | //no errors , let's continue |
109 | //no errors , let's continue |
| 112 | Handle<Value> result = script->Run(); |
110 | Handle<Value> result = script->Run(); |
| 113 | 111 | ||
| 114 | //check if execution ended with errors |
112 | //check if execution ended with errors |
| 115 | if (result.IsEmpty()) |
113 | if (result.IsEmpty()) |
| 116 | { |
114 | { |
| 117 | // Print errors that happened during execution. |
- | |
| 118 |
|
115 | printException(&tryCatch); |
| 119 |
|
116 | slog << "\n"; |
| 120 |
|
117 | return; |
| 121 | } |
118 | } |
| 122 | else |
119 | else |
| 123 | { |
120 | { |
| 124 | loadScript("System/Startup.js"); |
121 | loadScript("System/Startup.js"); |
| 125 | 122 | ||
| Line 165... | Line 162... | ||
| 165 | } |
162 | } |
| 166 | else |
163 | else |
| 167 | { |
164 | { |
| 168 | callHandleMessage(canMessage); |
165 | callHandleMessage(canMessage); |
| 169 | } |
166 | } |
| 170 | } |
167 | } |
| 171 | 168 | ||
| 172 | mySemaphore.wait(); |
169 | mySemaphore.wait(); |
| 173 | } |
170 | } |
| 174 | 171 | ||
| 175 | mySemaphore.unlock(); |
172 | mySemaphore.unlock(); |
| 176 | } |
173 | } |
| 177 | 174 | ||
| 178 | void VirtualMachine::queueCanMessage(CanMessage canMessage) |
175 | void VirtualMachine::queueCanMessage(CanMessage canMessage) |
| 179 | { |
176 | { |
| 180 | myCanMessages.push(canMessage); |
177 | myCanMessages.push(canMessage); |
| 181 | mySemaphore.broadcast(); |
178 | mySemaphore.broadcast(); |
| 182 | } |
179 | } |
| 183 | 180 | ||
| 184 | void VirtualMachine::queueExpression(string expression) |
181 | void VirtualMachine::queueExpression(string expression) |
| 185 | { |
182 | { |
| 186 | myExpressions.push(expression); |
183 | myExpressions.push(expression); |
| Line 191... | Line 188... | ||
| 191 | { |
188 | { |
| 192 | SyslogStream &slog = SyslogStream::getInstance(); |
189 | SyslogStream &slog = SyslogStream::getInstance(); |
| 193 | 190 | ||
| 194 | string basePath = Settings::get("BasePath") + "Services/"; |
191 | string basePath = Settings::get("BasePath") + "Services/"; |
| 195 | string scriptFileName = basePath + scriptName; |
192 | string scriptFileName = basePath + scriptName; |
| 196 | 193 | ||
| 197 | if (!file_exists(scriptFileName)) |
194 | if (!file_exists(scriptFileName)) |
| 198 | { |
195 | { |
| 199 | slog << "Failed to load " + scriptFileName + "\n"; |
196 | slog << "Failed to load " + scriptFileName + "\n"; |
| 200 | return false; |
197 | return false; |
| 201 | } |
198 | } |
| 202 | 199 | ||
| 203 | string scriptSource = file_get_contents(scriptFileName); |
200 | string scriptSource = file_get_contents(scriptFileName); |
| 204 | 201 | ||
| 205 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
202 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
| 206 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
203 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
| 207 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
204 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 208 | 205 | ||
| 209 | //access global context within this scope |
206 | //access global context within this scope |
| 210 | Context::Scope context_scope(myContext); |
207 | Context::Scope context_scope(myContext); |
| 211 | //exception handler |
208 | //exception handler |
| 212 | TryCatch |
209 | TryCatch tryCatch; |
| 213 | //compile script to binary code - JIT |
210 | //compile script to binary code - JIT |
| 214 | Handle<Script> script = Script::Compile(source, name); |
211 | Handle<Script> script = Script::Compile(source, name); |
| 215 | 212 | ||
| 216 | //check if we got problems on compilation |
213 | //check if we got problems on compilation |
| 217 | if (script.IsEmpty()) |
214 | if (script.IsEmpty()) |
| 218 | { |
215 | { |
| 219 | // Print errors that happened during compilation. |
- | |
| 220 |
|
216 | printException(&tryCatch); |
| 221 |
|
217 | slog << "\n"; |
| 222 | slog << "loadScript: Failed to compile script: " + sError + "\n"; |
- | |
| 223 | return false; |
218 | return false; |
| 224 | } |
219 | } |
| 225 | else |
220 | else |
| 226 | { |
221 | { |
| 227 | //no errors , let's continue |
222 | //no errors , let's continue |
| 228 | Handle<Value> result = script->Run(); |
223 | Handle<Value> result = script->Run(); |
| 229 | 224 | ||
| 230 | //check if execution ended with errors |
225 | //check if execution ended with errors |
| 231 | if (result.IsEmpty()) |
226 | if (result.IsEmpty()) |
| 232 | { |
227 | { |
| 233 | // Print errors that happened during execution. |
- | |
| 234 |
|
228 | printException(&tryCatch); |
| 235 |
|
229 | slog << "\n"; |
| 236 | slog << "loadScript: Failed to run script: " + sError + "\n"; |
- | |
| 237 | return false; |
230 | return false; |
| 238 | } |
231 | } |
| 239 | 232 | ||
| 240 | slog << "Loaded " + scriptName + "\n"; |
233 | slog << "Loaded " + scriptName + "\n"; |
| 241 | } |
234 | } |
| 242 | 235 | ||
| 243 | return true; |
236 | return true; |
| 244 | } |
237 | } |
| 245 | 238 | ||
| 246 | void VirtualMachine::callHandleNMTMessage(CanMessage canMessage) |
239 | void VirtualMachine::callHandleNMTMessage(CanMessage canMessage) |
| 247 | { |
240 | { |
| 248 | const int argc = 3; |
241 | const int argc = 3; |
| 249 | 242 | ||
| 250 | string jsonData = canMessage.getJSONData(); |
243 | string jsonData = canMessage.getJSONData(); |
| 251 | 244 | ||
| 252 | Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()), |
245 | Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()), |
| 253 | String::New(canMessage.getCommandName().c_str()), |
246 | String::New(canMessage.getCommandName().c_str()), |
| 254 | String::New(jsonData.c_str()) }; |
247 | String::New(jsonData.c_str()) }; |
| 255 | 248 | ||
| 256 | Handle<Value> result = myFunctionHandleNMTMessage->Call(myFunctionHandleNMTMessage, argc, argv); // argc and argv are your standard arguments to a function |
249 | Handle<Value> result = myFunctionHandleNMTMessage->Call(myFunctionHandleNMTMessage, argc, argv); // argc and argv are your standard arguments to a function |
| 257 | } |
250 | } |
| 258 | 251 | ||
| 259 | void VirtualMachine::callHandleMessage(CanMessage canMessage) |
252 | void VirtualMachine::callHandleMessage(CanMessage canMessage) |
| 260 | { |
253 | { |
| 261 | const int argc = 6; |
254 | const int argc = 6; |
| 262 | 255 | ||
| 263 | string jsonData = canMessage.getJSONData(); |
256 | string jsonData = canMessage.getJSONData(); |
| 264 | 257 | ||
| 265 | Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()), |
258 | Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()), |
| 266 | String::New(canMessage.getDirectionFlag().c_str()), |
259 | String::New(canMessage.getDirectionFlag().c_str()), |
| 267 | String::New(canMessage.getModuleName().c_str()), |
260 | String::New(canMessage.getModuleName().c_str()), |
| 268 | Integer::New(canMessage.getModuleId()), |
261 | Integer::New(canMessage.getModuleId()), |
| 269 | String::New(canMessage.getCommandName().c_str()), |
262 | String::New(canMessage.getCommandName().c_str()), |
| 270 | String::New(jsonData.c_str()) }; |
263 | String::New(jsonData.c_str()) }; |
| 271 | 264 | ||
| 272 | Handle<Value> result = myFunctionHandleMessage->Call(myFunctionHandleMessage, argc, argv); // argc and argv are your standard arguments to a function |
265 | Handle<Value> result = myFunctionHandleMessage->Call(myFunctionHandleMessage, argc, argv); // argc and argv are your standard arguments to a function |
| 273 | } |
266 | } |
| 274 | 267 | ||
| 275 | unsigned int VirtualMachine::startIntervalThread(unsigned int timeout) |
268 | unsigned int VirtualMachine::startIntervalThread(unsigned int timeout) |
| 276 | { |
269 | { |
| 277 | IntervalThread intervalThread(timeout); |
270 | IntervalThread intervalThread(timeout); |
| 278 | 271 | ||
| 279 | myIntervalThreads[intervalThread.getId()] = intervalThread; |
272 | myIntervalThreads[intervalThread.getId()] = intervalThread; |
| 280 | myIntervalThreads[intervalThread.getId()].start(); |
273 | myIntervalThreads[intervalThread.getId()].start(); |
| 281 | 274 | ||
| 282 | return intervalThread.getId(); |
275 | return intervalThread.getId(); |
| 283 | } |
276 | } |
| 284 | 277 | ||
| 285 | bool VirtualMachine::stopIntervalThread(unsigned int id) |
278 | bool VirtualMachine::stopIntervalThread(unsigned int id) |
| 286 | { |
279 | { |
| 287 | if (myIntervalThreads.find(id) != myIntervalThreads.end()) |
280 | if (myIntervalThreads.find(id) != myIntervalThreads.end()) |
| 288 | { |
281 | { |
| 289 | myIntervalThreads.erase(id); |
282 | myIntervalThreads.erase(id); |
| Line 295... | Line 288... | ||
| 295 | 288 | ||
| 296 | unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout) |
289 | unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout) |
| 297 | { |
290 | { |
| 298 | SocketThread *socketThread = new SocketThread(address, port, reconnectTimeout); |
291 | SocketThread *socketThread = new SocketThread(address, port, reconnectTimeout); |
| 299 | mySocketThreads[socketThread->getId()] = socketThread; |
292 | mySocketThreads[socketThread->getId()] = socketThread; |
| 300 | mySocketThreads[socketThread->getId()]->start(); |
293 | mySocketThreads[socketThread->getId()]->start(); |
| 301 | 294 | ||
| 302 | return socketThread->getId(); |
295 | return socketThread->getId(); |
| 303 | } |
296 | } |
| 304 | 297 | ||
| 305 | bool VirtualMachine::stopSocketThread(unsigned int id) |
298 | bool VirtualMachine::stopSocketThread(unsigned int id) |
| 306 | { |
299 | { |
| 307 | 300 | ||
| 308 | 301 | ||
| Line 317... | Line 310... | ||
| 317 | 310 | ||
| 318 | return false; |
311 | return false; |
| 319 | } |
312 | } |
| 320 | 313 | ||
| 321 | void VirtualMachine::sendToSocketThread(unsigned int id, string data) |
314 | void VirtualMachine::sendToSocketThread(unsigned int id, string data) |
| 322 | { |
315 | { |
| 323 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
316 | if (mySocketThreads.find(id) != mySocketThreads.end()) |
| 324 | { |
317 | { |
| 325 | mySocketThreads[id]->send(data); |
318 | mySocketThreads[id]->send(data); |
| 326 | } |
319 | } |
| 327 | } |
320 | } |
| 328 | 321 | ||
| 329 | bool VirtualMachine::runExpression(string expression) |
322 | bool VirtualMachine::runExpression(string expression) |
| 330 | { |
323 | { |
| 331 | SyslogStream &slog = SyslogStream::getInstance(); |
324 | SyslogStream &slog = SyslogStream::getInstance(); |
| 332 | 325 | ||
| 333 | string scriptName = "runExpression"; |
326 | string scriptName = "runExpression"; |
| 334 | 327 | ||
| 335 | Handle<String> source = String::New(expression.c_str(), expression.length()); |
328 | Handle<String> source = String::New(expression.c_str(), expression.length()); |
| 336 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
329 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
| 337 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
330 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 338 | 331 | ||
| 339 | //access global context within this scope |
332 | //access global context within this scope |
| 340 | Context::Scope context_scope(myContext); |
333 | Context::Scope context_scope(myContext); |
| 341 | //exception handler |
334 | //exception handler |
| 342 | TryCatch |
335 | TryCatch tryCatch; |
| 343 | //compile script to binary code - JIT |
336 | //compile script to binary code - JIT |
| 344 | Handle<Script> script = Script::Compile(source, name); |
337 | Handle<Script> script = Script::Compile(source, name); |
| 345 | 338 | ||
| 346 | //check if we got problems on compilation |
339 | //check if we got problems on compilation |
| 347 | if (script.IsEmpty()) |
340 | if (script.IsEmpty()) |
| 348 | { |
341 | { |
| 349 | // Print errors that happened during compilation. |
- | |
| 350 | String::AsciiValue error(try_catch.Exception()); |
- | |
| 351 |
|
342 | printException(&tryCatch); |
| 352 | slog << "runExpression: Failed to compile script: " + sError + "\n"; |
- | |
| 353 | slog |
343 | slog << "\n"; |
| 354 | return false; |
344 | return false; |
| 355 | } |
345 | } |
| 356 | else |
346 | else |
| 357 | { |
347 | { |
| 358 | //no errors , let's continue |
348 | //no errors , let's continue |
| 359 | Handle<Value> result = script->Run(); |
349 | Handle<Value> result = script->Run(); |
| 360 | 350 | ||
| 361 | //check if execution ended with errors |
351 | //check if execution ended with errors |
| 362 | if (result.IsEmpty()) |
352 | if (result.IsEmpty()) |
| 363 | { |
353 | { |
| 364 | // Print errors that happened during execution. |
- | |
| 365 |
|
354 | printException(&tryCatch); |
| 366 |
|
355 | slog << "\n"; |
| 367 | slog << "runExpression: Failed to run script: " + sError + "\n"; |
- | |
| 368 | return false; |
356 | return false; |
| 369 | } |
357 | } |
| 370 | } |
358 | } |
| 371 | 359 | ||
| 372 | return true; |
360 | return true; |
| - | 361 | } |
|
| - | 362 | ||
| - | 363 | void VirtualMachine::printException(TryCatch* tryCatch) |
|
| - | 364 | { |
|
| - | 365 | SyslogStream &slog = SyslogStream::getInstance(); |
|
| - | 366 | ||
| - | 367 | HandleScope handle_scope; |
|
| - | 368 | String::Utf8Value exception(tryCatch->Exception()); |
|
| - | 369 | Handle<v8::Message> message = tryCatch->Message(); |
|
| - | 370 | ||
| - | 371 | string strException = *exception; |
|
| - | 372 | ||
| - | 373 | if (message.IsEmpty()) |
|
| - | 374 | { |
|
| - | 375 | // V8 didn't provide any extra information about this error; just print the exception. |
|
| - | 376 | slog << strException + "\n"; |
|
| - | 377 | } |
|
| - | 378 | else |
|
| - | 379 | { |
|
| - | 380 | // Print (filename):(line number): (message). |
|
| - | 381 | String::Utf8Value filename(message->GetScriptResourceName()); |
|
| - | 382 | int linenum = message->GetLineNumber(); |
|
| - | 383 | ||
| - | 384 | string strFilename = *filename; |
|
| - | 385 | ||
| - | 386 | slog << strFilename + ":" + itos(linenum) + ": " + strException + "\n"; |
|
| - | 387 | ||
| - | 388 | // Print line of source code. |
|
| - | 389 | String::Utf8Value sourceline(message->GetSourceLine()); |
|
| - | 390 | ||
| - | 391 | string strSourceline = *sourceline; |
|
| - | 392 | ||
| - | 393 | slog << strSourceline + "\n"; |
|
| - | 394 | ||
| - | 395 | string underline; |
|
| - | 396 | // Print wavy underline (GetUnderline is deprecated). |
|
| - | 397 | int start = message->GetStartColumn(); |
|
| - | 398 | for (int i = 0; i < start; i++) |
|
| - | 399 | { |
|
| - | 400 | underline += " "; |
|
| - | 401 | } |
|
| - | 402 | ||
| - | 403 | int end = message->GetEndColumn(); |
|
| - | 404 | for (int i = start; i < end; i++) |
|
| - | 405 | { |
|
| - | 406 | underline += "^"; |
|
| - | 407 | } |
|
| - | 408 | ||
| - | 409 | slog << underline + "\n"; |
|
| - | 410 | } |
|
| 373 | } |
411 | } |
| - | 412 | ||
| 374 | 413 | ||
| 375 | Handle<Value> VirtualMachine_log(const Arguments& args) |
414 | Handle<Value> VirtualMachine_log(const Arguments& args) |
| 376 | { |
415 | { |
| 377 | SyslogStream &slog = SyslogStream::getInstance(); |
416 | SyslogStream &slog = SyslogStream::getInstance(); |
| 378 | 417 | ||