Subversion Repositories HomeAutomation

Rev

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

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