Subversion Repositories HomeAutomation

Rev

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

Rev 999 Rev 1007
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.h                                            *
4
 *   virtualmachine.h                                            *
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
#ifndef _VIRTUALMACHINE_H
22
#ifndef _VIRTUALMACHINE_H
23
#define _VIRTUALMACHINE_H
23
#define _VIRTUALMACHINE_H
24
 
24
 
25
using namespace std;
25
using namespace std;
26
 
26
 
27
#include <map>
27
#include <map>
28
#include <string>
28
#include <string>
29
#include <queue>
29
#include <queue>
30
 
30
 
31
#include "../v8/include/v8.h"
31
#include "../v8/include/v8.h"
32
#include "../SyslogStream/syslogstream.h"
32
#include "../SyslogStream/syslogstream.h"
33
#include "../Settings/settings.h"
33
#include "../Settings/settings.h"
34
#include "../CanNet/cannetmanager.h"
34
#include "../CanNet/cannetmanager.h"
35
#include "../CanNet/canmessage.h"
35
#include "../CanNet/canmessage.h"
36
#include "../Threads/semaphore.h"
36
#include "../Threads/semaphore.h"
37
#include "../Threads/threadsafequeue.h"
37
#include "../Threads/threadsafequeue.h"
38
#include "intervalthread.h"
38
#include "intervalthread.h"
39
#include "socketthread.h"
39
#include "socketthread.h"
40
#include "commandthread.h"
40
#include "commandthread.h"
41
#include "expression.h"
41
#include "expression.h"
42
 
42
 
43
using namespace v8;
43
using namespace v8;
44
 
44
 
45
class VirtualMachine : public Thread<VirtualMachine>
45
class VirtualMachine : public Thread<VirtualMachine>
46
{
46
{
47
public:
47
public:
48
    static VirtualMachine& getInstance();
48
    static VirtualMachine& getInstance();
49
    static void deleteInstance();
49
    static void deleteInstance();
50
 
50
 
51
    void run();
51
    void run();
52
 
52
 
53
    void queueExpression(Expression expression);
53
    void queueExpression(Expression expression);
54
    void queueExpression(string script) { queueExpression(Expression(script)); };
54
    void queueExpression(string script) { queueExpression(Expression(script)); };
55
 
55
 
56
    bool loadScript(string scriptName);
56
    bool loadScript(string scriptName);
-
 
57
    bool loadDataStore(string storeName);
57
 
58
 
58
    unsigned int startIntervalThread(unsigned int timeout);
59
    unsigned int startIntervalThread(unsigned int timeout);
59
    bool stopIntervalThread(unsigned int id);
60
    bool stopIntervalThread(unsigned int id);
60
 
61
 
61
    unsigned int startSocketThread(string address, int port, unsigned int reconnectTimeout);
62
    unsigned int startSocketThread(string address, int port, unsigned int reconnectTimeout);
62
    bool stopSocketThread(unsigned int id);
63
    bool stopSocketThread(unsigned int id);
63
    void sendToSocketThread(unsigned int id, string data);
64
    void sendToSocketThread(unsigned int id, string data);
64
 
65
 
65
    void sendToCommandClient(unsigned int id, string data);
66
    void sendToCommandClient(unsigned int id, string data);
66
    void disconnectCommandClient(unsigned int id);
67
    void disconnectCommandClient(unsigned int id);
67
 
68
 
68
    void print(unsigned int id, string data);
69
    void print(unsigned int id, string data);
69
 
70
 
70
    static Handle<Value> _quit(const Arguments& args);
71
    static Handle<Value> _quit(const Arguments& args);
71
    static Handle<Value> _print(const Arguments& args);
72
    static Handle<Value> _print(const Arguments& args);
72
    static Handle<Value> _log(const Arguments& args);
73
    static Handle<Value> _log(const Arguments& args);
73
    static Handle<Value> _sendCanMessage(const Arguments& args);
74
    static Handle<Value> _sendCanMessage(const Arguments& args);
74
    static Handle<Value> _sendCanNMTMessage(const Arguments& args);
75
    static Handle<Value> _sendCanNMTMessage(const Arguments& args);
75
    static Handle<Value> _loadScript(const Arguments& args);
76
    static Handle<Value> _loadScript(const Arguments& args);
76
    static Handle<Value> _stopIntervalThread(const Arguments& args);
77
    static Handle<Value> _stopIntervalThread(const Arguments& args);
77
    static Handle<Value> _startIntervalThread(const Arguments& args);
78
    static Handle<Value> _startIntervalThread(const Arguments& args);
78
    static Handle<Value> _stopSocketThread(const Arguments& args);
79
    static Handle<Value> _stopSocketThread(const Arguments& args);
79
    static Handle<Value> _startSocketThread(const Arguments& args);
80
    static Handle<Value> _startSocketThread(const Arguments& args);
80
    static Handle<Value> _sendToSocketThread(const Arguments& args);
81
    static Handle<Value> _sendToSocketThread(const Arguments& args);
81
    static Handle<Value> _uint2hex(const Arguments& args);
82
    static Handle<Value> _uint2hex(const Arguments& args);
-
 
83
    static Handle<Value> _loadDataStore(const Arguments& args);
82
 
84
 
83
private:
85
private:
84
    bool runExpression(Expression expression);
86
    bool runExpression(Expression expression);
85
    string formatException(TryCatch* tryCatch);
87
    string formatException(TryCatch* tryCatch);
86
 
88
 
87
    VirtualMachine();
89
    VirtualMachine();
88
    ~VirtualMachine();
90
    ~VirtualMachine();
89
    static VirtualMachine* myInstance;
91
    static VirtualMachine* myInstance;
90
 
92
 
91
    Handle<Function> myFunctionStartup;
93
    Handle<Function> myFunctionStartup;
92
 
94
 
93
    Handle<ObjectTemplate> myGlobal;
95
    Handle<ObjectTemplate> myGlobal;
94
    HandleScope myHandleScope;
96
    HandleScope myHandleScope;
95
    Handle<Context> myContext;
97
    Handle<Context> myContext;
96
 
98
 
97
    Semaphore mySemaphore;
99
    Semaphore mySemaphore;
98
 
100
 
99
    ThreadSafeQueue<Expression> myExpressions;
101
    ThreadSafeQueue<Expression> myExpressions;
100
 
102
 
101
    map<int, IntervalThread*> myIntervalThreads;
103
    map<int, IntervalThread*> myIntervalThreads;
102
    map<int, SocketThread*> mySocketThreads;
104
    map<int, SocketThread*> mySocketThreads;
103
 
105
 
104
    CommandThread myCommandThread;
106
    CommandThread myCommandThread;
105
};
107
};
106
 
108
 
107
#endif  /* _VIRTUALMACHINE_H */
109
#endif  /* _VIRTUALMACHINE_H */
108
 
110
 
109
 
111