Rev 987 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 987 | Rev 991 | ||
|---|---|---|---|
| 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 | myChannel->stop(); |
| 56 | delete myChannel; |
56 | delete myChannel; |
| 57 | } |
57 | } |
| 58 | 58 | ||
| 59 | void CanNetManager::openChannel() |
59 | void CanNetManager::openChannel() |
| 60 | { |
60 | { |
| 61 | SyslogStream &slog = SyslogStream::getInstance(); |
61 | SyslogStream &slog = SyslogStream::getInstance(); |
| 62 | VirtualMachine &vm = VirtualMachine::getInstance(); |
62 | VirtualMachine &vm = VirtualMachine::getInstance(); |
| 63 | CanDebug &canDebug = CanDebug::getInstance(); |
63 | CanDebug &canDebug = CanDebug::getInstance(); |
| 64 | 64 | ||
| 65 | string address = Settings::get("CanNetAddress"); |
65 | string address = Settings::get("CanNetAddress"); |
| 66 | if (address == "") |
66 | if (address == "") |
| 67 | { |
67 | { |
| 68 | throw new Atom::Exception("CanNetAddress is not defined in the config file, can not start."); |
68 | throw new Atom::Exception("CanNetAddress is not defined in the config file, can not start."); |
| 69 | } |
69 | } |
| 70 | 70 | ||
| 71 | string port = Settings::get("CanNetPort"); |
71 | string port = Settings::get("CanNetPort"); |
| 72 | if (port == "") |
72 | if (port == "") |
| 73 | { |
73 | { |
| 74 | throw new Atom::Exception("CanNetPort is not defined in the config file, can not start."); |
74 | throw new Atom::Exception("CanNetPort is not defined in the config file, can not start."); |
| 75 | } |
75 | } |
| 76 | 76 | ||
| 77 | myChannel->setAddress(address); |
77 | myChannel->setAddress(address); |
| 78 | myChannel->setPort(stoi(port)); |
78 | myChannel->setPort(stoi(port)); |
| 79 | myChannel->setReconnectTimeout(10); |
79 | myChannel->setReconnectTimeout(10); |
| 80 | 80 | ||
| 81 | myChannel->start(); |
81 | myChannel->start(); |
| 82 | 82 | ||
| 83 | myChannel->eventStartListen(); |
83 | myChannel->eventStartListen(); |
| 84 | 84 | ||
| 85 | bool waitingForPong = false; |
85 | bool waitingForPong = false; |
| 86 | vector<string> dataLines; |
86 | vector<string> dataLines; |
| 87 | string data; |
87 | string data; |
| 88 | CanMessage *canMessage = NULL; |
88 | CanMessage *canMessage = NULL; |
| 89 | 89 | ||
| 90 | while (1) |
90 | while (1) |
| 91 | { |
91 | { |
| 92 | myChannel->eventWait(); |
92 | myChannel->eventWait(); |
| 93 | 93 | ||
| 94 | while (myChannel->eventIsAvailable()) |
94 | while (myChannel->eventIsAvailable()) |
| 95 | { |
95 | { |
| 96 | SocketEvent socketEvent = myChannel->eventFetch(); |
96 | SocketEvent socketEvent = myChannel->eventFetch(); |
| 97 | 97 | ||
| 98 | switch (socketEvent.getType()) |
98 | switch (socketEvent.getType()) |
| 99 | { |
99 | { |
| 100 | case SocketEvent::TYPE_CONNECTED: |
100 | case SocketEvent::TYPE_CONNECTED: |
| 101 | slog << "Connected to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n"; |
101 | slog << "Connected to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n"; |
| 102 | break; |
102 | break; |
| 103 | 103 | ||
| 104 | case SocketEvent::TYPE_CONNECTING: |
104 | case SocketEvent::TYPE_CONNECTING: |
| 105 | slog << "Connecting to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n"; |
105 | slog << "Connecting to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + ".\n"; |
| 106 | break; |
106 | break; |
| 107 | 107 | ||
| 108 | case SocketEvent::TYPE_CONNECTION_CLOSED: |
108 | case SocketEvent::TYPE_CONNECTION_CLOSED: |
| 109 | slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " closed.\n"; |
109 | slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " closed.\n"; |
| 110 | vm.queueExpression("setAllOffline();"); |
110 | vm.queueExpression("setAllOffline();"); |
| 111 | break; |
111 | break; |
| 112 | 112 | ||
| 113 | case SocketEvent::TYPE_CONNECTION_DIED: |
113 | case SocketEvent::TYPE_CONNECTION_DIED: |
| 114 | slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " died :: " + socketEvent.getData() + "\n"; |
114 | slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " died :: " + socketEvent.getData() + "\n"; |
| 115 | vm.queueExpression("setAllOffline();"); |
115 | vm.queueExpression("setAllOffline();"); |
| 116 | break; |
116 | break; |
| 117 | 117 | ||
| 118 | case SocketEvent::TYPE_CONNECTION_RESET: |
118 | case SocketEvent::TYPE_CONNECTION_RESET: |
| 119 | slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " was reset :: " + socketEvent.getData() + "\n"; |
119 | slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " was reset :: " + socketEvent.getData() + "\n"; |
| 120 | vm.queueExpression("setAllOffline();"); |
120 | vm.queueExpression("setAllOffline();"); |
| 121 | break; |
121 | break; |
| 122 | 122 | ||
| 123 | case SocketEvent::TYPE_CONNECTION_FAILED: |
123 | case SocketEvent::TYPE_CONNECTION_FAILED: |
| 124 | slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " failed :: " + socketEvent.getData() + "\n"; |
124 | slog << "Connection to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " failed :: " + socketEvent.getData() + "\n"; |
| 125 | vm.queueExpression("setAllOffline();"); |
125 | vm.queueExpression("setAllOffline();"); |
| 126 | break; |
126 | break; |
| 127 | 127 | ||
| 128 | case SocketEvent::TYPE_DATA: |
128 | case SocketEvent::TYPE_DATA: |
| 129 | waitingForPong = false; |
129 | waitingForPong = false; |
| 130 | try |
130 | try |
| 131 | { |
131 | { |
| 132 | dataLines = explode("\n", socketEvent.getData()); |
132 | dataLines = explode("\n", socketEvent.getData()); |
| 133 | 133 | ||
| 134 | for (int n = 0; n < dataLines.size(); n++) |
134 | for (int n = 0; n < dataLines.size(); n++) |
| 135 | { |
135 | { |
| 136 | data = trim(dataLines[n], '\n'); |
136 | data = trim(dataLines[n], '\n'); |
| 137 | 137 | ||
| 138 | if (data == "PONG") |
138 | if (data == "PONG") |
| 139 | { |
139 | { |
| 140 | slog << "Received pong.\n"; |
140 | slog << "Received pong.\n"; |
| 141 | continue; |
141 | continue; |
| 142 | } |
142 | } |
| 143 | 143 | ||
| 144 | canMessage = new CanMessage(data); |
144 | canMessage = new CanMessage(data); |
| 145 | 145 | ||
| 146 | if (!canMessage->isUnknown()) |
146 | if (!canMessage->isUnknown()) |
| 147 | { |
147 | { |
| 148 | vm.queueCanMessage(*canMessage); |
148 | vm.queueCanMessage(*canMessage); |
| 149 | canDebug.sendCanMessage(*canMessage); |
149 | canDebug.sendCanMessage(*canMessage); |
| 150 | } |
150 | } |
| 151 | else |
151 | else |
| 152 | { |
152 | { |
| 153 | canDebug.sendData(data + "\n"); |
153 | canDebug.sendData(data + "\n"); |
| 154 | } |
154 | } |
| 155 | 155 | ||
| 156 | delete canMessage; |
156 | delete canMessage; |
| 157 | canMessage = NULL; |
157 | canMessage = NULL; |
| 158 | } |
158 | } |
| 159 | } |
159 | } |
| 160 | catch (CanMessageException* e) |
160 | catch (CanMessageException* e) |
| 161 | { |
161 | { |
| 162 | if (canMessage != NULL) |
162 | if (canMessage != NULL) |
| 163 | { |
163 | { |
| 164 | delete canMessage; |
164 | delete canMessage; |
| 165 | } |
165 | } |
| 166 | 166 | ||
| 167 | slog << "CanMessageException was caught:\n"; |
167 | slog << "CanMessageException was caught:\n"; |
| 168 | slog << e->getDescription() + "\n"; |
168 | slog << e->getDescription() + "\n"; |
| 169 | } |
169 | } |
| 170 | break; |
170 | break; |
| 171 | 171 | ||
| 172 | case SocketEvent::TYPE_INACTIVITY: |
172 | case SocketEvent::TYPE_INACTIVITY: |
| 173 | if (waitingForPong) |
173 | if (waitingForPong) |
| 174 | { |
174 | { |
| 175 | slog << "We have not received a pong for our ping.\n"; |
175 | slog << "We have not received a pong for our ping.\n"; |
| 176 | waitingForPong = false; |
176 | waitingForPong = false; |
| 177 | vm.queueExpression("setAllOffline();"); |
177 | vm.queueExpression("setAllOffline();"); |
| 178 | myChannel->forceReconnect(); |
178 | myChannel->forceReconnect(); |
| 179 | } |
179 | } |
| 180 | else |
180 | else |
| 181 | { |
181 | { |
| 182 | //slog << "We have not received anything from the canDaemon in some time.\n"; |
182 | //slog << "We have not received anything from the canDaemon in some time.\n"; |
| 183 | waitingForPong = true; |
183 | waitingForPong = true; |
| 184 | slog << "Sending ping.\n"; |
184 | slog << "Sending ping.\n"; |
| 185 | myChannel->sendData("PING"); |
185 | myChannel->sendData("PING"); |
| 186 | } |
186 | } |
| 187 | break; |
187 | break; |
| 188 | 188 | ||
| 189 | case SocketEvent::TYPE_WAITING_RECONNECT: |
189 | case SocketEvent::TYPE_WAITING_RECONNECT: |
| 190 | slog << "Will try to reconnect to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " in " + itos(myChannel->getReconnectTimeout()) + " seconds.\n"; |
190 | slog << "Will try to reconnect to " + myChannel->getAddress() + ":" + itos(myChannel->getPort()) + " in " + itos(myChannel->getReconnectTimeout()) + " seconds.\n"; |
| 191 | break; |
191 | break; |
| 192 | } |
192 | } |
| 193 | } |
193 | } |
| 194 | } |
194 | } |
| 195 | 195 | ||
| 196 | myChannel->eventStopListen(); |
196 | myChannel->eventStopListen(); |
| 197 | } |
197 | } |
| 198 | 198 | ||
| 199 | void CanNetManager::sendMessage(CanMessage canMessage) |
199 | void CanNetManager::sendMessage(CanMessage canMessage) |
| 200 | { |
200 | { |
| 201 | CanDebug &canDebug = CanDebug::getInstance(); |
201 | CanDebug &canDebug = CanDebug::getInstance(); |
| 202 | canDebug.sendCanMessage(canMessage); |
- | |
| 203 | myChannel->sendData(canMessage.getRaw()); |
202 | myChannel->sendData(canMessage.getRaw()); |
| - | 203 | canDebug.sendCanMessage(canMessage); |
|
| 204 | } |
204 | } |
| 205 | 205 | ||
| 206 | 206 | ||