Rev 1598 | Rev 1601 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1598 | runge | 1 | /* |
| 2 | * |
||
| 3 | * Copyright (C) 2010 Mattias Runge |
||
| 4 | * |
||
| 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 |
||
| 7 | * the Free Software Foundation; either version 2 of the License, or |
||
| 8 | * (at your option) any later version. |
||
| 9 | * |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * |
||
| 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., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | |||
| 21 | #include "Manager.h" |
||
| 22 | |||
| 23 | #include <boost/lexical_cast.hpp> |
||
| 24 | |||
| 25 | #include "broker/Manager.h" |
||
| 26 | #include "broker/Message.h" |
||
| 27 | #include "Message.h" |
||
| 28 | #include <type/common.h> |
||
| 29 | |||
| 30 | namespace atom { |
||
| 31 | namespace can { |
||
| 32 | |||
| 33 | |||
| 1599 | runge | 34 | Manager::Pointer Manager::instance_; |
| 1598 | runge | 35 | |
| 36 | Manager::Manager() : broker::Subscriber(false), LOG("can::Manager") |
||
| 37 | { |
||
| 38 | // TODO: Set offline timer |
||
| 39 | } |
||
| 40 | |||
| 41 | Manager::~Manager() |
||
| 42 | { |
||
| 43 | } |
||
| 44 | |||
| 45 | Manager::Pointer Manager::Instance() |
||
| 46 | { |
||
| 47 | return Manager::instance_; |
||
| 48 | } |
||
| 49 | |||
| 1599 | runge | 50 | void Manager::Create() |
| 51 | { |
||
| 52 | Manager::instance_ = Manager::Pointer(new Manager()); |
||
| 53 | } |
||
| 54 | |||
| 1598 | runge | 55 | void Manager::Delete() |
| 56 | { |
||
| 57 | Manager::instance_.reset(); |
||
| 58 | } |
||
| 59 | |||
| 60 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
||
| 61 | { |
||
| 62 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
||
| 63 | { |
||
| 64 | Message* payload = static_cast<Message*>(message->GetPayload().get()); |
||
| 65 | Node::Pointer node; |
||
| 66 | |||
| 67 | if (payload->GetClassName() == "nmt") |
||
| 68 | { |
||
| 69 | if (payload->GetCommandName() == "Bios_Start") |
||
| 70 | { |
||
| 71 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
||
| 72 | this->RemoveModules(node->GetId()); |
||
| 73 | node->SetState(Node::STATE_ONLINE); |
||
| 74 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went online."); |
||
| 75 | } |
||
| 76 | else if (payload->GetCommandName() == "Reset") |
||
| 77 | { |
||
| 78 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
||
| 79 | this->RemoveModules(node->GetId()); |
||
| 80 | node->SetState(Node::STATE_OFFLINE); |
||
| 81 | LOG.Info("Node " + type::ToHex(node->GetId()) + " went offline."); |
||
| 82 | } |
||
| 83 | else if (payload->GetCommandName() == "App_start") |
||
| 84 | { |
||
| 85 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
||
| 86 | this->RequestModules(node->GetId()); |
||
| 87 | node->SetState(Node::STATE_PENDING); |
||
| 88 | LOG.Info("Node " + type::ToHex(node->GetId()) + " started, requesting modules..."); |
||
| 89 | } |
||
| 90 | else if (payload->GetCommandName() == "Heartbeat") |
||
| 91 | { |
||
| 92 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
||
| 93 | |||
| 94 | if (node->GetState() == Node::STATE_OFFLINE || node->GetState() == Node::STATE_ONLINE) |
||
| 95 | { |
||
| 96 | this->RequestModules(node->GetId()); |
||
| 97 | node->SetState(Node::STATE_PENDING); |
||
| 98 | LOG.Info("Node " + type::ToHex(node->GetId()) + " was found, requesting modules..."); |
||
| 99 | } |
||
| 100 | else if (node->GetState() == Node::STATE_INITIALIZED) |
||
| 101 | { |
||
| 102 | if (this->GetNumberOfModules(node->GetId()) != boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
||
| 103 | { |
||
| 104 | this->RequestModules(node->GetId()); |
||
| 105 | node->SetState(Node::STATE_PENDING); |
||
| 106 | LOG.Info("Node " + type::ToHex(node->GetId()) + " reports a different number of modules than previously known, requesting modules..."); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | else |
||
| 112 | { |
||
| 113 | if (payload->GetCommandName() == "List" && payload->GetDirectionName() == "From_Owner") |
||
| 114 | { |
||
| 115 | node = this->GetNode(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])); |
||
| 116 | |||
| 117 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
||
| 118 | module->SetNodeId(node->GetId()); |
||
| 119 | |||
| 120 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
||
| 121 | { |
||
| 122 | node->SetState(Node::STATE_INITIALIZED); |
||
| 123 | } |
||
| 124 | |||
| 125 | LOG.Info("Module " + module->GetFullId() + " is available on node " + type::ToHex(node->GetId()) + "."); |
||
| 126 | // TODO: Notify that module is available |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | Node::Pointer Manager::GetNode(Node::Id node_id) |
||
| 133 | { |
||
| 134 | Node::Pointer node; |
||
| 135 | NodeList::iterator it = this->nodes_.find(node_id); |
||
| 136 | |||
| 137 | if (it == this->nodes_.end()) |
||
| 138 | { |
||
| 139 | node = Node::Pointer(new Node(node_id)); |
||
| 140 | this->nodes_[node_id] = node; |
||
| 141 | } |
||
| 142 | else |
||
| 143 | { |
||
| 144 | node = it->second; |
||
| 145 | } |
||
| 146 | |||
| 147 | return node; |
||
| 148 | } |
||
| 149 | |||
| 150 | Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name) |
||
| 151 | { |
||
| 152 | Module::Pointer module; |
||
| 153 | ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name)); |
||
| 154 | |||
| 155 | if (it == this->modules_.end()) |
||
| 156 | { |
||
| 157 | module = Module::Pointer(new Module(module_id, module_name, class_name)); |
||
| 158 | this->modules_[module->GetFullId()] = module; |
||
| 159 | } |
||
| 160 | else |
||
| 161 | { |
||
| 162 | module = it->second; |
||
| 163 | } |
||
| 164 | |||
| 165 | return module; |
||
| 166 | } |
||
| 167 | |||
| 168 | void Manager::RemoveModules(Node::Id node_id) |
||
| 169 | { |
||
| 170 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
||
| 171 | { |
||
| 172 | if (it->second->GetNodeId() == node_id) |
||
| 173 | { |
||
| 174 | LOG.Info("Module " + it->second->GetFullId() + " is unavailable."); |
||
| 175 | // TODO: Notify that module is unavailable |
||
| 176 | |||
| 177 | this->modules_.erase(it); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | void Manager::RequestModules(Node::Id node_id) |
||
| 183 | { |
||
| 184 | this->RemoveModules(node_id); |
||
| 185 | |||
| 186 | Message* payload = new Message("mnmt", "To_Owner", "", 0, "List"); |
||
| 187 | payload->SetVariable("HardwareId", boost::lexical_cast<std::string>(node_id)); |
||
| 188 | |||
| 189 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
||
| 190 | |||
| 191 | // TODO: Set timeout if no response |
||
| 192 | } |
||
| 193 | |||
| 194 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
||
| 195 | { |
||
| 196 | unsigned int number_of_modules = 0; |
||
| 197 | |||
| 198 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
||
| 199 | { |
||
| 200 | if (it->second->GetNodeId() == node_id) |
||
| 201 | { |
||
| 202 | number_of_modules++; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | return number_of_modules; |
||
| 207 | } |
||
| 208 | |||
| 209 | |||
| 210 | }; // namespace can |
||
| 211 | }; // namespace atom |