Subversion Repositories HomeAutomation

Rev

Rev 1647 | Rev 1652 | 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 <signal.h>
  22.  
  23. #include <boost/program_options.hpp>
  24. #include <boost/thread/mutex.hpp>
  25. #include <boost/thread/condition.hpp>
  26. #include <boost/thread/locks.hpp>
  27.  
  28. #include "logging/Logger.h"
  29. #include "net/Manager.h"
  30. #include "timer/Manager.h"
  31. #include "config/Manager.h"
  32. #include "can/Protocol.h"
  33. #include "broker/Manager.h"
  34. #include "control/Manager.h"
  35. #include "can/Network.h"
  36. #include "can/Monitor.h"
  37. #include "vm/Manager.h"
  38. #include "storage/Manager.h"
  39.  
  40. #include "vm/plugin/System.h"
  41. #include "vm/plugin/Timer.h"
  42. #include "vm/plugin/Module.h"
  43. #include "vm/plugin/Console.h"
  44. #include "vm/plugin/Storage.h"
  45. #include "vm/plugin/Xorg.h"
  46. #include "vm/plugin/Socket.h"
  47.  
  48. using namespace atom;
  49.  
  50. logging::Logger LOG("Main");
  51. std::vector<broker::Subscriber::Pointer> subscribers;
  52. boost::condition on_message_condition;
  53. boost::mutex guard_mutex;
  54.  
  55. void Handler(int status);
  56. void CleanUp();
  57.  
  58. int main(int argc, char **argv)
  59. {
  60.     LOG.Info("Atom Daemon, version 1.5.0 starting...");
  61.     LOG.Info("Written by Mattias Runge 2010.");
  62.     LOG.Info("Released under GPL version 2.");
  63.    
  64.     signal(SIGTERM, Handler);
  65.     signal(SIGINT, Handler);
  66.     signal(SIGQUIT, Handler);
  67.     signal(SIGABRT, Handler);
  68.     signal(SIGPIPE, Handler);
  69.    
  70.     config::Manager::Create();
  71.    
  72.     if (!config::Manager::Instance()->Set(argc, argv))
  73.     {
  74.         CleanUp();
  75.         return EXIT_SUCCESS;
  76.     }
  77.    
  78.     if (config::Manager::Instance()->Exist("LogFile"))
  79.     {
  80.         logging::Logger::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
  81.     }
  82.    
  83.     if (config::Manager::Instance()->Exist("LogLevel"))
  84.     {
  85.         logging::Logger::SetLevel((logging::Logger::Level)config::Manager::Instance()->GetAsInt("LogLevel"));
  86.     }
  87.    
  88.     if (config::Manager::Instance()->Exist("daemon"))
  89.     {
  90.         LOG.Info("Entering daemon mode...");
  91.        
  92.         if (daemon(0, 0) == -1)
  93.         {
  94.             LOG.Error("Could not enter daemon mode. Exiting...");
  95.             CleanUp();
  96.             return EXIT_FAILURE;
  97.         }
  98.        
  99.         LOG.Info("Deamon mode entered successfully!");
  100.     }
  101.  
  102.     storage::Manager::Create();
  103.     timer::Manager::Create();
  104.     broker::Manager::Create();
  105.     net::Manager::Create();
  106.     can::Protocol::Create();
  107.     control::Manager::Create();
  108.     vm::Manager::Create();
  109.    
  110.     storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath"));
  111.    
  112.     can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile"));
  113.    
  114.     if (config::Manager::Instance()->Exist("MonitorPort"))
  115.     {
  116.         subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort"))));  
  117.     }
  118.    
  119.     subscribers.push_back(control::Manager::Instance());  
  120.    
  121.     vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService())));
  122.     vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Console(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("CommandPort"))));
  123.     vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService())));
  124.     vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService())));
  125.     vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService())));
  126.     vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService())));
  127.     vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService())));
  128.    
  129.    
  130.     vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"));
  131.    
  132.     if (config::Manager::Instance()->Exist("CanNet"))
  133.     {
  134.         common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
  135.        
  136.         for (int n = 0; n < cannetworks.size(); n++)
  137.         {
  138.             subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));            
  139.         }
  140.     }
  141.    
  142.     boost::mutex::scoped_lock guard(guard_mutex);
  143.     on_message_condition.wait(guard);
  144.    
  145.     LOG.Info("Cleaning up...");
  146.    
  147.     CleanUp();
  148.    
  149.     LOG.Info("Thank you for using Atom. Goodbye!");
  150.    
  151.     return EXIT_SUCCESS;
  152. }
  153.  
  154. void CleanUp()
  155. {
  156.     subscribers.clear();
  157.     config::Manager::Delete();
  158.     timer::Manager::Delete();
  159.     control::Manager::Delete();
  160.     vm::Manager::Delete();
  161.     can::Protocol::Delete();
  162.     broker::Manager::Delete();
  163.     net::Manager::Delete();
  164.     storage::Manager::Delete();
  165. }
  166.  
  167. void Handler(int status)
  168. {
  169.     std::string signal_name = "Unknown";
  170.    
  171.     switch (status)
  172.     {
  173.         case SIGTERM:
  174.         {
  175.             signal_name = "Terminate";
  176.             break;
  177.         }  
  178.         case SIGINT:
  179.         {
  180.             signal_name = "Interupt";
  181.             break;
  182.         }  
  183.         case SIGQUIT:
  184.         {
  185.             signal_name = "Quit";
  186.             break;
  187.         }  
  188.         case SIGABRT:
  189.         {
  190.             signal_name = "Abort";
  191.             break;
  192.         }  
  193.         case SIGIO:
  194.         {
  195.             signal_name = "I/O";
  196.             break;
  197.         }  
  198.         case SIGPIPE:
  199.         {
  200.             signal_name = "Pipe";
  201.             break;
  202.         }
  203.     }
  204.    
  205.     LOG.Debug("Received signal " + signal_name + "(" + boost::lexical_cast<std::string>(status) + ").");
  206.    
  207.     if (status != SIGPIPE)
  208.     {
  209.         on_message_condition.notify_all();
  210.     }
  211. }