Subversion Repositories HomeAutomation

Rev

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

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