Subversion Repositories HomeAutomation

Rev

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

Rev 969 Rev 976
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
 
24
 
25
#include <stdlib.h>
25
#include <stdlib.h>
26
#include <iostream>
26
#include <iostream>
27
#include <signal.h>
27
#include <signal.h>
28
 
28
 
29
#include "version.h"
29
#include "version.h"
30
#include "Tools/tools.h"
30
#include "Tools/tools.h"
31
#include "SyslogStream/syslogstream.h"
31
#include "SyslogStream/syslogstream.h"
32
#include "Settings/settings.h"
32
#include "Settings/settings.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
 
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 " << 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
 
68
 
69
    VirtualMachine &vm = VirtualMachine::getInstance();
69
    VirtualMachine &vm = VirtualMachine::getInstance();
70
 
70
 
71
    vm.start();
71
    vm.start();
72
 
72
 
73
    if (Settings::get("DaemonMode") == "yes")
73
    if (Settings::get("DaemonMode") == "yes")
74
    {
74
    {
75
        slog << "Entering daemon mode...\n";
75
        slog << "Entering daemon mode...\n";
76
        if (daemon(0, 0) == -1)
76
        if (daemon(0, 0) == -1)
77
        {
77
        {
78
            slog << "Could not enter daemon mode. Exiting...\n";
78
            slog << "Could not enter daemon mode. Exiting...\n";
79
            return cleanUp();
79
            return cleanUp();
80
        }
80
        }
81
    }
81
    }
82
 
82
 
83
    CanNetManager &canMan = CanNetManager::getInstance();
83
    CanNetManager &canMan = CanNetManager::getInstance();
84
 
84
 
85
    canMan.openChannel();
85
    canMan.openChannel();
86
 
86
 
87
    return cleanUp();
87
    return cleanUp();
88
}
88
}
89
 
89
 
90
int cleanUp()
90
int cleanUp()
91
{
91
{
92
    SyslogStream &slog = SyslogStream::getInstance();
92
    SyslogStream &slog = SyslogStream::getInstance();
93
 
93
 
94
    slog << "\nGoodbye!\n";
94
    slog << "\nGoodbye!\n";
95
   
95
   
96
    CanNetManager::deleteInstance();
96
    CanNetManager::deleteInstance();
97
    VirtualMachine::deleteInstance();
97
    VirtualMachine::deleteInstance();
98
    SyslogStream::deleteInstance();
98
    SyslogStream::deleteInstance();
99
 
99
 
100
    return EXIT_SUCCESS;
100
    return EXIT_SUCCESS;
101
}
101
}
102
 
102
 
103
void handler(int status)
103
void handler(int status)
104
{
104
{
105
    //cout << "In the handler function. Status: " << status << endl;
105
    //cout << "In the handler function. Status: " << status << endl;
106
 
106
 
107
/*
107
/*
108
    cout << "SIGTERM: " << SIGTERM << endl;
108
    cout << "SIGTERM: " << SIGTERM << endl;
109
    cout << "SIGINT: " << SIGINT << endl;
109
    cout << "SIGINT: " << SIGINT << endl;
110
    cout << "SIGQUIT: " << SIGQUIT << endl;
110
    cout << "SIGQUIT: " << SIGQUIT << endl;
111
    cout << "SIGABRT: " << SIGABRT << endl;
111
    cout << "SIGABRT: " << SIGABRT << endl;
112
    cout << "SIGIO: " << SIGIO << endl;
112
    cout << "SIGIO: " << SIGIO << endl;
113
    cout << "SIGPIPE: " << SIGPIPE << endl;
113
    cout << "SIGPIPE: " << SIGPIPE << endl;
114
 
114
 
115
*/
115
*/
116
    cleanUp();
116
    cleanUp();
117
 
117
 
118
    /*The SIGTERM signal tells a process to exit, so we exit. You can't make a
118
    /*The SIGTERM signal tells a process to exit, so we exit. You can't make a
119
    process invincible by ignoreing SIGTERM as there are still the
119
    process invincible by ignoreing SIGTERM as there are still the
120
    SIGKILL and SIGSTOP signals which you can't set a handler for (catch) or
120
    SIGKILL and SIGSTOP signals which you can't set a handler for (catch) or
121
    ignore*/
121
    ignore*/
122
    exit(EXIT_SUCCESS);
122
    exit(EXIT_SUCCESS);
123
}
123
}
124
 
124