Subversion Repositories HomeAutomation

Rev

Rev 1604 | Rev 1609 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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