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