Subversion Repositories HomeAutomation

Rev

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

Rev 1914 Rev 1916
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 "Manager.h"
21
#include "Manager.h"
22
 
22
 
23
#include <boost/lexical_cast.hpp>
23
#include <boost/lexical_cast.hpp>
24
 
24
 
25
#include "broker/Manager.h"
25
#include "broker/Manager.h"
26
#include "broker/Message.h"
26
#include "broker/Message.h"
27
#include "common/common.h"
27
#include "common/common.h"
28
#include "timer/Manager.h"
28
#include "timer/Manager.h"
29
#include "storage/Manager.h"
29
#include "storage/Manager.h"
30
 
30
 
31
#include "can/Message.h"
31
#include "can/Message.h"
32
#include "can/CanDaemon.cpp"
-
 
33
 
32
 
34
namespace atom {
33
namespace atom {
35
namespace control {
34
namespace control {
36
   
35
   
37
Manager::Pointer Manager::instance_;
36
Manager::Pointer Manager::instance_;
38
 
37
 
39
Manager::Manager() : broker::Subscriber(false), LOG("control::Manager")
38
Manager::Manager() : broker::Subscriber(false), LOG("control::Manager")
40
{
39
{
41
    Node::SetupStateMachine();
40
    Node::SetupStateMachine();
42
   
41
   
43
    this->active_programming_node_id_ = "";
42
    this->active_programming_node_id_ = "";
44
   
43
   
45
    // Nyquist–Shannon sampling theorem state that we need to double the time, modules send every 10 seconds
44
    // Nyquist–Shannon sampling theorem state that we need to double the time, modules send every 10 seconds
46
    this->timer_id_ = timer::Manager::Instance()->SetTimer(20000, true);
45
    this->timer_id_ = timer::Manager::Instance()->SetTimer(20000, true);
47
}
46
}
48
 
47
 
49
Manager::~Manager()
48
Manager::~Manager()
50
{
49
{
51
}
50
}
52
 
51
 
53
Manager::Pointer Manager::Instance()
52
Manager::Pointer Manager::Instance()
54
{
53
{
55
    return Manager::instance_;
54
    return Manager::instance_;
56
}
55
}
57
 
56
 
58
void Manager::Create()
57
void Manager::Create()
59
{
58
{
60
    Manager::instance_ = Manager::Pointer(new Manager());
59
    Manager::instance_ = Manager::Pointer(new Manager());
61
}
60
}
62
 
61
 
63
void Manager::Delete()
62
void Manager::Delete()
64
{
63
{
65
    Manager::instance_.reset();
64
    Manager::instance_.reset();
66
}
65
}
67
 
66
 
68
void Manager::ConnectSlotNode(const SignalOnNodeChange::slot_type& signal_on_node_change_)
67
void Manager::ConnectSlotNode(const SignalOnNodeChange::slot_type& signal_on_node_change_)
69
{
68
{
70
    this->signal_on_node_change_.connect(signal_on_node_change_);
69
    this->signal_on_node_change_.connect(signal_on_node_change_);
71
}
70
}
72
 
71
 
73
void Manager::ConnectSlotModule(const SignalOnModuleChange::slot_type& slot_on_module_change)
72
void Manager::ConnectSlotModule(const SignalOnModuleChange::slot_type& slot_on_module_change)
74
{
73
{
75
    this->signal_on_module_change_.connect(slot_on_module_change);
74
    this->signal_on_module_change_.connect(slot_on_module_change);
76
}
75
}
77
 
76
 
78
void Manager::ConnectSlotModule(const SignalOnModuleMessage::slot_type& slot_on_module_message)
77
void Manager::ConnectSlotModule(const SignalOnModuleMessage::slot_type& slot_on_module_message)
79
{
78
{
80
    this->signal_on_module_message_.connect(slot_on_module_message);
79
    this->signal_on_module_message_.connect(slot_on_module_message);
81
}
80
}
82
 
81
 
83
void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
82
void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
84
{
83
{
85
    if (timer_id != this->timer_id_)
84
    if (timer_id != this->timer_id_)
86
    {
85
    {
87
        return;
86
        return;
88
    }
87
    }
89
   
88
   
90
    for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
89
    for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
91
    {
90
    {
92
        if (!it->second->CheckTimeout())
91
        if (!it->second->CheckTimeout())
93
        {
92
        {
94
            LOG.Info("Node " +it->second->GetId() + " has not sent anything in a long time, setting offline.");
93
            LOG.Info("Node " +it->second->GetId() + " has not sent anything in a long time, setting offline.");
95
            this->RemoveModules(it->second->GetId());
94
            this->RemoveModules(it->second->GetId());
96
           
95
           
97
            storage::Manager::Instance()->FlushStore("NodeList");
96
            storage::Manager::Instance()->FlushStore("NodeList");
98
            storage::Manager::Instance()->FlushStore("ModuleList");
97
            storage::Manager::Instance()->FlushStore("ModuleList");
99
        }
98
        }
100
    }
99
    }
101
}
100
}
102
 
101
 
103
void Manager::SlotOnMessageHandler(broker::Message::Pointer message)
102
void Manager::SlotOnMessageHandler(broker::Message::Pointer message)
104
{
103
{
105
    if (message->GetType() == broker::Message::CAN_MESSAGE)
104
    if (message->GetType() == broker::Message::CAN_MESSAGE)
106
    {
105
    {
107
        //LOG.Debug("Message received");
106
        //LOG.Debug("Message received");
108
        can::Message* payload = static_cast<can::Message*>(message->GetPayload().get());
107
        can::Message* payload = static_cast<can::Message*>(message->GetPayload().get());
109
        Node::Pointer node;
108
        Node::Pointer node;
110
       
109
       
111
        if (payload->GetClassName() == "nmt")
110
        if (payload->GetClassName() == "nmt")
112
        {
111
        {
113
            if (payload->GetCommandName() == "Bios_Start")
112
            if (payload->GetCommandName() == "Bios_Start")
114
            {
113
            {
115
                node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
114
                node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
116
                node->Trigger(Node::EVENT_BIOS_START, payload->GetVariables());
115
                node->Trigger(Node::EVENT_BIOS_START, payload->GetVariables());
117
            }
116
            }
118
            else if (payload->GetCommandName() == "App_Start")
117
            else if (payload->GetCommandName() == "App_Start")
119
            {
118
            {
120
                node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
119
                node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
121
                node->Trigger(Node::EVENT_APP_START, payload->GetVariables());
120
                node->Trigger(Node::EVENT_APP_START, payload->GetVariables());
122
            }
121
            }
123
            else if (payload->GetCommandName() == "Heartbeat")
122
            else if (payload->GetCommandName() == "Heartbeat")
124
            {
123
            {
125
                node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
124
                node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
126
                node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables());
125
                node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables());
127
            }
126
            }
128
            else if (payload->GetCommandName() == "Pgm_Ack")
127
            else if (payload->GetCommandName() == "Pgm_Ack")
129
            {
128
            {
130
                node = this->GetNode(this->active_programming_node_id_);
129
                node = this->GetNode(this->active_programming_node_id_);
131
                node->Trigger(Node::EVENT_PGM_ACK, payload->GetVariables());
130
                node->Trigger(Node::EVENT_PGM_ACK, payload->GetVariables());
132
            }
131
            }
133
            else if (payload->GetCommandName() == "Pgm_Nack")
132
            else if (payload->GetCommandName() == "Pgm_Nack")
134
            {
133
            {
135
                node = this->GetNode(this->active_programming_node_id_);
134
                node = this->GetNode(this->active_programming_node_id_);
136
                node->Trigger(Node::EVENT_PGM_NACK, payload->GetVariables());
135
                node->Trigger(Node::EVENT_PGM_NACK, payload->GetVariables());
137
            }
136
            }
138
        }
137
        }
139
        else if (payload->GetDirectionName() == "From_Owner")
138
        else if (payload->GetDirectionName() == "From_Owner")
140
        {
139
        {
141
            Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
140
            Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName());
142
           
141
           
143
            if (payload->GetCommandName() == "List")
142
            if (payload->GetCommandName() == "List")
144
            {
143
            {
145
                node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
144
                node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"])));
146
                node->ResetTimeout();
145
                node->ResetTimeout();
147
               
146
               
148
                module->SetNodeId(node->GetId());
147
                module->SetNodeId(node->GetId());
149
               
148
               
150
                if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
149
                if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"]))
151
                {
150
                {
152
                    node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables());
151
                    node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables());
153
                }
152
                }
154
               
153
               
155
                LOG.Info("Module " + module->GetFullId() + " is available on node " + node->GetId() + ".");
154
                LOG.Info("Module " + module->GetFullId() + " is available on node " + node->GetId() + ".");
156
                this->signal_on_module_change_(module->GetFullId(), true);
155
                this->signal_on_module_change_(module->GetFullId(), true);
157
            }
156
            }
158
            else if (module->GetNodeId() != "")
157
            else if (module->GetNodeId() != "")
159
            {
158
            {
160
                node = this->GetNode(module->GetNodeId());
159
                node = this->GetNode(module->GetNodeId());
161
                node->ResetTimeout();
160
                node->ResetTimeout();
162
               
161
               
163
                this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables());
162
                this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables());
164
            }
163
            }
165
        }
164
        }
166
    }
165
    }
167
}
166
}
168
 
167
 
169
void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state)
168
void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state)
170
{
169
{
171
    if (target_state != Node::STATE_NORM_INITIALIZED)
170
    if (target_state != Node::STATE_NORM_INITIALIZED)
172
    {
171
    {
173
        this->RemoveModules(node_id);
172
        this->RemoveModules(node_id);
174
    }
173
    }
175
   
174
   
176
    if (current_state != target_state)
175
    if (current_state != target_state)
177
    {
176
    {
178
        this->signal_on_node_change_(node_id, (target_state == Node::STATE_NORM_INITIALIZED || target_state == Node::STATE_NORM_LIST || target_state == Node::STATE_NORM_ONLINE));
177
        this->signal_on_node_change_(node_id, (target_state == Node::STATE_NORM_INITIALIZED || target_state == Node::STATE_NORM_LIST || target_state == Node::STATE_NORM_ONLINE));
179
    }
178
    }
180
   
179
   
181
    if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE)
180
    if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE)
182
    {
181
    {
183
        this->active_programming_node_id_ = node_id;
182
        this->active_programming_node_id_ = node_id;
184
    }
183
    }
185
    else if (target_state == Node::STATE_NORM_OFFLINE)
184
    else if (target_state == Node::STATE_NORM_OFFLINE)
186
    {
185
    {
187
        this->active_programming_node_id_ = "";
186
        this->active_programming_node_id_ = "";
188
    }
187
    }
189
}
188
}
190
 
189
 
191
void Manager::SendMessageHandler(std::string full_id, std::string command, common::StringMap variables)
190
void Manager::SendMessageHandler(std::string full_id, std::string command, common::StringMap variables)
192
{
191
{
193
    ModuleList::iterator it = this->modules_.find(full_id);
192
    ModuleList::iterator it = this->modules_.find(full_id);
194
   
193
   
195
    if (it == this->modules_.end())
194
    if (it == this->modules_.end())
196
    {
195
    {
197
        LOG.Warning("Can not send message to " + full_id + ", it is not available");
196
        LOG.Warning("Can not send message to " + full_id + ", it is not available");
198
        return;
197
        return;
199
    }
198
    }
200
   
199
   
201
    try
200
    try
202
    {
201
    {
203
        can::Message* payload = new can::Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command);
202
        can::Message* payload = new can::Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command);
204
        payload->SetVariables(variables);
203
        payload->SetVariables(variables);
205
       
204
       
206
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
205
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
207
        //LOG.Debug("Command " + command + " sent to " + it->second->GetName());
206
        //LOG.Debug("Command " + command + " sent to " + it->second->GetName());
208
    }
207
    }
209
    catch (std::runtime_error& e)
208
    catch (std::runtime_error& e)
210
    {
209
    {
211
        LOG.Error("Failed to send message, " + std::string(e.what()));
210
        LOG.Error("Failed to send message, " + std::string(e.what()));
212
    }
211
    }
213
}
212
}
214
 
213
 
215
void Manager::SendMessage(std::string full_id, std::string command, common::StringMap variables)
214
void Manager::SendMessage(std::string full_id, std::string command, common::StringMap variables)
216
{
215
{
217
    this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables));
216
    this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables));
218
}
217
}
219
 
218
 
220
common::StringList Manager::GetAvailableModules()
219
common::StringList Manager::GetAvailableModules()
221
{
220
{
222
    common::StringList available_modules;
221
    common::StringList available_modules;
223
   
222
   
224
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
223
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
225
    {
224
    {
226
        available_modules.push_back(it->first);
225
        available_modules.push_back(it->first);
227
    }
226
    }
228
   
227
   
229
    return available_modules;
228
    return available_modules;
230
}
229
}
231
 
230
 
232
common::StringList Manager::GetAvailableNodes()
231
common::StringList Manager::GetAvailableNodes()
233
{
232
{
234
    common::StringList available_nodes;
233
    common::StringList available_nodes;
235
   
234
   
236
    for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
235
    for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++)
237
    {
236
    {
238
        std::string node = it->first;
237
        std::string node = it->first;
239
 
238
 
240
        for (ModuleList::iterator it2 = this->modules_.begin(); it2 != this->modules_.end(); it2++)
239
        for (ModuleList::iterator it2 = this->modules_.begin(); it2 != this->modules_.end(); it2++)
241
        {
240
        {
242
            if (it2->second->GetNodeId() == it->first)
241
            if (it2->second->GetNodeId() == it->first)
243
            {
242
            {
244
                node += "," + it2->first;
243
                node += "," + it2->first;
245
            }
244
            }
246
        }
245
        }
247
       
246
       
248
        available_nodes.push_back(node);
247
        available_nodes.push_back(node);
249
    }
248
    }
250
   
249
   
251
    return available_nodes;
250
    return available_nodes;
252
}
251
}
253
 
252
 
254
bool Manager::ResetNode(Node::Id node_id)
253
bool Manager::ResetNode(Node::Id node_id)
255
{
254
{
256
    NodeList::iterator it = this->nodes_.find(node_id);
255
    NodeList::iterator it = this->nodes_.find(node_id);
257
   
256
   
258
    if (it == this->nodes_.end())
257
    if (it == this->nodes_.end())
259
    {
258
    {
260
        LOG.Error("Could not find node " +  node_id);
259
        LOG.Error("Could not find node " +  node_id);
261
        return false;
260
        return false;
262
    }
261
    }
263
   
262
   
264
    it->second->Reset();
263
    it->second->Reset();
265
    return true;
264
    return true;
266
}
265
}
267
 
266
 
268
bool Manager::ProgramNode(Node::Id node_id, bool is_bios, std::string filename)
267
bool Manager::ProgramNode(Node::Id node_id, bool is_bios, std::string filename)
269
{
268
{
270
    NodeList::iterator it = this->nodes_.find(node_id);
269
    NodeList::iterator it = this->nodes_.find(node_id);
271
   
270
   
272
    if (it == this->nodes_.end())
271
    if (it == this->nodes_.end())
273
    {
272
    {
274
        LOG.Error("Could not find node " +  node_id);
273
        LOG.Error("Could not find node " +  node_id);
275
        return false;
274
        return false;
276
    }
275
    }
277
   
276
   
278
    Code::Pointer code = Code::Pointer(new Code());
277
    Code::Pointer code = Code::Pointer(new Code());
279
    if (!code->LoadIntelHexFile(filename))
278
    if (!code->LoadIntelHexFile(filename))
280
    {
279
    {
281
        LOG.Error("Failed to parse " + filename);
280
        LOG.Error("Failed to parse " + filename);
282
        return false;
281
        return false;
283
    }
282
    }
284
   
283
   
285
    if (is_bios)
284
    if (is_bios)
286
    {
285
    {
287
        LOG.Debug("is_bios true");
286
        LOG.Debug("is_bios true");
288
        it->second->ProgramBios(code);
287
        it->second->ProgramBios(code);
289
    }
288
    }
290
    else
289
    else
291
    {
290
    {
292
        LOG.Debug("is_bios false");
291
        LOG.Debug("is_bios false");
293
        it->second->ProgramApplication(code);
292
        it->second->ProgramApplication(code);
294
    }
293
    }
295
   
294
   
296
    return true;
295
    return true;
297
}
296
}
298
 
297
 
299
/* Program node with hexdata as argument */
298
/* Program node with hexdata as argument */
300
bool Manager::ProgramNodeHex(Node::Id node_id, bool is_bios, std::string hex_data)
299
bool Manager::ProgramNodeHex(Node::Id node_id, bool is_bios, std::string hex_data)
301
{
300
{
302
    NodeList::iterator it = this->nodes_.find(node_id);
301
    NodeList::iterator it = this->nodes_.find(node_id);
303
   
302
   
304
    if (it == this->nodes_.end())
303
    if (it == this->nodes_.end())
305
    {
304
    {
306
        LOG.Error("Could not find node " +  node_id);
305
        LOG.Error("Could not find node " +  node_id);
307
        return false;
306
        return false;
308
    }
307
    }
309
   
308
   
310
    Code::Pointer code = Code::Pointer(new Code());
309
    Code::Pointer code = Code::Pointer(new Code());
311
    if (!code->ParseIntelHex(hex_data))
310
    if (!code->ParseIntelHex(hex_data))
312
    {
311
    {
313
        LOG.Error("Failed to parse hexdata " + hex_data);
312
        LOG.Error("Failed to parse hexdata " + hex_data);
314
        return false;
313
        return false;
315
    }
314
    }
316
   
315
   
317
    if (is_bios)
316
    if (is_bios)
318
    {
317
    {
319
        LOG.Debug("is_bios true");
318
        LOG.Debug("is_bios true");
320
        it->second->ProgramBios(code);
319
        it->second->ProgramBios(code);
321
    }
320
    }
322
    else
321
    else
323
    {
322
    {
324
        LOG.Debug("is_bios false");
323
        LOG.Debug("is_bios false");
325
        it->second->ProgramApplication(code);
324
        it->second->ProgramApplication(code);
326
    }
325
    }
327
   
326
   
328
    return true;
327
    return true;
329
}
328
}
330
 
329
 
331
Node::Information Manager::GetNodeInformation(Node::Id node_id)
330
Node::Information Manager::GetNodeInformation(Node::Id node_id)
332
{
331
{
333
    NodeList::iterator it = this->nodes_.find(node_id);
332
    NodeList::iterator it = this->nodes_.find(node_id);
334
   
333
   
335
    if (it == this->nodes_.end())
334
    if (it == this->nodes_.end())
336
    {
335
    {
337
        throw std::runtime_error("No such node exists, " + node_id);
336
        throw std::runtime_error("No such node exists, " + node_id);
338
    }
337
    }
339
       
338
       
340
    return it->second->GetInformation();
339
    return it->second->GetInformation();
341
}
340
}
342
 
341
 
343
Node::Pointer Manager::GetNode(Node::Id node_id)
342
Node::Pointer Manager::GetNode(Node::Id node_id)
344
{
343
{
345
    Node::Pointer node;
344
    Node::Pointer node;
346
    NodeList::iterator it = this->nodes_.find(node_id);
345
    NodeList::iterator it = this->nodes_.find(node_id);
347
   
346
   
348
    if (it == this->nodes_.end())
347
    if (it == this->nodes_.end())
349
    {
348
    {
350
        node = Node::Pointer(new Node(node_id));
349
        node = Node::Pointer(new Node(node_id));
351
        node->ConnectSlots(Node::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(this->tracker_));
350
        node->ConnectSlots(Node::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(this->tracker_));
352
        this->nodes_[node_id] = node;
351
        this->nodes_[node_id] = node;
353
    }
352
    }
354
    else
353
    else
355
    {
354
    {
356
        node = it->second;
355
        node = it->second;
357
    }
356
    }
358
   
357
   
359
    return node;
358
    return node;
360
}
359
}
361
 
360
 
362
Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name)
361
Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name)
363
{
362
{
364
    Module::Pointer module;
363
    Module::Pointer module;
365
    ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name));
364
    ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name));
366
   
365
   
367
    if (it == this->modules_.end())
366
    if (it == this->modules_.end())
368
    {
367
    {
369
        module = Module::Pointer(new Module(module_id, module_name, class_name));
368
        module = Module::Pointer(new Module(module_id, module_name, class_name));
370
        this->modules_[module->GetFullId()] = module;
369
        this->modules_[module->GetFullId()] = module;
371
    }
370
    }
372
    else
371
    else
373
    {
372
    {
374
        module = it->second;
373
        module = it->second;
375
    }
374
    }
376
   
375
   
377
    return module;
376
    return module;
378
}
377
}
379
 
378
 
380
void Manager::RemoveModules(Node::Id node_id)
379
void Manager::RemoveModules(Node::Id node_id)
381
{
380
{
382
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
381
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
383
    {
382
    {
384
        if (it->second->GetNodeId() == node_id)
383
        if (it->second->GetNodeId() == node_id)
385
        {
384
        {
386
            LOG.Info("Module " + it->second->GetFullId() + " is unavailable.");
385
            LOG.Info("Module " + it->second->GetFullId() + " is unavailable.");
387
            this->signal_on_module_change_(it->second->GetFullId(), false);
386
            this->signal_on_module_change_(it->second->GetFullId(), false);
388
           
387
           
389
            this->modules_.erase(it);
388
            this->modules_.erase(it);
390
        }
389
        }
391
    }
390
    }
392
}
391
}
393
 
392
 
394
unsigned int Manager::GetNumberOfModules(Node::Id node_id)
393
unsigned int Manager::GetNumberOfModules(Node::Id node_id)
395
{
394
{
396
    unsigned int number_of_modules = 0;
395
    unsigned int number_of_modules = 0;
397
   
396
   
398
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
397
    for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++)
399
    {
398
    {
400
        if (it->second->GetNodeId() == node_id)
399
        if (it->second->GetNodeId() == node_id)
401
        {
400
        {
402
            number_of_modules++;
401
            number_of_modules++;
403
        }
402
        }
404
    }
403
    }
405
   
404
   
406
    return number_of_modules;
405
    return number_of_modules;
407
}
406
}
408
 
407
 
409
}; // namespace control
408
}; // namespace control
410
}; // namespace atom
409
}; // namespace atom
411
 
410