Rev 1607 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1607 | Rev 1614 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * |
2 | * |
| 3 | * Copyright (C) 2010 Mattias Runge |
3 | * Copyright (C) 2010 Mattias Runge |
| 4 | * |
4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
8 | * (at your option) any later version. |
| 9 | * |
9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
13 | * GNU General Public License for more details. |
| 14 | * |
14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
18 | * |
| 19 | */ |
19 | */ |
| 20 | 20 | ||
| 21 | #include "Manager.h" |
21 | #include "Manager.h" |
| 22 | 22 | ||
| 23 | #include <boost/lexical_cast.hpp> |
23 | #include <boost/lexical_cast.hpp> |
| 24 | 24 | ||
| 25 | #include "broker/Manager.h" |
25 | #include "broker/Manager.h" |
| 26 | #include "broker/Message.h" |
26 | #include "broker/Message.h" |
| 27 | #include "type/common.h" |
27 | #include "type/common.h" |
| 28 | #include "timer/Manager.h" |
28 | #include "timer/Manager.h" |
| 29 | 29 | ||
| 30 | #include "Message.h" |
30 | #include "Message.h" |
| 31 | 31 | ||
| 32 | namespace atom { |
32 | namespace atom { |
| 33 | namespace can { |
33 | namespace can { |
| 34 | 34 | ||
| 35 | Manager::Pointer Manager::instance_; |
35 | Manager::Pointer Manager::instance_; |
| 36 | 36 | ||
| 37 | Manager::Manager() : broker::Subscriber(false), LOG("can::Manager") |
37 | Manager::Manager() : broker::Subscriber(false), LOG("can::Manager") |
| 38 | { |
38 | { |
| 39 | this->timer_id_ = timer::Manager::Instance()->Set(10000, true); |
39 | this->timer_id_ = timer::Manager::Instance()->Set(10000, true); |
| 40 | } |
40 | } |
| 41 | 41 | ||
| 42 | Manager::~Manager() |
42 | Manager::~Manager() |
| 43 | { |
43 | { |
| 44 | } |
44 | } |
| 45 | 45 | ||
| 46 | Manager::Pointer Manager::Instance() |
46 | Manager::Pointer Manager::Instance() |
| 47 | { |
47 | { |
| 48 | return Manager::instance_; |
48 | return Manager::instance_; |
| 49 | } |
49 | } |
| 50 | 50 | ||
| 51 | void Manager::Create() |
51 | void Manager::Create() |
| 52 | { |
52 | { |
| 53 | Manager::instance_ = Manager::Pointer(new Manager()); |
53 | Manager::instance_ = Manager::Pointer(new Manager()); |
| 54 | } |
54 | } |
| 55 | 55 | ||
| 56 | void Manager::Delete() |
56 | void Manager::Delete() |
| 57 | { |
57 | { |
| 58 | Manager::instance_.reset(); |
58 | Manager::instance_.reset(); |
| 59 | } |
59 | } |
| 60 | 60 | ||
| 61 | void Manager::ConnectSlots(const SignalOnModuleChange::slot_type& slot_on_module_change, const SignalOnModuleMessage::slot_type& slot_on_module_message) |
61 | void Manager::ConnectSlots(const SignalOnNodeChange::slot_type& signal_on_node_change_, const SignalOnModuleChange::slot_type& slot_on_module_change, const SignalOnModuleMessage::slot_type& slot_on_module_message) |
| 62 | { |
62 | { |
| - | 63 | this->signal_on_node_change_.connect(signal_on_node_change_); |
|
| 63 | this->signal_on_module_change_.connect(slot_on_module_change); |
64 | this->signal_on_module_change_.connect(slot_on_module_change); |
| 64 | this->signal_on_module_message_.connect(slot_on_module_message); |
65 | this->signal_on_module_message_.connect(slot_on_module_message); |
| 65 | } |
66 | } |
| 66 | 67 | ||
| 67 | void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat) |
68 | void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat) |
| 68 | { |
69 | { |
| 69 | if (timer_id != this->timer_id_) |
70 | if (timer_id != this->timer_id_) |
| 70 | { |
71 | { |
| 71 | return; |
72 | return; |
| 72 | } |
73 | } |
| 73 | 74 | ||
| 74 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
75 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
| 75 | { |
76 | { |
| 76 | if (!it->second->CheckTimeout()) |
77 | if (!it->second->CheckTimeout()) |
| 77 | { |
78 | { |
| 78 | LOG.Info("Node " + type::ToHex(it->second->GetId()) + " has not sent anything in a long time, setting offline."); |
79 | LOG.Info("Node " + type::ToHex(it->second->GetId()) + " has not sent anything in a long time, setting offline."); |
| 79 | this->RemoveModules(it->second->GetId()); |
80 | this->RemoveModules(it->second->GetId()); |
| 80 | } |
81 | } |
| 81 | } |
82 | } |
| 82 | } |
83 | } |
| 83 | 84 | ||
| 84 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
85 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
| 85 | { |
86 | { |
| 86 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
87 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
| 87 | { |
88 | { |
| 88 | //LOG.Debug("Message received"); |
89 | //LOG.Debug("Message received"); |
| 89 | Message* payload = static_cast<Message*>(message->GetPayload().get()); |
90 | Message* payload = static_cast<Message*>(message->GetPayload().get()); |
| 90 | Node::Pointer node; |
91 | Node::Pointer node; |
| 91 | 92 | ||
| 92 | if (payload->GetClassName() == "nmt") |
93 | if (payload->GetClassName() == "nmt") |
| 93 | { |
94 | { |
| 94 | if (payload->GetCommandName() == "Bios_Start") |
95 | if (payload->GetCommandName() == "Bios_Start") |
| 95 | { |
96 | { |
| 96 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
97 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 97 | node->ResetTimeout(); |
98 | node->ResetTimeout(); |
| 98 | 99 | ||
| 99 | this->RemoveModules(node->GetId()); |
100 | this->RemoveModules(node->GetId()); |
| 100 | node->SetState(Node::STATE_ONLINE); |
101 | node->SetState(Node::STATE_ONLINE); |
| 101 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went online."); |
102 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went online."); |
| 102 | } |
103 | } |
| 103 | else if (payload->GetCommandName() == "Reset") |
104 | else if (payload->GetCommandName() == "Reset") |
| 104 | { |
105 | { |
| 105 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
106 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 106 | 107 | ||
| 107 | this->RemoveModules(node->GetId()); |
108 | this->RemoveModules(node->GetId()); |
| 108 | node->SetState(Node::STATE_OFFLINE); |
109 | node->SetState(Node::STATE_OFFLINE); |
| 109 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline."); |
110 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline."); |
| 110 | } |
111 | } |
| 111 | else if (payload->GetCommandName() == "App_Start") |
112 | else if (payload->GetCommandName() == "App_Start") |
| 112 | { |
113 | { |
| 113 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
114 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 114 | node->ResetTimeout(); |
115 | node->ResetTimeout(); |
| 115 | 116 | ||
| 116 | this->RequestModules(node->GetId()); |
117 | this->RequestModules(node->GetId()); |
| 117 | node->SetState(Node::STATE_PENDING); |
118 | node->SetState(Node::STATE_PENDING); |
| 118 | LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules..."); |
119 | LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules..."); |
| 119 | } |
120 | } |
| 120 | else if (payload->GetCommandName() == "Heartbeat") |
121 | else if (payload->GetCommandName() == "Heartbeat") |
| 121 | { |
122 | { |
| 122 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
123 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 123 | node->ResetTimeout(); |
124 | node->ResetTimeout(); |
| 124 | 125 | ||
| 125 | if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE) |
126 | if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE) |
| 126 | { |
127 | { |
| 127 | this->RequestModules(node->GetId()); |
128 | this->RequestModules(node->GetId()); |
| 128 | node->SetState(Node::STATE_PENDING); |
129 | node->SetState(Node::STATE_PENDING); |
| 129 | LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules..."); |
130 | LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules..."); |
| 130 | } |
131 | } |
| 131 | else if (node->GetState() == Node::STATE_INITIALIZED) |
132 | else if (node->GetState() == Node::STATE_INITIALIZED) |
| 132 | { |
133 | { |
| 133 | if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
134 | if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
| 134 | { |
135 | { |
| 135 | this->RequestModules(node->GetId()); |
136 | this->RequestModules(node->GetId()); |
| 136 | node->SetState(Node::STATE_PENDING); |
137 | node->SetState(Node::STATE_PENDING); |
| 137 | LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules..."); |
138 | LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules..."); |
| 138 | } |
139 | } |
| 139 | } |
140 | } |
| 140 | } |
141 | } |
| 141 | } |
142 | } |
| 142 | else if (payload->GetDirectionName() == "From_Owner") |
143 | else if (payload->GetDirectionName() == "From_Owner") |
| 143 | { |
144 | { |
| 144 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
145 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
| 145 | 146 | ||
| 146 | if (payload->GetCommandName() == "List") |
147 | if (payload->GetCommandName() == "List") |
| 147 | { |
148 | { |
| 148 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
149 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 149 | node->ResetTimeout(); |
150 | node->ResetTimeout(); |
| 150 | 151 | ||
| 151 | 152 | ||
| 152 | module->SetNodeId(node->GetId()); |
153 | module->SetNodeId(node->GetId()); |
| 153 | 154 | ||
| 154 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
155 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
| 155 | { |
156 | { |
| 156 | node->SetState(Node::STATE_INITIALIZED); |
157 | node->SetState(Node::STATE_INITIALIZED); |
| 157 | } |
158 | } |
| 158 | 159 | ||
| 159 | LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + "."); |
160 | LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + "."); |
| 160 | this->signal_on_module_change_(module->GetFullId(), true); |
161 | this->signal_on_module_change_(module->GetFullId(), true); |
| 161 | } |
162 | } |
| 162 | else |
163 | else |
| 163 | { |
164 | { |
| 164 | this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables()); |
165 | this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables()); |
| 165 | } |
166 | } |
| 166 | } |
167 | } |
| 167 | } |
168 | } |
| 168 | } |
169 | } |
| 169 | 170 | ||
| 170 | void Manager::SendMessageHandler(std::string full_id, std::string command, type::StringMap variables) |
171 | void Manager::SendMessageHandler(std::string full_id, std::string command, type::StringMap variables) |
| 171 | { |
172 | { |
| 172 | ModuleList::iterator it = this->modules_.find(full_id); |
173 | ModuleList::iterator it = this->modules_.find(full_id); |
| 173 | 174 | ||
| 174 | if (it == this->modules_.end()) |
175 | if (it == this->modules_.end()) |
| 175 | { |
176 | { |
| 176 | LOG.Warning("Can not send message to " + full_id + ", it is not available"); |
177 | LOG.Warning("Can not send message to " + full_id + ", it is not available"); |
| 177 | return; |
178 | return; |
| 178 | } |
179 | } |
| 179 | 180 | ||
| 180 | try |
181 | try |
| 181 | { |
182 | { |
| 182 | Message* payload = new Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command); |
183 | Message* payload = new Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command); |
| 183 | payload->SetVariables(variables); |
184 | payload->SetVariables(variables); |
| 184 | 185 | ||
| 185 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
186 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
| 186 | //LOG.Debug("Command " + command + " sent to " + it->second->GetName()); |
187 | //LOG.Debug("Command " + command + " sent to " + it->second->GetName()); |
| 187 | } |
188 | } |
| 188 | catch (std::runtime_error& e) |
189 | catch (std::runtime_error& e) |
| 189 | { |
190 | { |
| 190 | LOG.Error("Failed to send message, " + std::string(e.what())); |
191 | LOG.Error("Failed to send message, " + std::string(e.what())); |
| 191 | } |
192 | } |
| 192 | } |
193 | } |
| 193 | 194 | ||
| 194 | void Manager::SendMessage(std::string full_id, std::string command, type::StringMap variables) |
195 | void Manager::SendMessage(std::string full_id, std::string command, type::StringMap variables) |
| 195 | { |
196 | { |
| 196 | this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables)); |
197 | this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables)); |
| 197 | } |
198 | } |
| 198 | 199 | ||
| 199 | bool Manager::IsModuleAvailable(std::string full_id) |
200 | bool Manager::IsModuleAvailable(std::string full_id) |
| 200 | { |
201 | { |
| 201 | return this->modules_.find(full_id) != this->modules_.end(); |
202 | return this->modules_.find(full_id) != this->modules_.end(); |
| 202 | } |
203 | } |
| 203 | 204 | ||
| 204 | Node::Pointer Manager::GetNode(Node::Id node_id) |
205 | Node::Pointer Manager::GetNode(Node::Id node_id) |
| 205 | { |
206 | { |
| 206 | Node::Pointer node; |
207 | Node::Pointer node; |
| 207 | NodeList::iterator it = this->nodes_.find(node_id); |
208 | NodeList::iterator it = this->nodes_.find(node_id); |
| 208 | 209 | ||
| 209 | if (it == this->nodes_.end()) |
210 | if (it == this->nodes_.end()) |
| 210 | { |
211 | { |
| 211 | node = Node::Pointer(new Node(node_id)); |
212 | node = Node::Pointer(new Node(node_id)); |
| 212 | this->nodes_[node_id] = node; |
213 | this->nodes_[node_id] = node; |
| 213 | } |
214 | } |
| 214 | else |
215 | else |
| 215 | { |
216 | { |
| 216 | node = it->second; |
217 | node = it->second; |
| 217 | } |
218 | } |
| 218 | 219 | ||
| 219 | return node; |
220 | return node; |
| 220 | } |
221 | } |
| 221 | 222 | ||
| 222 | Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name) |
223 | Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name) |
| 223 | { |
224 | { |
| 224 | Module::Pointer module; |
225 | Module::Pointer module; |
| 225 | ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name)); |
226 | ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name)); |
| 226 | 227 | ||
| 227 | if (it == this->modules_.end()) |
228 | if (it == this->modules_.end()) |
| 228 | { |
229 | { |
| 229 | module = Module::Pointer(new Module(module_id, module_name, class_name)); |
230 | module = Module::Pointer(new Module(module_id, module_name, class_name)); |
| 230 | this->modules_[module->GetFullId()] = module; |
231 | this->modules_[module->GetFullId()] = module; |
| 231 | } |
232 | } |
| 232 | else |
233 | else |
| 233 | { |
234 | { |
| 234 | module = it->second; |
235 | module = it->second; |
| 235 | } |
236 | } |
| 236 | 237 | ||
| 237 | return module; |
238 | return module; |
| 238 | } |
239 | } |
| 239 | 240 | ||
| 240 | void Manager::RemoveModules(Node::Id node_id) |
241 | void Manager::RemoveModules(Node::Id node_id) |
| 241 | { |
242 | { |
| 242 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
243 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
| 243 | { |
244 | { |
| 244 | if (it->second->GetNodeId() == node_id) |
245 | if (it->second->GetNodeId() == node_id) |
| 245 | { |
246 | { |
| 246 | LOG.Info("Module " + it->second->GetFullId() + " is unavailable."); |
247 | LOG.Info("Module " + it->second->GetFullId() + " is unavailable."); |
| 247 | this->signal_on_module_change_(it->second->GetFullId(), false); |
248 | this->signal_on_module_change_(it->second->GetFullId(), false); |
| 248 | 249 | ||
| 249 | this->modules_.erase(it); |
250 | this->modules_.erase(it); |
| 250 | } |
251 | } |
| 251 | } |
252 | } |
| 252 | } |
253 | } |
| 253 | 254 | ||
| 254 | void Manager::RequestModules(Node::Id node_id) |
255 | void Manager::RequestModules(Node::Id node_id) |
| 255 | { |
256 | { |
| 256 | this->RemoveModules(node_id); |
257 | this->RemoveModules(node_id); |
| 257 | 258 | ||
| 258 | Message* payload = new Message("mnmt", "To_Owner", "", 0, "List"); |
259 | Message* payload = new Message("mnmt", "To_Owner", "", 0, "List"); |
| 259 | payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id)); |
260 | payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id)); |
| 260 | 261 | ||
| 261 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
262 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
| 262 | } |
263 | } |
| 263 | 264 | ||
| 264 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
265 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
| 265 | { |
266 | { |
| 266 | unsigned int number_of_modules = 0; |
267 | unsigned int number_of_modules = 0; |
| 267 | 268 | ||
| 268 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
269 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
| 269 | { |
270 | { |
| 270 | if (it->second->GetNodeId() == node_id) |
271 | if (it->second->GetNodeId() == node_id) |
| 271 | { |
272 | { |
| 272 | number_of_modules++; |
273 | number_of_modules++; |
| 273 | } |
274 | } |
| 274 | } |
275 | } |
| 275 | 276 | ||
| 276 | return number_of_modules; |
277 | return number_of_modules; |
| 277 | } |
278 | } |
| 278 | 279 | ||
| 279 | 280 | ||
| 280 | }; // namespace can |
281 | }; // namespace can |
| 281 | }; // namespace atom |
282 | }; // namespace atom |
| 282 | 283 | ||