Subversion Repositories HomeAutomation

Rev

Rev 1964 | 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. #include "net/GetFile.h"
  45. #include "net/Url.h"
  46.  
  47. #include "vm/plugin/System.h"
  48. #include "vm/plugin/Timer.h"
  49. #include "vm/plugin/Module.h"
  50. #include "vm/plugin/Node.h"
  51. #include "vm/plugin/Console.h"
  52. #include "vm/plugin/Storage.h"
  53. #include "vm/plugin/Socket.h"
  54.  
  55. #ifdef USE_PLUGIN_XORG
  56. #include "vm/plugin/Xorg.h"
  57. #endif // USE_PLUGIN_XORG
  58.  
  59. #ifdef USE_PLUGIN_MYSQL
  60. #include "vm/plugin/MySql.h"
  61. #endif // USE_PLUGIN_MYSQL
  62.  
  63. using namespace atom;
  64.  
  65. logging::Logger LOG("Main"); // Deprecated
  66. static const std::string log_module_ = "main";
  67. std::vector<broker::Subscriber::Pointer> subscribers;
  68. boost::condition on_message_condition;
  69. boost::mutex guard_mutex;
  70. net::GetFile::Pointer downloader;
  71.  
  72. void ContinueInitialization(std::string protocol_filename);
  73. void Handler(int status);
  74. void CleanUp();
  75.  
  76.  
  77.  
  78.  
  79. void HandleProtocolFileDownload(bool success, net::Url url, std::string temporary_filepath)
  80. {
  81.   if (success)
  82.   {
  83.     log::Info(log_module_, "Successfully downloaded protocol file!");
  84.     ContinueInitialization(temporary_filepath);
  85.   }
  86.   else
  87.   {
  88.     ContinueInitialization(url.GetRaw());
  89.   }
  90. }
  91.  
  92. int main(int argc, char **argv)
  93. {
  94.   /* Set core dump parameters */
  95.   rlimit core_limit = { RLIM_INFINITY, RLIM_INFINITY };
  96.   setrlimit( RLIMIT_CORE, &core_limit ); // enable core dumps
  97.  
  98.  
  99.   /* Print startup information */
  100.   log::Info(log_module_, "Atom daemon, version %s starting...", VERSION);
  101.   log::Info(log_module_, "Released under %s.", LICENSE);
  102.   log::Info(log_module_, "Written by Mattias Runge 2010-2012.");
  103.  
  104.  
  105.   /* Register signal handlers */
  106.   signal(SIGTERM, Handler);
  107.   signal(SIGINT,  Handler);
  108.   signal(SIGQUIT, Handler);
  109.   signal(SIGABRT, Handler);
  110.   signal(SIGPIPE, Handler);
  111.  
  112.  
  113.   /* Check configuration */
  114.   config::Manager::Create();
  115.  
  116.   if (!config::Manager::Instance()->Set(argc, argv))
  117.   {
  118.     CleanUp();
  119.     return EXIT_SUCCESS;
  120.   }
  121.  
  122.  
  123.   /* Set up logging */
  124.   if (config::Manager::Instance()->Exist("LogFile"))
  125.   {
  126.     log::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
  127.   }
  128.  
  129.   if (config::Manager::Instance()->Exist("LogLevelMask"))
  130.   {
  131.     log::SetLevelByString(config::Manager::Instance()->GetAsString("LogLevelMask"));
  132.   }
  133.  
  134.  
  135.   /* Enter daemon mode if requested */
  136.   if (config::Manager::Instance()->Exist("daemon"))
  137.   {
  138.     LOG.Info("Entering daemon mode...");
  139.  
  140.     if (daemon(0, 0) == -1)
  141.     {
  142.       log::Error(log_module_, "Could not enter daemon mode. Exiting...");
  143.       CleanUp();
  144.       return EXIT_FAILURE;
  145.     }
  146.  
  147.     log::Info(log_module_, "Deamon mode entered successfully!");
  148.   }
  149.  
  150.  
  151.   /* Create singletons */
  152.   storage::Manager::Create();
  153.   timer::Manager::Create();
  154.   broker::Manager::Create();
  155.   net::Manager::Create();
  156.   can::Protocol::Create();
  157.   control::Manager::Create();
  158.   vm::Manager::Create();
  159.  
  160.  
  161.  
  162.   std::string protocol_filename = config::Manager::Instance()->GetAsString("ProtocolFile");
  163.  
  164.   try
  165.   {
  166.     net::Url url(protocol_filename);
  167.    
  168.     downloader = net::GetFile::Pointer(new net::GetFile());
  169.    
  170.     log::Info(log_module_, "Will try to download protocol file from \"%s\"...", protocol_filename.data());
  171.    
  172.     downloader->Start(url, HandleProtocolFileDownload);
  173.   }
  174.   catch (std::exception& exception)
  175.   {
  176.     log::Debug(log_module_, "Could not parse ProtocolFile as a URL, \"%s\".", protocol_filename.data());
  177.     ContinueInitialization(protocol_filename);
  178.   }
  179.  
  180.  
  181.   /* Lock main thread and wait for exit */
  182.   boost::mutex::scoped_lock guard(guard_mutex);
  183.  
  184.   on_message_condition.wait(guard);
  185.  
  186.  
  187.   /* Cleanup */
  188.   log::Info(log_module_, "Cleaning up...");
  189.  
  190.   CleanUp();
  191.  
  192.   log::Info(log_module_, "Thank you for using Atom. Goodbye!");
  193.  
  194.   return EXIT_SUCCESS;
  195. }
  196.  
  197. void ContinueInitialization(std::string protocol_filename)
  198. {  
  199.   /* Load protocol file */
  200.   if (!can::Protocol::Instance()->Load(protocol_filename))
  201.   {
  202.     log::Error(log_module_, "Failed to load %s!", protocol_filename.data());
  203.  
  204.     on_message_condition.notify_all();
  205.   }
  206.  
  207.  
  208.   /* Set up storage path */
  209.   storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath"));
  210.  
  211.  
  212.   /* Start monitor server */
  213.   if (config::Manager::Instance()->Exist("MonitorPort"))
  214.   {
  215.     subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort"))));
  216.   }
  217.  
  218.  
  219.   /* Start CanDaemon server */
  220.   if (config::Manager::Instance()->Exist("DaemonPort"))
  221.   {
  222.     subscribers.push_back(can::CanDaemon::Pointer(new can::CanDaemon(config::Manager::Instance()->GetAsInt("DaemonPort"))));
  223.   }
  224.  
  225.  
  226.   /* Subscribe Can control manager to data */
  227.   subscribers.push_back(control::Manager::Instance());
  228.  
  229.  
  230.   /* Start VM plugins */
  231.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService())));
  232.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Console(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("CommandPort"))));
  233.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService())));
  234.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService())));
  235.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService())));
  236.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Node(vm::Manager::Instance()->GetIoService())));
  237.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService())));
  238.  
  239. #ifdef USE_PLUGIN_XORG
  240.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService())));
  241. #endif // USE_PLUGIN_XORG
  242.  
  243. #ifdef USE_PLUGIN_MYSQL
  244.   vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::MySql(vm::Manager::Instance()->GetIoService())));
  245. #endif // USE_PLUGIN_MYSQL
  246.  
  247.  
  248.   /* Start VM */
  249.   vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"), config::Manager::Instance()->GetAsString("UserScriptPath"));
  250.  
  251.  
  252.   /* Connect to Can network */
  253.   if (config::Manager::Instance()->Exist("CanNet"))
  254.   {
  255.     common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
  256.  
  257.     for (uint n = 0; n < cannetworks.size(); n++)
  258.     {
  259.       subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));
  260.     }
  261.   }
  262. }
  263.  
  264. void CleanUp()
  265. {
  266.   downloader.reset();
  267.  
  268.   if (net::Manager::Instance().use_count() > 0)
  269.   {
  270.     net::Manager::Instance()->Stop();
  271.   }
  272.  
  273.   subscribers.clear();
  274.   config::Manager::Delete();
  275.   timer::Manager::Delete();
  276.   control::Manager::Delete();
  277.   can::Protocol::Delete();
  278.   broker::Manager::Delete();
  279.   vm::Manager::Delete();
  280.   net::Manager::Delete();
  281.   storage::Manager::Delete();
  282.  
  283.   log::CloseFile();
  284. }
  285.  
  286. void Handler(int status)
  287. {
  288.   std::string signal_name = "Unknown";
  289.  
  290.   switch (status)
  291.   {
  292.     case SIGTERM:
  293.     {
  294.       signal_name = "Terminate";
  295.       break;
  296.     }
  297.  
  298.     case SIGINT:
  299.     {
  300.       signal_name = "Interrupt";
  301.       break;
  302.     }
  303.  
  304.     case SIGQUIT:
  305.     {
  306.       signal_name = "Quit";
  307.       break;
  308.     }
  309.  
  310.     case SIGABRT:
  311.     {
  312.       signal_name = "Abort";
  313.       break;
  314.     }
  315.  
  316.     case SIGIO:
  317.     {
  318.       signal_name = "I/O";
  319.       break;
  320.     }
  321.  
  322.     case SIGPIPE:
  323.     {
  324.       signal_name = "Pipe";
  325.       break;
  326.     }
  327.   }
  328.  
  329.   log::Info(log_module_, "Received signal %s (%d).", signal_name.c_str(), status);
  330.  
  331.   if (status != SIGPIPE)
  332.   {
  333.     on_message_condition.notify_all();
  334.   }
  335. }
  336.