Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1598 runge 1
/*
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 */
1592 runge 20
 
1599 runge 21
#include <signal.h>
22
 
1594 runge 23
#include <boost/program_options.hpp>
1599 runge 24
#include <boost/thread/mutex.hpp>
25
#include <boost/thread/condition.hpp>
26
#include <boost/thread/locks.hpp>
1592 runge 27
 
1599 runge 28
#include "logging/Logger.h"
1592 runge 29
#include "net/Manager.h"
1593 runge 30
#include "timer/Manager.h"
1594 runge 31
#include "config/Manager.h"
1599 runge 32
#include "can/Protocol.h"
33
#include "broker/Manager.h"
34
#include "can/Manager.h"
1596 runge 35
#include "can/Network.h"
36
#include "can/Monitor.h"
1601 runge 37
#include "vm/Manager.h"
1596 runge 38
 
1601 runge 39
#include "vm/plugin/System.h"
40
#include "vm/plugin/Timer.h"
1602 runge 41
#include "vm/plugin/Module.h"
1601 runge 42
 
1592 runge 43
using namespace atom;
1593 runge 44
 
1596 runge 45
logging::Logger LOG("Main");
1599 runge 46
std::vector<broker::Subscriber::Pointer> subscribers;
47
boost::condition on_message_condition;
48
boost::mutex guard_mutex;
1592 runge 49
 
1599 runge 50
void Handler(int status);
1598 runge 51
void CleanUp();
1592 runge 52
 
53
int main(int argc, char **argv)
54
{
1599 runge 55
    LOG.Info("Atom version 1.5.0 starting...");
56
    LOG.Info("Written by Mattias Runge 2010.");
57
    LOG.Info("Released under GPL version 2.");
58
 
59
    signal(SIGTERM, Handler);
60
    signal(SIGINT, Handler);
61
    signal(SIGQUIT, Handler);
62
    signal(SIGABRT, Handler);
63
    signal(SIGPIPE, Handler);
64
 
65
    config::Manager::Create();
66
 
1598 runge 67
    if (!config::Manager::Instance()->Set(argc, argv))
1594 runge 68
    {
1598 runge 69
        CleanUp();
70
        return EXIT_SUCCESS;
1594 runge 71
    }
72
 
1599 runge 73
    if (config::Manager::Instance()->Exist("LogFile"))
74
    {
75
        logging::Logger::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
76
    }
77
 
78
    if (config::Manager::Instance()->Exist("LogLevel"))
79
    {
80
        logging::Logger::SetLevel((logging::Logger::Level)config::Manager::Instance()->GetAsInt("LogLevel"));
81
    }
82
 
83
    if (config::Manager::Instance()->Exist("daemon"))
84
    {
85
        LOG.Info("Entering daemon mode...");
86
 
87
        if (daemon(0, 0) == -1)
88
        {
89
            LOG.Error("Could not enter daemon mode. Exiting...");
90
            CleanUp();
91
            return EXIT_FAILURE;
92
        }
93
 
94
        LOG.Info("Deamon mode entered successfully!");
95
    }
96
 
97
    timer::Manager::Create();
98
    broker::Manager::Create();
99
    net::Manager::Create();
100
    can::Protocol::Create();
101
    can::Manager::Create();
1601 runge 102
    vm::Manager::Create();
103
 
1598 runge 104
    if (config::Manager::Instance()->Exist("ProtocolFile"))
1596 runge 105
    {
1598 runge 106
        can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile"));
1596 runge 107
    }
108
 
1598 runge 109
    if (config::Manager::Instance()->Exist("MonitorPort"))
1594 runge 110
    {
1598 runge 111
        subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort"))));  
1594 runge 112
    }
113
 
1598 runge 114
    subscribers.push_back(can::Manager::Instance());  
115
 
116
    if (config::Manager::Instance()->Exist("CanNet"))
1594 runge 117
    {
1598 runge 118
        type::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
1594 runge 119
 
1598 runge 120
        for (int n = 0; n < cannetworks.size(); n++)
1594 runge 121
        {
1598 runge 122
            subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));            
1594 runge 123
        }
124
    }
1601 runge 125
 
126
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System()));
127
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer()));
1602 runge 128
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module()));
1601 runge 129
 
130
    if (config::Manager::Instance()->Exist("ScriptPath"))
131
    {
132
        vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"));
133
    }
1596 runge 134
 
1599 runge 135
    LOG.Info("Initialization complete.");
1596 runge 136
 
1599 runge 137
    boost::mutex::scoped_lock guard(guard_mutex);
138
    on_message_condition.wait(guard);
1594 runge 139
 
1599 runge 140
    LOG.Info("Cleaning up...");
141
 
1598 runge 142
    CleanUp();
1594 runge 143
 
1599 runge 144
    LOG.Info("Thank you for using Atom. Goodbye!");
145
 
1598 runge 146
    return EXIT_SUCCESS;
1592 runge 147
}
1598 runge 148
 
149
void CleanUp()
150
{
1599 runge 151
    subscribers.clear();
1598 runge 152
    config::Manager::Delete();
1599 runge 153
    timer::Manager::Delete();
154
    can::Manager::Delete();
1602 runge 155
    vm::Manager::Delete();
1599 runge 156
    can::Protocol::Delete();
157
    broker::Manager::Delete();
1598 runge 158
    net::Manager::Delete();
1599 runge 159
}
160
 
161
void Handler(int status)
162
{
163
    std::string signal_name = "Unknown";
164
 
165
    switch (status)
166
    {
167
        case SIGTERM:
1600 runge 168
        {
1599 runge 169
            signal_name = "Terminate";
170
            break;
1600 runge 171
        }  
1599 runge 172
        case SIGINT:
1600 runge 173
        {
1599 runge 174
            signal_name = "Interupt";
175
            break;
1600 runge 176
        }  
1599 runge 177
        case SIGQUIT:
1600 runge 178
        {
1599 runge 179
            signal_name = "Quit";
180
            break;
1600 runge 181
        }  
1599 runge 182
        case SIGABRT:
1600 runge 183
        {
1599 runge 184
            signal_name = "Abort";
185
            break;
1600 runge 186
        }  
1599 runge 187
        case SIGIO:
1600 runge 188
        {
1599 runge 189
            signal_name = "I/O";
190
            break;
1600 runge 191
        }  
1599 runge 192
        case SIGPIPE:
1600 runge 193
        {
1599 runge 194
            signal_name = "Pipe";
195
            break;
1600 runge 196
        }
1599 runge 197
    }
198
 
199
    LOG.Debug("Received signal " + signal_name + "(" + boost::lexical_cast<std::string>(status) + ").");
200
 
1600 runge 201
    if (status != SIGPIPE)
1599 runge 202
    {
1600 runge 203
        on_message_condition.notify_all();
1599 runge 204
    }
1598 runge 205
}