Subversion Repositories HomeAutomation

Rev

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

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