Rev 986 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 986 | Rev 991 | ||
|---|---|---|---|
| Line 49... | Line 49... | ||
| 49 | 49 | ||
| 50 | CanDebug::~CanDebug() |
50 | CanDebug::~CanDebug() |
| 51 | { |
51 | { |
| 52 | stop(); |
52 | stop(); |
| 53 | 53 | ||
| 54 | map<int, AsyncSocket*>::iterator iter; |
- | |
| 55 | - | ||
| 56 | for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++) |
54 | for (map<int, AsyncSocket*>::iterator iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++) |
| 57 | { |
55 | { |
| 58 | if (iter->second->isConnected()) |
56 | if (iter->second->isConnected()) |
| 59 | { |
57 | { |
| 60 | iter->second->stop(); |
58 | iter->second->stop(); |
| 61 | } |
59 | } |
| 62 | 60 | ||
| 63 | delete iter->second; |
61 | delete iter->second; |
| 64 | } |
62 | } |
| 65 | } |
63 | } |
| 66 | 64 | ||
| 67 | void CanDebug::run() |
65 | void CanDebug::run() |
| 68 | { |
66 | { |
| 69 | SyslogStream &slog = SyslogStream::getInstance(); |
67 | SyslogStream &slog = SyslogStream::getInstance(); |
| 70 | 68 | ||
| 71 | string port = Settings::get("CanDebugPort"); |
69 | string port = Settings::get("CanDebugPort"); |
| 72 | 70 | ||
| 73 | if (port == "") |
71 | if (port == "") |
| 74 | { |
72 | { |
| 75 | slog << "CanDebugPort is not defined in the config file, will not start debug interface\n"; |
73 | slog << "CanDebugPort is not defined in the config file, will not start debug interface\n"; |
| 76 | return; |
74 | return; |
| 77 | } |
75 | } |
| 78 | 76 | ||
| 79 | try |
77 | try |
| 80 | { |
78 | { |
| 81 | mySocket.setPort(stoi(port)); |
79 | mySocket.setPort(stoi(port)); |
| 82 | 80 | ||
| 83 | mySocket.startListen(); |
81 | mySocket.startListen(); |
| 84 | 82 | ||
| 85 | slog << "CanDebug is listening for connections on port " + port + ".\n"; |
83 | slog << "CanDebug is listening for connections on port " + port + ".\n"; |
| 86 | 84 | ||
| 87 | while (true) |
85 | while (true) |
| 88 | { |
86 | { |
| 89 | AsyncSocket *newSocket = new AsyncSocket(); |
87 | AsyncSocket *newSocket = new AsyncSocket(); |
| 90 | 88 | ||
| 91 | if (mySocket.accept(newSocket)) |
89 | if (mySocket.accept(newSocket)) |
| 92 | { |
90 | { |
| 93 | newSocket->sendData("Welcome to Atom " + ftos(VERSION) + " - CanDebug output\n"); |
91 | newSocket->sendData("Welcome to Atom " + ftos(VERSION) + " - CanDebug output\n"); |
| 94 | 92 | ||
| 95 | slog << "CanDebug accepted new client on socket " + itos(newSocket->getSocket()) + ".\n"; |
93 | slog << "CanDebug accepted new client on socket " + itos(newSocket->getSocket()) + ".\n"; |
| 96 | myClientSockets[newSocket->getSocket()] = newSocket; |
94 | myClientSockets[newSocket->getSocket()] = newSocket; |
| 97 | myClientSockets[newSocket->getSocket()]->start(); |
95 | myClientSockets[newSocket->getSocket()]->start(); |
| 98 | } |
96 | } |
| 99 | } |
97 | } |
| 100 | } |
98 | } |
| 101 | catch (SocketException* e) |
99 | catch (SocketException* e) |
| 102 | { |
100 | { |
| 103 | slog << "Exception was caught:" + e->getDescription() + ".\n"; |
101 | slog << "Exception was caught:" + e->getDescription() + ".\n"; |
| 104 | } |
102 | } |
| 105 | } |
103 | } |
| 106 | 104 | ||
| 107 | void CanDebug::sendData(string data) |
105 | void CanDebug::sendData(string data) |
| 108 | { |
106 | { |
| 109 | SyslogStream &slog = SyslogStream::getInstance(); |
107 | SyslogStream &slog = SyslogStream::getInstance(); |
| 110 | 108 | ||
| 111 | map<int, AsyncSocket*>::iterator iter; |
- | |
| 112 | - | ||
| 113 | for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++) |
109 | for (map<int, AsyncSocket*>::iterator iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++) |
| 114 | { |
110 | { |
| 115 | if (iter->second->isConnected()) |
111 | if (iter->second->isConnected()) |
| 116 | { |
112 | { |
| 117 | //slog << "CanDebug: sent data to client " + itos(iter->second->getSocket()) + ".\n"; |
113 | //slog << "CanDebug: sent data to client " + itos(iter->second->getSocket()) + ".\n"; |
| 118 | 114 | ||
| 119 | try |
115 | try |
| 120 | { |
116 | { |
| 121 | iter->second->sendData(data); |
117 | iter->second->sendData(data); |
| 122 | } |
118 | } |
| 123 | catch (SocketException* e) |
119 | catch (SocketException* e) |
| 124 | { |
120 | { |
| 125 | slog << "CanDebug caught an exception:" + e->getDescription() + ".\n"; |
121 | slog << "CanDebug caught an exception:" + e->getDescription() + ".\n"; |
| 126 | } |
122 | } |
| 127 | } |
123 | } |
| 128 | else |
124 | else |
| 129 | { |
125 | { |
| 130 | slog << "CanDebug had a client disconnect.\n"; |
126 | slog << "CanDebug had a client disconnect.\n"; |
| 131 | delete iter->second; |
127 | delete iter->second; |
| 132 | myClientSockets.erase(iter); |
128 | myClientSockets.erase(iter); |
| 133 | } |
129 | } |
| Line 137... | Line 133... | ||
| 137 | void CanDebug::sendCanMessage(CanMessage canMessage) |
133 | void CanDebug::sendCanMessage(CanMessage canMessage) |
| 138 | { |
134 | { |
| 139 | if (myClientSockets.size() > 0) |
135 | if (myClientSockets.size() > 0) |
| 140 | { |
136 | { |
| 141 | string debugData = ""; |
137 | string debugData = ""; |
| 142 | 138 | ||
| 143 | if (canMessage.getClassName() == "nmt") |
139 | if (canMessage.getClassName() == "nmt") |
| 144 | { |
140 | { |
| 145 | debugData += "NMT"; |
141 | debugData += "NMT"; |
| 146 | } |
142 | } |
| 147 | else |
143 | else |
| 148 | { |
144 | { |
| 149 | if (canMessage.getDirectionFlag() == "To_Owner") |
145 | if (canMessage.getDirectionFlag() == "To_Owner") |
| 150 | { |
146 | { |
| 151 | debugData += "RX"; |
147 | debugData += "RX"; |
| 152 | } |
148 | } |
| 153 | else if (canMessage.getDirectionFlag() == "From_Owner") |
149 | else if (canMessage.getDirectionFlag() == "From_Owner") |
| 154 | { |
150 | { |
| 155 | debugData += "TX"; |
151 | debugData += "TX"; |
| 156 | } |
152 | } |
| 157 | else |
153 | else |
| 158 | { |
154 | { |
| 159 | debugData += "??"; |
155 | debugData += "??"; |
| 160 | } |
156 | } |
| 161 | 157 | ||
| 162 | debugData += " " + canMessage.getClassName(); |
158 | debugData += " " + canMessage.getClassName(); |
| 163 | debugData += "_" + canMessage.getModuleName(); |
159 | debugData += "_" + canMessage.getModuleName(); |
| 164 | debugData += ":" + itos(canMessage.getModuleId()); |
160 | debugData += ":" + itos(canMessage.getModuleId()); |
| 165 | } |
161 | } |
| 166 | 162 | ||
| 167 | debugData += " CMD=" + canMessage.getCommandName() + " "; |
163 | debugData += " CMD=" + canMessage.getCommandName() + " "; |
| 168 | 164 | ||
| 169 | map<string, CanVariable> vars = canMessage.getData(); |
165 | map<string, CanVariable> vars = canMessage.getData(); |
| - | 166 | map<int, string> sortMap; |
|
| 170 | 167 | ||
| 171 | map<string, |
168 | for (map<string, CanVariable>::iterator iter = vars.begin(); iter != vars.end(); iter++) |
| - | 169 | { |
|
| - | 170 | sortMap[iter->second.getStartBit()] = " " + iter->second.getName() + "=" + iter->second.getValue(); |
|
| - | 171 | } |
|
| 172 | 172 | ||
| 173 | for (iter = |
173 | for (map<int, string>::iterator iter = sortMap.begin(); iter != sortMap.end(); iter++) |
| 174 | { |
174 | { |
| 175 | debugData += |
175 | debugData += iter->second; |
| 176 | } |
176 | } |
| 177 | 177 | ||
| 178 | sendData("[" + niceTime() + "] " + debugData + "\n"); |
178 | sendData("[" + niceTime() + "] " + debugData + "\n"); |
| 179 | } |
179 | } |
| 180 | } |
180 | } |