Subversion Repositories HomeAutomation

Rev

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

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