Rev 1604 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1604 | Rev 1607 | ||
|---|---|---|---|
| 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 SignalOnModuleChange::slot_type& slot_on_module_change, const SignalOnModuleMessage::slot_type& slot_on_module_message) |
| 62 | { |
62 | { |
| 63 | this->signal_on_module_change_.connect(slot_on_module_change); |
63 | this->signal_on_module_change_.connect(slot_on_module_change); |
| 64 | this->signal_on_module_message_.connect(slot_on_module_message); |
64 | this->signal_on_module_message_.connect(slot_on_module_message); |
| 65 | } |
65 | } |
| 66 | 66 | ||
| 67 | void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat) |
67 | void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat) |
| 68 | { |
68 | { |
| 69 | if (timer_id != this->timer_id_) |
69 | if (timer_id != this->timer_id_) |
| 70 | { |
70 | { |
| 71 | return; |
71 | return; |
| 72 | } |
72 | } |
| 73 | 73 | ||
| 74 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
74 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
| 75 | { |
75 | { |
| 76 | if (!it->second->CheckTimeout()) |
76 | if (!it->second->CheckTimeout()) |
| 77 | { |
77 | { |
| 78 | LOG.Info("Node " + type::ToHex(it->second->GetId()) + " has not sent anything in a long time, setting offline."); |
78 | LOG.Info("Node " + type::ToHex(it->second->GetId()) + " has not sent anything in a long time, setting offline."); |
| 79 | this->RemoveModules(it->second->GetId()); |
79 | this->RemoveModules(it->second->GetId()); |
| 80 | } |
80 | } |
| 81 | } |
81 | } |
| 82 | } |
82 | } |
| 83 | 83 | ||
| 84 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
84 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
| 85 | { |
85 | { |
| 86 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
86 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
| 87 | { |
87 | { |
| 88 | //LOG.Debug("Message received"); |
88 | //LOG.Debug("Message received"); |
| 89 | Message* payload = static_cast<Message*>(message->GetPayload().get()); |
89 | Message* payload = static_cast<Message*>(message->GetPayload().get()); |
| 90 | Node::Pointer node; |
90 | Node::Pointer node; |
| 91 | 91 | ||
| 92 | if (payload->GetClassName() == "nmt") |
92 | if (payload->GetClassName() == "nmt") |
| 93 | { |
93 | { |
| 94 | if (payload->GetCommandName() == "Bios_Start") |
94 | if (payload->GetCommandName() == "Bios_Start") |
| 95 | { |
95 | { |
| 96 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
96 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 97 | node->ResetTimeout(); |
97 | node->ResetTimeout(); |
| 98 | 98 | ||
| 99 | this->RemoveModules(node->GetId()); |
99 | this->RemoveModules(node->GetId()); |
| 100 | node->SetState(Node::STATE_ONLINE); |
100 | node->SetState(Node::STATE_ONLINE); |
| 101 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went online."); |
101 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went online."); |
| 102 | } |
102 | } |
| 103 | else if (payload->GetCommandName() == "Reset") |
103 | else if (payload->GetCommandName() == "Reset") |
| 104 | { |
104 | { |
| 105 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
105 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 106 | 106 | ||
| 107 | this->RemoveModules(node->GetId()); |
107 | this->RemoveModules(node->GetId()); |
| 108 | node->SetState(Node::STATE_OFFLINE); |
108 | node->SetState(Node::STATE_OFFLINE); |
| 109 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline."); |
109 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline."); |
| 110 | } |
110 | } |
| 111 | else if (payload->GetCommandName() == "App_Start") |
111 | else if (payload->GetCommandName() == "App_Start") |
| 112 | { |
112 | { |
| 113 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
113 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 114 | node->ResetTimeout(); |
114 | node->ResetTimeout(); |
| 115 | 115 | ||
| 116 | this->RequestModules(node->GetId()); |
116 | this->RequestModules(node->GetId()); |
| 117 | node->SetState(Node::STATE_PENDING); |
117 | node->SetState(Node::STATE_PENDING); |
| 118 | LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules..."); |
118 | LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules..."); |
| 119 | } |
119 | } |
| 120 | else if (payload->GetCommandName() == "Heartbeat") |
120 | else if (payload->GetCommandName() == "Heartbeat") |
| 121 | { |
121 | { |
| 122 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
122 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 123 | node->ResetTimeout(); |
123 | node->ResetTimeout(); |
| 124 | 124 | ||
| 125 | if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE) |
125 | if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE) |
| 126 | { |
126 | { |
| 127 | this->RequestModules(node->GetId()); |
127 | this->RequestModules(node->GetId()); |
| 128 | node->SetState(Node::STATE_PENDING); |
128 | node->SetState(Node::STATE_PENDING); |
| 129 | LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules..."); |
129 | LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules..."); |
| 130 | } |
130 | } |
| 131 | else if (node->GetState() == Node::STATE_INITIALIZED) |
131 | else if (node->GetState() == Node::STATE_INITIALIZED) |
| 132 | { |
132 | { |
| 133 | if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
133 | if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
| 134 | { |
134 | { |
| 135 | this->RequestModules(node->GetId()); |
135 | this->RequestModules(node->GetId()); |
| 136 | node->SetState(Node::STATE_PENDING); |
136 | node->SetState(Node::STATE_PENDING); |
| 137 | LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules..."); |
137 | LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules..."); |
| 138 | } |
138 | } |
| 139 | } |
139 | } |
| 140 | } |
140 | } |
| 141 | } |
141 | } |
| 142 | else if (payload->GetDirectionName() == "From_Owner") |
142 | else if (payload->GetDirectionName() == "From_Owner") |
| 143 | { |
143 | { |
| 144 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
144 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
| 145 | 145 | ||
| 146 | if (payload->GetCommandName() == "List") |
146 | if (payload->GetCommandName() == "List") |
| 147 | { |
147 | { |
| 148 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
148 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 149 | node->ResetTimeout(); |
149 | node->ResetTimeout(); |
| 150 | 150 | ||
| 151 | 151 | ||
| 152 | module->SetNodeId(node->GetId()); |
152 | module->SetNodeId(node->GetId()); |
| 153 | 153 | ||
| 154 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
154 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
| 155 | { |
155 | { |
| 156 | node->SetState(Node::STATE_INITIALIZED); |
156 | node->SetState(Node::STATE_INITIALIZED); |
| 157 | } |
157 | } |
| 158 | 158 | ||
| 159 | LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + "."); |
159 | LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + "."); |
| 160 | this->signal_on_module_change_(module->GetFullId(), true); |
160 | this->signal_on_module_change_(module->GetFullId(), true); |
| 161 | } |
161 | } |
| 162 | else |
162 | else |
| 163 | { |
163 | { |
| 164 | this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables()); |
164 | this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables()); |
| 165 | } |
165 | } |
| 166 | } |
166 | } |
| 167 | } |
167 | } |
| 168 | } |
168 | } |
| 169 | 169 | ||
| 170 | void Manager::SendMessageHandler(std::string full_id, std::string command, type::StringMap variables) |
170 | void Manager::SendMessageHandler(std::string full_id, std::string command, type::StringMap variables) |
| 171 | { |
171 | { |
| 172 | ModuleList::iterator it = this->modules_.find(full_id); |
172 | ModuleList::iterator it = this->modules_.find(full_id); |
| 173 | 173 | ||
| 174 | if (it == this->modules_.end()) |
174 | if (it == this->modules_.end()) |
| 175 | { |
175 | { |
| 176 | LOG.Warning("Can not send message to " + full_id + ", it is not available"); |
176 | LOG.Warning("Can not send message to " + full_id + ", it is not available"); |
| 177 | return; |
177 | return; |
| 178 | } |
178 | } |
| 179 | 179 | ||
| 180 | try |
180 | try |
| 181 | { |
181 | { |
| 182 | Message* payload = new Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command); |
182 | Message* payload = new Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command); |
| 183 | payload->SetVariables(variables); |
183 | payload->SetVariables(variables); |
| 184 | 184 | ||
| 185 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
185 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
| 186 | LOG. |
186 | //LOG.Debug("Command " + command + " sent to " + it->second->GetName()); |
| 187 | } |
187 | } |
| 188 | catch (std::runtime_error& e) |
188 | catch (std::runtime_error& e) |
| 189 | { |
189 | { |
| 190 | LOG.Error("Failed to send message, " + std::string(e.what())); |
190 | LOG.Error("Failed to send message, " + std::string(e.what())); |
| 191 | } |
191 | } |
| 192 | } |
192 | } |
| 193 | 193 | ||
| 194 | void Manager::SendMessage(std::string full_id, std::string command, type::StringMap variables) |
194 | void Manager::SendMessage(std::string full_id, std::string command, type::StringMap variables) |
| 195 | { |
195 | { |
| 196 | this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables)); |
196 | this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables)); |
| 197 | } |
197 | } |
| 198 | 198 | ||
| 199 | bool Manager::IsModuleAvailable(std::string full_id) |
199 | bool Manager::IsModuleAvailable(std::string full_id) |
| 200 | { |
200 | { |
| 201 | return this->modules_.find(full_id) != this->modules_.end(); |
201 | return this->modules_.find(full_id) != this->modules_.end(); |
| 202 | } |
202 | } |
| 203 | 203 | ||
| 204 | Node::Pointer Manager::GetNode(Node::Id node_id) |
204 | Node::Pointer Manager::GetNode(Node::Id node_id) |
| 205 | { |
205 | { |
| 206 | Node::Pointer node; |
206 | Node::Pointer node; |
| 207 | NodeList::iterator it = this->nodes_.find(node_id); |
207 | NodeList::iterator it = this->nodes_.find(node_id); |
| 208 | 208 | ||
| 209 | if (it == this->nodes_.end()) |
209 | if (it == this->nodes_.end()) |
| 210 | { |
210 | { |
| 211 | node = Node::Pointer(new Node(node_id)); |
211 | node = Node::Pointer(new Node(node_id)); |
| 212 | this->nodes_[node_id] = node; |
212 | this->nodes_[node_id] = node; |
| 213 | } |
213 | } |
| 214 | else |
214 | else |
| 215 | { |
215 | { |
| 216 | node = it->second; |
216 | node = it->second; |
| 217 | } |
217 | } |
| 218 | 218 | ||
| 219 | return node; |
219 | return node; |
| 220 | } |
220 | } |
| 221 | 221 | ||
| 222 | Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name) |
222 | Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name) |
| 223 | { |
223 | { |
| 224 | Module::Pointer module; |
224 | Module::Pointer module; |
| 225 | ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name)); |
225 | ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name)); |
| 226 | 226 | ||
| 227 | if (it == this->modules_.end()) |
227 | if (it == this->modules_.end()) |
| 228 | { |
228 | { |
| 229 | module = Module::Pointer(new Module(module_id, module_name, class_name)); |
229 | module = Module::Pointer(new Module(module_id, module_name, class_name)); |
| 230 | this->modules_[module->GetFullId()] = module; |
230 | this->modules_[module->GetFullId()] = module; |
| 231 | } |
231 | } |
| 232 | else |
232 | else |
| 233 | { |
233 | { |
| 234 | module = it->second; |
234 | module = it->second; |
| 235 | } |
235 | } |
| 236 | 236 | ||
| 237 | return module; |
237 | return module; |
| 238 | } |
238 | } |
| 239 | 239 | ||
| 240 | void Manager::RemoveModules(Node::Id node_id) |
240 | void Manager::RemoveModules(Node::Id node_id) |
| 241 | { |
241 | { |
| 242 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
242 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
| 243 | { |
243 | { |
| 244 | if (it->second->GetNodeId() == node_id) |
244 | if (it->second->GetNodeId() == node_id) |
| 245 | { |
245 | { |
| 246 | LOG.Info("Module " + it->second->GetFullId() + " is unavailable."); |
246 | LOG.Info("Module " + it->second->GetFullId() + " is unavailable."); |
| 247 | this->signal_on_module_change_(it->second->GetFullId(), false); |
247 | this->signal_on_module_change_(it->second->GetFullId(), false); |
| 248 | 248 | ||
| 249 | this->modules_.erase(it); |
249 | this->modules_.erase(it); |
| 250 | } |
250 | } |
| 251 | } |
251 | } |
| 252 | } |
252 | } |
| 253 | 253 | ||
| 254 | void Manager::RequestModules(Node::Id node_id) |
254 | void Manager::RequestModules(Node::Id node_id) |
| 255 | { |
255 | { |
| 256 | this->RemoveModules(node_id); |
256 | this->RemoveModules(node_id); |
| 257 | 257 | ||
| 258 | Message* payload = new Message("mnmt", "To_Owner", "", 0, "List"); |
258 | Message* payload = new Message("mnmt", "To_Owner", "", 0, "List"); |
| 259 | payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id)); |
259 | payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id)); |
| 260 | 260 | ||
| 261 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
261 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
| 262 | } |
262 | } |
| 263 | 263 | ||
| 264 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
264 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
| 265 | { |
265 | { |
| 266 | unsigned int number_of_modules = 0; |
266 | unsigned int number_of_modules = 0; |
| 267 | 267 | ||
| 268 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
268 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
| 269 | { |
269 | { |
| 270 | if (it->second->GetNodeId() == node_id) |
270 | if (it->second->GetNodeId() == node_id) |
| 271 | { |
271 | { |
| 272 | number_of_modules++; |
272 | number_of_modules++; |
| 273 | } |
273 | } |
| 274 | } |
274 | } |
| 275 | 275 | ||
| 276 | return number_of_modules; |
276 | return number_of_modules; |
| 277 | } |
277 | } |
| 278 | 278 | ||
| 279 | 279 | ||
| 280 | }; // namespace can |
280 | }; // namespace can |
| 281 | }; // namespace atom |
281 | }; // namespace atom |
| 282 | 282 | ||