Subversion Repositories HomeAutomation

Rev

Rev 1196 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

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