Subversion Repositories HomeAutomation

Rev

Rev 1024 | Rev 1124 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1024 Rev 1026
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
 
37
 
38
int cleanUp();
38
int cleanUp();
39
void handler(int status);
39
void handler(int status);
40
 
40
 
41
int main(int argc, char** argv)
41
int main(int argc, char** argv)
42
{
42
{
43
    signal(SIGTERM, handler);
43
    signal(SIGTERM, handler);
44
    signal(SIGINT, handler);
44
    signal(SIGINT, handler);
45
    signal(SIGQUIT, handler);
45
    signal(SIGQUIT, handler);
46
    signal(SIGABRT, handler);
46
    signal(SIGABRT, handler);
47
    signal(SIGPIPE, handler);
47
    signal(SIGPIPE, handler);
48
 
48
 
49
    Logger &log = Logger::getInstance();
49
    Logger &log = Logger::getInstance();
50
 
50
 
51
    log.addToSyslog("Atom " + ftos(VERSION) + " starting...\n");
51
    log.addToSyslog("Atom " + ftos(VERSION) + " starting...\n");
52
    log.add("Written by Mattias Runge 2008-2009\n\n");
52
    log.add("Written by Mattias Runge 2008-2009\n\n");
53
 
53
 
54
    if (file_exists("/etc/atom.conf"))
54
    if (file_exists("/etc/atom.conf"))
55
    {
55
    {
56
        if (!Settings::read("/etc/atom.conf"))
56
        if (!Settings::read("/etc/atom.conf"))
57
        {
57
        {
58
            return cleanUp();
58
            return cleanUp();
59
        }
59
        }
60
    }
60
    }
61
    else
61
    else
62
    {
62
    {
63
        if (!Settings::read("atom.conf"))
63
        if (!Settings::read("atom.conf"))
64
        {
64
        {
65
            return cleanUp();
65
            return cleanUp();
66
        }
66
        }
67
    }
67
    }
68
 
68
 
69
    string logfile = Settings::get("Logfile");
69
    string logfile = Settings::get("Logfile");
70
    if (logfile != "")
70
    if (logfile != "")
71
    {
71
    {
72
        if (!log.open(logfile))
72
        if (!log.open(logfile))
73
        {
73
        {
74
            log.addToSyslog("Could not open logfile, " + logfile + "\n");
74
            log.addToSyslog("Could not open logfile, " + logfile + "\n");
75
        }
75
        }
76
        else
76
        else
77
        {
77
        {
78
            log.add("Logging to " + logfile + "\n");
78
            log.add("Logging to " + logfile + "\n");
79
        }
79
        }
80
    }
80
    }
81
    else
81
    else
82
    {
82
    {
83
        log.add("Logfile is not defined, logging will be done to syslog and console only\n");
83
        log.add("Logfile is not defined, logging will be done to syslog and console only\n");
84
    }
84
    }
85
 
85
 
86
    VirtualMachine &vm = VirtualMachine::getInstance();
86
    VirtualMachine &vm = VirtualMachine::getInstance();
87
    vm.start();
87
    vm.start();
88
 
88
 
89
    string canDebugPort = Settings::get("CanDebugPort");
89
    string canDebugPort = Settings::get("CanDebugPort");
90
    if (canDebugPort != "")
90
    if (canDebugPort != "")
91
    {
91
    {
92
        CanDebug &canDebug = CanDebug::getInstance();
92
        CanDebug &canDebug = CanDebug::getInstance();
93
        canDebug.setSettings(stoi(canDebugPort), "CanDebug");
93
        canDebug.setSettings(stoi(canDebugPort), "CanDebug");
94
        canDebug.start();
94
        canDebug.start();
95
    }
95
    }
96
    else
96
    else
97
    {
97
    {
98
        log.add("CanDebugPort is not defined, can not start CanDebug socket\n");
98
        log.add("CanDebugPort is not defined, can not start CanDebug socket\n");
99
    }
99
    }
100
 
100
 
101
    CanNetManager &canMan = CanNetManager::getInstance();
101
    CanNetManager &canMan = CanNetManager::getInstance();
102
    canMan.start();
102
    canMan.start();
103
 
103
 
104
    if (Settings::get("DaemonMode") == "yes")
104
    if (Settings::get("DaemonMode") == "yes")
105
    {
105
    {
106
        log.add("Entering daemon mode...\n");
106
        log.add("Entering daemon mode...\n");
107
        if (daemon(0, 0) == -1)
107
        if (daemon(0, 0) == -1)
108
        {
108
        {
109
            log.addToSyslog("Could not enter daemon mode. Exiting...\n");
109
            log.addToSyslog("Could not enter daemon mode. Exiting...\n");
110
            return cleanUp();
110
            return cleanUp();
111
        }
111
        }
112
    }
112
    }
113
 
113
 
114
    vm.join();
114
    vm.join();
115
 
115
 
116
    return cleanUp();
116
    return cleanUp();
117
}
117
}
118
 
118
 
119
int cleanUp()
119
int cleanUp()
120
{
120
{
121
    Logger &log = Logger::getInstance();
121
    Logger &log = Logger::getInstance();
122
 
122
 
123
    log.add("\n");
123
    log.add("\n");
124
    log.addToSyslog("Thank you for using Atom. Goodbye!\n");
124
    log.addToSyslog("Thank you for using Atom. Goodbye!\n");
125
 
125
 
126
    CanDebug::deleteInstance();
126
    CanDebug::deleteInstance();
127
    VirtualMachine::deleteInstance();
127
    VirtualMachine::deleteInstance();
128
    CanNetManager::deleteInstance();
128
    CanNetManager::deleteInstance();
129
    Logger::deleteInstance();
129
    Logger::deleteInstance();
130
 
130
 
131
    return EXIT_SUCCESS;
131
    return EXIT_SUCCESS;
132
}
132
}
133
 
133
 
134
void handler(int status)
134
void handler(int status)
135
{
135
{
136
    exit(cleanUp());
136
    exit(cleanUp());
137
}
137
}
138
 
138