Subversion Repositories HomeAutomation

Rev

Rev 1402 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
969 runge 1
/***************************************************************************
2
 *   Copyright (C) December 10, 2008 by Mattias Runge                             *
3
 *   mattias@runge.se                                                      *
4
 *   virtualmachine.h                                            *
5
 *                                                                         *
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  *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 *   (at your option) any later version.                                   *
10
 *                                                                         *
11
 *   This program is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU General Public License for more details.                          *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
21
 
22
#ifndef _VIRTUALMACHINE_H
23
#define _VIRTUALMACHINE_H
24
 
25
using namespace std;
26
 
27
#include <map>
28
#include <string>
29
#include <queue>
30
 
1196 runge 31
#include "../v8/include/v8-debug.h"
1024 runge 32
#include "../Logger/logger.h"
969 runge 33
#include "../Settings/settings.h"
34
#include "../CanNet/cannetmanager.h"
35
#include "../CanNet/canmessage.h"
36
#include "../Threads/semaphore.h"
37
#include "../Threads/threadsafequeue.h"
38
#include "intervalthread.h"
976 runge 39
#include "socketthread.h"
999 runge 40
#include "commandthread.h"
41
#include "expression.h"
1229 arune 42
#include "../Tools/base64.h"
969 runge 43
 
44
using namespace v8;
45
 
46
class VirtualMachine : public Thread<VirtualMachine>
47
{
48
public:
49
    static VirtualMachine& getInstance();
50
    static void deleteInstance();
51
 
52
    void run();
53
 
999 runge 54
    void queueExpression(Expression expression);
55
    void queueExpression(string script) { queueExpression(Expression(script)); };
56
 
969 runge 57
    bool loadScript(string scriptName);
1007 runge 58
    bool loadDataStore(string storeName);
976 runge 59
 
971 runge 60
    unsigned int startIntervalThread(unsigned int timeout);
1042 runge 61
    bool setIntervalThreadTimeout(unsigned int id, unsigned int timeout);
971 runge 62
    bool stopIntervalThread(unsigned int id);
969 runge 63
 
976 runge 64
    unsigned int startSocketThread(string address, int port, unsigned int reconnectTimeout);
65
    bool stopSocketThread(unsigned int id);
66
    void sendToSocketThread(unsigned int id, string data);
67
 
999 runge 68
    void sendToCommandClient(unsigned int id, string data);
69
    void disconnectCommandClient(unsigned int id);
70
 
71
    void print(unsigned int id, string data);
72
 
73
    static Handle<Value> _quit(const Arguments& args);
74
    static Handle<Value> _print(const Arguments& args);
75
    static Handle<Value> _log(const Arguments& args);
76
    static Handle<Value> _sendCanMessage(const Arguments& args);
77
    static Handle<Value> _sendCanNMTMessage(const Arguments& args);
78
    static Handle<Value> _loadScript(const Arguments& args);
79
    static Handle<Value> _stopIntervalThread(const Arguments& args);
80
    static Handle<Value> _startIntervalThread(const Arguments& args);
1042 runge 81
    static Handle<Value> _setIntervalThreadTimeout(const Arguments& args);
999 runge 82
    static Handle<Value> _stopSocketThread(const Arguments& args);
83
    static Handle<Value> _startSocketThread(const Arguments& args);
84
    static Handle<Value> _sendToSocketThread(const Arguments& args);
1402 runge 85
    static Handle<Value> _system(const Arguments& args);
86
 
999 runge 87
    static Handle<Value> _uint2hex(const Arguments& args);
1010 runge 88
    static Handle<Value> _hex2bin(const Arguments& args);
89
    static Handle<Value> _bin2hex(const Arguments& args);
90
    static Handle<Value> _bin2float(const Arguments& args);
91
    static Handle<Value> _float2bin(const Arguments& args);
92
    static Handle<Value> _bin2uint(const Arguments& args);
93
    static Handle<Value> _uint2bin(const Arguments& args);
1011 runge 94
    static Handle<Value> _hex2uint(const Arguments& args);
1010 runge 95
 
1060 runge 96
    static Handle<Value> _sleep(const Arguments& args);
1010 runge 97
 
1007 runge 98
    static Handle<Value> _loadDataStore(const Arguments& args);
1008 runge 99
    static Handle<Value> _getFileContents(const Arguments& args);
1405 runge 100
    static Handle<Value> _setFileContents(const Arguments& args);
999 runge 101
 
969 runge 102
private:
999 runge 103
    bool runExpression(Expression expression);
104
    string formatException(TryCatch* tryCatch);
969 runge 105
 
106
    VirtualMachine();
107
    ~VirtualMachine();
108
    static VirtualMachine* myInstance;
109
 
110
    Handle<Function> myFunctionStartup;
111
 
112
    Handle<ObjectTemplate> myGlobal;
113
    HandleScope myHandleScope;
114
    Handle<Context> myContext;
115
 
116
    Semaphore mySemaphore;
117
 
999 runge 118
    ThreadSafeQueue<Expression> myExpressions;
969 runge 119
 
999 runge 120
    map<int, IntervalThread*> myIntervalThreads;
1203 arune 121
    map<unsigned long int, SocketThread*> mySocketThreads;
999 runge 122
 
123
    CommandThread myCommandThread;
969 runge 124
};
125
 
126
#endif  /* _VIRTUALMACHINE_H */
127