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) December 27, 2008 by Mattias Runge * |
2 | * Copyright (C) December 27, 2008 by Mattias Runge * |
| 3 | * mattias@runge.se * |
3 | * mattias@runge.se * |
| 4 | * candebug.cpp * |
4 | * candebug.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 "candebug.h" |
22 | #include "candebug.h" |
| 23 | 23 | ||
| 24 | CanDebug* CanDebug::myInstance = NULL; |
24 | CanDebug* CanDebug::myInstance = NULL; |
| 25 | 25 | ||
| 26 | CanDebug& CanDebug::getInstance() |
26 | CanDebug& CanDebug::getInstance() |
| 27 | { |
27 | { |
| 28 | if (myInstance == NULL) |
28 | if (myInstance == NULL) |
| 29 | { |
29 | { |
| 30 | myInstance = new CanDebug(); |
30 | myInstance = new CanDebug(); |
| 31 | } |
31 | } |
| 32 | 32 | ||
| 33 | return *myInstance; |
33 | return *myInstance; |
| 34 | } |
34 | } |
| 35 | 35 | ||
| 36 | void CanDebug::deleteInstance() |
36 | void CanDebug::deleteInstance() |
| 37 | { |
37 | { |
| 38 | if (myInstance != NULL) |
38 | if (myInstance != NULL) |
| 39 | { |
39 | { |
| 40 | delete myInstance; |
40 | delete myInstance; |
| 41 | myInstance = NULL; |
41 | myInstance = NULL; |
| 42 | } |
42 | } |
| 43 | } |
43 | } |
| 44 | 44 | ||
| 45 | CanDebug::CanDebug() |
45 | CanDebug::CanDebug() |
| 46 | { |
46 | { |
| 47 | Thread<CanDebug>(); |
47 | Thread<CanDebug>(); |
| 48 | } |
48 | } |
| 49 | 49 | ||
| 50 | CanDebug::~CanDebug() |
50 | CanDebug::~CanDebug() |
| 51 | { |
51 | { |
| 52 | stop(); |
52 | stop(); |
| 53 | 53 | ||
| 54 | map<int, AsyncSocket*>::iterator iter; |
54 | map<int, AsyncSocket*>::iterator iter; |
| 55 | 55 | ||
| 56 | for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++) |
56 | for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++) |
| 57 | { |
57 | { |
| 58 | if (iter->second->isConnected()) |
58 | if (iter->second->isConnected()) |
| 59 | { |
59 | { |
| 60 | iter->second->stop(); |
60 | iter->second->stop(); |
| 61 | } |
61 | } |
| 62 | 62 | ||
| 63 | delete iter->second; |
63 | delete iter->second; |
| 64 | } |
64 | } |
| 65 | } |
65 | } |
| 66 | 66 | ||
| 67 | void CanDebug::run() |
67 | void CanDebug::run() |
| 68 | { |
68 | { |
| 69 | SyslogStream &slog = SyslogStream::getInstance(); |
69 | SyslogStream &slog = SyslogStream::getInstance(); |
| 70 | 70 | ||
| 71 | string port = Settings::get("CanDebugPort"); |
71 | string port = Settings::get("CanDebugPort"); |
| 72 | 72 | ||
| 73 | try |
73 | try |
| 74 | { |
74 | { |
| 75 | mySocket.setPort(stoi(port)); |
75 | mySocket.setPort(stoi(port)); |
| 76 | 76 | ||
| 77 | mySocket.startListen(); |
77 | mySocket.startListen(); |
| 78 | 78 | ||
| 79 | slog << "CanDebug is listening for connections on port " + port + ".\n"; |
79 | slog << "CanDebug is listening for connections on port " + port + ".\n"; |
| 80 | 80 | ||
| 81 | while (true) |
81 | while (true) |
| 82 | { |
82 | { |
| 83 | AsyncSocket *newSocket = new AsyncSocket(); |
83 | AsyncSocket *newSocket = new AsyncSocket(); |
| 84 | 84 | ||
| 85 | if (mySocket.accept(newSocket)) |
85 | if (mySocket.accept(newSocket)) |
| 86 | { |
86 | { |
| 87 | newSocket-> |
87 | newSocket->sendData("Welcome to Atom " + ftos(VERSION) + " - CanDebug output\n"); |
| 88 | 88 | ||
| 89 | slog << "CanDebug accepted new client on socket " + itos(newSocket->getSocket()) + ".\n"; |
89 | slog << "CanDebug accepted new client on socket " + itos(newSocket->getSocket()) + ".\n"; |
| 90 | myClientSockets[newSocket->getSocket()] = newSocket; |
90 | myClientSockets[newSocket->getSocket()] = newSocket; |
| 91 | - | ||
| 92 | - | ||
| - | 91 | myClientSockets[newSocket->getSocket()]->start(); |
|
| 93 | } |
92 | } |
| 94 | } |
93 | } |
| 95 | } |
94 | } |
| 96 | catch (SocketException* e) |
95 | catch (SocketException* e) |
| 97 | { |
96 | { |
| 98 | slog << "Exception was caught:" + e->getDescription() + ".\n"; |
97 | slog << "Exception was caught:" + e->getDescription() + ".\n"; |
| 99 | } |
98 | } |
| 100 | } |
99 | } |
| 101 | 100 | ||
| 102 | void CanDebug::sendData(string data) |
101 | void CanDebug::sendData(string data) |
| 103 | { |
102 | { |
| 104 | SyslogStream &slog = SyslogStream::getInstance(); |
103 | SyslogStream &slog = SyslogStream::getInstance(); |
| 105 | 104 | ||
| 106 | map<int, AsyncSocket*>::iterator iter; |
105 | map<int, AsyncSocket*>::iterator iter; |
| 107 | 106 | ||
| 108 | for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++) |
107 | for (iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++) |
| 109 | { |
108 | { |
| 110 | if (iter->second->isConnected()) |
109 | if (iter->second->isConnected()) |
| 111 | { |
110 | { |
| 112 | //slog << "CanDebug sent data to client " + itos(iter->second->getSocket()) + ".\n"; |
111 | //slog << "CanDebug: sent data to client " + itos(iter->second->getSocket()) + ".\n"; |
| - | 112 | ||
| - | 113 | try |
|
| - | 114 | { |
|
| 113 | iter->second-> |
115 | iter->second->sendData(data); |
| - | 116 | } |
|
| - | 117 | catch (SocketException* e) |
|
| - | 118 | { |
|
| - | 119 | slog << "CanDebug caught an exception:" + e->getDescription() + ".\n"; |
|
| - | 120 | } |
|
| 114 | } |
121 | } |
| 115 | else |
122 | else |
| 116 | { |
123 | { |
| 117 | slog << "CanDebug client |
124 | slog << "CanDebug had a client disconnect.\n"; |
| 118 | delete iter->second; |
125 | delete iter->second; |
| 119 | myClientSockets.erase(iter); |
126 | myClientSockets.erase(iter); |
| 120 | } |
127 | } |
| 121 | } |
128 | } |
| 122 | } |
129 | } |
| 123 | 130 | ||
| 124 | void CanDebug::sendCanMessage(CanMessage canMessage) |
131 | void CanDebug::sendCanMessage(CanMessage canMessage) |
| 125 | { |
132 | { |
| 126 | if (!canMessage.isHeartbeat() && myClientSockets.size() > 0) |
133 | if (!canMessage.isHeartbeat() && myClientSockets.size() > 0) |
| 127 | { |
134 | { |
| 128 | string debugData = ""; |
135 | string debugData = ""; |
| 129 | 136 | ||
| 130 | if (canMessage.getDirectionFlag() == "To_Owner") |
137 | if (canMessage.getDirectionFlag() == "To_Owner") |
| 131 | { |
138 | { |
| 132 | debugData += "RX"; |
139 | debugData += "RX"; |
| 133 | } |
140 | } |
| 134 | else if (canMessage.getDirectionFlag() == "From_Owner") |
141 | else if (canMessage.getDirectionFlag() == "From_Owner") |
| 135 | { |
142 | { |
| 136 | debugData += "TX"; |
143 | debugData += "TX"; |
| 137 | } |
144 | } |
| 138 | else |
145 | else |
| 139 | { |
146 | { |
| 140 | debugData += "??"; |
147 | debugData += "??"; |
| 141 | } |
148 | } |
| 142 | 149 | ||
| 143 | debugData += " " + canMessage.getClassName(); |
150 | debugData += " " + canMessage.getClassName(); |
| 144 | debugData += "_" + canMessage.getModuleName(); |
151 | debugData += "_" + canMessage.getModuleName(); |
| 145 | debugData += ":" + itos(canMessage.getModuleId()); |
152 | debugData += ":" + itos(canMessage.getModuleId()); |
| 146 | debugData += " CMD=" + canMessage.getCommandName() + " "; |
153 | debugData += " CMD=" + canMessage.getCommandName() + " "; |
| 147 | 154 | ||
| 148 | map<string, CanVariable> vars = canMessage.getData(); |
155 | map<string, CanVariable> vars = canMessage.getData(); |
| 149 | 156 | ||
| 150 | map<string, CanVariable>::iterator iter; |
157 | map<string, CanVariable>::iterator iter; |
| 151 | 158 | ||
| 152 | for (iter = vars.begin(); iter != vars.end(); iter++) |
159 | for (iter = vars.begin(); iter != vars.end(); iter++) |
| 153 | { |
160 | { |
| 154 | debugData += " " + iter->second.getName() + "=" + iter->second.getValue(); |
161 | debugData += " " + iter->second.getName() + "=" + iter->second.getValue(); |
| 155 | } |
162 | } |
| 156 | 163 | ||
| 157 | sendData("[" + niceTime() + "] " + debugData + "\n"); |
164 | sendData("[" + niceTime() + "] " + debugData + "\n"); |
| 158 | } |
165 | } |
| 159 | } |
166 | } |
| 160 | 167 | ||