Subversion Repositories HomeAutomation

Rev

Details | 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();
55
    delete myChannel;
56
}
57
 
58
void CanNetManager::openChannel()
59
{
60
    SyslogStream &slog = SyslogStream::getInstance();
61
    VirtualMachine &vm = VirtualMachine::getInstance();
981 runge 62
    CanDebug &canDebug = CanDebug::getInstance();
969 runge 63
 
64
    string address = Settings::get("CanNetAddress");
65
    string port = Settings::get("CanNetPort");
66
 
67
    myChannel->setAddress(address, stoi(port));
68
 
976 runge 69
    myChannel->setReconnectTimeout(10);
70
 
969 runge 71
    myChannel->start();
72
 
73
    myChannel->startEvent();
74
 
974 runge 75
    bool waitingForPong = false;
76
 
969 runge 77
    while (1)
78
    {
79
        myChannel->waitForEvent();
80
 
81
        int event = myChannel->getEvent();
82
 
83
        if (event == ASYNCSOCKET_EVENT_DATA)
84
        {
85
            while (myChannel->availableData())
86
            {
974 runge 87
                waitingForPong = false;
88
 
969 runge 89
                string data = myChannel->getData();
90
 
91
                try
92
                {
976 runge 93
                    ///FIXME: Verify that \n is right here, have seen that it does not always work
969 runge 94
                    vector<string> dataLines = explode("\n", data);
95
 
96
                    for (int n = 0; n < dataLines.size(); n++)
97
                    {
981 runge 98
                        data = trim(dataLines[n], '\n');
974 runge 99
 
100
                        if (data == "PONG")
101
                        {
102
                            slog << "Received pong.\n";
103
                            continue;
104
                        }
105
 
969 runge 106
                        CanMessage canMessage;
107
                        canMessage.setRaw(data);
108
 
109
                        //slog << "Received: " << data;
110
 
972 runge 111
                        if (!canMessage.isUnknown())
969 runge 112
                        {
972 runge 113
                            vm.queueCanMessage(canMessage);
981 runge 114
                            canDebug.sendCanMessage(canMessage);
969 runge 115
                        }
981 runge 116
                        else
117
                        {
118
                            canDebug.sendData(data + "\n");
119
                        }
972 runge 120
                        /*else
969 runge 121
                        {
972 runge 122
                            slog << "Received unknown message. Skipping...\n";
123
                        }*/
969 runge 124
                    }
125
                }
126
                catch (CanMessageException* e)
127
                {
128
                    slog << "CanMessageException was caught:\n";
976 runge 129
                    slog << e->getDescription() + "\n";
969 runge 130
                }
131
            }
132
        }
975 runge 133
        else if (event == ASYNCSOCKET_EVENT_CLOSED)
134
        {
135
            vm.queueExpression("setAllOffline();");
136
        }
976 runge 137
        else if (event == ASYNCSOCKET_EVENT_DIED)
138
        {
139
            vm.queueExpression("setAllOffline();");
140
            break;
141
        }
974 runge 142
        else if (event == ASYNCSOCKET_EVENT_INACTIVITY)
143
        {
144
            if (waitingForPong)
145
            {
146
                slog << "We have not received a pong for our ping.\n";
147
                waitingForPong = false;
148
                vm.queueExpression("setAllOffline();");
149
                myChannel->forceReconnect();
150
            }
151
            else
152
            {
976 runge 153
                //slog << "We have not received anything from the canDaemon in some time.\n";
974 runge 154
                waitingForPong = true;
155
                slog << "Sending ping.\n";
156
                myChannel->sendData("PING");
157
            }
158
 
159
        }
969 runge 160
    }
161
 
162
    myChannel->stopEvent();
163
}
164
 
165
void CanNetManager::sendMessage(CanMessage canMessage)
166
{
981 runge 167
    CanDebug &canDebug = CanDebug::getInstance();
168
    canDebug.sendCanMessage(canMessage);
969 runge 169
    myChannel->sendData(canMessage.getRaw());
170
}
171