Subversion Repositories HomeAutomation

Rev

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

Rev 981 Rev 997
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 "SyslogStream/syslogstream.h"
30
#include "SyslogStream/syslogstream.h"
31
#include "Settings/settings.h"
31
#include "Settings/settings.h"
32
#include "CanNet/cannetmanager.h"
32
#include "CanNet/cannetmanager.h"
33
#include "Socket/asyncsocket.h"
33
#include "Socket/asyncsocket.h"
34
#include "VM/virtualmachine.h"
34
#include "VM/virtualmachine.h"
35
#include "CanNet/candebug.h"
35
#include "CanNet/candebug.h"
36
 
36
 
37
int cleanUp();
37
int cleanUp();
38
void handler(int status);
38
void handler(int status);
39
 
39
 
40
int main(int argc, char** argv)
40
int main(int argc, char** argv)
41
{
41
{
42
    signal(SIGTERM, handler);
42
    signal(SIGTERM, handler);
43
    signal(SIGINT, handler);
43
    signal(SIGINT, handler);
44
    signal(SIGQUIT, handler);
44
    signal(SIGQUIT, handler);
45
    signal(SIGABRT, handler);
45
    signal(SIGABRT, handler);
46
    signal(SIGPIPE, handler);
46
    signal(SIGPIPE, handler);
47
 
47
 
48
    SyslogStream &slog = SyslogStream::getInstance();
48
    SyslogStream &slog = SyslogStream::getInstance();
49
 
49
 
50
    slog << "Atom " + ftos(VERSION) + " starting...\n";
50
    slog << "Atom " + ftos(VERSION) + " starting...\n";
51
    slog << "Written by Mattias Runge 2008\n\n";
51
    slog << "Written by Mattias Runge 2008\n\n";
52
 
52
 
53
    if (file_exists("/etc/atom.conf"))
53
    if (file_exists("/etc/atom.conf"))
54
    {
54
    {
55
        if (!Settings::read("/etc/atom.conf"))
55
        if (!Settings::read("/etc/atom.conf"))
56
        {
56
        {
57
            return cleanUp();
57
            return cleanUp();
58
        }
58
        }
59
    }
59
    }
60
    else
60
    else
61
    {
61
    {
62
        if (!Settings::read("atom.conf"))
62
        if (!Settings::read("atom.conf"))
63
        {
63
        {
64
            return cleanUp();
64
            return cleanUp();
65
        }
65
        }
66
    }
66
    }
67
 
67
 
68
    VirtualMachine &vm = VirtualMachine::getInstance();
68
    VirtualMachine &vm = VirtualMachine::getInstance();
69
    vm.start();
69
    vm.start();
70
 
70
 
71
    CanDebug &canDebug = CanDebug::getInstance();
71
    CanDebug &canDebug = CanDebug::getInstance();
72
    canDebug.start();
72
    canDebug.start();
73
 
73
 
74
    if (Settings::get("DaemonMode") == "yes")
74
    if (Settings::get("DaemonMode") == "yes")
75
    {
75
    {
76
        slog << "Entering daemon mode...\n";
76
        slog << "Entering daemon mode...\n";
77
        if (daemon(0, 0) == -1)
77
        if (daemon(0, 0) == -1)
78
        {
78
        {
79
            slog << "Could not enter daemon mode. Exiting...\n";
79
            slog << "Could not enter daemon mode. Exiting...\n";
80
            return cleanUp();
80
            return cleanUp();
81
        }
81
        }
82
    }
82
    }
83
 
83
 
84
 
84
 
85
 
85
 
86
    CanNetManager &canMan = CanNetManager::getInstance();
86
    CanNetManager &canMan = CanNetManager::getInstance();
87
 
87
 
88
    canMan.openChannel();
88
    canMan.openChannel();
89
 
89
 
90
    return cleanUp();
90
    return cleanUp();
91
}
91
}
92
 
92
 
93
int cleanUp()
93
int cleanUp()
94
{
94
{
95
    SyslogStream &slog = SyslogStream::getInstance();
95
    SyslogStream &slog = SyslogStream::getInstance();
96
 
96
 
97
    slog << "\n";
97
    slog << "\n";
98
    slog << "Goodbye!\n";
98
    slog << "Goodbye!\n";
99
 
99
 
100
    CanDebug::deleteInstance();
100
    CanDebug::deleteInstance();
101
    CanNetManager::deleteInstance();
-
 
102
    VirtualMachine::deleteInstance();
101
    VirtualMachine::deleteInstance();
-
 
102
    CanNetManager::deleteInstance();
103
    SyslogStream::deleteInstance();
103
    SyslogStream::deleteInstance();
104
 
104
 
105
    return EXIT_SUCCESS;
105
    return EXIT_SUCCESS;
106
}
106
}
107
 
107
 
108
void handler(int status)
108
void handler(int status)
109
{
109
{
110
    //cout << "In the handler function. Status: " << status << endl;
110
    //cout << "In the handler function. Status: " << status << endl;
111
 
111
 
112
/*
112
/*
113
    cout << "SIGTERM: " << SIGTERM << endl;
113
    cout << "SIGTERM: " << SIGTERM << endl;
114
    cout << "SIGINT: " << SIGINT << endl;
114
    cout << "SIGINT: " << SIGINT << endl;
115
    cout << "SIGQUIT: " << SIGQUIT << endl;
115
    cout << "SIGQUIT: " << SIGQUIT << endl;
116
    cout << "SIGABRT: " << SIGABRT << endl;
116
    cout << "SIGABRT: " << SIGABRT << endl;
117
    cout << "SIGIO: " << SIGIO << endl;
117
    cout << "SIGIO: " << SIGIO << endl;
118
    cout << "SIGPIPE: " << SIGPIPE << endl;
118
    cout << "SIGPIPE: " << SIGPIPE << endl;
119
 
119
 
120
*/
120
*/
121
    cleanUp();
121
    cleanUp();
122
 
122
 
123
    /*The SIGTERM signal tells a process to exit, so we exit. You can't make a
123
    /*The SIGTERM signal tells a process to exit, so we exit. You can't make a
124
    process invincible by ignoreing SIGTERM as there are still the
124
    process invincible by ignoreing SIGTERM as there are still the
125
    SIGKILL and SIGSTOP signals which you can't set a handler for (catch) or
125
    SIGKILL and SIGSTOP signals which you can't set a handler for (catch) or
126
    ignore*/
126
    ignore*/
127
    exit(EXIT_SUCCESS);
127
    exit(EXIT_SUCCESS);
128
}
128
}
129
 
129