Subversion Repositories HomeAutomation

Rev

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

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