Subversion Repositories HomeAutomation

Rev

Rev 1196 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1196 Rev 1197
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) November 26, 2008, 3:00 PM by Mattias Runge             *
2
 *   Copyright (C) November 26, 2008, 3:00 PM by Mattias Runge             *
3
 *   mattias@runge.se                                                      *
3
 *   mattias@runge.se                                                      *
4
 *   main.cpp                                                              *
4
 *   main.cpp                                                              *
5
 *                                                                         *
5
 *                                                                         *
6
 *   This program is free software; you can redistribute it and/or modify  *
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  *
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     *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 *   (at your option) any later version.                                   *
9
 *   (at your option) any later version.                                   *
10
 *                                                                         *
10
 *                                                                         *
11
 *   This program is distributed in the hope that it will be useful,       *
11
 *   This program is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU General Public License for more details.                          *
14
 *   GNU General Public License for more details.                          *
15
 *                                                                         *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU General Public License     *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
20
 ***************************************************************************/
21
 
21
 
22
using namespace std;
22
using namespace std;
23
 
23
 
24
#include <stdlib.h>
24
#include <stdlib.h>
25
#include <iostream>
25
#include <iostream>
26
#include <signal.h>
26
#include <signal.h>
27
 
27
 
28
#include "version.h"
28
#include "version.h"
29
#include "Tools/tools.h"
29
#include "Tools/tools.h"
30
#include "Logger/logger.h"
30
#include "Logger/logger.h"
31
#include "Settings/settings.h"
31
#include "Settings/settings.h"
32
#include "Socket/server.h"
32
#include "Socket/server.h"
33
#include "CanNet/cannetmanager.h"
33
#include "CanNet/cannetmanager.h"
34
#include "Socket/asyncsocket.h"
34
#include "Socket/asyncsocket.h"
35
#include "VM/virtualmachine.h"
35
#include "VM/virtualmachine.h"
36
#include "CanNet/candebug.h"
36
#include "CanNet/candebug.h"
37
 
-
 
38
#include "boost/thread.hpp"
-
 
39
 
37
 
40
bool cleanUpRunning = false;
38
bool cleanUpRunning = false;
41
int cleanUp();
39
int cleanUp();
42
void handler(int status);
40
void handler(int status);
43
 
41
 
44
int main(int argc, char** argv)
42
int main(int argc, char** argv)
45
{
43
{
46
    signal(SIGTERM, handler);
44
    signal(SIGTERM, handler);
47
    signal(SIGINT, handler);
45
    signal(SIGINT, handler);
48
    signal(SIGQUIT, handler);
46
    signal(SIGQUIT, handler);
49
    signal(SIGABRT, handler);
47
    signal(SIGABRT, handler);
50
    signal(SIGPIPE, handler);
48
    signal(SIGPIPE, handler);
51
 
49
 
52
    Logger &log = Logger::getInstance();
50
    Logger &log = Logger::getInstance();
53
 
51
 
54
    log.addToSyslog("Atom " + ftos(VERSION) + " starting...\n");
52
    log.addToSyslog("Atom " + ftos(VERSION) + " starting...\n");
55
    log.add("Written by Mattias Runge 2008-2009\n\n");
53
    log.add("Written by Mattias Runge 2008-2009\n\n");
56
 
54
 
57
    if (file_exists("/etc/atom.conf"))
55
    if (file_exists("/etc/atom.conf"))
58
    {
56
    {
59
        if (!Settings::read("/etc/atom.conf"))
57
        if (!Settings::read("/etc/atom.conf"))
60
        {
58
        {
61
            return cleanUp();
59
            return cleanUp();
62
        }
60
        }
63
    }
61
    }
64
    else
62
    else
65
    {
63
    {
66
        if (!Settings::read("atom.conf"))
64
        if (!Settings::read("atom.conf"))
67
        {
65
        {
68
            return cleanUp();
66
            return cleanUp();
69
        }
67
        }
70
    }
68
    }
71
 
69
 
72
    string logfile = Settings::get("Logfile");
70
    string logfile = Settings::get("Logfile");
73
    if (logfile != "")
71
    if (logfile != "")
74
    {
72
    {
75
        if (!log.open(logfile))
73
        if (!log.open(logfile))
76
        {
74
        {
77
            log.addToSyslog("Could not open logfile, " + logfile + "\n");
75
            log.addToSyslog("Could not open logfile, " + logfile + "\n");
78
        }
76
        }
79
        else
77
        else
80
        {
78
        {
81
            log.add("Logging to " + logfile + "\n");
79
            log.add("Logging to " + logfile + "\n");
82
        }
80
        }
83
    }
81
    }
84
    else
82
    else
85
    {
83
    {
86
        log.add("Logfile is not defined, logging will be done to syslog and console only\n");
84
        log.add("Logfile is not defined, logging will be done to syslog and console only\n");
87
    }
85
    }
88
 
86
 
89
    VirtualMachine &vm = VirtualMachine::getInstance();
87
    VirtualMachine &vm = VirtualMachine::getInstance();
90
    vm.start();
88
    vm.start();
91
 
89
 
92
    string canDebugPort = Settings::get("CanDebugPort");
90
    string canDebugPort = Settings::get("CanDebugPort");
93
    if (canDebugPort != "")
91
    if (canDebugPort != "")
94
    {
92
    {
95
        CanDebug &canDebug = CanDebug::getInstance();
93
        CanDebug &canDebug = CanDebug::getInstance();
96
        canDebug.setSettings(stoi(canDebugPort), "CanDebug");
94
        canDebug.setSettings(stoi(canDebugPort), "CanDebug");
97
        canDebug.start();
95
        canDebug.start();
98
    }
96
    }
99
    else
97
    else
100
    {
98
    {
101
        log.add("CanDebugPort is not defined, can not start CanDebug socket\n");
99
        log.add("CanDebugPort is not defined, can not start CanDebug socket\n");
102
    }
100
    }
103
 
101
 
104
    CanNetManager &canMan = CanNetManager::getInstance();
102
    CanNetManager &canMan = CanNetManager::getInstance();
105
    canMan.start();
103
    canMan.start();
106
 
104
 
107
    if (Settings::get("DaemonMode") == "yes")
105
    if (Settings::get("DaemonMode") == "yes")
108
    {
106
    {
109
        log.add("Entering daemon mode...\n");
107
        log.add("Entering daemon mode...\n");
110
        if (daemon(0, 0) == -1)
108
        if (daemon(0, 0) == -1)
111
        {
109
        {
112
            log.addToSyslog("Could not enter daemon mode. Exiting...\n");
110
            log.addToSyslog("Could not enter daemon mode. Exiting...\n");
113
            return cleanUp();
111
            return cleanUp();
114
        }
112
        }
115
    }
113
    }
116
 
114
 
117
    vm.join();
115
    vm.join();
118
 
116
 
119
    return cleanUp();
117
    return cleanUp();
120
}
118
}
121
 
119
 
122
int cleanUp()
120
int cleanUp()
123
{
121
{
124
    Logger &log = Logger::getInstance();
122
    Logger &log = Logger::getInstance();
125
 
123
 
126
    if (!cleanUpRunning)
124
    if (!cleanUpRunning)
127
    {
125
    {
128
        cleanUpRunning = true;
126
        cleanUpRunning = true;
129
 
127
 
130
        log.add("\n");
128
        log.add("\n");
131
        log.addToSyslog("Thank you for using Atom. Goodbye!\n");
129
        log.addToSyslog("Thank you for using Atom. Goodbye!\n");
132
 
130
 
133
        CanDebug::deleteInstance();
131
        CanDebug::deleteInstance();
134
        VirtualMachine::deleteInstance();
132
        VirtualMachine::deleteInstance();
135
        CanNetManager::deleteInstance();
133
        CanNetManager::deleteInstance();
136
        Logger::deleteInstance();
134
        Logger::deleteInstance();
137
    }
135
    }
138
    else
136
    else
139
    {
137
    {
140
        log.addToSyslog("Something has gone wrong clean up is already running!\n");
138
        log.addToSyslog("Something has gone wrong clean up is already running!\n");
141
        return EXIT_FAILURE;
139
        return EXIT_FAILURE;
142
    }
140
    }
143
 
141
 
144
    return EXIT_SUCCESS;
142
    return EXIT_SUCCESS;
145
}
143
}
146
 
144
 
147
void handler(int status)
145
void handler(int status)
148
{
146
{
149
    string signalName = "Unknown";
147
    string signalName = "Unknown";
150
 
148
 
151
    switch (status)
149
    switch (status)
152
    {
150
    {
153
    case SIGTERM:
151
    case SIGTERM:
154
    signalName = "Terminate";
152
    signalName = "Terminate";
155
    break;
153
    break;
156
 
154
 
157
    case SIGINT:
155
    case SIGINT:
158
    signalName = "Interupt";
156
    signalName = "Interupt";
159
    break;
157
    break;
160
 
158
 
161
    case SIGQUIT:
159
    case SIGQUIT:
162
    signalName = "Quit";
160
    signalName = "Quit";
163
    break;
161
    break;
164
 
162
 
165
    case SIGABRT:
163
    case SIGABRT:
166
    signalName = "Abort";
164
    signalName = "Abort";
167
    break;
165
    break;
168
 
166
 
169
    case SIGIO:
167
    case SIGIO:
170
    signalName = "I/O";
168
    signalName = "I/O";
171
    break;
169
    break;
172
 
170
 
173
    case SIGPIPE:
171
    case SIGPIPE:
174
    signalName = "Pipe";
172
    signalName = "Pipe";
175
    break;
173
    break;
176
    }
174
    }
177
 
175
 
178
    Logger &log = Logger::getInstance();
176
    Logger &log = Logger::getInstance();
179
    log.addToSyslog("Received signal " + signalName + "(" + itos(status) + ")\n");
177
    log.addToSyslog("Received signal " + signalName + "(" + itos(status) + ")\n");
180
 
178
 
181
    if (status == SIGPIPE)
179
    if (status == SIGPIPE)
182
    {
180
    {
183
        return;
181
        return;
184
    }
182
    }
185
 
183
 
186
    exit(cleanUp());
184
    exit(cleanUp());
187
}
185
}
188
 
186