Subversion Repositories HomeAutomation

Rev

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