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 1024
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) November 29, 2008 by Mattias Runge                             *
2
 *   Copyright (C) November 29, 2008 by Mattias Runge                             *
3
 *   mattias@runge.se                                                      *
3
 *   mattias@runge.se                                                      *
4
 *   cannetmanager.cpp                                            *
4
 *   cannetmanager.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 "canmessage.h"
22
#include "canmessage.h"
23
#include "cannetmanager.h"
23
#include "cannetmanager.h"
24
 
24
 
25
CanNetManager* CanNetManager::myInstance = NULL;
25
CanNetManager* CanNetManager::myInstance = NULL;
26
 
26
 
27
CanNetManager& CanNetManager::getInstance()
27
CanNetManager& CanNetManager::getInstance()
28
{
28
{
29
    if (myInstance == NULL)
29
    if (myInstance == NULL)
30
    {
30
    {
31
        myInstance = new CanNetManager();
31
        myInstance = new CanNetManager();
32
    }
32
    }
33
 
33
 
34
    return *myInstance;
34
    return *myInstance;
35
}
35
}
36
 
36
 
37
void CanNetManager::deleteInstance()
37
void CanNetManager::deleteInstance()
38
{
38
{
39
    if (myInstance != NULL)
39
    if (myInstance != NULL)
40
    {
40
    {
41
        delete myInstance;
41
        delete myInstance;
42
        myInstance = NULL;
42
        myInstance = NULL;
43
    }
43
    }
44
}
44
}
45
 
45
 
46
CanNetManager::CanNetManager()
46
CanNetManager::CanNetManager()
47
{
47
{
48
    CanIdTranslator &translator = CanIdTranslator::getInstance();
48
    CanIdTranslator &translator = CanIdTranslator::getInstance();
49
    myChannel = new AsyncSocket();
49
    myChannel = new AsyncSocket();
50
}
50
}
51
 
51
 
52
CanNetManager::~CanNetManager()
52
CanNetManager::~CanNetManager()
53
{
53
{
54
    stop();
54
    stop();
55
    CanIdTranslator::deleteInstance();
55
    CanIdTranslator::deleteInstance();
56
    myChannel->stop();
56
    myChannel->stop();
57
    delete myChannel;
57
    delete myChannel;
58
}
58
}
59
 
59
 
60
void CanNetManager::run()
60
void CanNetManager::run()
61
{
61
{
62
    SyslogStream &slog = SyslogStream::getInstance();
62
    Logger &log = Logger::getInstance();
63
    VirtualMachine &vm = VirtualMachine::getInstance();
63
    VirtualMachine &vm = VirtualMachine::getInstance();
64
    CanDebug &canDebug = CanDebug::getInstance();
64
    CanDebug &canDebug = CanDebug::getInstance();
65
 
65
 
66
    string address = Settings::get("CanNetAddress");
66
    string address = Settings::get("CanNetAddress");
67
    if (address == "")
67
    if (address == "")
68
    {
68
    {
69
        throw new Atom::Exception("CanNetAddress is not defined in the config file, can not start.");
69
        throw new Atom::Exception("CanNetAddress is not defined in the config file, can not start.");
70
    }
70
    }
71
 
71
 
72
    string port = Settings::get("CanNetPort");
72
    string port = Settings::get("CanNetPort");
73
    if (port == "")
73
    if (port == "")
74
    {
74
    {
75
        throw new Atom::Exception("CanNetPort is not defined in the config file, can not start.");
75
        throw new Atom::Exception("CanNetPort is not defined in the config file, can not start.");
76
    }
76
    }
77
 
77
 
78
    myChannel->setAddress(address);
78
    myChannel->setAddress(address);
79
    myChannel->setPort(stoi(port));
79
    myChannel->setPort(stoi(port));
80
    myChannel->setReconnectTimeout(10);
80
    myChannel->setReconnectTimeout(10);
81
 
81
 
82
    myChannel->start();
82
    myChannel->start();
83
 
83
 
84
    myChannel->eventStartListen();
84
    myChannel->eventStartListen();
85
 
85
 
86
    bool waitingForPong = false;
86
    bool waitingForPong = false;
87
    vector<string> dataLines;
87
    vector<string> dataLines;
88
    string data;
88
    string data;
89
    CanMessage *canMessage = NULL;
89
    CanMessage *canMessage = NULL;
90
    string expression;
90
    string expression;
91
 
91
 
92
    while (1)
92
    while (1)
93
    {
93
    {
94
        myChannel->eventWait();
94
        myChannel->eventWait();
95
 
95
 
96
        while (myChannel->eventIsAvailable())
96
        while (myChannel->eventIsAvailable())
97
        {
97
        {
98
            SocketEvent socketEvent = myChannel->eventFetch();
98
            SocketEvent socketEvent = myChannel->eventFetch();
99
 
99
 
100
            switch (socketEvent.getType())
100
            switch (socketEvent.getType())
101
            {
101
            {
102
                case SocketEvent::TYPE_CONNECTED:
102
                case SocketEvent::TYPE_CONNECTED:
103
                slog << "Connected to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n";
103
                log.add("Connected to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n");
104
                break;
104
                break;
105
 
105
 
106
                case SocketEvent::TYPE_CONNECTING:
106
                case SocketEvent::TYPE_CONNECTING:
107
                slog << "Connecting to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n";
107
                log.add("Connecting to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n");
108
                break;
108
                break;
109
 
109
 
110
                case SocketEvent::TYPE_CONNECTION_CLOSED:
110
                case SocketEvent::TYPE_CONNECTION_CLOSED:
111
                slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " closed.\n";
111
                log.add("Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " closed.\n");
112
                vm.queueExpression("setAllOffline();");
112
                vm.queueExpression("setAllOffline();");
113
                break;
113
                break;
114
 
114
 
115
                case SocketEvent::TYPE_CONNECTION_DIED:
115
                case SocketEvent::TYPE_CONNECTION_DIED:
116
                slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " died :: " + socketEvent.getData() + "\n";
116
                log.add("Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " died :: " + socketEvent.getData() + "\n");
117
                vm.queueExpression("setAllOffline();");
117
                vm.queueExpression("setAllOffline();");
118
                break;
118
                break;
119
 
119
 
120
                case SocketEvent::TYPE_CONNECTION_RESET:
120
                case SocketEvent::TYPE_CONNECTION_RESET:
121
                slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " was reset :: " + socketEvent.getData() + "\n";
121
                log.add("Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " was reset :: " + socketEvent.getData() + "\n");
122
                vm.queueExpression("setAllOffline();");
122
                vm.queueExpression("setAllOffline();");
123
                break;
123
                break;
124
 
124
 
125
                case SocketEvent::TYPE_CONNECTION_FAILED:
125
                case SocketEvent::TYPE_CONNECTION_FAILED:
126
                slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " failed :: " + socketEvent.getData() + "\n";
126
                log.add("Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " failed :: " + socketEvent.getData() + "\n");
127
                vm.queueExpression("setAllOffline();");
127
                vm.queueExpression("setAllOffline();");
128
                break;
128
                break;
129
 
129
 
130
                case SocketEvent::TYPE_DATA:
130
                case SocketEvent::TYPE_DATA:
131
                waitingForPong = false;
131
                waitingForPong = false;
132
                try
132
                try
133
                {
133
                {
134
                    dataLines = explode("\n", socketEvent.getData());
134
                    dataLines = explode("\n", socketEvent.getData());
135
 
135
 
136
                    for (int n = 0; n < dataLines.size(); n++)
136
                    for (int n = 0; n < dataLines.size(); n++)
137
                    {
137
                    {
138
                        data = trim(dataLines[n], '\n');
138
                        data = trim(dataLines[n], '\n');
139
 
139
 
140
                        if (data == "PONG")
140
                        if (data == "PONG")
141
                        {
141
                        {
142
                            slog << "Received pong.\n";
142
                            log.add("Received pong.\n");
143
                            continue;
143
                            continue;
144
                        }
144
                        }
145
 
145
 
146
                        canMessage = new CanMessage(data);
146
                        canMessage = new CanMessage(data);
147
 
147
 
148
                        if (!canMessage->isUnknown())
148
                        if (!canMessage->isUnknown())
149
                        {
149
                        {
150
                            if (canMessage->getClassName() == "nmt")
150
                            if (canMessage->getClassName() == "nmt")
151
                            {
151
                            {
152
                                expression = "handleNMTMessage(";
152
                                expression = "handleNMTMessage(";
153
                                expression += "'" + canMessage->getClassName() + "', ";
153
                                expression += "'" + canMessage->getClassName() + "', ";
154
                                expression += "'" + canMessage->getCommandName() + "', ";
154
                                expression += "'" + canMessage->getCommandName() + "', ";
155
                                expression += canMessage->getJSONData();
155
                                expression += canMessage->getJSONData();
156
                                expression += ");";
156
                                expression += ");";
157
                            }
157
                            }
158
                            else
158
                            else
159
                            {
159
                            {
160
                                expression = "handleMessage(";
160
                                expression = "handleMessage(";
161
                                expression += "'" + canMessage->getClassName() + "', ";
161
                                expression += "'" + canMessage->getClassName() + "', ";
162
                                expression += "'" + canMessage->getDirectionFlag() + "', ";
162
                                expression += "'" + canMessage->getDirectionFlag() + "', ";
163
                                expression += "'" + canMessage->getModuleName() + "', ";
163
                                expression += "'" + canMessage->getModuleName() + "', ";
164
                                expression += "" + itos(canMessage->getModuleId()) + ", ";
164
                                expression += "" + itos(canMessage->getModuleId()) + ", ";
165
                                expression += "'" + canMessage->getCommandName() + "', ";
165
                                expression += "'" + canMessage->getCommandName() + "', ";
166
                                expression += canMessage->getJSONData();
166
                                expression += canMessage->getJSONData();
167
                                expression += ");";
167
                                expression += ");";
168
                            }
168
                            }
169
 
169
 
170
                            vm.queueExpression(expression);
170
                            vm.queueExpression(expression);
171
                            canDebug.sendCanMessageToAll(*canMessage);
171
                            canDebug.sendCanMessageToAll(*canMessage);
172
                        }
172
                        }
173
                        else
173
                        else
174
                        {
174
                        {
175
                            canDebug.sendToAll(data + "\n");
175
                            canDebug.sendToAll(data + "\n");
176
                        }
176
                        }
177
 
177
 
178
                        delete canMessage;
178
                        delete canMessage;
179
                        canMessage = NULL;
179
                        canMessage = NULL;
180
                    }
180
                    }
181
                }
181
                }
182
                catch (CanMessageException* e)
182
                catch (CanMessageException* e)
183
                {
183
                {
184
                    if (canMessage != NULL)
184
                    if (canMessage != NULL)
185
                    {
185
                    {
186
                        delete canMessage;
186
                        delete canMessage;
187
                    }
187
                    }
188
 
188
 
189
                    slog << "CanMessageException was caught:\n";
189
                    log.add("CanMessageException was caught:\n");
190
                    slog << e->getDescription() + "\n";
190
                    log.add(e->getDescription() + "\n");
191
                }
191
                }
192
                break;
192
                break;
193
 
193
 
194
                case SocketEvent::TYPE_INACTIVITY:
194
                case SocketEvent::TYPE_INACTIVITY:
195
                if (waitingForPong)
195
                if (waitingForPong)
196
                {
196
                {
197
                    slog << "We have not received a pong for our ping.\n";
197
                    log.add("We have not received a pong for our ping.\n");
198
                    waitingForPong = false;
198
                    waitingForPong = false;
199
                    vm.queueExpression("setAllOffline();");
199
                    vm.queueExpression("setAllOffline();");
200
                    myChannel->forceReconnect();
200
                    myChannel->forceReconnect();
201
                }
201
                }
202
                else
202
                else
203
                {
203
                {
204
                    //slog << "We have not received anything from the canDaemon in some time.\n";
204
                    //log.add("We have not received anything from the canDaemon in some time.\n";
205
                    waitingForPong = true;
205
                    waitingForPong = true;
206
                    slog << "Sending ping.\n";
206
                    log.add("Sending ping.\n");
207
                    myChannel->sendData("PING");
207
                    myChannel->sendData("PING");
208
                }
208
                }
209
                break;
209
                break;
210
 
210
 
211
                case SocketEvent::TYPE_WAITING_RECONNECT:
211
                case SocketEvent::TYPE_WAITING_RECONNECT:
212
                slog << "Will try to reconnect to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " in " + itos(myChannel->getReconnectTimeout()) + " seconds.\n";
212
                log.add("Will try to reconnect to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " in " + itos(myChannel->getReconnectTimeout()) + " seconds.\n");
213
                break;
213
                break;
214
            }
214
            }
215
        }
215
        }
216
    }
216
    }
217
 
217
 
218
    myChannel->eventStopListen();
218
    myChannel->eventStopListen();
219
}
219
}
220
 
220
 
221
void CanNetManager::sendMessage(CanMessage canMessage)
221
void CanNetManager::sendMessage(CanMessage canMessage)
222
{
222
{
223
    CanDebug &canDebug = CanDebug::getInstance();
223
    CanDebug &canDebug = CanDebug::getInstance();
224
    myChannel->sendData(canMessage.getRaw());
224
    myChannel->sendData(canMessage.getRaw());
225
    canDebug.sendCanMessageToAll(canMessage);
225
    canDebug.sendCanMessageToAll(canMessage);
226
}
226
}
227
 
227
 
228
 
228