Subversion Repositories HomeAutomation

Rev

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

Rev 974 Rev 975
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
 
67
 
68
    myChannel->start();
68
    myChannel->start();
69
 
69
 
70
    myChannel->startEvent();
70
    myChannel->startEvent();
71
 
71
 
72
    bool waitingForPong = false;
72
    bool waitingForPong = false;
73
 
73
 
74
    while (1)
74
    while (1)
75
    {
75
    {
76
        myChannel->waitForEvent();
76
        myChannel->waitForEvent();
77
 
77
 
78
        int event = myChannel->getEvent();
78
        int event = myChannel->getEvent();
79
 
79
 
80
        if (event == ASYNCSOCKET_EVENT_DATA)
80
        if (event == ASYNCSOCKET_EVENT_DATA)
81
        {
81
        {
82
            while (myChannel->availableData())
82
            while (myChannel->availableData())
83
            {
83
            {
84
                waitingForPong = false;
84
                waitingForPong = false;
85
 
85
 
86
                string data = myChannel->getData();
86
                string data = myChannel->getData();
87
 
87
 
88
                try
88
                try
89
                {
89
                {
90
                    vector<string> dataLines = explode("\n", data);
90
                    vector<string> dataLines = explode("\n", data);
91
 
91
 
92
                    for (int n = 0; n < dataLines.size(); n++)
92
                    for (int n = 0; n < dataLines.size(); n++)
93
                    {
93
                    {
94
                        data = trim(data, '\n');
94
                        data = trim(data, '\n');
95
 
95
 
96
                        if (data == "PONG")
96
                        if (data == "PONG")
97
                        {
97
                        {
98
                            slog << "Received pong.\n";
98
                            slog << "Received pong.\n";
99
                            continue;
99
                            continue;
100
                        }
100
                        }
101
 
101
 
102
                        CanMessage canMessage;
102
                        CanMessage canMessage;
103
                        canMessage.setRaw(data);
103
                        canMessage.setRaw(data);
104
 
104
 
105
                        //slog << "Received: " << data;
105
                        //slog << "Received: " << data;
106
 
106
 
107
                        if (!canMessage.isUnknown())
107
                        if (!canMessage.isUnknown())
108
                        {
108
                        {
109
                            vm.queueCanMessage(canMessage);
109
                            vm.queueCanMessage(canMessage);
110
                        }
110
                        }
111
                        /*else
111
                        /*else
112
                        {
112
                        {
113
                            slog << "Received unknown message. Skipping...\n";
113
                            slog << "Received unknown message. Skipping...\n";
114
                        }*/
114
                        }*/
115
                    }
115
                    }
116
                }
116
                }
117
                catch (CanMessageException* e)
117
                catch (CanMessageException* e)
118
                {
118
                {
119
                    slog << "CanMessageException was caught:\n";
119
                    slog << "CanMessageException was caught:\n";
120
                    slog << e->getDescription() << "\n";
120
                    slog << e->getDescription() << "\n";
121
                }
121
                }
122
            }
122
            }
-
 
123
        }
-
 
124
        else if (event == ASYNCSOCKET_EVENT_CLOSED)
-
 
125
        {
-
 
126
            vm.queueExpression("setAllOffline();");
123
        }
127
        }
124
        else if (event == ASYNCSOCKET_EVENT_INACTIVITY)
128
        else if (event == ASYNCSOCKET_EVENT_INACTIVITY)
125
        {
129
        {
126
            if (waitingForPong)
130
            if (waitingForPong)
127
            {
131
            {
128
                slog << "We have not received a pong for our ping.\n";
132
                slog << "We have not received a pong for our ping.\n";
129
                waitingForPong = false;
133
                waitingForPong = false;
130
                vm.queueExpression("setAllOffline();");
134
                vm.queueExpression("setAllOffline();");
131
                myChannel->forceReconnect();
135
                myChannel->forceReconnect();
132
            }
136
            }
133
            else
137
            else
134
            {
138
            {
135
                slog << "We have not received anything from the canDaemon in some time.\n";
139
                slog << "We have not received anything from the canDaemon in some time.\n";
136
                waitingForPong = true;
140
                waitingForPong = true;
137
                slog << "Sending ping.\n";
141
                slog << "Sending ping.\n";
138
                myChannel->sendData("PING");
142
                myChannel->sendData("PING");
139
            }
143
            }
140
 
144
 
141
        }
145
        }
142
    }
146
    }
143
 
147
 
144
    myChannel->stopEvent();
148
    myChannel->stopEvent();
145
}
149
}
146
 
150
 
147
void CanNetManager::sendMessage(CanMessage canMessage)
151
void CanNetManager::sendMessage(CanMessage canMessage)
148
{
152
{
149
    myChannel->sendData(canMessage.getRaw());
153
    myChannel->sendData(canMessage.getRaw());
150
}
154
}
151
 
155
 
152
 
156