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"
41
 
1592 runge 42
using namespace atom;
1593 runge 43
 
1596 runge 44
logging::Logger LOG("Main");
1599 runge 45
std::vector<broker::Subscriber::Pointer> subscribers;
46
boost::condition on_message_condition;
47
boost::mutex guard_mutex;
1592 runge 48
 
1599 runge 49
void Handler(int status);
1598 runge 50
void CleanUp();
1592 runge 51
 
52
int main(int argc, char **argv)
53
{
1599 runge 54
    LOG.Info("Atom version 1.5.0 starting...");
55
    LOG.Info("Written by Mattias Runge 2010.");
56
    LOG.Info("Released under GPL version 2.");
57
 
58
    signal(SIGTERM, Handler);
59
    signal(SIGINT, Handler);
60
    signal(SIGQUIT, Handler);
61
    signal(SIGABRT, Handler);
62
    signal(SIGPIPE, Handler);
63
 
64
    config::Manager::Create();
65
 
1598 runge 66
    if (!config::Manager::Instance()->Set(argc, argv))
1594 runge 67
    {
1598 runge 68
        CleanUp();
69
        return EXIT_SUCCESS;
1594 runge 70
    }
71
 
1599 runge 72
    if (config::Manager::Instance()->Exist("LogFile"))
73
    {
74
        logging::Logger::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
75
    }
76
 
77
    if (config::Manager::Instance()->Exist("LogLevel"))
78
    {
79
        logging::Logger::SetLevel((logging::Logger::Level)config::Manager::Instance()->GetAsInt("LogLevel"));
80
    }
81
 
82
    if (config::Manager::Instance()->Exist("daemon"))
83
    {
84
        LOG.Info("Entering daemon mode...");
85
 
86
        if (daemon(0, 0) == -1)
87
        {
88
            LOG.Error("Could not enter daemon mode. Exiting...");
89
            CleanUp();
90
            return EXIT_FAILURE;
91
        }
92
 
93
        LOG.Info("Deamon mode entered successfully!");
94
    }
95
 
96
    timer::Manager::Create();
97
    broker::Manager::Create();
98
    net::Manager::Create();
99
    can::Protocol::Create();
100
    can::Manager::Create();
1601 runge 101
    vm::Manager::Create();
102
 
1598 runge 103
    if (config::Manager::Instance()->Exist("ProtocolFile"))
1596 runge 104
    {
1598 runge 105
        can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile"));
1596 runge 106
    }
107
 
1598 runge 108
    if (config::Manager::Instance()->Exist("MonitorPort"))
1594 runge 109
    {
1598 runge 110
        subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort"))));  
1594 runge 111
    }
112
 
1598 runge 113
    subscribers.push_back(can::Manager::Instance());  
114
 
115
    if (config::Manager::Instance()->Exist("CanNet"))
1594 runge 116
    {
1598 runge 117
        type::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
1594 runge 118
 
1598 runge 119
        for (int n = 0; n < cannetworks.size(); n++)
1594 runge 120
        {
1598 runge 121
            subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));            
1594 runge 122
        }
123
    }
1601 runge 124
 
125
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System()));
126
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer()));
127
 
128
    if (config::Manager::Instance()->Exist("ScriptPath"))
129
    {
130
        vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"));
131
    }
1596 runge 132
 
1599 runge 133
    LOG.Info("Initialization complete.");
1596 runge 134
 
1599 runge 135
    boost::mutex::scoped_lock guard(guard_mutex);
136
    on_message_condition.wait(guard);
1594 runge 137
 
1599 runge 138
    LOG.Info("Cleaning up...");
139
 
1598 runge 140
    CleanUp();
1594 runge 141
 
1599 runge 142
    LOG.Info("Thank you for using Atom. Goodbye!");
143
 
1598 runge 144
    return EXIT_SUCCESS;
1592 runge 145
}
1598 runge 146
 
147
void CleanUp()
148
{
1599 runge 149
    subscribers.clear();
1601 runge 150
    vm::Manager::Delete();
1598 runge 151
    config::Manager::Delete();
1599 runge 152
    timer::Manager::Delete();
153
    can::Manager::Delete();
154
    can::Protocol::Delete();
155
    broker::Manager::Delete();
1598 runge 156
    net::Manager::Delete();
1599 runge 157
}
158
 
159
void Handler(int status)
160
{
161
    std::string signal_name = "Unknown";
162
 
163
    switch (status)
164
    {
165
        case SIGTERM:
1600 runge 166
        {
1599 runge 167
            signal_name = "Terminate";
168
            break;
1600 runge 169
        }  
1599 runge 170
        case SIGINT:
1600 runge 171
        {
1599 runge 172
            signal_name = "Interupt";
173
            break;
1600 runge 174
        }  
1599 runge 175
        case SIGQUIT:
1600 runge 176
        {
1599 runge 177
            signal_name = "Quit";
178
            break;
1600 runge 179
        }  
1599 runge 180
        case SIGABRT:
1600 runge 181
        {
1599 runge 182
            signal_name = "Abort";
183
            break;
1600 runge 184
        }  
1599 runge 185
        case SIGIO:
1600 runge 186
        {
1599 runge 187
            signal_name = "I/O";
188
            break;
1600 runge 189
        }  
1599 runge 190
        case SIGPIPE:
1600 runge 191
        {
1599 runge 192
            signal_name = "Pipe";
193
            break;
1600 runge 194
        }
1599 runge 195
    }
196
 
197
    LOG.Debug("Received signal " + signal_name + "(" + boost::lexical_cast<std::string>(status) + ").");
198
 
1600 runge 199
    if (status != SIGPIPE)
1599 runge 200
    {
1600 runge 201
        on_message_condition.notify_all();
1599 runge 202
    }
1598 runge 203
}