Subversion Repositories HomeAutomation

Rev

Rev 1599 | Rev 1601 | 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.  
  23. #include <boost/program_options.hpp>
  24. #include <boost/thread/mutex.hpp>
  25. #include <boost/thread/condition.hpp>
  26. #include <boost/thread/locks.hpp>
  27.  
  28. #include "logging/Logger.h"
  29. #include "net/Manager.h"
  30. #include "timer/Manager.h"
  31. #include "config/Manager.h"
  32. #include "can/Protocol.h"
  33. #include "broker/Manager.h"
  34. #include "can/Manager.h"
  35. #include "can/Network.h"
  36. #include "can/Monitor.h"
  37.  
  38. using namespace atom;
  39.  
  40. logging::Logger LOG("Main");
  41. std::vector<broker::Subscriber::Pointer> subscribers;
  42. boost::condition on_message_condition;
  43. boost::mutex guard_mutex;
  44.  
  45. void Handler(int status);
  46. void CleanUp();
  47.  
  48. int main(int argc, char **argv)
  49. {
  50.     LOG.Info("Atom version 1.5.0 starting...");
  51.     LOG.Info("Written by Mattias Runge 2010.");
  52.     LOG.Info("Released under GPL version 2.");
  53.    
  54.     signal(SIGTERM, Handler);
  55.     signal(SIGINT, Handler);
  56.     signal(SIGQUIT, Handler);
  57.     signal(SIGABRT, Handler);
  58.     signal(SIGPIPE, Handler);
  59.    
  60.     config::Manager::Create();
  61.    
  62.     if (!config::Manager::Instance()->Set(argc, argv))
  63.     {
  64.         CleanUp();
  65.         return EXIT_SUCCESS;
  66.     }
  67.    
  68.     if (config::Manager::Instance()->Exist("LogFile"))
  69.     {
  70.         logging::Logger::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
  71.     }
  72.    
  73.     if (config::Manager::Instance()->Exist("LogLevel"))
  74.     {
  75.         logging::Logger::SetLevel((logging::Logger::Level)config::Manager::Instance()->GetAsInt("LogLevel"));
  76.     }
  77.    
  78.     if (config::Manager::Instance()->Exist("daemon"))
  79.     {
  80.         LOG.Info("Entering daemon mode...");
  81.        
  82.         if (daemon(0, 0) == -1)
  83.         {
  84.             LOG.Error("Could not enter daemon mode. Exiting...");
  85.             CleanUp();
  86.             return EXIT_FAILURE;
  87.         }
  88.        
  89.         LOG.Info("Deamon mode entered successfully!");
  90.     }
  91.  
  92.     timer::Manager::Create();
  93.     broker::Manager::Create();
  94.     net::Manager::Create();
  95.     can::Protocol::Create();
  96.     can::Manager::Create();
  97.  
  98.     if (config::Manager::Instance()->Exist("ProtocolFile"))
  99.     {
  100.         can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile"));
  101.     }
  102.    
  103.     if (config::Manager::Instance()->Exist("MonitorPort"))
  104.     {
  105.         subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort"))));  
  106.     }
  107.    
  108.     subscribers.push_back(can::Manager::Instance());  
  109.    
  110.     if (config::Manager::Instance()->Exist("CanNet"))
  111.     {
  112.         type::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
  113.        
  114.         for (int n = 0; n < cannetworks.size(); n++)
  115.         {
  116.             subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));            
  117.         }
  118.     }
  119.    
  120.     LOG.Info("Initialization complete.");
  121.    
  122.     boost::mutex::scoped_lock guard(guard_mutex);
  123.     on_message_condition.wait(guard);
  124.    
  125.     LOG.Info("Cleaning up...");
  126.    
  127.     CleanUp();
  128.    
  129.     LOG.Info("Thank you for using Atom. Goodbye!");
  130.    
  131.     return EXIT_SUCCESS;
  132. }
  133.  
  134. void CleanUp()
  135. {
  136.     subscribers.clear();
  137.     config::Manager::Delete();
  138.     timer::Manager::Delete();
  139.     can::Manager::Delete();
  140.     can::Protocol::Delete();
  141.     broker::Manager::Delete();
  142.     net::Manager::Delete();
  143. }
  144.  
  145. void Handler(int status)
  146. {
  147.     std::string signal_name = "Unknown";
  148.    
  149.     switch (status)
  150.     {
  151.         case SIGTERM:
  152.         {
  153.             signal_name = "Terminate";
  154.             break;
  155.         }  
  156.         case SIGINT:
  157.         {
  158.             signal_name = "Interupt";
  159.             break;
  160.         }  
  161.         case SIGQUIT:
  162.         {
  163.             signal_name = "Quit";
  164.             break;
  165.         }  
  166.         case SIGABRT:
  167.         {
  168.             signal_name = "Abort";
  169.             break;
  170.         }  
  171.         case SIGIO:
  172.         {
  173.             signal_name = "I/O";
  174.             break;
  175.         }  
  176.         case SIGPIPE:
  177.         {
  178.             signal_name = "Pipe";
  179.             break;
  180.         }
  181.     }
  182.    
  183.     LOG.Debug("Received signal " + signal_name + "(" + boost::lexical_cast<std::string>(status) + ").");
  184.    
  185.     if (status != SIGPIPE)
  186.     {
  187.         on_message_condition.notify_all();
  188.     }
  189. }