Subversion Repositories HomeAutomation

Rev

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

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