Subversion Repositories HomeAutomation

Rev

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

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