Subversion Repositories HomeAutomation

Rev

Rev 1599 | Rev 1602 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

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