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