ua_subscription.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2015-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2015 (c) Chris Iatrou
  7. * Copyright 2015-2016 (c) Sten Grüner
  8. * Copyright 2017-2018 (c) Thomas Stalder, Blue Time Concept SA
  9. * Copyright 2015 (c) Joakim L. Gilje
  10. * Copyright 2016-2017 (c) Florian Palm
  11. * Copyright 2015-2016 (c) Oleksiy Vasylyev
  12. * Copyright 2017 (c) frax2222
  13. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  14. * Copyright 2017 (c) Ari Breitkreuz, fortiss GmbH
  15. * Copyright 2017 (c) Mattias Bornhager
  16. */
  17. #include "ua_server_internal.h"
  18. #include "ua_subscription.h"
  19. #ifdef UA_ENABLE_SUBSCRIPTIONS /* conditional compilation */
  20. void
  21. UA_Notification_enqueue(UA_Server *server, UA_Subscription *sub,
  22. UA_MonitoredItem *mon, UA_Notification *n) {
  23. /* Add to the MonitoredItem */
  24. TAILQ_INSERT_TAIL(&mon->queue, n, listEntry);
  25. ++mon->queueSize;
  26. /* Add to the subscription */
  27. TAILQ_INSERT_TAIL(&sub->notificationQueue, n, globalEntry);
  28. ++sub->notificationQueueSize;
  29. if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_CHANGENOTIFY) {
  30. ++sub->dataChangeNotifications;
  31. } else if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_EVENTNOTIFY) {
  32. ++sub->eventNotifications;
  33. } else if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_STATUSNOTIFY) {
  34. ++sub->statusChangeNotifications;
  35. }
  36. /* Ensure enough space is available in the MonitoredItem. Do this only after
  37. * adding the new Notification. */
  38. MonitoredItem_ensureQueueSpace(server, mon);
  39. }
  40. void
  41. UA_Notification_delete(UA_Subscription *sub, UA_MonitoredItem *mon,
  42. UA_Notification *n) {
  43. if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_CHANGENOTIFY) {
  44. UA_DataValue_deleteMembers(&n->data.value);
  45. --sub->dataChangeNotifications;
  46. } else if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_EVENTNOTIFY) {
  47. UA_EventFieldList_deleteMembers(&n->data.event.fields);
  48. /* EventFilterResult currently isn't being used
  49. * UA_EventFilterResult_delete(notification->data.event->result); */
  50. --sub->eventNotifications;
  51. } else if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_STATUSNOTIFY) {
  52. --sub->statusChangeNotifications;
  53. }
  54. TAILQ_REMOVE(&mon->queue, n, listEntry);
  55. --mon->queueSize;
  56. TAILQ_REMOVE(&sub->notificationQueue, n, globalEntry);
  57. --sub->notificationQueueSize;
  58. UA_free(n);
  59. }
  60. UA_Subscription *
  61. UA_Subscription_new(UA_Session *session, UA_UInt32 subscriptionId) {
  62. /* Allocate the memory */
  63. UA_Subscription *newSub =
  64. (UA_Subscription*)UA_calloc(1, sizeof(UA_Subscription));
  65. if(!newSub)
  66. return NULL;
  67. /* Remaining members are covered by calloc zeroing out the memory */
  68. newSub->session = session;
  69. newSub->subscriptionId = subscriptionId;
  70. newSub->state = UA_SUBSCRIPTIONSTATE_NORMAL; /* The first publish response is sent immediately */
  71. TAILQ_INIT(&newSub->retransmissionQueue);
  72. TAILQ_INIT(&newSub->notificationQueue);
  73. return newSub;
  74. }
  75. void
  76. UA_Subscription_deleteMembers(UA_Server *server, UA_Subscription *sub) {
  77. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session, "Subscription %u | "
  78. "Delete the subscription", sub->subscriptionId);
  79. Subscription_unregisterPublishCallback(server, sub);
  80. /* Delete monitored Items */
  81. UA_MonitoredItem *mon, *tmp_mon;
  82. LIST_FOREACH_SAFE(mon, &sub->monitoredItems, listEntry, tmp_mon) {
  83. LIST_REMOVE(mon, listEntry);
  84. UA_MonitoredItem_delete(server, mon);
  85. }
  86. sub->monitoredItemsSize = 0;
  87. /* Delete Retransmission Queue */
  88. UA_NotificationMessageEntry *nme, *nme_tmp;
  89. TAILQ_FOREACH_SAFE(nme, &sub->retransmissionQueue, listEntry, nme_tmp) {
  90. TAILQ_REMOVE(&sub->retransmissionQueue, nme, listEntry);
  91. UA_NotificationMessage_deleteMembers(&nme->message);
  92. UA_free(nme);
  93. }
  94. sub->retransmissionQueueSize = 0;
  95. }
  96. UA_MonitoredItem *
  97. UA_Subscription_getMonitoredItem(UA_Subscription *sub, UA_UInt32 monitoredItemId) {
  98. UA_MonitoredItem *mon;
  99. LIST_FOREACH(mon, &sub->monitoredItems, listEntry) {
  100. if(mon->monitoredItemId == monitoredItemId)
  101. break;
  102. }
  103. return mon;
  104. }
  105. UA_StatusCode
  106. UA_Subscription_deleteMonitoredItem(UA_Server *server, UA_Subscription *sub,
  107. UA_UInt32 monitoredItemId) {
  108. /* Find the MonitoredItem */
  109. UA_MonitoredItem *mon;
  110. LIST_FOREACH(mon, &sub->monitoredItems, listEntry) {
  111. if(mon->monitoredItemId == monitoredItemId)
  112. break;
  113. }
  114. if(!mon)
  115. return UA_STATUSCODE_BADMONITOREDITEMIDINVALID;
  116. /* Remove the MonitoredItem */
  117. LIST_REMOVE(mon, listEntry);
  118. sub->monitoredItemsSize--;
  119. /* Remove content and delayed free */
  120. UA_MonitoredItem_delete(server, mon);
  121. return UA_STATUSCODE_GOOD;
  122. }
  123. void
  124. UA_Subscription_addMonitoredItem(UA_Subscription *sub, UA_MonitoredItem *newMon) {
  125. sub->monitoredItemsSize++;
  126. LIST_INSERT_HEAD(&sub->monitoredItems, newMon, listEntry);
  127. }
  128. static void
  129. UA_Subscription_addRetransmissionMessage(UA_Server *server, UA_Subscription *sub,
  130. UA_NotificationMessageEntry *entry) {
  131. /* Release the oldest entry if there is not enough space */
  132. if(server->config.maxRetransmissionQueueSize > 0 &&
  133. sub->retransmissionQueueSize >= server->config.maxRetransmissionQueueSize) {
  134. UA_NotificationMessageEntry *lastentry =
  135. TAILQ_LAST(&sub->retransmissionQueue, ListOfNotificationMessages);
  136. TAILQ_REMOVE(&sub->retransmissionQueue, lastentry, listEntry);
  137. --sub->retransmissionQueueSize;
  138. UA_NotificationMessage_deleteMembers(&lastentry->message);
  139. UA_free(lastentry);
  140. }
  141. /* Add entry */
  142. TAILQ_INSERT_HEAD(&sub->retransmissionQueue, entry, listEntry);
  143. ++sub->retransmissionQueueSize;
  144. }
  145. UA_StatusCode
  146. UA_Subscription_removeRetransmissionMessage(UA_Subscription *sub, UA_UInt32 sequenceNumber) {
  147. /* Find the retransmission message */
  148. UA_NotificationMessageEntry *entry;
  149. TAILQ_FOREACH(entry, &sub->retransmissionQueue, listEntry) {
  150. if(entry->message.sequenceNumber == sequenceNumber)
  151. break;
  152. }
  153. if(!entry)
  154. return UA_STATUSCODE_BADSEQUENCENUMBERUNKNOWN;
  155. /* Remove the retransmission message */
  156. TAILQ_REMOVE(&sub->retransmissionQueue, entry, listEntry);
  157. --sub->retransmissionQueueSize;
  158. UA_NotificationMessage_deleteMembers(&entry->message);
  159. UA_free(entry);
  160. return UA_STATUSCODE_GOOD;
  161. }
  162. static UA_StatusCode
  163. prepareNotificationMessage(UA_Server *server, UA_Subscription *sub,
  164. UA_NotificationMessage *message, size_t notifications) {
  165. UA_assert(notifications > 0);
  166. /* Allocate an ExtensionObject for events and data */
  167. message->notificationData = (UA_ExtensionObject*)
  168. UA_Array_new(2, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT]);
  169. if(!message->notificationData)
  170. return UA_STATUSCODE_BADOUTOFMEMORY;
  171. message->notificationDataSize = 2;
  172. /* Pre-allocate DataChangeNotifications */
  173. size_t notificationDataIdx = 0;
  174. UA_DataChangeNotification *dcn = NULL;
  175. if(sub->dataChangeNotifications > 0) {
  176. dcn = UA_DataChangeNotification_new();
  177. if(!dcn) {
  178. UA_NotificationMessage_deleteMembers(message);
  179. return UA_STATUSCODE_BADOUTOFMEMORY;
  180. }
  181. message->notificationData->encoding = UA_EXTENSIONOBJECT_DECODED;
  182. message->notificationData->content.decoded.data = dcn;
  183. message->notificationData->content.decoded.type = &UA_TYPES[UA_TYPES_DATACHANGENOTIFICATION];
  184. size_t dcnSize = sub->dataChangeNotifications;
  185. if(dcnSize > notifications)
  186. dcnSize = notifications;
  187. dcn->monitoredItems = (UA_MonitoredItemNotification*)
  188. UA_Array_new(dcnSize, &UA_TYPES[UA_TYPES_MONITOREDITEMNOTIFICATION]);
  189. if(!dcn->monitoredItems) {
  190. UA_NotificationMessage_deleteMembers(message);
  191. return UA_STATUSCODE_BADOUTOFMEMORY;
  192. }
  193. dcn->monitoredItemsSize = dcnSize;
  194. notificationDataIdx++;
  195. }
  196. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  197. UA_EventNotificationList *enl = NULL;
  198. UA_StatusChangeNotification *scn = NULL;
  199. /* Pre-allocate either StatusChange or EventNotifications. Sending a
  200. * (single) StatusChangeNotification has priority. */
  201. if(sub->statusChangeNotifications > 0) {
  202. scn = UA_StatusChangeNotification_new();
  203. if(!scn) {
  204. UA_NotificationMessage_deleteMembers(message);
  205. return UA_STATUSCODE_BADOUTOFMEMORY;
  206. }
  207. message->notificationData[notificationDataIdx].encoding = UA_EXTENSIONOBJECT_DECODED;
  208. message->notificationData[notificationDataIdx].content.decoded.data = scn;
  209. message->notificationData[notificationDataIdx].content.decoded.type = &UA_TYPES[UA_TYPES_STATUSCHANGENOTIFICATION];
  210. notificationDataIdx++;
  211. } else if(sub->eventNotifications > 0) {
  212. enl = UA_EventNotificationList_new();
  213. if(!enl) {
  214. UA_NotificationMessage_deleteMembers(message);
  215. return UA_STATUSCODE_BADOUTOFMEMORY;
  216. }
  217. message->notificationData[notificationDataIdx].encoding = UA_EXTENSIONOBJECT_DECODED;
  218. message->notificationData[notificationDataIdx].content.decoded.data = enl;
  219. message->notificationData[notificationDataIdx].content.decoded.type = &UA_TYPES[UA_TYPES_EVENTNOTIFICATIONLIST];
  220. size_t enlSize = sub->eventNotifications;
  221. if(enlSize > notifications)
  222. enlSize = notifications;
  223. enl->events = (UA_EventFieldList*) UA_Array_new(enlSize, &UA_TYPES[UA_TYPES_EVENTFIELDLIST]);
  224. if(!enl->events) {
  225. UA_NotificationMessage_deleteMembers(message);
  226. return UA_STATUSCODE_BADOUTOFMEMORY;
  227. }
  228. enl->eventsSize = enlSize;
  229. notificationDataIdx++;
  230. }
  231. #endif
  232. UA_assert(notificationDataIdx > 0);
  233. message->notificationDataSize = notificationDataIdx;
  234. /* <-- The point of no return --> */
  235. size_t totalNotifications = 0; /* How many notifications were moved to the response overall? */
  236. size_t dcnPos = 0; /* How many DataChangeNotifications were put into the list? */
  237. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  238. size_t enlPos = 0; /* How many EventNotifications were moved into the list */
  239. #endif
  240. UA_Notification *notification, *notification_tmp;
  241. TAILQ_FOREACH_SAFE(notification, &sub->notificationQueue, globalEntry, notification_tmp) {
  242. if(totalNotifications >= notifications)
  243. break;
  244. UA_MonitoredItem *mon = notification->mon;
  245. /* Move the content to the response */
  246. if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_CHANGENOTIFY) {
  247. UA_assert(dcn != NULL); /* Have at least one change notification */
  248. /* Move the content to the response */
  249. UA_MonitoredItemNotification *min = &dcn->monitoredItems[dcnPos];
  250. min->clientHandle = mon->clientHandle;
  251. min->value = notification->data.value;
  252. UA_DataValue_init(&notification->data.value); /* Reset after the value has been moved */
  253. dcnPos++;
  254. }
  255. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  256. else if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_STATUSNOTIFY && scn) {
  257. // TODO: Handling of StatusChangeNotifications
  258. scn = NULL; /* At most one per PublishReponse */
  259. } else if(mon->monitoredItemType == UA_MONITOREDITEMTYPE_EVENTNOTIFY && enl) {
  260. UA_assert(enl != NULL); /* Have at least one event notification */
  261. /* TODO: The following lead to crashes when we assumed notifications to be ready... */
  262. /* /\* removing an overflowEvent should not reduce the queueSize *\/ */
  263. /* UA_NodeId overflowId = UA_NODEID_NUMERIC(0, UA_NS0ID_SIMPLEOVERFLOWEVENTTYPE); */
  264. /* if (!(notification->data.event.fields.eventFieldsSize == 1 */
  265. /* && notification->data.event.fields.eventFields->type == &UA_TYPES[UA_TYPES_NODEID] */
  266. /* && UA_NodeId_equal((UA_NodeId *)notification->data.event.fields.eventFields->data, &overflowId))) { */
  267. /* --mon->queueSize; */
  268. /* --sub->notificationQueueSize; */
  269. /* } */
  270. /* Move the content to the response */
  271. UA_EventFieldList *efl = &enl->events[enlPos];
  272. *efl = notification->data.event.fields;
  273. UA_EventFieldList_init(&notification->data.event.fields);
  274. efl->clientHandle = mon->clientHandle;
  275. /* EventFilterResult currently isn't being used
  276. UA_EventFilterResult_deleteMembers(&notification->data.event.result); */
  277. enlPos++;
  278. }
  279. #endif
  280. else {
  281. continue; /* Nothing to do */
  282. }
  283. /* Remove the notification from the queues */
  284. UA_Notification_delete(sub, mon, notification);
  285. totalNotifications++;
  286. }
  287. /* Set sizes */
  288. if(dcn) {
  289. dcn->monitoredItemsSize = dcnPos;
  290. if(dcnPos == 0) {
  291. UA_free(dcn->monitoredItems);
  292. dcn->monitoredItems = NULL;
  293. }
  294. }
  295. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  296. if(enl) {
  297. enl->eventsSize = enlPos;
  298. if(enlPos == 0) {
  299. UA_free(enl->events);
  300. enl->events = NULL;
  301. }
  302. }
  303. #endif
  304. return UA_STATUSCODE_GOOD;
  305. }
  306. /* According to OPC Unified Architecture, Part 4 5.13.1.1 i) The value 0 is
  307. * never used for the sequence number */
  308. static UA_UInt32
  309. UA_Subscription_nextSequenceNumber(UA_UInt32 sequenceNumber) {
  310. UA_UInt32 nextSequenceNumber = sequenceNumber + 1;
  311. if(nextSequenceNumber == 0)
  312. nextSequenceNumber = 1;
  313. return nextSequenceNumber;
  314. }
  315. static void
  316. publishCallback(UA_Server *server, UA_Subscription *sub) {
  317. sub->readyNotifications = sub->notificationQueueSize;
  318. UA_Subscription_publish(server, sub);
  319. }
  320. void
  321. UA_Subscription_publish(UA_Server *server, UA_Subscription *sub) {
  322. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session, "Subscription %u | "
  323. "Publish Callback", sub->subscriptionId);
  324. /* Dequeue a response */
  325. UA_PublishResponseEntry *pre = UA_Session_dequeuePublishReq(sub->session);
  326. if(pre) {
  327. sub->currentLifetimeCount = 0; /* Reset the LifetimeCounter */
  328. } else {
  329. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  330. "Subscription %u | The publish queue is empty",
  331. sub->subscriptionId);
  332. ++sub->currentLifetimeCount;
  333. if(sub->currentLifetimeCount > sub->lifeTimeCount) {
  334. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  335. "Subscription %u | End of lifetime "
  336. "for subscription", sub->subscriptionId);
  337. UA_Session_deleteSubscription(server, sub->session, sub->subscriptionId);
  338. /* TODO: send a StatusChangeNotification with Bad_Timeout */
  339. return;
  340. }
  341. }
  342. /* If there are several late publish responses... */
  343. if(sub->readyNotifications > sub->notificationQueueSize)
  344. sub->readyNotifications = sub->notificationQueueSize;
  345. /* Count the available notifications */
  346. UA_UInt32 notifications = sub->readyNotifications;
  347. if(!sub->publishingEnabled)
  348. notifications = 0;
  349. UA_Boolean moreNotifications = false;
  350. if(notifications > sub->notificationsPerPublish) {
  351. notifications = sub->notificationsPerPublish;
  352. moreNotifications = true;
  353. }
  354. /* Return if no notifications and no keepalive */
  355. if(notifications == 0) {
  356. ++sub->currentKeepAliveCount;
  357. if(sub->currentKeepAliveCount < sub->maxKeepAliveCount) {
  358. if(pre)
  359. UA_Session_queuePublishReq(sub->session, pre, true); /* Re-enqueue */
  360. return;
  361. }
  362. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  363. "Subscription %u | Sending a KeepAlive",
  364. sub->subscriptionId);
  365. }
  366. /* We want to send a response. Is the channel open? */
  367. UA_SecureChannel *channel = sub->session->header.channel;
  368. if(!channel || !pre) {
  369. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  370. "Subscription %u | Want to send a publish response but can't. "
  371. "The subscription is late.", sub->subscriptionId);
  372. sub->state = UA_SUBSCRIPTIONSTATE_LATE;
  373. if(pre)
  374. UA_Session_queuePublishReq(sub->session, pre, true); /* Re-enqueue */
  375. return;
  376. }
  377. /* Prepare the response */
  378. UA_PublishResponse *response = &pre->response;
  379. UA_NotificationMessage *message = &response->notificationMessage;
  380. UA_NotificationMessageEntry *retransmission = NULL;
  381. if(notifications > 0) {
  382. /* Allocate the retransmission entry */
  383. retransmission = (UA_NotificationMessageEntry*)UA_malloc(sizeof(UA_NotificationMessageEntry));
  384. if(!retransmission) {
  385. UA_LOG_WARNING_SESSION(server->config.logger, sub->session,
  386. "Subscription %u | Could not allocate memory for retransmission. "
  387. "The subscription is late.", sub->subscriptionId);
  388. sub->state = UA_SUBSCRIPTIONSTATE_LATE;
  389. UA_Session_queuePublishReq(sub->session, pre, true); /* Re-enqueue */
  390. return;
  391. }
  392. /* Prepare the response */
  393. UA_StatusCode retval = prepareNotificationMessage(server, sub, message, notifications);
  394. if(retval != UA_STATUSCODE_GOOD) {
  395. UA_LOG_WARNING_SESSION(server->config.logger, sub->session,
  396. "Subscription %u | Could not prepare the notification message. "
  397. "The subscription is late.", sub->subscriptionId);
  398. UA_free(retransmission);
  399. sub->state = UA_SUBSCRIPTIONSTATE_LATE;
  400. UA_Session_queuePublishReq(sub->session, pre, true); /* Re-enqueue */
  401. return;
  402. }
  403. }
  404. /* <-- The point of no return --> */
  405. /* Adjust the number of ready notifications */
  406. UA_assert(sub->readyNotifications >= notifications);
  407. sub->readyNotifications -= notifications;
  408. /* Set up the response */
  409. response->responseHeader.timestamp = UA_DateTime_now();
  410. response->subscriptionId = sub->subscriptionId;
  411. response->moreNotifications = moreNotifications;
  412. message->publishTime = response->responseHeader.timestamp;
  413. /* Set the sequence number. The sequence number will be reused if there are
  414. * no notifications (and this is a keepalive message). */
  415. message->sequenceNumber = UA_Subscription_nextSequenceNumber(sub->sequenceNumber);
  416. if(notifications > 0) {
  417. /* There are notifications. So we can't reuse the sequence number. */
  418. sub->sequenceNumber = message->sequenceNumber;
  419. /* Put the notification message into the retransmission queue. This
  420. * needs to be done here, so that the message itself is included in the
  421. * available sequence numbers for acknowledgement. */
  422. retransmission->message = response->notificationMessage;
  423. UA_Subscription_addRetransmissionMessage(server, sub, retransmission);
  424. }
  425. /* Get the available sequence numbers from the retransmission queue */
  426. size_t available = sub->retransmissionQueueSize;
  427. UA_STACKARRAY(UA_UInt32, seqNumbers, available);
  428. if(available > 0) {
  429. response->availableSequenceNumbers = seqNumbers;
  430. response->availableSequenceNumbersSize = available;
  431. size_t i = 0;
  432. UA_NotificationMessageEntry *nme;
  433. TAILQ_FOREACH(nme, &sub->retransmissionQueue, listEntry) {
  434. response->availableSequenceNumbers[i] = nme->message.sequenceNumber;
  435. ++i;
  436. }
  437. }
  438. /* Send the response */
  439. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  440. "Subscription %u | Sending out a publish response "
  441. "with %u notifications", sub->subscriptionId,
  442. (UA_UInt32)notifications);
  443. UA_SecureChannel_sendSymmetricMessage(sub->session->header.channel, pre->requestId,
  444. UA_MESSAGETYPE_MSG, response,
  445. &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  446. /* Reset subscription state to normal */
  447. sub->state = UA_SUBSCRIPTIONSTATE_NORMAL;
  448. sub->currentKeepAliveCount = 0;
  449. /* Free the response */
  450. UA_Array_delete(response->results, response->resultsSize, &UA_TYPES[UA_TYPES_UINT32]);
  451. UA_free(pre); /* No need for UA_PublishResponse_deleteMembers */
  452. /* Repeat sending responses if there are more notifications to send */
  453. if(moreNotifications)
  454. UA_Subscription_publish(server, sub);
  455. }
  456. UA_Boolean
  457. UA_Subscription_reachedPublishReqLimit(UA_Server *server, UA_Session *session) {
  458. UA_LOG_DEBUG_SESSION(server->config.logger, session, "Reached number of publish request limit");
  459. /* Dequeue a response */
  460. UA_PublishResponseEntry *pre = UA_Session_dequeuePublishReq(session);
  461. /* Cannot publish without a response */
  462. if(!pre) {
  463. UA_LOG_FATAL_SESSION(server->config.logger, session, "No publish requests available");
  464. return false;
  465. }
  466. /* <-- The point of no return --> */
  467. UA_PublishResponse *response = &pre->response;
  468. UA_NotificationMessage *message = &response->notificationMessage;
  469. /* Set up the response. Note that this response has no related subscription id */
  470. response->responseHeader.timestamp = UA_DateTime_now();
  471. response->responseHeader.serviceResult = UA_STATUSCODE_BADTOOMANYPUBLISHREQUESTS;
  472. response->subscriptionId = 0;
  473. response->moreNotifications = false;
  474. message->publishTime = response->responseHeader.timestamp;
  475. message->sequenceNumber = 0;
  476. response->availableSequenceNumbersSize = 0;
  477. /* Send the response */
  478. UA_LOG_DEBUG_SESSION(server->config.logger, session,
  479. "Sending out a publish response triggered by too many publish requests");
  480. UA_SecureChannel_sendSymmetricMessage(session->header.channel, pre->requestId,
  481. UA_MESSAGETYPE_MSG, response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  482. /* Free the response */
  483. UA_Array_delete(response->results, response->resultsSize, &UA_TYPES[UA_TYPES_UINT32]);
  484. UA_free(pre); /* no need for UA_PublishResponse_deleteMembers */
  485. return true;
  486. }
  487. UA_StatusCode
  488. Subscription_registerPublishCallback(UA_Server *server, UA_Subscription *sub) {
  489. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  490. "Subscription %u | Register subscription "
  491. "publishing callback", sub->subscriptionId);
  492. if(sub->publishCallbackIsRegistered)
  493. return UA_STATUSCODE_GOOD;
  494. UA_StatusCode retval =
  495. UA_Server_addRepeatedCallback(server, (UA_ServerCallback)publishCallback,
  496. sub, (UA_UInt32)sub->publishingInterval, &sub->publishCallbackId);
  497. if(retval != UA_STATUSCODE_GOOD)
  498. return retval;
  499. sub->publishCallbackIsRegistered = true;
  500. return UA_STATUSCODE_GOOD;
  501. }
  502. UA_StatusCode
  503. Subscription_unregisterPublishCallback(UA_Server *server, UA_Subscription *sub) {
  504. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session, "Subscription %u | "
  505. "Unregister subscription publishing callback", sub->subscriptionId);
  506. if(!sub->publishCallbackIsRegistered)
  507. return UA_STATUSCODE_GOOD;
  508. UA_StatusCode retval = UA_Server_removeRepeatedCallback(server, sub->publishCallbackId);
  509. if(retval != UA_STATUSCODE_GOOD)
  510. return retval;
  511. sub->publishCallbackIsRegistered = false;
  512. return UA_STATUSCODE_GOOD;
  513. }
  514. /* When the session has publish requests stored but the last subscription is
  515. * deleted... Send out empty responses */
  516. void
  517. UA_Subscription_answerPublishRequestsNoSubscription(UA_Server *server, UA_Session *session) {
  518. /* No session or there are remaining subscriptions */
  519. if(!session || LIST_FIRST(&session->serverSubscriptions))
  520. return;
  521. /* Send a response for every queued request */
  522. UA_PublishResponseEntry *pre;
  523. while((pre = UA_Session_dequeuePublishReq(session))) {
  524. UA_PublishResponse *response = &pre->response;
  525. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOSUBSCRIPTION;
  526. response->responseHeader.timestamp = UA_DateTime_now();
  527. UA_SecureChannel_sendSymmetricMessage(session->header.channel, pre->requestId, UA_MESSAGETYPE_MSG,
  528. response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  529. UA_PublishResponse_deleteMembers(response);
  530. UA_free(pre);
  531. }
  532. }
  533. #endif /* UA_ENABLE_SUBSCRIPTIONS */