Subversion Repositories HomeAutomation

Rev

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

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