Subversion Repositories HomeAutomation

Rev

Rev 1598 | Rev 1600 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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