Subversion Repositories HomeAutomation

Rev

Rev 997 | Rev 1007 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 997 Rev 999
Line 47... Line 47...
47
}
47
}
48
 
48
 
49
VirtualMachine::~VirtualMachine()
49
VirtualMachine::~VirtualMachine()
50
{
50
{
51
    stop();
51
    stop();
-
 
52
 
-
 
53
    myCommandThread.stop();
52
 
54
 
53
    ///FIXME: Verify that this works and does not cause segfaults
55
    ///FIXME: Verify that this works and does not cause segfaults
54
    for (map<int, SocketThread*>::iterator iter = mySocketThreads.begin(); iter != mySocketThreads.end(); iter++)
56
    for (map<int, SocketThread*>::iterator iter = mySocketThreads.begin(); iter != mySocketThreads.end(); iter++)
55
    {
57
    {
-
 
58
        iter->second->stop();
-
 
59
        delete iter->second;
-
 
60
        //mySocketThreads.erase(iter);
-
 
61
    }
-
 
62
 
-
 
63
    mySocketThreads.clear();
-
 
64
 
-
 
65
    for (map<int, IntervalThread*>::iterator iter = myIntervalThreads.begin(); iter != myIntervalThreads.end(); iter++)
-
 
66
    {
-
 
67
        iter->second->stop();
56
        delete iter->second;
68
        delete iter->second;
57
        mySocketThreads.erase(iter);
69
        //myIntervalThreads.erase(iter);
58
    }
70
    }
-
 
71
 
-
 
72
    myIntervalThreads.clear();
59
}
73
}
60
 
74
 
61
void VirtualMachine::run()
75
void VirtualMachine::run()
62
{
76
{
63
    myGlobal = ObjectTemplate::New();
77
    myGlobal = ObjectTemplate::New();
-
 
78
    myGlobal->Set(String::New("_quit"), FunctionTemplate::New(VirtualMachine::_quit));
-
 
79
    myGlobal->Set(String::New("_print"), FunctionTemplate::New(VirtualMachine::_print));
64
    myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine_log));
80
    myGlobal->Set(String::New("log"), FunctionTemplate::New(VirtualMachine::_log));
65
    myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine_sendCanMessage));
81
    myGlobal->Set(String::New("sendCanMessage"), FunctionTemplate::New(VirtualMachine::_sendCanMessage));
66
    myGlobal->Set(String::New("sendCanNMTMessage"), FunctionTemplate::New(VirtualMachine_sendCanNMTMessage));
82
    myGlobal->Set(String::New("sendCanNMTMessage"), FunctionTemplate::New(VirtualMachine::_sendCanNMTMessage));
67
    myGlobal->Set(String::New("loadScript"), FunctionTemplate::New(VirtualMachine_loadScript));
83
    myGlobal->Set(String::New("loadScript"), FunctionTemplate::New(VirtualMachine::_loadScript));
68
    myGlobal->Set(String::New("startIntervalThread"), FunctionTemplate::New(VirtualMachine_startIntervalThread));
84
    myGlobal->Set(String::New("startIntervalThread"), FunctionTemplate::New(VirtualMachine::_startIntervalThread));
69
    myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine_stopIntervalThread));
85
    myGlobal->Set(String::New("stopIntervalThread"), FunctionTemplate::New(VirtualMachine::_stopIntervalThread));
70
    myGlobal->Set(String::New("startSocketThread"), FunctionTemplate::New(VirtualMachine_startSocketThread));
86
    myGlobal->Set(String::New("startSocketThread"), FunctionTemplate::New(VirtualMachine::_startSocketThread));
71
    myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine_stopSocketThread));
87
    myGlobal->Set(String::New("stopSocketThread"), FunctionTemplate::New(VirtualMachine::_stopSocketThread));
72
    myGlobal->Set(String::New("sendToSocketThread"), FunctionTemplate::New(VirtualMachine_sendToSocketThread));
88
    myGlobal->Set(String::New("sendToSocketThread"), FunctionTemplate::New(VirtualMachine::_sendToSocketThread));
73
    myGlobal->Set(String::New("uint2hex"), FunctionTemplate::New(VirtualMachine_uint2hex));
89
    myGlobal->Set(String::New("uint2hex"), FunctionTemplate::New(VirtualMachine::_uint2hex));
74
    myContext = Context::New(NULL, myGlobal);
90
    myContext = Context::New(NULL, myGlobal);
75
 
91
 
76
 
92
 
77
    if (loadScript("System/Base.js"))
93
    if (loadScript("System/Base.js"))
78
    {
94
    {
79
        loadScript("System/Startup.js");
95
        loadScript("System/Startup.js");
80
 
96
 
81
        loadScript("Autostart.js");
97
        loadScript("Autostart.js");
82
 
98
 
83
        Context::Scope contextScope(myContext);
99
        Context::Scope contextScope(myContext);
84
 
100
 
85
        myFunctionHandleNMTMessage = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleNMTMessage")));
-
 
86
        myFunctionOfflineCheck = Handle<Function>::Cast(myContext->Global()->Get(String::New("offlineCheck")));
-
 
87
        myFunctionHandleMessage = Handle<Function>::Cast(myContext->Global()->Get(String::New("handleMessage")));
-
 
88
        myFunctionStartup = Handle<Function>::Cast(myContext->Global()->Get(String::New("startup")));
101
        myFunctionStartup = Handle<Function>::Cast(myContext->Global()->Get(String::New("startup")));
89
    }
102
    }
90
    else
103
    else
91
    {
104
    {
92
        ///FIXME: We should probably exit the application here
105
        ///FIXME: We should probably exit the application here
Line 95... Line 108...
95
 
108
 
96
    const int argc = 0;
109
    const int argc = 0;
97
    Handle<Value> argv[argc] = { };
110
    Handle<Value> argv[argc] = { };
98
    Handle<Value> result = myFunctionStartup->Call(myFunctionStartup, argc, argv);
111
    Handle<Value> result = myFunctionStartup->Call(myFunctionStartup, argc, argv);
99
 
112
 
100
    mySemaphore.lock();
-
 
101
 
113
 
-
 
114
    string commandPort = Settings::get("CommandPort");
-
 
115
    if (commandPort != "")
-
 
116
    {
-
 
117
        myCommandThread.setSettings(stoi(commandPort), "Command console");
102
    string expression;
118
        myCommandThread.start();
-
 
119
    }
-
 
120
    else
-
 
121
    {
-
 
122
        SyslogStream &slog = SyslogStream::getInstance();
-
 
123
        slog << "CommandPort is not defined, can not start Command console socket\n";
-
 
124
    }
-
 
125
 
-
 
126
 
103
    CanMessage canMessage;
127
    mySemaphore.lock();
104
 
128
 
105
    while (true)
129
    while (true)
106
    {
130
    {
107
        while (myExpressions.size() > 0)
131
        while (myExpressions.size() > 0)
108
        {
132
        {
109
            expression = myExpressions.pop();
133
            runExpression(myExpressions.pop());
-
 
134
        }
-
 
135
 
110
            runExpression(expression);
136
        mySemaphore.wait();
111
        }
137
    }
112
 
138
 
113
        while (myCanMessages.size() > 0)
139
    mySemaphore.unlock();
-
 
140
}
-
 
141
 
-
 
142
void VirtualMachine::queueExpression(Expression expression)
114
        {
143
{
115
            canMessage = myCanMessages.pop();
144
    myExpressions.push(expression);
-
 
145
    mySemaphore.broadcast();
-
 
146
}
116
 
147
 
117
            if (canMessage.getClassName() == "nmt")
148
string VirtualMachine::formatException(TryCatch* tryCatch)
118
            {
149
{
-
 
150
    string result;
-
 
151
 
119
                expression = "handleNMTMessage(";
152
    HandleScope handleScope;
120
                expression += "'" + canMessage.getClassName() + "', ";
153
    String::Utf8Value exception(tryCatch->Exception());
121
                expression += "'" + canMessage.getCommandName() + "', ";
154
    Handle<v8::Message> message = tryCatch->Message();
-
 
155
 
122
                expression += canMessage.getJSONData();
156
    string strException = *exception;
-
 
157
 
123
                expression += ");";
158
    if (message.IsEmpty())
-
 
159
    {
124
                runExpression(expression);
160
        result += strException + "\n";
125
            }
161
    }
126
            else
162
    else
127
            {
163
    {
128
                expression = "handleMessage(";
-
 
129
                expression += "'" + canMessage.getClassName() + "', ";
-
 
130
                expression += "'" + canMessage.getDirectionFlag() + "', ";
164
        String::Utf8Value filename(message->GetScriptResourceName());
131
                expression += "'" + canMessage.getModuleName() + "', ";
165
        string strFilename = *filename;
-
 
166
 
132
                expression += "" + itos(canMessage.getModuleId()) + ", ";
167
        result += strFilename + ":" + itos(message->GetLineNumber()) + ": " + strException + "\n";
133
                expression += "'" + canMessage.getCommandName() + "', ";
-
 
-
 
168
 
134
                expression += canMessage.getJSONData();
169
        String::Utf8Value sourceline(message->GetSourceLine());
135
                expression += ");";
-
 
136
                runExpression(expression);
170
        string strSourceline = *sourceline;
137
            }
-
 
138
        }
-
 
139
 
171
 
140
        mySemaphore.wait();
172
        result += strSourceline + "\n";
141
    }
173
 
-
 
174
        string underline = rpad("", message->GetStartColumn(), ' ');
-
 
175
        underline = rpad(underline, message->GetEndColumn(), '^');
142
 
176
 
143
    mySemaphore.unlock();
177
        result += underline + "\n";
144
}
178
    }
145
 
179
 
146
void VirtualMachine::queueCanMessage(CanMessage canMessage)
-
 
147
{
-
 
148
    myCanMessages.push(canMessage);
-
 
149
    mySemaphore.broadcast();
180
    return result;
150
}
-
 
151
 
-
 
152
void VirtualMachine::queueExpression(string expression)
-
 
153
{
-
 
154
    myExpressions.push(expression);
-
 
155
    mySemaphore.broadcast();
-
 
156
}
181
}
157
 
182
 
158
bool VirtualMachine::loadScript(string scriptName)
183
bool VirtualMachine::loadScript(string scriptName)
159
{
184
{
160
    SyslogStream &slog = SyslogStream::getInstance();
185
    SyslogStream &slog = SyslogStream::getInstance();
Line 169... Line 194...
169
    }
194
    }
170
 
195
 
171
    string scriptSource = file_get_contents(scriptFileName);
196
    string scriptSource = file_get_contents(scriptFileName);
172
 
197
 
173
    Handle<String> source =  String::New(scriptSource.c_str(), scriptSource.length());
198
    Handle<String> source =  String::New(scriptSource.c_str(), scriptSource.length());
174
    Handle<String> name = String::New(scriptName.c_str(), scriptName.length());
199
    Handle<String> name = String::New(scriptName.c_str(), scriptName.length());
175
 
200
 
176
    Context::Scope contextScope(myContext);
201
    Context::Scope contextScope(myContext);
177
 
202
 
178
    TryCatch tryCatch;
203
    TryCatch tryCatch;
179
 
204
 
180
    Handle<Script> script = Script::Compile(source, name);
205
    Handle<Script> script = Script::Compile(source, name);
181
 
206
 
182
    if (script.IsEmpty())
207
    if (script.IsEmpty())
183
    {
208
    {
184
        printException(&tryCatch);
209
        slog << formatException(&tryCatch);
185
        return false;
210
        return false;
186
    }
211
    }
187
    else
212
    else
188
    {
213
    {
189
        Handle<Value> result = script->Run();
214
        Handle<Value> result = script->Run();
190
 
215
 
191
        if (result.IsEmpty())
216
        if (result.IsEmpty())
192
        {
217
        {
193
            printException(&tryCatch);
218
            slog << formatException(&tryCatch);
194
            return false;
219
            return false;
195
        }
220
        }
196
 
221
 
197
        slog << "Loaded " + scriptName + "\n";
222
        slog << "Loaded " + scriptName + "\n";
198
    }
223
    }
199
 
224
 
200
    return true;
225
    return true;
201
}
-
 
202
 
-
 
203
void VirtualMachine::callHandleNMTMessage(CanMessage canMessage)
-
 
204
{
-
 
205
    const int argc = 3;
-
 
206
 
-
 
207
    string jsonData = canMessage.getJSONData();
-
 
208
 
-
 
209
    Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()),
-
 
210
                                String::New(canMessage.getCommandName().c_str()),
-
 
211
                                String::New(jsonData.c_str()) };
-
 
212
 
-
 
213
    Handle<Value> result = myFunctionHandleNMTMessage->Call(myFunctionHandleNMTMessage, argc, argv); // argc and argv are your standard arguments to a function
-
 
214
}
-
 
215
 
-
 
216
void VirtualMachine::callHandleMessage(CanMessage canMessage)
-
 
217
{
-
 
218
    const int argc = 6;
-
 
219
 
-
 
220
    string jsonData = canMessage.getJSONData();
-
 
221
 
-
 
222
    Handle<Value> argv[argc] = {String::New(canMessage.getClassName().c_str()),
-
 
223
                                String::New(canMessage.getDirectionFlag().c_str()),
-
 
224
                                String::New(canMessage.getModuleName().c_str()),
-
 
225
                                Integer::New(canMessage.getModuleId()),
-
 
226
                                String::New(canMessage.getCommandName().c_str()),
-
 
227
                                String::New(jsonData.c_str()) };
-
 
228
 
-
 
229
    Handle<Value> result = myFunctionHandleMessage->Call(myFunctionHandleMessage, argc, argv); // argc and argv are your standard arguments to a function
-
 
230
}
226
}
231
 
227
 
232
unsigned int VirtualMachine::startIntervalThread(unsigned int timeout)
228
unsigned int VirtualMachine::startIntervalThread(unsigned int timeout)
233
{
229
{
234
    IntervalThread intervalThread(timeout);
230
    IntervalThread *intervalThread = new IntervalThread(timeout);
235
 
231
 
236
    myIntervalThreads[intervalThread.getId()] = intervalThread;
232
    myIntervalThreads[intervalThread->getId()] = intervalThread;
237
    myIntervalThreads[intervalThread.getId()].start();
233
    myIntervalThreads[intervalThread->getId()]->start();
238
 
234
 
239
    return intervalThread.getId();
235
    return intervalThread->getId();
240
}
236
}
241
 
237
 
242
bool VirtualMachine::stopIntervalThread(unsigned int id)
238
bool VirtualMachine::stopIntervalThread(unsigned int id)
243
{
239
{
244
    if (myIntervalThreads.find(id) != myIntervalThreads.end())
240
    if (myIntervalThreads.find(id) != myIntervalThreads.end())
245
    {
241
    {
-
 
242
        delete myIntervalThreads[id];
246
        myIntervalThreads.erase(id);
243
        myIntervalThreads.erase(id);
247
        return true;
244
        return true;
248
    }
245
    }
249
 
246
 
250
    return false;
247
    return false;
251
}
248
}
252
 
249
 
253
unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout)
250
unsigned int VirtualMachine::startSocketThread(string address, int port, unsigned int reconnectTimeout)
254
{
251
{
255
    SocketThread *socketThread = new SocketThread(address, port, reconnectTimeout);
252
    SocketThread *socketThread = new SocketThread(address, port, reconnectTimeout);
256
    mySocketThreads[socketThread->getId()] = socketThread;
253
    mySocketThreads[socketThread->getId()] = socketThread;
257
    mySocketThreads[socketThread->getId()]->start();
254
    mySocketThreads[socketThread->getId()]->start();
258
 
255
 
259
    return socketThread->getId();
256
    return socketThread->getId();
260
}
257
}
261
 
258
 
262
bool VirtualMachine::stopSocketThread(unsigned int id)
259
bool VirtualMachine::stopSocketThread(unsigned int id)
263
{
260
{
264
    if (mySocketThreads.find(id) != mySocketThreads.end())
261
    if (mySocketThreads.find(id) != mySocketThreads.end())
265
    {
262
    {
266
        delete mySocketThreads[id];
263
        delete mySocketThreads[id];
267
 
-
 
268
        mySocketThreads.erase(id);
264
        mySocketThreads.erase(id);
269
        return true;
265
        return true;
270
    }
266
    }
271
 
267
 
272
    return false;
268
    return false;
273
}
269
}
274
 
270
 
275
void VirtualMachine::sendToSocketThread(unsigned int id, string data)
271
void VirtualMachine::sendToSocketThread(unsigned int id, string data)
276
{
272
{
277
    if (mySocketThreads.find(id) != mySocketThreads.end())
273
    if (mySocketThreads.find(id) != mySocketThreads.end())
278
    {
274
    {
279
        mySocketThreads[id]->send(data);
275
        mySocketThreads[id]->send(data);
280
    }
276
    }
-
 
277
}
-
 
278
 
-
 
279
void VirtualMachine::disconnectCommandClient(unsigned int id)
-
 
280
{
-
 
281
    myCommandThread.disconnectClient(id);
-
 
282
}
-
 
283
 
-
 
284
void VirtualMachine::sendToCommandClient(unsigned int id, string data)
-
 
285
{
-
 
286
    myCommandThread.sendTo(id, data);
-
 
287
}
-
 
288
 
-
 
289
void VirtualMachine::print(unsigned int id, string data)
-
 
290
{
-
 
291
    if (id == 0)
-
 
292
    {
-
 
293
        SyslogStream &slog = SyslogStream::getInstance();
-
 
294
        slog << data;
-
 
295
    }
-
 
296
    else
-
 
297
    {
-
 
298
        VirtualMachine &vm = VirtualMachine::getInstance();
-
 
299
        vm.sendToCommandClient(id, data);
-
 
300
    }
281
}
301
}
282
 
302
 
283
bool VirtualMachine::runExpression(string expression)
303
bool VirtualMachine::runExpression(Expression expression)
284
{
304
{
285
    string scriptName = "runExpression";
305
    string scriptName = "runExpression";
-
 
306
 
-
 
307
    string scriptData = expression.getScript();
-
 
308
 
-
 
309
    scriptData = "setClientId(" + itos(expression.getTargetId()) + ");\n" + scriptData;
286
 
310
 
287
    Handle<String> source =  String::New(expression.c_str(), expression.length());
311
    Handle<String> source =  String::New(scriptData.c_str(), scriptData.length());
288
    Handle<String> name = String::New(scriptName.c_str(), scriptName.length());
312
    Handle<String> name = String::New(scriptName.c_str(), scriptName.length());
289
 
313
 
290
    Context::Scope contextScope(myContext);
314
    Context::Scope contextScope(myContext);
291
 
315
 
292
    TryCatch tryCatch;
316
    TryCatch tryCatch;
293
 
317
 
294
    Handle<Script> script = Script::Compile(source, name);
318
    Handle<Script> script = Script::Compile(source, name);
295
 
319
 
296
    if (script.IsEmpty())
320
    if (script.IsEmpty())
297
    {
321
    {
298
        printException(&tryCatch);
322
        print(expression.getTargetId(), formatException(&tryCatch));
299
        return false;
323
        return false;
300
    }
324
    }
301
    else
325
    else
302
    {
326
    {
303
        Handle<Value> result = script->Run();
327
        Handle<Value> result = script->Run();
304
 
328
 
305
        if (result.IsEmpty())
329
        if (result.IsEmpty())
306
        {
330
        {
307
            printException(&tryCatch);
331
            print(expression.getTargetId(), formatException(&tryCatch));
308
            return false;
332
            return false;
309
        }
333
        }
-
 
334
        else if (expression.getTargetId() != 0)
-
 
335
        {
-
 
336
             String::AsciiValue ascii(result);
-
 
337
             string resultString = *ascii;
-
 
338
 
-
 
339
             if (resultString != "undefined")
-
 
340
             {
-
 
341
                print(expression.getTargetId(), resultString + "\n");
-
 
342
             }
-
 
343
        }
-
 
344
    }
-
 
345
 
-
 
346
    return true;
310
    }
347
}
311
 
348
 
-
 
349
 
-
 
350
Handle<Value> VirtualMachine::_quit(const Arguments& args)
-
 
351
{
-
 
352
    VirtualMachine &vm = VirtualMachine::getInstance();
-
 
353
 
-
 
354
    vm.disconnectCommandClient(args[0]->Uint32Value());
-
 
355
 
312
    return true;
356
    return Undefined();
313
}
357
}
314
 
358
 
315
void VirtualMachine::printException(TryCatch* tryCatch)
-
 
316
{
-
 
317
    SyslogStream &slog = SyslogStream::getInstance();
-
 
318
 
-
 
319
    HandleScope handleScope;
-
 
320
    String::Utf8Value exception(tryCatch->Exception());
-
 
321
    Handle<v8::Message> message = tryCatch->Message();
-
 
322
 
-
 
323
    string strException = *exception;
-
 
324
 
-
 
325
    if (message.IsEmpty())
-
 
326
    {
-
 
327
        slog << strException + "\n\n";
-
 
328
    }
-
 
329
    else
-
 
330
    {
-
 
331
        String::Utf8Value filename(message->GetScriptResourceName());
-
 
332
        string strFilename = *filename;
-
 
333
       
-
 
334
        slog << strFilename + ":" + itos(message->GetLineNumber()) + ": " + strException + "\n";
-
 
335
 
-
 
336
        String::Utf8Value sourceline(message->GetSourceLine());
-
 
337
        string strSourceline = *sourceline;
-
 
338
 
-
 
339
        slog << strSourceline + "\n";
-
 
340
 
-
 
341
        string underline = rpad("", message->GetStartColumn(), ' ');
-
 
342
        underline = rpad(underline, message->GetEndColumn(), '^');
-
 
343
 
-
 
344
        slog << underline + "\n\n";
-
 
345
    }
-
 
346
}
-
 
347
 
-
 
348
 
-
 
349
Handle<Value> VirtualMachine_log(const Arguments& args)
359
Handle<Value> VirtualMachine::_log(const Arguments& args)
350
{
360
{
351
    SyslogStream &slog = SyslogStream::getInstance();
361
    SyslogStream &slog = SyslogStream::getInstance();
352
 
362
 
353
    String::AsciiValue str(args[0]);
363
    String::AsciiValue str(args[0]);
354
 
364
 
355
    slog << *str;
365
    slog << *str;
356
 
366
 
357
    return Undefined();
367
    return Undefined();
358
}
368
}
359
 
369
 
-
 
370
Handle<Value> VirtualMachine::_print(const Arguments& args)
-
 
371
{
-
 
372
    VirtualMachine &vm = VirtualMachine::getInstance();
-
 
373
 
-
 
374
    String::AsciiValue str(args[1]);
-
 
375
 
-
 
376
    vm.print(args[0]->Uint32Value(), *str);
-
 
377
 
-
 
378
    return Undefined();
-
 
379
}
-
 
380
 
360
Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args)
381
Handle<Value> VirtualMachine::_sendCanMessage(const Arguments& args)
361
{
382
{
362
    CanNetManager &canMan = CanNetManager::getInstance();
383
    CanNetManager &canMan = CanNetManager::getInstance();
363
 
384
 
364
    CanMessage canMessage;
385
    CanMessage canMessage;
365
 
386
 
Line 390... Line 411...
390
    canMan.sendMessage(canMessage);
411
    canMan.sendMessage(canMessage);
391
 
412
 
392
    return Undefined();
413
    return Undefined();
393
}
414
}
394
 
415
 
395
Handle<Value> VirtualMachine_sendCanNMTMessage(const Arguments& args)
416
Handle<Value> VirtualMachine::_sendCanNMTMessage(const Arguments& args)
396
{
417
{
397
    CanNetManager &canMan = CanNetManager::getInstance();
418
    CanNetManager &canMan = CanNetManager::getInstance();
398
 
419
 
399
    CanMessage canMessage;
420
    CanMessage canMessage;
400
 
421
 
Line 420... Line 441...
420
    canMan.sendMessage(canMessage);
441
    canMan.sendMessage(canMessage);
421
 
442
 
422
    return Undefined();
443
    return Undefined();
423
}
444
}
424
 
445
 
425
Handle<Value> VirtualMachine_loadScript(const Arguments& args)
446
Handle<Value> VirtualMachine::_loadScript(const Arguments& args)
426
{
447
{
427
    VirtualMachine &vm = VirtualMachine::getInstance();
448
    VirtualMachine &vm = VirtualMachine::getInstance();
428
 
449
 
429
    String::AsciiValue str(args[0]);
450
    String::AsciiValue str(args[0]);
430
 
451
 
431
    return Boolean::New(vm.loadScript(*str));
452
    return Boolean::New(vm.loadScript(*str));
432
}
453
}
433
 
454
 
434
Handle<Value> VirtualMachine_startIntervalThread(const Arguments& args)
455
Handle<Value> VirtualMachine::_startIntervalThread(const Arguments& args)
435
{
456
{
436
    VirtualMachine &vm = VirtualMachine::getInstance();
457
    VirtualMachine &vm = VirtualMachine::getInstance();
-
 
458
    return Integer::New(vm.startIntervalThread(args[0]->Uint32Value()));
-
 
459
}
-
 
460
 
-
 
461
Handle<Value> VirtualMachine::_stopIntervalThread(const Arguments& args)
-
 
462
{
-
 
463
    VirtualMachine &vm = VirtualMachine::getInstance();
-
 
464
    return Boolean::New(vm.stopIntervalThread(args[0]->Uint32Value()));
-
 
465
}
437
 
466
 
438
    unsigned int timeout = args[0]->Uint32Value();
-
 
439
 
-
 
440
    return Integer::New(vm.startIntervalThread(timeout));
-
 
441
}
-
 
442
 
-
 
443
Handle<Value> VirtualMachine_stopIntervalThread(const Arguments& args)
-
 
444
{
-
 
445
    VirtualMachine &vm = VirtualMachine::getInstance();
-
 
446
 
-
 
447
    unsigned int id = args[0]->Uint32Value();
-
 
448
 
-
 
449
    return Boolean::New(vm.stopIntervalThread(id));
-
 
450
}
-
 
451
 
-
 
452
Handle<Value> VirtualMachine_startSocketThread(const Arguments& args)
467
Handle<Value> VirtualMachine::_startSocketThread(const Arguments& args)
453
{
468
{
454
    VirtualMachine &vm = VirtualMachine::getInstance();
469
    VirtualMachine &vm = VirtualMachine::getInstance();
455
 
470
 
456
    String::AsciiValue address(args[0]);
471
    String::AsciiValue address(args[0]);
457
    int port = args[1]->Uint32Value();
-
 
458
    unsigned int reconnectTimeout = args[2]->Uint32Value();
-
 
459
 
472
 
460
    return Integer::New(vm.startSocketThread(*address, port, reconnectTimeout));
473
    return Integer::New(vm.startSocketThread(*address, args[1]->Uint32Value(), args[2]->Uint32Value()));
461
}
474
}
462
 
475
 
463
Handle<Value> VirtualMachine_stopSocketThread(const Arguments& args)
476
Handle<Value> VirtualMachine::_stopSocketThread(const Arguments& args)
464
{
477
{
465
    VirtualMachine &vm = VirtualMachine::getInstance();
478
    VirtualMachine &vm = VirtualMachine::getInstance();
466
 
479
 
467
    unsigned int id = args[0]->Uint32Value();
-
 
468
 
-
 
469
    return Boolean::New(vm.stopSocketThread(id));
480
    return Boolean::New(vm.stopSocketThread(args[0]->Uint32Value()));
470
}
481
}
471
 
482
 
472
Handle<Value> VirtualMachine_sendToSocketThread(const Arguments& args)
483
Handle<Value> VirtualMachine::_sendToSocketThread(const Arguments& args)
473
{
484
{
474
    VirtualMachine &vm = VirtualMachine::getInstance();
485
    VirtualMachine &vm = VirtualMachine::getInstance();
475
 
486
 
476
    unsigned int id = args[0]->Uint32Value();
-
 
477
    String::AsciiValue data(args[1]);
487
    String::AsciiValue data(args[1]);
478
 
488
 
479
    vm.sendToSocketThread(id, *data);
489
    vm.sendToSocketThread(args[0]->Uint32Value(), *data);
480
 
490
 
481
    return Undefined();
491
    return Undefined();
482
}
492
}
483
 
493
 
484
Handle<Value> VirtualMachine_uint2hex(const Arguments& args)
494
Handle<Value> VirtualMachine::_uint2hex(const Arguments& args)
485
{
495
{
486
    VirtualMachine &vm = VirtualMachine::getInstance();
496
    VirtualMachine &vm = VirtualMachine::getInstance();
487
 
497
 
488
    unsigned int id = args[0]->Uint32Value();
-
 
489
    unsigned int length = args[1]->Uint32Value();
498
    string hexId = uint2hex(args[0]->Uint32Value(), args[1]->Uint32Value());
490
    string hexId = uint2hex(id, length);
-
 
491
 
499
 
492
    return String::New(hexId.c_str());
500
    return String::New(hexId.c_str());
493
}
501
}
494
 
502