Subversion Repositories HomeAutomation

Rev

Rev 1961 | Rev 2107 | 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.     log::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
  98.   }
  99.  
  100.   if (config::Manager::Instance()->Exist("LogLevelMask"))
  101.   {
  102.     log::SetLevelByString(config::Manager::Instance()->GetAsString("LogLevelMask"));
  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(log_module_, "Could not enter daemon mode. Exiting...");
  112.       CleanUp();
  113.       return EXIT_FAILURE;
  114.     }
  115.  
  116.     log::Info(log_module_, "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::Storage(vm::Manager::Instance()->GetIoService())));
  146.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService())));
  147.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService())));
  148.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Node(vm::Manager::Instance()->GetIoService())));
  149.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService())));
  150.  
  151. #ifdef USE_PLUGIN_XORG
  152.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService())));
  153. #endif // USE_PLUGIN_XORG
  154.  
  155. #ifdef USE_PLUGIN_MYSQL
  156.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::MySql(vm::Manager::Instance()->GetIoService())));
  157. #endif // USE_PLUGIN_MYSQL
  158.  
  159.   vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"), config::Manager::Instance()->GetAsString("UserScriptPath"));
  160.  
  161.   if (config::Manager::Instance()->Exist("CanNet"))
  162.   {
  163.     common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
  164.  
  165.     for (uint n = 0; n < cannetworks.size(); n++)
  166.     {
  167.       subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));
  168.     }
  169.   }
  170.  
  171.   boost::mutex::scoped_lock guard(guard_mutex);
  172.  
  173.   on_message_condition.wait(guard);
  174.  
  175.   log::Info(log_module_, "Cleaning up...");
  176.  
  177.   CleanUp();
  178.  
  179.   log::Info(log_module_, "Thank you for using Atom. Goodbye!");
  180.  
  181.   return EXIT_SUCCESS;
  182. }
  183.  
  184. void CleanUp()
  185. {
  186.   if (net::Manager::Instance().use_count() > 0)
  187.   {
  188.     net::Manager::Instance()->Stop();
  189.   }
  190.  
  191.   subscribers.clear();
  192.   config::Manager::Delete();
  193.   timer::Manager::Delete();
  194.   control::Manager::Delete();
  195.   can::Protocol::Delete();
  196.   broker::Manager::Delete();
  197.   vm::Manager::Delete();
  198.   net::Manager::Delete();
  199.   storage::Manager::Delete();
  200.  
  201.   log::CloseFile();
  202. }
  203.  
  204. void Handler(int status)
  205. {
  206.   std::string signal_name = "Unknown";
  207.  
  208.   switch (status)
  209.   {
  210.     case SIGTERM:
  211.     {
  212.       signal_name = "Terminate";
  213.       break;
  214.     }
  215.  
  216.     case SIGINT:
  217.     {
  218.       signal_name = "Interrupt";
  219.       break;
  220.     }
  221.  
  222.     case SIGQUIT:
  223.     {
  224.       signal_name = "Quit";
  225.       break;
  226.     }
  227.  
  228.     case SIGABRT:
  229.     {
  230.       signal_name = "Abort";
  231.       break;
  232.     }
  233.  
  234.     case SIGIO:
  235.     {
  236.       signal_name = "I/O";
  237.       break;
  238.     }
  239.  
  240.     case SIGPIPE:
  241.     {
  242.       signal_name = "Pipe";
  243.       break;
  244.     }
  245.   }
  246.  
  247.   log::Info(log_module_, "Received signal %s (%d).", signal_name.c_str(), status);
  248.  
  249.   if (status != SIGPIPE)
  250.   {
  251.     on_message_condition.notify_all();
  252.   }
  253. }
  254.