Rev 1598 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1598 | Rev 1599 | ||
|---|---|---|---|
| 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 "Message.h" |
27 | #include "Message.h" |
| 28 | #include <type/common.h> |
28 | #include <type/common.h> |
| 29 | 29 | ||
| 30 | namespace atom { |
30 | namespace atom { |
| 31 | namespace can { |
31 | namespace can { |
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | Manager::Pointer Manager::instance_ |
34 | Manager::Pointer Manager::instance_; |
| 35 | 35 | ||
| 36 | Manager::Manager() : broker::Subscriber(false), LOG("can::Manager") |
36 | Manager::Manager() : broker::Subscriber(false), LOG("can::Manager") |
| 37 | { |
37 | { |
| 38 | // TODO: Set offline timer |
38 | // TODO: Set offline timer |
| 39 | } |
39 | } |
| 40 | 40 | ||
| 41 | Manager::~Manager() |
41 | Manager::~Manager() |
| 42 | { |
42 | { |
| 43 | } |
43 | } |
| 44 | 44 | ||
| 45 | Manager::Pointer Manager::Instance() |
45 | Manager::Pointer Manager::Instance() |
| 46 | { |
46 | { |
| 47 | return Manager::instance_; |
47 | return Manager::instance_; |
| - | 48 | } |
|
| - | 49 | ||
| - | 50 | void Manager::Create() |
|
| - | 51 | { |
|
| - | 52 | Manager::instance_ = Manager::Pointer(new Manager()); |
|
| 48 | } |
53 | } |
| 49 | 54 | ||
| 50 | void Manager::Delete() |
55 | void Manager::Delete() |
| 51 | { |
56 | { |
| 52 | Manager::instance_.reset(); |
57 | Manager::instance_.reset(); |
| 53 | } |
58 | } |
| 54 | 59 | ||
| 55 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
60 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
| 56 | { |
61 | { |
| 57 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
62 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
| 58 | { |
63 | { |
| 59 | Message* payload = static_cast<Message*>(message->GetPayload().get()); |
64 | Message* payload = static_cast<Message*>(message->GetPayload().get()); |
| 60 | Node::Pointer node; |
65 | Node::Pointer node; |
| 61 | 66 | ||
| 62 | if (payload->GetClassName() == "nmt") |
67 | if (payload->GetClassName() == "nmt") |
| 63 | { |
68 | { |
| 64 | if (payload->GetCommandName() == "Bios_Start") |
69 | if (payload->GetCommandName() == "Bios_Start") |
| 65 | { |
70 | { |
| 66 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
71 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 67 | this->RemoveModules(node->GetId()); |
72 | this->RemoveModules(node->GetId()); |
| 68 | node->SetState(Node::STATE_ONLINE); |
73 | node->SetState(Node::STATE_ONLINE); |
| 69 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went online."); |
74 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went online."); |
| 70 | } |
75 | } |
| 71 | else if (payload->GetCommandName() == "Reset") |
76 | else if (payload->GetCommandName() == "Reset") |
| 72 | { |
77 | { |
| 73 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
78 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 74 | this->RemoveModules(node->GetId()); |
79 | this->RemoveModules(node->GetId()); |
| 75 | node->SetState(Node::STATE_OFFLINE); |
80 | node->SetState(Node::STATE_OFFLINE); |
| 76 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline."); |
81 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline."); |
| 77 | } |
82 | } |
| 78 | else if (payload->GetCommandName() == "App_start") |
83 | else if (payload->GetCommandName() == "App_start") |
| 79 | { |
84 | { |
| 80 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
85 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 81 | this->RequestModules(node->GetId()); |
86 | this->RequestModules(node->GetId()); |
| 82 | node->SetState(Node::STATE_PENDING); |
87 | node->SetState(Node::STATE_PENDING); |
| 83 | LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules..."); |
88 | LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules..."); |
| 84 | } |
89 | } |
| 85 | else if (payload->GetCommandName() == "Heartbeat") |
90 | else if (payload->GetCommandName() == "Heartbeat") |
| 86 | { |
91 | { |
| 87 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
92 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 88 | 93 | ||
| 89 | if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE) |
94 | if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE) |
| 90 | { |
95 | { |
| 91 | this->RequestModules(node->GetId()); |
96 | this->RequestModules(node->GetId()); |
| 92 | node->SetState(Node::STATE_PENDING); |
97 | node->SetState(Node::STATE_PENDING); |
| 93 | LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules..."); |
98 | LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules..."); |
| 94 | } |
99 | } |
| 95 | else if (node->GetState() == Node::STATE_INITIALIZED) |
100 | else if (node->GetState() == Node::STATE_INITIALIZED) |
| 96 | { |
101 | { |
| 97 | if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
102 | if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
| 98 | { |
103 | { |
| 99 | this->RequestModules(node->GetId()); |
104 | this->RequestModules(node->GetId()); |
| 100 | node->SetState(Node::STATE_PENDING); |
105 | node->SetState(Node::STATE_PENDING); |
| 101 | LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules..."); |
106 | LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules..."); |
| 102 | } |
107 | } |
| 103 | } |
108 | } |
| 104 | } |
109 | } |
| 105 | } |
110 | } |
| 106 | else |
111 | else |
| 107 | { |
112 | { |
| 108 | if (payload->GetCommandName() == "List" && payload->GetDirectionName() == "From_Owner") |
113 | if (payload->GetCommandName() == "List" && payload->GetDirectionName() == "From_Owner") |
| 109 | { |
114 | { |
| 110 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
115 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
| 111 | 116 | ||
| 112 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
117 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
| 113 | module->SetNodeId(node->GetId()); |
118 | module->SetNodeId(node->GetId()); |
| 114 | 119 | ||
| 115 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
120 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
| 116 | { |
121 | { |
| 117 | node->SetState(Node::STATE_INITIALIZED); |
122 | node->SetState(Node::STATE_INITIALIZED); |
| 118 | } |
123 | } |
| 119 | 124 | ||
| 120 | LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + "."); |
125 | LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + "."); |
| 121 | // TODO: Notify that module is available |
126 | // TODO: Notify that module is available |
| 122 | } |
127 | } |
| 123 | } |
128 | } |
| 124 | } |
129 | } |
| 125 | } |
130 | } |
| 126 | 131 | ||
| 127 | Node::Pointer Manager::GetNode(Node::Id node_id) |
132 | Node::Pointer Manager::GetNode(Node::Id node_id) |
| 128 | { |
133 | { |
| 129 | Node::Pointer node; |
134 | Node::Pointer node; |
| 130 | NodeList::iterator it = this->nodes_.find(node_id); |
135 | NodeList::iterator it = this->nodes_.find(node_id); |
| 131 | 136 | ||
| 132 | if (it == this->nodes_.end()) |
137 | if (it == this->nodes_.end()) |
| 133 | { |
138 | { |
| 134 | node = Node::Pointer(new Node(node_id)); |
139 | node = Node::Pointer(new Node(node_id)); |
| 135 | this->nodes_[node_id] = node; |
140 | this->nodes_[node_id] = node; |
| 136 | } |
141 | } |
| 137 | else |
142 | else |
| 138 | { |
143 | { |
| 139 | node = it->second; |
144 | node = it->second; |
| 140 | } |
145 | } |
| 141 | 146 | ||
| 142 | return node; |
147 | return node; |
| 143 | } |
148 | } |
| 144 | 149 | ||
| 145 | Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name) |
150 | Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name) |
| 146 | { |
151 | { |
| 147 | Module::Pointer module; |
152 | Module::Pointer module; |
| 148 | ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name)); |
153 | ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name)); |
| 149 | 154 | ||
| 150 | if (it == this->modules_.end()) |
155 | if (it == this->modules_.end()) |
| 151 | { |
156 | { |
| 152 | module = Module::Pointer(new Module(module_id, module_name, class_name)); |
157 | module = Module::Pointer(new Module(module_id, module_name, class_name)); |
| 153 | this->modules_[module->GetFullId()] = module; |
158 | this->modules_[module->GetFullId()] = module; |
| 154 | } |
159 | } |
| 155 | else |
160 | else |
| 156 | { |
161 | { |
| 157 | module = it->second; |
162 | module = it->second; |
| 158 | } |
163 | } |
| 159 | 164 | ||
| 160 | return module; |
165 | return module; |
| 161 | } |
166 | } |
| 162 | 167 | ||
| 163 | void Manager::RemoveModules(Node::Id node_id) |
168 | void Manager::RemoveModules(Node::Id node_id) |
| 164 | { |
169 | { |
| 165 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
170 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
| 166 | { |
171 | { |
| 167 | if (it->second->GetNodeId() == node_id) |
172 | if (it->second->GetNodeId() == node_id) |
| 168 | { |
173 | { |
| 169 | LOG.Info("Module " + it->second->GetFullId() + " is unavailable."); |
174 | LOG.Info("Module " + it->second->GetFullId() + " is unavailable."); |
| 170 | // TODO: Notify that module is unavailable |
175 | // TODO: Notify that module is unavailable |
| 171 | 176 | ||
| 172 | this->modules_.erase(it); |
177 | this->modules_.erase(it); |
| 173 | } |
178 | } |
| 174 | } |
179 | } |
| 175 | } |
180 | } |
| 176 | 181 | ||
| 177 | void Manager::RequestModules(Node::Id node_id) |
182 | void Manager::RequestModules(Node::Id node_id) |
| 178 | { |
183 | { |
| 179 | this->RemoveModules(node_id); |
184 | this->RemoveModules(node_id); |
| 180 | 185 | ||
| 181 | Message* payload = new Message("mnmt", "To_Owner", "", 0, "List"); |
186 | Message* payload = new Message("mnmt", "To_Owner", "", 0, "List"); |
| 182 | payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id)); |
187 | payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id)); |
| 183 | 188 | ||
| 184 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
189 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
| 185 | 190 | ||
| 186 | // TODO: Set timeout if no response |
191 | // TODO: Set timeout if no response |
| 187 | } |
192 | } |
| 188 | 193 | ||
| 189 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
194 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
| 190 | { |
195 | { |
| 191 | unsigned int number_of_modules = 0; |
196 | unsigned int number_of_modules = 0; |
| 192 | 197 | ||
| 193 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
198 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
| 194 | { |
199 | { |
| 195 | if (it->second->GetNodeId() == node_id) |
200 | if (it->second->GetNodeId() == node_id) |
| 196 | { |
201 | { |
| 197 | number_of_modules++; |
202 | number_of_modules++; |
| 198 | } |
203 | } |
| 199 | } |
204 | } |
| 200 | 205 | ||
| 201 | return number_of_modules; |
206 | return number_of_modules; |
| 202 | } |
207 | } |
| 203 | 208 | ||
| 204 | 209 | ||
| 205 | }; // namespace can |
210 | }; // namespace can |
| 206 | }; // namespace atom |
211 | }; // namespace atom |
| 207 | 212 | ||