-
Notifications
You must be signed in to change notification settings - Fork 41
/
Subscription.cpp
288 lines (229 loc) · 8.05 KB
/
Subscription.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
Copyright 2015-2021 Kai Huebl ([email protected])
Lizenziert gemäß Apache Licence Version 2.0 (die „Lizenz“); Nutzung dieser
Datei nur in Übereinstimmung mit der Lizenz erlaubt.
Eine Kopie der Lizenz erhalten Sie auf http://www.apache.org/licenses/LICENSE-2.0.
Sofern nicht gemäß geltendem Recht vorgeschrieben oder schriftlich vereinbart,
erfolgt die Bereitstellung der im Rahmen der Lizenz verbreiteten Software OHNE
GEWÄHR ODER VORBEHALTE – ganz gleich, ob ausdrücklich oder stillschweigend.
Informationen über die jeweiligen Bedingungen für Genehmigungen und Einschränkungen
im Rahmen der Lizenz finden Sie in der Lizenz.
Autor: Kai Huebl ([email protected])
*/
#include "OpcUaStackServer/ServiceSet/Subscription.h"
#include "OpcUaStackCore/StandardDataTypes/DataChangeNotification.h"
using namespace OpcUaStackCore;
namespace OpcUaStackServer
{
uint32_t Subscription::uniqueSubscriptionId_ = 0;
boost::mutex Subscription::mutex_;
uint32_t
Subscription::uniqueSubscriptionId(void)
{
boost::mutex::scoped_lock g(mutex_);
uniqueSubscriptionId_++;
return uniqueSubscriptionId_;
}
Subscription::Subscription(void)
: subscriptionId_(uniqueSubscriptionId())
, slotTimerElement_(boost::make_shared<SlotTimerElement>())
, retransmissionQueue_()
, monitorManager_()
, acknowledgementManager_()
{
monitorManager_.subscriptionId(subscriptionId_);
}
Subscription::~Subscription(void)
{
retransmissionQueue_.clear();
}
uint32_t
Subscription::subscriptionId(void)
{
return subscriptionId_;
}
void
Subscription::publishingInterval(double publishingInterval)
{
publishingInterval_ = publishingInterval;
}
SlotTimerElement::SPtr
Subscription::slotTimerElement(void)
{
return slotTimerElement_;
}
void
Subscription::lifetimeCount(uint32_t lifetimeCount)
{
lifetimeCount_ = lifetimeCount;
actLifetimeCount_ = lifetimeCount;
}
void
Subscription::maxKeepAliveCount(uint32_t maxKeepAliveCount)
{
maxKeepAliveCount_ = maxKeepAliveCount;
actMaxKeepAliveCount_ = maxKeepAliveCount;
}
void
Subscription::ioThread(IOThread* ioThread)
{
ioThread_ = ioThread;
monitorManager_.ioThread(ioThread);
}
void
Subscription::strand(boost::shared_ptr<boost::asio::io_service::strand>& strand)
{
strand_ = strand;
monitorManager_.strand(strand);
}
void
Subscription::informationModel(InformationModel::SPtr& informationModel)
{
monitorManager_.informationModel(informationModel);
}
void
Subscription::forwardGlobalSync(ForwardGlobalSync::SPtr& forwardGlobalSync)
{
monitorManager_.forwardGlobalSync(forwardGlobalSync);
}
OpcUaStatusCode
Subscription::receiveAcknowledgement(uint32_t acknowledgmentNumber)
{
acknowledgementManager_.deleteNotification(acknowledgmentNumber);
return Success;
}
uint32_t
Subscription::publishPre(void)
{
return 0;
}
PublishResult
Subscription::publish(ServiceTransactionPublish::SPtr& trx)
{
if (trx.get() == NULL) {
actLifetimeCount_--;
// check subscription timeout
if (actLifetimeCount_ == 0) {
return SubscriptionTimeout;
}
// check keepalive count
if (actMaxKeepAliveCount_ == 0) {
return NothingTodo;
}
// calculate keepalive count
actMaxKeepAliveCount_--;
if (actMaxKeepAliveCount_ == 0) {
return NeedAttention;
}
return NothingTodo;
}
// set lifetime counter
actLifetimeCount_ = lifetimeCount_;
//
// handle event notification
//
auto extensibleParameter = boost::make_shared<OpcUaExtensibleParameter>();
extensibleParameter->parameterTypeId().nodeId(OpcUaId_EventNotificationList_Encoding_DefaultBinary);
auto eventNotificationList = extensibleParameter->parameter<EventNotificationList>();
auto statusCode = monitorManager_.receive(eventNotificationList->events());
if (eventNotificationList->events().size() > 0) {
actMaxKeepAliveCount_ = maxKeepAliveCount_;
uint32_t sequencenumber = acknowledgementManager_.nextSequenceNumber();
acknowledgementManager_.addNotification(sequencenumber, extensibleParameter);
auto publishResponse = trx->response();
publishResponse->notificationMessage()->notificationData().set(0, extensibleParameter);
publishResponse->notificationMessage()->publishTime().dateTime(boost::posix_time::microsec_clock::local_time());
publishResponse->notificationMessage()->sequenceNumber() = sequencenumber;
publishResponse->subscriptionId(subscriptionId_);
publishResponse->moreNotifications(false);
acknowledgementManager_.availableSequenceNumbers(publishResponse->availableSequenceNumbers());
if (statusCode == BadOutOfMemory) {
return NeedAttention;
}
return SendPublish;
}
//
// handle data change notification
//
extensibleParameter = boost::make_shared<OpcUaExtensibleParameter>();
extensibleParameter->parameterTypeId().nodeId(OpcUaId_DataChangeNotification_Encoding_DefaultBinary);
auto dataChangeNotification = extensibleParameter->parameter<DataChangeNotification>();
statusCode = monitorManager_.receive(dataChangeNotification->monitoredItems());
if (dataChangeNotification->monitoredItems().size() > 0) {
actMaxKeepAliveCount_ = maxKeepAliveCount_;
uint32_t sequencenumber = acknowledgementManager_.nextSequenceNumber();
acknowledgementManager_.addNotification(sequencenumber, extensibleParameter);
auto publishResponse = trx->response();
publishResponse->notificationMessage()->notificationData().set(0, extensibleParameter);
publishResponse->notificationMessage()->publishTime().dateTime(boost::posix_time::microsec_clock::local_time());
publishResponse->notificationMessage()->sequenceNumber() = sequencenumber;
publishResponse->subscriptionId(subscriptionId_);
publishResponse->moreNotifications(false);
acknowledgementManager_.availableSequenceNumbers(publishResponse->availableSequenceNumbers());
if (statusCode == BadOutOfMemory) return NeedAttention;
return SendPublish;
}
// check keepalive counter
actMaxKeepAliveCount_--;
if (actMaxKeepAliveCount_ == 0) {
actMaxKeepAliveCount_ = maxKeepAliveCount_;
createKeepalive(trx);
return SendPublishKeepalive;
}
return NothingTodo;
}
void
Subscription::createKeepalive(ServiceTransactionPublish::SPtr& trx)
{
auto publishResponse = trx->response();
uint32_t sequencenumber = acknowledgementManager_.nextSequenceNumber(true);
publishResponse->notificationMessage()->publishTime().dateTime(boost::posix_time::microsec_clock::local_time());
publishResponse->notificationMessage()->sequenceNumber() = sequencenumber;
publishResponse->subscriptionId(subscriptionId_);
publishResponse->moreNotifications(false);
acknowledgementManager_.availableSequenceNumbers(publishResponse->availableSequenceNumbers());
}
void
Subscription::retransmissionQueue(SubscriptionAcknowledgement::SPtr& subscriptionAcknowledgement)
{
retransmissionQueue_.erase(subscriptionAcknowledgement->sequenceNumber());
}
void
Subscription::retransmissionQueue(PublishResponse::SPtr& publishResponse)
{
publishResponse->availableSequenceNumbers()->resize(retransmissionQueue_.size());
uint32_t idx = 0;
for (auto it=retransmissionQueue_.begin(); it!=retransmissionQueue_.end(); it++) {
publishResponse->availableSequenceNumbers()->set(idx, it->first);
idx++;
}
retransmissionQueue_.insert(std::make_pair(publishResponse->notificationMessage()->sequenceNumber(), publishResponse));
}
OpcUaStatusCode
Subscription::receive(ServiceTransactionCreateMonitoredItems::SPtr trx)
{
// FIXME: todo - handle events
return monitorManager_.receive(trx);
}
OpcUaStatusCode
Subscription::receive(ServiceTransactionDeleteMonitoredItems::SPtr trx)
{
// FIXME: todo - handle events
return monitorManager_.receive(trx);
}
OpcUaStatusCode
Subscription::receive(ServiceTransactionModifyMonitoredItems::SPtr trx)
{
return monitorManager_.receive(trx);
}
OpcUaStatusCode
Subscription::receive(ServiceTransactionSetMonitoringMode::SPtr trx)
{
return monitorManager_.receive(trx);
}
OpcUaStatusCode
Subscription::receive(ServiceTransactionSetTriggering::SPtr trx)
{
return monitorManager_.receive(trx);
}
}