-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHub.cpp
More file actions
115 lines (85 loc) · 2.42 KB
/
Copy pathHub.cpp
File metadata and controls
115 lines (85 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "Hub.h"
#include "HubAPI.h"
#include "safeQ.h"
#include "Agent.h"
#include "event.h"
#include "Loader.h"
#include "configure.h"
#include <string>
#include <memory>
#include <sstream>
Hub::Hub(){
}
Hub::~Hub(){
}
void Hub::Notify(){
while(!(h_smq.GetQueue().empty())){
shared_ptr<Event> receivedEvent = h_smq.ReadEvent();
string eventKey = GenerateEventKey(receivedEvent);
map<string, AgentsByRoom>::iterator it = h_subscribedAgents.find(eventKey);
if(it == h_subscribedAgents.end()){
return;
}
shared_ptr<multimap<string, shared_ptr<Agent>>> mmap;
mmap = it->second;
multimap<string, shared_ptr<Agent>>::iterator mmit = mmap->begin();
while(((mmit->first).compare(receivedEvent->GetEventRoom()) == 0) ||((mmit->first).compare("0") == 0)){
(mmit->second)->GetNotification(receivedEvent);
++mmit;
}
}
}
string Hub::GenerateEventKey(shared_ptr<Event> event){
string eventKey = event->GetEventFloor() + event->GetEventTopic();
return eventKey;
}
void Hub::Publish(shared_ptr<Event> event){
h_smq.SendEvent(event);
}
void Hub::Subscribe(shared_ptr<Agent> _agent){
string agRoom, agFloor, agConf, Bigtoken, Bigevent, Bigfrom, token, event, from, tmp, longConf;
agFloor = _agent->GetAgentFloor();
agConf = _agent->GetAgentConfig();
istringstream bigConf(agConf);
while(bigConf){
getline(bigConf, Bigtoken, ',');
getline(bigConf, Bigevent, ',');
getline(bigConf, Bigfrom, ',');
}
istringstream bigTokss(Bigtoken);
while(bigTokss){
getline(bigTokss, token, ':');
}
istringstream bigEvess(Bigevent);
while(bigEvess){
getline(bigEvess, event, ':');
}
istringstream bigFross(Bigfrom);
while(bigFross){
getline(bigFross, from, ':');
}
if(from.empty()){
from = "0";
}
string eventKey = agFloor + event;
shared_ptr<multimap<string, shared_ptr<Agent>>> mmap (new multimap<string, shared_ptr<Agent>>);
mmap->insert(pair<string, shared_ptr<Agent>>(from, _agent));
h_subscribedAgents.insert(pair<string, shared_ptr<multimap<string, shared_ptr<Agent>>>>(eventKey, mmap));
}
void Hub::HubRun(){
Parser parser("config.txt");
while(parser.m_configFilePath){
shared_ptr<Configuration> configInfo = parser.ParseNextSection();
if (!configInfo){
continue;
}
shared_ptr<Agent> agent = h_loader.CreateInitAgent(configInfo, this);
h_agentsVec.push_back(agent);
}
for (size_t i = 0; i< h_agentsVec.size(); ++i){
h_agentsVec[i]->Run();
}
while (1){
Notify();
}
}