Subversion Repositories HomeAutomation

Rev

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.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
 
40
 
41
using namespace v8;
41
using namespace v8;
42
 
42
 
43
Handle<Value> VirtualMachine_log(const Arguments& args);
43
Handle<Value> VirtualMachine_log(const Arguments& args);
44
Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args);
44
Handle<Value> VirtualMachine_sendCanMessage(const Arguments& args);
45
Handle<Value> VirtualMachine_loadScript(const Arguments& args);
45
Handle<Value> VirtualMachine_loadScript(const Arguments& args);
46
Handle<Value> VirtualMachine_stopIntervalThread(const Arguments& args);
46
Handle<Value> VirtualMachine_stopIntervalThread(const Arguments& args);
47
Handle<Value> VirtualMachine_startIntervalThread(const Arguments& args);
47
Handle<Value> VirtualMachine_startIntervalThread(const Arguments& args);
48
Handle<Value> VirtualMachine_stopSocketThread(const Arguments& args);
48
Handle<Value> VirtualMachine_stopSocketThread(const Arguments& args);
49
Handle<Value> VirtualMachine_startSocketThread(const Arguments& args);
49
Handle<Value> VirtualMachine_startSocketThread(const Arguments& args);
50
Handle<Value> VirtualMachine_sendToSocketThread(const Arguments& args);
50
Handle<Value> VirtualMachine_sendToSocketThread(const Arguments& args);
51
Handle<Value> VirtualMachine_uint2hex(const Arguments& args);
51
Handle<Value> VirtualMachine_uint2hex(const Arguments& args);
52
 
52
 
53
class VirtualMachine : public Thread<VirtualMachine>
53
class VirtualMachine : public Thread<VirtualMachine>
54
{
54
{
55
public:
55
public:
56
    static VirtualMachine& getInstance();
56
    static VirtualMachine& getInstance();
57
    static void deleteInstance();
57
    static void deleteInstance();
58
 
58
 
59
    void queueCanMessage(CanMessage canMessage);
59
    void queueCanMessage(CanMessage canMessage);
60
    void queueExpression(string expression);
60
    void queueExpression(string expression);
61
 
61
 
62
    void run();
62
    void run();
63
 
63
 
64
    bool loadScript(string scriptName);
64
    bool loadScript(string scriptName);
65
 
65
 
66
    unsigned int startIntervalThread(unsigned int timeout);
66
    unsigned int startIntervalThread(unsigned int timeout);
67
    bool stopIntervalThread(unsigned int id);
67
    bool stopIntervalThread(unsigned int id);
68
 
68
 
69
    unsigned int startSocketThread(string address, int port, unsigned int reconnectTimeout);
69
    unsigned int startSocketThread(string address, int port, unsigned int reconnectTimeout);
70
    bool stopSocketThread(unsigned int id);
70
    bool stopSocketThread(unsigned int id);
71
    void sendToSocketThread(unsigned int id, string data);
71
    void sendToSocketThread(unsigned int id, string data);
72
 
72
 
73
private:
73
private:
74
    void callHandleHeartbeat(int hardwareId);
74
    void callHandleHeartbeat(int hardwareId);
75
    void callHandleMessage(CanMessage canMessage);
75
    void callHandleMessage(CanMessage canMessage);
76
    bool runExpression(string expression);
76
    bool runExpression(string expression);
77
 
77
 
78
    VirtualMachine();
78
    VirtualMachine();
79
    ~VirtualMachine();
79
    ~VirtualMachine();
80
    static VirtualMachine* myInstance;
80
    static VirtualMachine* myInstance;
81
 
81
 
82
    Handle<Function> myFunctionHandleHeartbeat;
82
    Handle<Function> myFunctionHandleHeartbeat;
83
    Handle<Function> myFunctionOfflineCheck;
83
    Handle<Function> myFunctionOfflineCheck;
84
    Handle<Function> myFunctionHandleMessage;
84
    Handle<Function> myFunctionHandleMessage;
85
    Handle<Function> myFunctionStartup;
85
    Handle<Function> myFunctionStartup;
86
 
86
 
87
    Handle<ObjectTemplate> myGlobal;
87
    Handle<ObjectTemplate> myGlobal;
88
    HandleScope myHandleScope;
88
    HandleScope myHandleScope;
89
    Handle<Context> myContext;
89
    Handle<Context> myContext;
90
 
90
 
91
    Semaphore mySemaphore;
91
    Semaphore mySemaphore;
92
 
92
 
93
    ThreadSafeQueue<string> myExpressions;
93
    ThreadSafeQueue<string> myExpressions;
94
    ThreadSafeQueue<CanMessage> myCanMessages;
94
    ThreadSafeQueue<CanMessage> myCanMessages;
95
 
95
 
96
    map<int, IntervalThread> myIntervalThreads;
96
    map<int, IntervalThread> myIntervalThreads;
97
    map<int, SocketThread> mySocketThreads;
97
    map<int, SocketThread*> mySocketThreads;
98
};
98
};
99
 
99
 
100
#endif  /* _VIRTUALMACHINE_H */
100
#endif  /* _VIRTUALMACHINE_H */
101
 
101
 
102
 
102