Subversion Repositories HomeAutomation

Rev

Rev 973 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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