Subversion Repositories HomeAutomation

Rev

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

Rev 984 Rev 985
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
    CanIdTranslator::deleteInstance();
54
    CanIdTranslator::deleteInstance();
55
    myChannel->stop();
55
    myChannel->stop();
56
    delete myChannel;
56
    delete myChannel;
57
}
57
}
58
 
58
 
59
void CanNetManager::openChannel()
59
void CanNetManager::openChannel()
60
{
60
{
61
    SyslogStream &slog = SyslogStream::getInstance();
61
    SyslogStream &slog = SyslogStream::getInstance();
62
    VirtualMachine &vm = VirtualMachine::getInstance();
62
    VirtualMachine &vm = VirtualMachine::getInstance();
63
    CanDebug &canDebug = CanDebug::getInstance();
63
    CanDebug &canDebug = CanDebug::getInstance();
64
 
64
 
65
    string address = Settings::get("CanNetAddress");
65
    string address = Settings::get("CanNetAddress");
-
 
66
    if (address == "")
-
 
67
    {
-
 
68
        throw new Atom::Exception("CanNetAddress is not defined in the config file, can not start.");
-
 
69
    }
-
 
70
 
66
    string port = Settings::get("CanNetPort");
71
    string port = Settings::get("CanNetPort");
-
 
72
    if (port == "")
-
 
73
    {
-
 
74
        throw new Atom::Exception("CanNetPort is not defined in the config file, can not start.");
-
 
75
    }
67
 
76
 
68
    myChannel->setAddress(address);
77
    myChannel->setAddress(address);
69
    myChannel->setPort(stoi(port));
78
    myChannel->setPort(stoi(port));
70
    myChannel->setReconnectTimeout(10);
79
    myChannel->setReconnectTimeout(10);
71
 
80
 
72
    myChannel->start();
81
    myChannel->start();
73
 
82
 
74
    myChannel->eventStartListen();
83
    myChannel->eventStartListen();
75
 
84
 
76
    bool waitingForPong = false;
85
    bool waitingForPong = false;
77
    vector<string> dataLines;
86
    vector<string> dataLines;
78
    string data;
87
    string data;
79
    CanMessage canMessage;
88
    CanMessage canMessage;
80
 
89
 
81
    while (1)
90
    while (1)
82
    {
91
    {
83
        myChannel->eventWait();
92
        myChannel->eventWait();
84
 
93
 
85
        while (myChannel->eventIsAvailable())
94
        while (myChannel->eventIsAvailable())
86
        {
95
        {
87
            SocketEvent socketEvent = myChannel->eventFetch();
96
            SocketEvent socketEvent = myChannel->eventFetch();
88
 
97
 
89
            switch (socketEvent.getType())
98
            switch (socketEvent.getType())
90
            {
99
            {
91
                case SocketEvent::TYPE_CONNECTED:
100
                case SocketEvent::TYPE_CONNECTED:
92
                slog << "Connected to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n";
101
                slog << "Connected to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n";
93
                break;
102
                break;
94
 
103
 
95
                case SocketEvent::TYPE_CONNECTING:
104
                case SocketEvent::TYPE_CONNECTING:
96
                slog << "Connecting to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n";
105
                slog << "Connecting to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n";
97
                break;
106
                break;
98
 
107
 
99
                case SocketEvent::TYPE_CONNECTION_CLOSED:
108
                case SocketEvent::TYPE_CONNECTION_CLOSED:
100
                slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " closed.\n";
109
                slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " closed.\n";
101
                vm.queueExpression("setAllOffline();");
110
                vm.queueExpression("setAllOffline();");
102
                break;
111
                break;
103
 
112
 
104
                case SocketEvent::TYPE_CONNECTION_DIED:
113
                case SocketEvent::TYPE_CONNECTION_DIED:
105
                slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " died :: " + socketEvent.getData() + "\n";
114
                slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " died :: " + socketEvent.getData() + "\n";
106
                vm.queueExpression("setAllOffline();");
115
                vm.queueExpression("setAllOffline();");
107
                break;
116
                break;
108
 
117
 
109
                case SocketEvent::TYPE_CONNECTION_RESET:
118
                case SocketEvent::TYPE_CONNECTION_RESET:
110
                slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " was reset :: " + socketEvent.getData() + "\n";
119
                slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " was reset :: " + socketEvent.getData() + "\n";
111
                vm.queueExpression("setAllOffline();");
120
                vm.queueExpression("setAllOffline();");
112
                break;
121
                break;
113
 
122
 
114
                case SocketEvent::TYPE_CONNECTION_FAILED:
123
                case SocketEvent::TYPE_CONNECTION_FAILED:
115
                slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " failed :: " + socketEvent.getData() + "\n";
124
                slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " failed :: " + socketEvent.getData() + "\n";
116
                vm.queueExpression("setAllOffline();");
125
                vm.queueExpression("setAllOffline();");
117
                break;
126
                break;
118
 
127
 
119
                case SocketEvent::TYPE_DATA:
128
                case SocketEvent::TYPE_DATA:
120
                waitingForPong = false;
129
                waitingForPong = false;
121
                try
130
                try
122
                {
131
                {
123
                    dataLines = explode("\n", socketEvent.getData());
132
                    dataLines = explode("\n", socketEvent.getData());
124
 
133
 
125
                    for (int n = 0; n < dataLines.size(); n++)
134
                    for (int n = 0; n < dataLines.size(); n++)
126
                    {
135
                    {
127
                        data = trim(dataLines[n], '\n');
136
                        data = trim(dataLines[n], '\n');
128
 
137
 
129
                        if (data == "PONG")
138
                        if (data == "PONG")
130
                        {
139
                        {
131
                            slog << "Received pong.\n";
140
                            slog << "Received pong.\n";
132
                            continue;
141
                            continue;
133
                        }
142
                        }
134
 
143
 
135
                        canMessage.setRaw(data);
144
                        canMessage.setRaw(data);
136
 
145
 
137
                        if (!canMessage.isUnknown())
146
                        if (!canMessage.isUnknown())
138
                        {
147
                        {
139
                            vm.queueCanMessage(canMessage);
148
                            vm.queueCanMessage(canMessage);
140
                            canDebug.sendCanMessage(canMessage);
149
                            canDebug.sendCanMessage(canMessage);
141
                        }
150
                        }
142
                        else
151
                        else
143
                        {
152
                        {
144
                            canDebug.sendData(data + "\n");
153
                            canDebug.sendData(data + "\n");
145
                        }
154
                        }
146
                    }
155
                    }
147
                }
156
                }
148
                catch (CanMessageException* e)
157
                catch (CanMessageException* e)
149
                {
158
                {
150
                    slog << "CanMessageException was caught:\n";
159
                    slog << "CanMessageException was caught:\n";
151
                    slog << e->getDescription() + "\n";
160
                    slog << e->getDescription() + "\n";
152
                }
161
                }
153
                break;
162
                break;
154
 
163
 
155
                case SocketEvent::TYPE_INACTIVITY:
164
                case SocketEvent::TYPE_INACTIVITY:
156
                if (waitingForPong)
165
                if (waitingForPong)
157
                {
166
                {
158
                    slog << "We have not received a pong for our ping.\n";
167
                    slog << "We have not received a pong for our ping.\n";
159
                    waitingForPong = false;
168
                    waitingForPong = false;
160
                    vm.queueExpression("setAllOffline();");
169
                    vm.queueExpression("setAllOffline();");
161
                    myChannel->forceReconnect();
170
                    myChannel->forceReconnect();
162
                }
171
                }
163
                else
172
                else
164
                {
173
                {
165
                    //slog << "We have not received anything from the canDaemon in some time.\n";
174
                    //slog << "We have not received anything from the canDaemon in some time.\n";
166
                    waitingForPong = true;
175
                    waitingForPong = true;
167
                    slog << "Sending ping.\n";
176
                    slog << "Sending ping.\n";
168
                    myChannel->sendData("PING");
177
                    myChannel->sendData("PING");
169
                }
178
                }
170
                break;
179
                break;
171
 
180
 
172
                case SocketEvent::TYPE_WAITING_RECONNECT:
181
                case SocketEvent::TYPE_WAITING_RECONNECT:
173
                slog << "Will try to reconnect to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " in " + itos(myChannel->getReconnectTimeout()) + " seconds.\n";
182
                slog << "Will try to reconnect to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " in " + itos(myChannel->getReconnectTimeout()) + " seconds.\n";
174
                break;
183
                break;
175
            }
184
            }
176
        }
185
        }
177
    }
186
    }
178
 
187
 
179
    myChannel->eventStopListen();
188
    myChannel->eventStopListen();
180
}
189
}
181
 
190
 
182
void CanNetManager::sendMessage(CanMessage canMessage)
191
void CanNetManager::sendMessage(CanMessage canMessage)
183
{
192
{
184
    CanDebug &canDebug = CanDebug::getInstance();
193
    CanDebug &canDebug = CanDebug::getInstance();
185
    canDebug.sendCanMessage(canMessage);
194
    canDebug.sendCanMessage(canMessage);
186
    myChannel->sendData(canMessage.getRaw());
195
    myChannel->sendData(canMessage.getRaw());
187
}
196
}
188
 
197
 
189
 
198