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