Subversion Repositories HomeAutomation

Rev

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