Subversion Repositories HomeAutomation

Rev

Rev 1959 | Rev 1989 | 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" // Deprecated
  32. #include "common/log.h"
  33. #include "net/Manager.h"
  34. #include "timer/Manager.h"
  35. #include "config/Manager.h"
  36. #include "can/Protocol.h"
  37. #include "broker/Manager.h"
  38. #include "control/Manager.h"
  39. #include "can/Network.h"
  40. #include "can/Monitor.h"
  41. #include "can/CanDaemon.h"
  42. #include "vm/Manager.h"
  43. #include "storage/Manager.h"
  44.  
  45. #include "vm/plugin/System.h"
  46. #include "vm/plugin/Timer.h"
  47. #include "vm/plugin/Module.h"
  48. #include "vm/plugin/Node.h"
  49. #include "vm/plugin/Console.h"
  50. #include "vm/plugin/Storage.h"
  51. #include "vm/plugin/Socket.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"); // Deprecated
  64. static const std::string log_module_ = "main";
  65. std::vector<broker::Subscriber::Pointer> subscribers;
  66. boost::condition on_message_condition;
  67. boost::mutex guard_mutex;
  68.  
  69. void Handler(int status);
  70. void CleanUp();
  71.  
  72. int main(int argc, char **argv)
  73. {
  74.   rlimit core_limit = { RLIM_INFINITY, RLIM_INFINITY };
  75.   setrlimit( RLIMIT_CORE, &core_limit ); // enable core dumps
  76.      
  77.   log::Info(log_module_, "Atom daemon, version %s starting...", VERSION);
  78.   log::Info(log_module_, "Released under %s.", LICENSE);
  79.   log::Info(log_module_, "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("LogLevelMask"))
  101.   {
  102.     log::SetLogLevelByString(config::Manager::Instance()->GetAsString("LogLevelMask"));
  103.   }
  104.  
  105.   if (config::Manager::Instance()->Exist("LogLevel")) // Deprecated
  106.   {
  107.     logging::Logger::SetLevel((logging::Logger::Level) config::Manager::Instance()->GetAsInt("LogLevel"));
  108.   }
  109.  
  110.   if (config::Manager::Instance()->Exist("daemon"))
  111.   {
  112.     LOG.Info("Entering daemon mode...");
  113.  
  114.     if (daemon(0, 0) == -1)
  115.     {
  116.       log::Error(log_module_, "Could not enter daemon mode. Exiting...");
  117.       CleanUp();
  118.       return EXIT_FAILURE;
  119.     }
  120.  
  121.     log::Info(log_module_, "Deamon mode entered successfully!");
  122.   }
  123.  
  124.   storage::Manager::Create();
  125.   timer::Manager::Create();
  126.   broker::Manager::Create();
  127.   net::Manager::Create();
  128.   can::Protocol::Create();
  129.   control::Manager::Create();
  130.   vm::Manager::Create();
  131.  
  132.   storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath"));
  133.  
  134.   can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile"));
  135.  
  136.   if (config::Manager::Instance()->Exist("MonitorPort"))
  137.   {
  138.     subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort"))));
  139.   }
  140.  
  141.   if (config::Manager::Instance()->Exist("DaemonPort"))
  142.   {
  143.     subscribers.push_back(can::CanDaemon::Pointer(new can::CanDaemon(config::Manager::Instance()->GetAsInt("DaemonPort"))));
  144.   }
  145.  
  146.   subscribers.push_back(control::Manager::Instance());
  147.  
  148.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService())));
  149.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Console(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("CommandPort"))));
  150.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService())));
  151.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService())));
  152.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService())));
  153.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Node(vm::Manager::Instance()->GetIoService())));
  154.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService())));
  155.  
  156. #ifdef USE_PLUGIN_XORG
  157.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService())));
  158. #endif // USE_PLUGIN_XORG
  159.  
  160. #ifdef USE_PLUGIN_MYSQL
  161.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::MySql(vm::Manager::Instance()->GetIoService())));
  162. #endif // USE_PLUGIN_MYSQL
  163.  
  164.   vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"), config::Manager::Instance()->GetAsString("UserScriptPath"));
  165.  
  166.   if (config::Manager::Instance()->Exist("CanNet"))
  167.   {
  168.     common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
  169.  
  170.     for (uint n = 0; n < cannetworks.size(); n++)
  171.     {
  172.       subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));
  173.     }
  174.   }
  175.  
  176.   boost::mutex::scoped_lock guard(guard_mutex);
  177.  
  178.   on_message_condition.wait(guard);
  179.  
  180.   log::Info(log_module_, "Cleaning up...");
  181.  
  182.   CleanUp();
  183.  
  184.   log::Info(log_module_, "Thank you for using Atom. Goodbye!");
  185.  
  186.   return EXIT_SUCCESS;
  187. }
  188.  
  189. void CleanUp()
  190. {
  191.   if (net::Manager::Instance().use_count() > 0)
  192.   {
  193.     net::Manager::Instance()->Stop();
  194.   }
  195.  
  196.   subscribers.clear();
  197.   config::Manager::Delete();
  198.   timer::Manager::Delete();
  199.   control::Manager::Delete();
  200.   can::Protocol::Delete();
  201.   broker::Manager::Delete();
  202.   vm::Manager::Delete();
  203.   net::Manager::Delete();
  204.   storage::Manager::Delete();
  205. }
  206.  
  207. void Handler(int status)
  208. {
  209.   std::string signal_name = "Unknown";
  210.  
  211.   switch (status)
  212.   {
  213.     case SIGTERM:
  214.     {
  215.       signal_name = "Terminate";
  216.       break;
  217.     }
  218.  
  219.     case SIGINT:
  220.     {
  221.       signal_name = "Interrupt";
  222.       break;
  223.     }
  224.  
  225.     case SIGQUIT:
  226.     {
  227.       signal_name = "Quit";
  228.       break;
  229.     }
  230.  
  231.     case SIGABRT:
  232.     {
  233.       signal_name = "Abort";
  234.       break;
  235.     }
  236.  
  237.     case SIGIO:
  238.     {
  239.       signal_name = "I/O";
  240.       break;
  241.     }
  242.  
  243.     case SIGPIPE:
  244.     {
  245.       signal_name = "Pipe";
  246.       break;
  247.     }
  248.   }
  249.  
  250.   log::Info(log_module_, "Received signal %s (%d).", signal_name.c_str(), status);
  251.  
  252.   if (status != SIGPIPE)
  253.   {
  254.     on_message_condition.notify_all();
  255.   }
  256. }
  257.