Subversion Repositories HomeAutomation

Rev

Rev 1124 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
969 runge 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"
1024 runge 30
#include "Logger/logger.h"
969 runge 31
#include "Settings/settings.h"
999 runge 32
#include "Socket/server.h"
969 runge 33
#include "CanNet/cannetmanager.h"
34
#include "Socket/asyncsocket.h"
35
#include "VM/virtualmachine.h"
981 runge 36
#include "CanNet/candebug.h"
969 runge 37
 
1196 runge 38
#include "boost/thread.hpp"
39
 
1124 runge 40
bool cleanUpRunning = false;
969 runge 41
int cleanUp();
42
void handler(int status);
43
 
44
int main(int argc, char** argv)
45
{
46
    signal(SIGTERM, handler);
47
    signal(SIGINT, handler);
48
    signal(SIGQUIT, handler);
49
    signal(SIGABRT, handler);
50
    signal(SIGPIPE, handler);
51
 
1024 runge 52
    Logger &log = Logger::getInstance();
969 runge 53
 
1024 runge 54
    log.addToSyslog("Atom " + ftos(VERSION) + " starting...\n");
55
    log.add("Written by Mattias Runge 2008-2009\n\n");
969 runge 56
 
57
    if (file_exists("/etc/atom.conf"))
58
    {
59
        if (!Settings::read("/etc/atom.conf"))
60
        {
61
            return cleanUp();
62
        }
63
    }
64
    else
65
    {
66
        if (!Settings::read("atom.conf"))
67
        {
68
            return cleanUp();
69
        }
70
    }
71
 
1024 runge 72
    string logfile = Settings::get("Logfile");
73
    if (logfile != "")
74
    {
75
        if (!log.open(logfile))
76
        {
77
            log.addToSyslog("Could not open logfile, " + logfile + "\n");
78
        }
79
        else
80
        {
81
            log.add("Logging to " + logfile + "\n");
82
        }
83
    }
84
    else
85
    {
86
        log.add("Logfile is not defined, logging will be done to syslog and console only\n");
87
    }
88
 
969 runge 89
    VirtualMachine &vm = VirtualMachine::getInstance();
90
    vm.start();
91
 
999 runge 92
    string canDebugPort = Settings::get("CanDebugPort");
93
    if (canDebugPort != "")
94
    {
95
        CanDebug &canDebug = CanDebug::getInstance();
96
        canDebug.setSettings(stoi(canDebugPort), "CanDebug");
97
        canDebug.start();
98
    }
99
    else
100
    {
1024 runge 101
        log.add("CanDebugPort is not defined, can not start CanDebug socket\n");
999 runge 102
    }
981 runge 103
 
999 runge 104
    CanNetManager &canMan = CanNetManager::getInstance();
105
    canMan.start();
106
 
969 runge 107
    if (Settings::get("DaemonMode") == "yes")
108
    {
1024 runge 109
        log.add("Entering daemon mode...\n");
969 runge 110
        if (daemon(0, 0) == -1)
111
        {
1024 runge 112
            log.addToSyslog("Could not enter daemon mode. Exiting...\n");
969 runge 113
            return cleanUp();
114
        }
115
    }
116
 
999 runge 117
    vm.join();
981 runge 118
 
969 runge 119
    return cleanUp();
120
}
121
 
122
int cleanUp()
123
{
1024 runge 124
    Logger &log = Logger::getInstance();
969 runge 125
 
1124 runge 126
    if (!cleanUpRunning)
127
    {
128
        cleanUpRunning = true;
1196 runge 129
 
1124 runge 130
        log.add("\n");
131
        log.addToSyslog("Thank you for using Atom. Goodbye!\n");
981 runge 132
 
1124 runge 133
        CanDebug::deleteInstance();
134
        VirtualMachine::deleteInstance();
135
        CanNetManager::deleteInstance();
136
        Logger::deleteInstance();
137
    }
138
    else
139
    {
140
        log.addToSyslog("Something has gone wrong clean up is already running!\n");
141
        return EXIT_FAILURE;
142
    }
969 runge 143
 
144
    return EXIT_SUCCESS;
145
}
146
 
147
void handler(int status)
148
{
1124 runge 149
    string signalName = "Unknown";
1196 runge 150
 
1124 runge 151
    switch (status)
152
    {
153
    case SIGTERM:
154
    signalName = "Terminate";
155
    break;
1196 runge 156
 
1124 runge 157
    case SIGINT:
158
    signalName = "Interupt";
159
    break;
1196 runge 160
 
1124 runge 161
    case SIGQUIT:
162
    signalName = "Quit";
163
    break;
1196 runge 164
 
1124 runge 165
    case SIGABRT:
166
    signalName = "Abort";
167
    break;
1196 runge 168
 
1124 runge 169
    case SIGIO:
170
    signalName = "I/O";
171
    break;
1196 runge 172
 
1124 runge 173
    case SIGPIPE:
174
    signalName = "Pipe";
175
    break;
176
    }
177
 
178
    Logger &log = Logger::getInstance();
179
    log.addToSyslog("Received signal " + signalName + "(" + itos(status) + ")\n");
1196 runge 180
 
1124 runge 181
    if (status == SIGPIPE)
182
    {
183
        return;
184
    }
1196 runge 185
 
999 runge 186
    exit(cleanUp());
969 runge 187
}