Subversion Repositories HomeAutomation

Rev

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