/
Blackboard.cpp
240 lines (205 loc) · 8.12 KB
/
Blackboard.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "Blackboard.hpp"
#include "core/architecture/time/SystemTime.hpp"
namespace Messaging
{
using google::protobuf::Message;
using namespace KSystem::Time;
using std::string;
Blackboard::Blackboard(const std::string& sub_name): EndPoint(sub_name)
{
}
void Blackboard::process_messages()
{
cleanup();
// cout << "SIZE OF BUFFER = " << buf->size() << endl;
// _blk.process_messages();
// cout << "After returns " << endl
std::vector<MessageEntry> msg = EndPoint::remove();
std::vector<MessageEntry>::iterator it = msg.begin();
signalentry newsig;
brecord nrec;
for(; it != msg.end(); ++it)
{
//std::cout<<"incoming:"<<(*it).msg->GetTypeName()<<(*it).msgclass<<" "<<MessageEntry::DATA<<std::endl;
type_t newtypeid = typeRegistry.registerNew((*it).msg->GetTypeName());
region_index i;
i.tid = (*it).topic;
i.hid = (*it).host;
//std::cout<<"Incoming region:"<<i.hid<<" "<<i.tid<<"<size:"<<allrecords.size()<<std::endl;
disjoint_region &r = allrecords[i];
nrec.msg = (*it).msg;
nrec.timestamp =SystemTime::unwrap((*it).timestamp);
switch ((*it).msgclass)
{
case MessageEntry::STATE:
r.blkstate[newtypeid] = nrec;
break;
case MessageEntry::DATA:
//std::cout<<(*it).msg->GetTypeName()<<i.hid<<std::endl;
if(r.blkdata[newtypeid].size() > 0)
r.blkdata[newtypeid].insert(--r.blkdata[newtypeid].end(), nrec); //Suggest last place to add it
else
r.blkdata[newtypeid].insert(nrec);
break;
case MessageEntry::SIGNAL:
newsig.d = nrec;
newsig.cleared = false;
r.blksignal[newtypeid] = newsig;
break;
default:
break;
/*
case MessageEntry::STATE:
statedata[(*it).msg->GetTypeName()]=(*it);
break;
*/
}
//std::cout<<"Blackboard:"<<(*it).msgclass<<" "<<(*it).msg->GetTypeName()<<std::endl;
}
// cout << "SIZE OF BLACKBOARD " << Subscriber::getName() << world_perception.size() << endl;
}
int Blackboard::cleanup()
{
regions::iterator rit = allrecords.begin();
//KSystem::Time::TimeDuration t;
KSystem::Time::TimeAbsolute now = SystemTime::now();
for(; rit != allrecords.end(); ++rit)
{
disjoint_region &r = (*rit).second;
//Data Structure
datastruct::iterator it = r.blkdata.begin();
for(; it != r.blkdata.end(); ++it)
{
recordlist &q = (*it).second;
if(r.blkdatatimeouts.find((*it).first) == r.blkdatatimeouts.end())
{
if(q.size() > 1 )q.erase(q.begin(), --q.end()); //Keep last only
}
else
{
KSystem::Time::TimeAbsolute timeoutstamp = now - r.blkdatatimeouts[(*it).first];
brecord ar;
ar.timestamp = timeoutstamp;
recordlist::iterator qit = q.upper_bound(ar);
//cout<<i<<endl;
//q.clear();
//std::cout<<(*qit).timestamp.toString()<<std::endl;
//std::cout<<(*qit).msg->GetTypeName()<<std::endl;
if(qit == q.end() && q.size() > 0)--qit;
q.erase(q.begin(), qit);
}
}
//Signal structure
signalstruct::iterator sigit = r.blksignal.begin();
while(sigit != r.blksignal.end())
{
if((*sigit).second.cleared == true)
{
signalstruct::iterator t = sigit++;
r.blksignal.erase(t);
//sigit=sigdata.begin();
}
else
++sigit;
}
}
return 0;
}
void Blackboard::publish_all()
{
EndPoint::publish(topublish);
topublish.clear();
}
void Blackboard::publishData(const google::protobuf::Message & msg, std::string const& topic)
{
MessageEntry nmsg, test;
brecord nrec;
google::protobuf::Message * newptr = msg.New();
newptr->CopyFrom(msg);
nmsg.msg.reset(newptr);
nrec.msg = nmsg.msg;
//std::cout<<"In:"<<&msg;
//cout<<"Copy:"<<nmsg.msg<<endl;
nmsg.host = MessageEntry::HOST_ID_LOCAL_HOST;
TimeAbsolute now =SystemTime::now();
//nmsg.timeoutstamp=now+KSystem::Time::millisec(timeout);
nmsg.timestamp = now.wrapTo<TimeStamp>();
nrec.timestamp = now;
nmsg.topic = Topics::Instance().getId(topic);
//nmsg.publisher=Publisher::getName();
//std::cout<<"Blacboard publish data:"<<msg.GetTypeName()<<std::endl;
nmsg.msgclass = MessageEntry::DATA;
type_t newtypeid = typeRegistry.registerNew(msg.GetTypeName());
region_index i;
i.tid = nmsg.topic;
i.hid = nmsg.host;
disjoint_region &r = allrecords[i];
if(r.blkdata[newtypeid].size() > 0)
r.blkdata[newtypeid].insert(--r.blkdata[newtypeid].end(), nrec); //Suggest last place to add it
else
r.blkdata[newtypeid].insert(nrec);
test = nmsg;
//std::cout<<test.msgclass<<" "<<nmsg.msgclass<<std::endl;
topublish.push_back(nmsg);
//cout<<blkdata[msg.GetTypeName()].size()<<endl;q
}
void Blackboard::publishSignal(const google::protobuf::Message & msg, std::string const& topic)
{
MessageEntry nmsg;
brecord nrec;
google::protobuf::Message * newptr = msg.New();
newptr->CopyFrom(msg);
nmsg.msg.reset(newptr);
nrec.msg = nmsg.msg;
//cout<<"In:"<<&msg;
//cout<<"Copy:"<<nmsg.msg<<endl;
nmsg.host = MessageEntry::HOST_ID_LOCAL_HOST;
TimeAbsolute now =SystemTime::now();
//nmsg.timeoutstamp=now;//Signal, no timeout
nmsg.timestamp = now.wrapTo<TimeStamp>();
nrec.timestamp = now;
nmsg.topic = Topics::Instance().getId(topic);
//nmsg.publisher=Publisher::getName();
nmsg.msgclass = MessageEntry::SIGNAL;
//cout<<msg.GetTypeName()<<":"<<blkdata[msg.GetTypeName()].size()<<endl;
type_t newtypeid = typeRegistry.registerNew(msg.GetTypeName());
region_index i;
i.tid = nmsg.topic;
i.hid = nmsg.host;
disjoint_region &r = allrecords[i];
signalentry newsig;
newsig.d = nrec;
newsig.cleared = false;
r.blksignal[newtypeid] = newsig; //If exists replace
topublish.push_back(nmsg);
//cout<<blkdata[msg.GetTypeName()].size()<<endl;
}
void Blackboard::publishState(const google::protobuf::Message & msg, std::string const& topic)
{
MessageEntry nmsg;
brecord nrec;
google::protobuf::Message * newptr = msg.New();
newptr->CopyFrom(msg);
nmsg.msg.reset(newptr);
nrec.msg = nmsg.msg;
//cout<<"In:"<<&msg;
//cout<<"Copy:"<<nmsg.msg<<endl;
nmsg.host = MessageEntry::HOST_ID_LOCAL_HOST;
TimeAbsolute now =SystemTime::now();
//nmsg.timeoutstamp=now;//State, no timeout :)
nmsg.timestamp = now.wrapTo<TimeStamp>();
nrec.timestamp = now;
nmsg.topic = Topics::Instance().getId(topic);
//nmsg.publisher=Publisher::getName();
nmsg.msgclass = MessageEntry::STATE;
//cout<<msg.GetTypeName()<<":"<<blkdata[msg.GetTypeName()].size()<<endl;
type_t newtypeid = typeRegistry.registerNew(msg.GetTypeName());
region_index i;
i.tid = nmsg.topic;
i.hid = MessageEntry::HOST_ID_LOCAL_HOST;
disjoint_region &r = allrecords[i];
r.blkstate[newtypeid] = nrec; //If exists replace
topublish.push_back(nmsg);
//cout<<blkdata[msg.GetTypeName()].size()<<endl;
}
}