Rev 970 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 970 | Rev 971 | ||
|---|---|---|---|
| 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 <map> |
22 | #include <map> |
| 23 | 23 | ||
| 24 | 24 | ||
| 25 | #include "virtualmachine.h" |
25 | #include "virtualmachine.h" |
| 26 | 26 | ||
| 27 | VirtualMachine* VirtualMachine::myInstance = NULL; |
27 | VirtualMachine* VirtualMachine::myInstance = NULL; |
| 28 | 28 | ||
| 29 | VirtualMachine& VirtualMachine::getInstance() |
29 | VirtualMachine& VirtualMachine::getInstance() |
| 30 | { |
30 | { |
| 31 | if (myInstance == NULL) |
31 | if (myInstance == NULL) |
| 32 | { |
32 | { |
| 33 | myInstance = new VirtualMachine(); |
33 | myInstance = new VirtualMachine(); |
| 34 | } |
34 | } |
| 35 | 35 | ||
| 36 | return *myInstance; |
36 | return *myInstance; |
| 37 | } |
37 | } |
| 38 | 38 | ||
| 39 | void VirtualMachine::deleteInstance() |
39 | void VirtualMachine::deleteInstance() |
| 40 | { |
40 | { |
| 41 | if (myInstance != NULL) |
41 | if (myInstance != NULL) |
| 42 | { |
42 | { |
| 43 | delete myInstance; |
43 | delete myInstance; |
| 44 | myInstance = NULL; |
44 | myInstance = NULL; |
| 45 | } |
45 | } |
| 46 | } |
46 | } |
| 47 | 47 | ||
| 48 | VirtualMachine::VirtualMachine() |
48 | VirtualMachine::VirtualMachine() |
| 49 | { |
49 | { |
| 50 | 50 | ||
| 51 | } |
51 | } |
| 52 | 52 | ||
| 53 | VirtualMachine::~VirtualMachine() |
53 | VirtualMachine::~VirtualMachine() |
| 54 | { |
54 | { |
| 55 | 55 | ||
| 56 | } |
56 | } |
| 57 | 57 | ||
| 58 | void VirtualMachine::run() |
58 | void VirtualMachine::run() |
| 59 | { |
59 | { |
| 60 | SyslogStream &slog = SyslogStream::getInstance(); |
60 | SyslogStream &slog = SyslogStream::getInstance(); |
| 61 | 61 | ||
| 62 | string basePath = Settings::get("BasePath") + " |
62 | string basePath = Settings::get("BasePath") + "Services/"; |
| 63 | string scriptName = "Base.js"; |
63 | string scriptName = "Base.js"; |
| 64 | string scriptFileName = basePath + scriptName; |
64 | string scriptFileName = basePath + "System/" + scriptName; |
| 65 | 65 | ||
| 66 | if (!file_exists(scriptFileName)) |
66 | if (!file_exists(scriptFileName)) |
| 67 | { |
67 | { |
| 68 | slog << "Failed to load :" << scriptFileName << "\n"; |
68 | slog << "Failed to load :" << scriptFileName << "\n"; |
| 69 | } |
69 | } |
| 70 | 70 | ||
| 71 | string scriptSource = file_get_contents(scriptFileName); |
71 | string scriptSource = file_get_contents(scriptFileName); |
| 72 | 72 | ||
| 73 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
73 | 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 |
74 | //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()); |
75 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 76 | 76 | ||
| 77 | // Create a template for the global object. |
77 | // Create a template for the global object. |
| 78 | myGlobal = ObjectTemplate::New(); |
78 | myGlobal = ObjectTemplate::New(); |
| 79 | 79 | ||
| 80 | //associates "print" on script to the Print function |
80 | //associates "print" on script to the Print function |
| 81 | myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine_log)); |
81 | myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine_log)); |
| 82 | myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine_sendCanMessage)); |
82 | myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine_sendCanMessage)); |
| 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("setTimeout"), FunctionTemplate::New(VirtualMachine_setTimeout)); |
- | |
| 85 | myGlobal->Set(String::New(" |
84 | myGlobal->Set(String::New("startIntervalThread"), FunctionTemplate::New(VirtualMachine_startIntervalThread)); |
| 86 | myGlobal->Set(String::New("clearTimeout"), FunctionTemplate::New(VirtualMachine_clearInterval)); |
- | |
| 87 | myGlobal->Set(String::New(" |
85 | myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine_stopIntervalThread)); |
| 88 | 86 | ||
| 89 | //create context for the script |
87 | //create context for the script |
| 90 | myContext = Context::New(NULL, myGlobal); |
88 | myContext = Context::New(NULL, myGlobal); |
| 91 | 89 | ||
| 92 | 90 | ||
| 93 | //access global context within this scope |
91 | //access global context within this scope |
| 94 | Context::Scope context_scope(myContext); |
92 | Context::Scope context_scope(myContext); |
| 95 | //exception handler |
93 | //exception handler |
| 96 | TryCatch try_catch; |
94 | TryCatch try_catch; |
| 97 | //compile script to binary code - JIT |
95 | //compile script to binary code - JIT |
| 98 | Handle<Script> script = Script::Compile(source, name); |
96 | Handle<Script> script = Script::Compile(source, name); |
| 99 | 97 | ||
| 100 | //check if we got problems on compilation |
98 | //check if we got problems on compilation |
| 101 | if (script.IsEmpty()) |
99 | if (script.IsEmpty()) |
| 102 | { |
100 | { |
| 103 | 101 | ||
| 104 | // Print errors that happened during compilation. |
102 | // Print errors that happened during compilation. |
| 105 | String::AsciiValue error(try_catch.Exception()); |
103 | String::AsciiValue error(try_catch.Exception()); |
| 106 | slog << "Failed to compile script: " << *error << "\n"; |
104 | slog << "Failed to compile script: " << *error << "\n"; |
| 107 | } |
105 | } |
| 108 | else |
106 | else |
| 109 | { |
107 | { |
| 110 | //no errors , let's continue |
108 | //no errors , let's continue |
| 111 | Handle<Value> result = script->Run(); |
109 | Handle<Value> result = script->Run(); |
| 112 | 110 | ||
| 113 | //check if execution ended with errors |
111 | //check if execution ended with errors |
| 114 | if (result.IsEmpty()) |
112 | if (result.IsEmpty()) |
| 115 | { |
113 | { |
| 116 | // Print errors that happened during execution. |
114 | // Print errors that happened during execution. |
| 117 | String::AsciiValue error(try_catch.Exception()); |
115 | String::AsciiValue error(try_catch.Exception()); |
| 118 | slog << "Constructor: Failed to run script: " << *error << "\n"; |
116 | slog << "Constructor: Failed to run script: " << *error << "\n"; |
| 119 | } |
117 | } |
| 120 | else |
118 | else |
| 121 | { |
119 | { |
| 122 | loadScript("System/Service.js"); |
- | |
| 123 | loadScript("System/ServiceManager.js"); |
- | |
| 124 | loadScript("System/CanMessage.js"); |
- | |
| 125 | loadScript("System/CanManager.js"); |
- | |
| 126 | loadScript("System/CanService.js"); |
- | |
| 127 | loadScript("System/Startup.js"); |
120 | loadScript("System/Startup.js"); |
| 128 | 121 | ||
| 129 | if (file_exists(basePath + "Autostart.js")) |
122 | if (file_exists(basePath + "Autostart.js")) |
| 130 | { |
123 | { |
| 131 | loadScript("Autostart.js"); |
124 | loadScript("Autostart.js"); |
| 132 | } |
125 | } |
| 133 | 126 | ||
| 134 | myFunctionHandleHeartbeat = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleHeartbeat"))); |
127 | myFunctionHandleHeartbeat = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleHeartbeat"))); |
| 135 | myFunctionOfflineCheck = Handle<Function>::Cast(myContext->Global()->Get(String::New("offlineCheck"))); |
128 | myFunctionOfflineCheck = Handle<Function>::Cast(myContext->Global()->Get(String::New("offlineCheck"))); |
| 136 | myFunctionHandleMessage = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleMessage"))); |
129 | myFunctionHandleMessage = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleMessage"))); |
| 137 | myFunctionStartup = Handle<Function>::Cast(myContext->Global()->Get(String::New("startup"))); |
130 | myFunctionStartup = Handle<Function>::Cast(myContext->Global()->Get(String::New("startup"))); |
| 138 | } |
131 | } |
| 139 | } |
132 | } |
| 140 | 133 | ||
| 141 | const int argc = 0; |
134 | const int argc = 0; |
| 142 | Handle<Value> argv[argc] = { }; |
135 | Handle<Value> argv[argc] = { }; |
| 143 | 136 | ||
| 144 | Handle<Value> result = myFunctionStartup->Call(myFunctionStartup, argc, argv); // argc and argv are your standard arguments to a function |
137 | Handle<Value> result = myFunctionStartup->Call(myFunctionStartup, argc, argv); // argc and argv are your standard arguments to a function |
| 145 | 138 | ||
| 146 | mySemaphore.lock(); |
139 | mySemaphore.lock(); |
| 147 | 140 | ||
| 148 | while (1) |
141 | while (1) |
| 149 | { |
142 | { |
| 150 | string expression; |
143 | string expression; |
| 151 | 144 | ||
| 152 | while (myExpressions.size() > 0) |
145 | while (myExpressions.size() > 0) |
| 153 | { |
146 | { |
| 154 | expression = myExpressions.pop(); |
147 | expression = myExpressions.pop(); |
| 155 | 148 | ||
| 156 | runExpression(expression); |
149 | runExpression(expression); |
| 157 | } |
150 | } |
| 158 | 151 | ||
| 159 | CanMessage canMessage; |
152 | CanMessage canMessage; |
| 160 | 153 | ||
| 161 | while (myCanMessages.size() > 0) |
154 | while (myCanMessages.size() > 0) |
| 162 | { |
155 | { |
| 163 | canMessage = myCanMessages.pop(); |
156 | canMessage = myCanMessages.pop(); |
| 164 | 157 | ||
| 165 | if (canMessage.isHeartbeat()) |
158 | if (canMessage.isHeartbeat()) |
| 166 | { |
159 | { |
| 167 | callHandleHeartbeat(stoi(canMessage.getData()["HardwareId"].getValue())); |
160 | callHandleHeartbeat(stoi(canMessage.getData()["HardwareId"].getValue())); |
| 168 | } |
161 | } |
| 169 | else |
162 | else |
| 170 | { |
163 | { |
| 171 | callHandleMessage(canMessage); |
164 | callHandleMessage(canMessage); |
| 172 | } |
165 | } |
| 173 | } |
166 | } |
| 174 | 167 | ||
| 175 | mySemaphore.wait(); |
168 | mySemaphore.wait(); |
| 176 | } |
169 | } |
| 177 | 170 | ||
| 178 | mySemaphore.unlock(); |
171 | mySemaphore.unlock(); |
| 179 | } |
172 | } |
| 180 | 173 | ||
| 181 | void VirtualMachine::queueCanMessage(CanMessage canMessage) |
174 | void VirtualMachine::queueCanMessage(CanMessage canMessage) |
| 182 | { |
175 | { |
| 183 | myCanMessages.push(canMessage); |
176 | myCanMessages.push(canMessage); |
| 184 | mySemaphore.broadcast(); |
177 | mySemaphore.broadcast(); |
| 185 | } |
178 | } |
| 186 | 179 | ||
| 187 | void VirtualMachine::queueExpression(string expression) |
180 | void VirtualMachine::queueExpression(string expression) |
| 188 | { |
181 | { |
| 189 | myExpressions.push(expression); |
182 | myExpressions.push(expression); |
| 190 | mySemaphore.broadcast(); |
183 | mySemaphore.broadcast(); |
| 191 | } |
184 | } |
| 192 | 185 | ||
| 193 | bool VirtualMachine::loadScript(string scriptName) |
186 | bool VirtualMachine::loadScript(string scriptName) |
| 194 | { |
187 | { |
| 195 | SyslogStream &slog = SyslogStream::getInstance(); |
188 | SyslogStream &slog = SyslogStream::getInstance(); |
| 196 | 189 | ||
| 197 | string basePath = Settings::get("BasePath") + "/Services/"; |
190 | string basePath = Settings::get("BasePath") + "/Services/"; |
| 198 | string scriptFileName = basePath + scriptName; |
191 | string scriptFileName = basePath + scriptName; |
| 199 | 192 | ||
| 200 | if (!file_exists(scriptFileName)) |
193 | if (!file_exists(scriptFileName)) |
| 201 | { |
194 | { |
| 202 | slog << "Failed to load :" << scriptFileName << "\n"; |
195 | slog << "Failed to load :" << scriptFileName << "\n"; |
| 203 | return false; |
196 | return false; |
| 204 | } |
197 | } |
| 205 | 198 | ||
| 206 | string scriptSource = file_get_contents(scriptFileName); |
199 | string scriptSource = file_get_contents(scriptFileName); |
| 207 | 200 | ||
| 208 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
201 | Handle<String> source = String::New(scriptSource.c_str(), scriptSource.length()); |
| 209 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
202 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
| 210 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
203 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 211 | 204 | ||
| 212 | //access global context within this scope |
205 | //access global context within this scope |
| 213 | Context::Scope context_scope(myContext); |
206 | Context::Scope context_scope(myContext); |
| 214 | //exception handler |
207 | //exception handler |
| 215 | TryCatch try_catch; |
208 | TryCatch try_catch; |
| 216 | //compile script to binary code - JIT |
209 | //compile script to binary code - JIT |
| 217 | Handle<Script> script = Script::Compile(source, name); |
210 | Handle<Script> script = Script::Compile(source, name); |
| 218 | 211 | ||
| 219 | //check if we got problems on compilation |
212 | //check if we got problems on compilation |
| 220 | if (script.IsEmpty()) |
213 | if (script.IsEmpty()) |
| 221 | { |
214 | { |
| 222 | // Print errors that happened during compilation. |
215 | // Print errors that happened during compilation. |
| 223 | String::AsciiValue error(try_catch.Exception()); |
216 | String::AsciiValue error(try_catch.Exception()); |
| 224 | slog << "Failed to compile script: " << *error << "\n"; |
217 | slog << "Failed to compile script: " << *error << "\n"; |
| 225 | return false; |
218 | return false; |
| 226 | } |
219 | } |
| 227 | else |
220 | else |
| 228 | { |
221 | { |
| 229 | //no errors , let's continue |
222 | //no errors , let's continue |
| 230 | Handle<Value> result = script->Run(); |
223 | Handle<Value> result = script->Run(); |
| 231 | 224 | ||
| 232 | //check if execution ended with errors |
225 | //check if execution ended with errors |
| 233 | if (result.IsEmpty()) |
226 | if (result.IsEmpty()) |
| 234 | { |
227 | { |
| 235 | // Print errors that happened during execution. |
228 | // Print errors that happened during execution. |
| 236 | String::AsciiValue error(try_catch.Exception()); |
229 | String::AsciiValue error(try_catch.Exception()); |
| 237 | slog << "loadScript: Failed to run script: " << *error << "\n"; |
230 | slog << "loadScript: Failed to run script: " << *error << "\n"; |
| 238 | return false; |
231 | return false; |
| 239 | } |
232 | } |
| 240 | } |
233 | } |
| 241 | 234 | ||
| 242 | return true; |
235 | return true; |
| 243 | } |
236 | } |
| 244 | 237 | ||
| 245 | void VirtualMachine::callHandleHeartbeat(int hardwareId) |
238 | void VirtualMachine::callHandleHeartbeat(int hardwareId) |
| 246 | { |
239 | { |
| 247 | const int argc = 1; |
240 | const int argc = 1; |
| 248 | Handle<Value> argv[argc] = { Integer::New(hardwareId) }; |
241 | Handle<Value> argv[argc] = { Integer::New(hardwareId) }; |
| 249 | 242 | ||
| 250 | Handle<Value> result = myFunctionHandleHeartbeat->Call(myFunctionHandleHeartbeat, argc, argv); // argc and argv are your standard arguments to a function |
243 | Handle<Value> result = myFunctionHandleHeartbeat->Call(myFunctionHandleHeartbeat, argc, argv); // argc and argv are your standard arguments to a function |
| 251 | } |
244 | } |
| 252 | 245 | ||
| 253 | void VirtualMachine::callHandleMessage(CanMessage canMessage) |
246 | void VirtualMachine::callHandleMessage(CanMessage canMessage) |
| 254 | { |
247 | { |
| 255 | const int argc = 6; |
248 | const int argc = 6; |
| 256 | Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()), |
249 | Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()), |
| 257 | String::New(canMessage.getDirectionFlag().c_str()), |
250 | String::New(canMessage.getDirectionFlag().c_str()), |
| 258 | String::New(canMessage.getModuleName().c_str()), |
251 | String::New(canMessage.getModuleName().c_str()), |
| 259 | Integer::New(canMessage.getModuleId()), |
252 | Integer::New(canMessage.getModuleId()), |
| 260 | String::New(canMessage.getCommandName().c_str()), |
253 | String::New(canMessage.getCommandName().c_str()), |
| 261 | String::New(canMessage.getJSONData().c_str()) }; |
254 | String::New(canMessage.getJSONData().c_str()) }; |
| 262 | 255 | ||
| 263 | Handle<Value> result = myFunctionHandleMessage->Call(myFunctionHandleMessage, argc, argv); // argc and argv are your standard arguments to a function |
256 | Handle<Value> result = myFunctionHandleMessage->Call(myFunctionHandleMessage, argc, argv); // argc and argv are your standard arguments to a function |
| 264 | } |
257 | } |
| 265 | 258 | ||
| 266 |
|
259 | unsigned int VirtualMachine::startIntervalThread(unsigned int timeout) |
| 267 | { |
260 | { |
| 268 | IntervalThread intervalThread( |
261 | IntervalThread intervalThread(timeout); |
| 269 | intervalThread.start(); |
- | |
| 270 | 262 | ||
| 271 | int timeoutId = myTimers.size(); |
- | |
| 272 |
|
263 | myIntervalThreads[intervalThread.getId()] = intervalThread; |
| 273 |
|
264 | myIntervalThreads[intervalThread.getId()].start(); |
| 274 | 265 | ||
| 275 | return |
266 | return intervalThread.getId(); |
| 276 | } |
267 | } |
| 277 | 268 | ||
| 278 | bool VirtualMachine:: |
269 | bool VirtualMachine::stopIntervalThread(unsigned int id) |
| 279 | { |
270 | { |
| 280 | if ( |
271 | if (myIntervalThreads.find(id) != myIntervalThreads.end()) |
| 281 | { |
272 | { |
| 282 |
|
273 | myIntervalThreads.erase(id); |
| 283 | return true; |
274 | return true; |
| 284 | } |
275 | } |
| 285 | 276 | ||
| 286 | return false; |
277 | return false; |
| 287 | } |
278 | } |
| 288 | 279 | ||
| 289 | bool VirtualMachine::runExpression(string expression) |
280 | bool VirtualMachine::runExpression(string expression) |
| 290 | { |
281 | { |
| 291 | SyslogStream &slog = SyslogStream::getInstance(); |
282 | SyslogStream &slog = SyslogStream::getInstance(); |
| 292 | 283 | ||
| 293 | string scriptName = "runExpression"; |
284 | string scriptName = "runExpression"; |
| 294 | 285 | ||
| 295 | Handle<String> source = String::New(expression.c_str(), expression.length()); |
286 | Handle<String> source = String::New(expression.c_str(), expression.length()); |
| 296 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
287 | //each script name must be unique , for this demo I just run one embedded script, so the name can be fixed |
| 297 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
288 | Handle<String> name = String::New(scriptName.c_str(), scriptName.length()); |
| 298 | 289 | ||
| 299 | //access global context within this scope |
290 | //access global context within this scope |
| 300 | Context::Scope context_scope(myContext); |
291 | Context::Scope context_scope(myContext); |
| 301 | //exception handler |
292 | //exception handler |
| 302 | TryCatch try_catch; |
293 | TryCatch try_catch; |
| 303 | //compile script to binary code - JIT |
294 | //compile script to binary code - JIT |
| 304 | Handle<Script> script = Script::Compile(source, name); |
295 | Handle<Script> script = Script::Compile(source, name); |
| 305 | 296 | ||
| 306 | //check if we got problems on compilation |
297 | //check if we got problems on compilation |
| 307 | if (script.IsEmpty()) |
298 | if (script.IsEmpty()) |
| 308 | { |
299 | { |
| 309 | // Print errors that happened during compilation. |
300 | // Print errors that happened during compilation. |
| 310 | String::AsciiValue error(try_catch.Exception()); |
301 | String::AsciiValue error(try_catch.Exception()); |
| 311 | slog << "Failed to compile script: " << *error << "\n"; |
302 | slog << "Failed to compile script: " << *error << "\n"; |
| 312 | 303 | ||
| 313 | return false; |
304 | return false; |
| 314 | } |
305 | } |
| 315 | else |
306 | else |
| 316 | { |
307 | { |
| 317 | //no errors , let's continue |
308 | //no errors , let's continue |
| 318 | Handle<Value> result = script->Run(); |
309 | Handle<Value> result = script->Run(); |
| 319 | 310 | ||
| 320 | //check if execution ended with errors |
311 | //check if execution ended with errors |
| 321 | if (result.IsEmpty()) |
312 | if (result.IsEmpty()) |
| 322 | { |
313 | { |
| 323 | // Print errors that happened during execution. |
314 | // Print errors that happened during execution. |
| 324 | String::AsciiValue error(try_catch.Exception()); |
315 | String::AsciiValue error(try_catch.Exception()); |
| 325 | slog << "runExpression: Failed to run script: " << *error << "\n"; |
316 | slog << "runExpression: Failed to run script: " << *error << "\n"; |
| 326 | return false; |
317 | return false; |
| 327 | } |
318 | } |
| 328 | } |
319 | } |
| 329 | 320 | ||
| 330 | return true; |
321 | return true; |
| 331 | } |
322 | } |
| 332 | 323 | ||
| 333 | Handle<Value> VirtualMachine_log(const Arguments& args) |
324 | Handle<Value> VirtualMachine_log(const Arguments& args) |
| 334 | { |
325 | { |
| 335 | SyslogStream &slog = SyslogStream::getInstance(); |
326 | SyslogStream &slog = SyslogStream::getInstance(); |
| 336 | 327 | ||
| 337 | String::AsciiValue str(args[0]); |
328 | String::AsciiValue str(args[0]); |
| 338 | 329 | ||
| 339 | slog << *str; |
330 | slog << *str; |
| 340 | 331 | ||
| 341 | return Undefined(); |
332 | return Undefined(); |
| 342 | } |
333 | } |
| 343 | 334 | ||
| 344 | Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args) |
335 | Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args) |
| 345 | { |
336 | { |
| 346 | SyslogStream &slog = SyslogStream::getInstance(); |
337 | SyslogStream &slog = SyslogStream::getInstance(); |
| 347 | CanNetManager &canMan = CanNetManager::getInstance(); |
338 | CanNetManager &canMan = CanNetManager::getInstance(); |
| 348 | 339 | ||
| 349 | CanMessage canMessage; |
340 | CanMessage canMessage; |
| 350 | 341 | ||
| 351 | String::AsciiValue className(args[0]); |
342 | String::AsciiValue className(args[0]); |
| 352 | canMessage.setClassName(*className); |
343 | canMessage.setClassName(*className); |
| 353 | String::AsciiValue directionFlag(args[1]); |
344 | String::AsciiValue directionFlag(args[1]); |
| 354 | canMessage.setDirectionFlag(*directionFlag); |
345 | canMessage.setDirectionFlag(*directionFlag); |
| 355 | String::AsciiValue moduleName(args[2]); |
346 | String::AsciiValue moduleName(args[2]); |
| 356 | canMessage.setModuleName(*moduleName); |
347 | canMessage.setModuleName(*moduleName); |
| 357 | canMessage.setModuleId(args[3]->Uint32Value()); |
348 | canMessage.setModuleId(args[3]->Uint32Value()); |
| 358 | String::AsciiValue commandName(args[4]); |
349 | String::AsciiValue commandName(args[4]); |
| 359 | canMessage.setCommandName(*commandName); |
350 | canMessage.setCommandName(*commandName); |
| 360 | 351 | ||
| 361 | String::AsciiValue dataString(args[5]); |
352 | String::AsciiValue dataString(args[5]); |
| 362 | 353 | ||
| 363 | vector<string> parts = explode(",", trim(*dataString, ',')); |
354 | vector<string> parts = explode(",", trim(*dataString, ',')); |
| 364 | map<string, CanVariable> data; |
355 | map<string, CanVariable> data; |
| 365 | 356 | ||
| 366 | for (int n = 0; n < parts.size(); n++) |
357 | for (int n = 0; n < parts.size(); n++) |
| 367 | { |
358 | { |
| 368 | vector<string> keyAndValue = explode(":", parts[n]); |
359 | vector<string> keyAndValue = explode(":", parts[n]); |
| 369 | 360 | ||
| 370 | //string key = trim(keyAndValue[0], ' '); |
361 | //string key = trim(keyAndValue[0], ' '); |
| 371 | //string value = trim(keyAndValue[1], ' '); |
362 | //string value = trim(keyAndValue[1], ' '); |
| 372 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
363 | data[keyAndValue[0]] = CanVariable(keyAndValue[0], keyAndValue[1]); |
| 373 | } |
364 | } |
| 374 | 365 | ||
| 375 | canMessage.setData(data); |
366 | canMessage.setData(data); |
| 376 | 367 | ||
| 377 | canMan.sendMessage(canMessage); |
368 | canMan.sendMessage(canMessage); |
| 378 | 369 | ||
| 379 | return Undefined(); |
370 | return Undefined(); |
| 380 | } |
371 | } |
| 381 | 372 | ||
| 382 | Handle<Value> VirtualMachine_loadScript(const Arguments& args) |
373 | Handle<Value> VirtualMachine_loadScript(const Arguments& args) |
| 383 | { |
374 | { |
| 384 | VirtualMachine &vm = VirtualMachine::getInstance(); |
375 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 385 | 376 | ||
| 386 | String::AsciiValue str(args[0]); |
377 | String::AsciiValue str(args[0]); |
| 387 | 378 | ||
| 388 | return Boolean::New(vm.loadScript(*str)); |
379 | return Boolean::New(vm.loadScript(*str)); |
| 389 | } |
380 | } |
| 390 | 381 | ||
| 391 | Handle<Value> |
382 | Handle<Value> VirtualMachine_startIntervalThread(const Arguments& args) |
| 392 | { |
- | |
| 393 | VirtualMachine &vm = VirtualMachine::getInstance(); |
- | |
| 394 | - | ||
| 395 | String::AsciiValue expression(args[0]); |
- | |
| 396 | unsigned int interval = args[1]->Uint32Value(); |
- | |
| 397 | - | ||
| 398 | return Integer::New(vm.setInterval(*expression, interval, false)); |
- | |
| 399 | } |
- | |
| 400 | - | ||
| 401 | Handle<Value> VirtualMachine_setTimeout(const Arguments& args) |
- | |
| 402 | { |
383 | { |
| 403 | VirtualMachine &vm = VirtualMachine::getInstance(); |
384 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 404 | 385 | ||
| 405 | String::AsciiValue expression(args[0]); |
- | |
| 406 | unsigned int |
386 | unsigned int timeout = args[0]->Uint32Value(); |
| 407 | 387 | ||
| 408 | return Integer::New(vm. |
388 | return Integer::New(vm.startIntervalThread(timeout)); |
| 409 | } |
389 | } |
| 410 | 390 | ||
| 411 | Handle<Value> |
391 | Handle<Value> VirtualMachine_stopIntervalThread(const Arguments& args) |
| 412 | { |
392 | { |
| 413 | VirtualMachine &vm = VirtualMachine::getInstance(); |
393 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 414 | 394 | ||
| - | 395 | unsigned int id = args[1]->Uint32Value(); |
|
| - | 396 | ||
| 415 | return |
397 | return Integer::New(vm.stopIntervalThread(id)); |
| 416 | } |
398 | } |
| 417 | 399 | ||