Subversion Repositories HomeAutomation

Rev

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