Subversion Repositories HomeAutomation

Rev

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

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