Subversion Repositories HomeAutomation

Rev

Rev 997 | Rev 1024 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /***************************************************************************
  2.  *   Copyright (C) November 26, 2008, 3:00 PM by Mattias Runge             *
  3.  *   mattias@runge.se                                                      *
  4.  *   main.cpp                                                              *
  5.  *                                                                         *
  6.  *   This program is free software; you can redistribute it and/or modify  *
  7.  *   it under the terms of the GNU General Public License as published by  *
  8.  *   the Free Software Foundation; either version 2 of the License, or     *
  9.  *   (at your option) any later version.                                   *
  10.  *                                                                         *
  11.  *   This program is distributed in the hope that it will be useful,       *
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  13.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  14.  *   GNU General Public License for more details.                          *
  15.  *                                                                         *
  16.  *   You should have received a copy of the GNU General Public License     *
  17.  *   along with this program; if not, write to the                         *
  18.  *   Free Software Foundation, Inc.,                                       *
  19.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  20.  ***************************************************************************/
  21.  
  22. using namespace std;
  23.  
  24. #include <stdlib.h>
  25. #include <iostream>
  26. #include <signal.h>
  27.  
  28. #include "version.h"
  29. #include "Tools/tools.h"
  30. #include "SyslogStream/syslogstream.h"
  31. #include "Settings/settings.h"
  32. #include "Socket/server.h"
  33. #include "CanNet/cannetmanager.h"
  34. #include "Socket/asyncsocket.h"
  35. #include "VM/virtualmachine.h"
  36. #include "CanNet/candebug.h"
  37.  
  38. int cleanUp();
  39. void handler(int status);
  40.  
  41. int main(int argc, char** argv)
  42. {
  43.     signal(SIGTERM, handler);
  44.     signal(SIGINT, handler);
  45.     signal(SIGQUIT, handler);
  46.     signal(SIGABRT, handler);
  47.     signal(SIGPIPE, handler);
  48.  
  49.     SyslogStream &slog = SyslogStream::getInstance();
  50.  
  51.     slog << "Atom " + ftos(VERSION) + " starting...\n";
  52.     slog << "Written by Mattias Runge 2008-2009\n\n";
  53.  
  54.     if (file_exists("/etc/atom.conf"))
  55.     {
  56.         if (!Settings::read("/etc/atom.conf"))
  57.         {
  58.             return cleanUp();
  59.         }
  60.     }
  61.     else
  62.     {
  63.         if (!Settings::read("atom.conf"))
  64.         {
  65.             return cleanUp();
  66.         }
  67.     }
  68.  
  69.     VirtualMachine &vm = VirtualMachine::getInstance();
  70.     vm.start();
  71.  
  72.     string canDebugPort = Settings::get("CanDebugPort");
  73.     if (canDebugPort != "")
  74.     {
  75.         CanDebug &canDebug = CanDebug::getInstance();
  76.         canDebug.setSettings(stoi(canDebugPort), "CanDebug");
  77.         canDebug.start();
  78.     }
  79.     else
  80.     {
  81.         slog << "CanDebugPort is not defined, can not start CanDebug socket\n";
  82.     }
  83.  
  84.     CanNetManager &canMan = CanNetManager::getInstance();
  85.     canMan.start();
  86.  
  87.     if (Settings::get("DaemonMode") == "yes")
  88.     {
  89.         slog << "Entering daemon mode...\n";
  90.         if (daemon(0, 0) == -1)
  91.         {
  92.             slog << "Could not enter daemon mode. Exiting...\n";
  93.             return cleanUp();
  94.         }
  95.     }
  96.  
  97.     vm.join();
  98.  
  99.     return cleanUp();
  100. }
  101.  
  102. int cleanUp()
  103. {
  104.     SyslogStream &slog = SyslogStream::getInstance();
  105.  
  106.     slog << "\n";
  107.     slog << "Goodbye!\n";
  108.  
  109.     CanDebug::deleteInstance();
  110.     VirtualMachine::deleteInstance();
  111.     CanNetManager::deleteInstance();
  112.     SyslogStream::deleteInstance();
  113.  
  114.     return EXIT_SUCCESS;
  115. }
  116.  
  117. void handler(int status)
  118. {
  119.     exit(cleanUp());
  120. }
  121.