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