ua_subscription.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. #include "ua_subscription.h"
  2. #include "ua_server_internal.h"
  3. #include "ua_types_encoding_binary.h"
  4. #include "ua_services.h"
  5. #include "ua_nodestore.h"
  6. #ifdef UA_ENABLE_SUBSCRIPTIONS /* conditional compilation */
  7. /*****************/
  8. /* MonitoredItem */
  9. /*****************/
  10. UA_MonitoredItem * UA_MonitoredItem_new() {
  11. UA_MonitoredItem *new = UA_malloc(sizeof(UA_MonitoredItem));
  12. new->subscription = NULL;
  13. new->currentQueueSize = 0;
  14. new->maxQueueSize = 0;
  15. new->monitoredItemType = UA_MONITOREDITEMTYPE_CHANGENOTIFY; /* currently hardcoded */
  16. new->timestampsToReturn = UA_TIMESTAMPSTORETURN_SOURCE;
  17. UA_String_init(&new->indexRange);
  18. TAILQ_INIT(&new->queue);
  19. UA_NodeId_init(&new->monitoredNodeId);
  20. new->lastSampledValue = UA_BYTESTRING_NULL;
  21. memset(&new->sampleJobGuid, 0, sizeof(UA_Guid));
  22. new->sampleJobIsRegistered = false;
  23. new->itemId = 0;
  24. return new;
  25. }
  26. void MonitoredItem_delete(UA_Server *server, UA_MonitoredItem *monitoredItem) {
  27. MonitoredItem_unregisterSampleJob(server, monitoredItem);
  28. /* clear the queued samples */
  29. MonitoredItem_queuedValue *val, *val_tmp;
  30. TAILQ_FOREACH_SAFE(val, &monitoredItem->queue, listEntry, val_tmp) {
  31. TAILQ_REMOVE(&monitoredItem->queue, val, listEntry);
  32. UA_DataValue_deleteMembers(&val->value);
  33. UA_free(val);
  34. }
  35. monitoredItem->currentQueueSize = 0;
  36. LIST_REMOVE(monitoredItem, listEntry);
  37. UA_String_deleteMembers(&monitoredItem->indexRange);
  38. UA_ByteString_deleteMembers(&monitoredItem->lastSampledValue);
  39. UA_NodeId_deleteMembers(&monitoredItem->monitoredNodeId);
  40. UA_free(monitoredItem);
  41. }
  42. void UA_MoniteredItem_SampleCallback(UA_Server *server, UA_MonitoredItem *monitoredItem) {
  43. UA_Subscription *sub = monitoredItem->subscription;
  44. if(monitoredItem->monitoredItemType != UA_MONITOREDITEMTYPE_CHANGENOTIFY) {
  45. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  46. "Subscription %u | MonitoredItem %i | "
  47. "Cannot process a monitoreditem that is not "
  48. "a data change notification",
  49. sub->subscriptionID, monitoredItem->itemId);
  50. return;
  51. }
  52. MonitoredItem_queuedValue *newvalue = UA_malloc(sizeof(MonitoredItem_queuedValue));
  53. if(!newvalue) {
  54. UA_LOG_WARNING_SESSION(server->config.logger, sub->session,
  55. "Subscription %u | MonitoredItem %i | "
  56. "Skipped a sample due to lack of memory",
  57. sub->subscriptionID, monitoredItem->itemId);
  58. return;
  59. }
  60. UA_DataValue_init(&newvalue->value);
  61. newvalue->clientHandle = monitoredItem->clientHandle;
  62. /* Adjust timestampstoreturn to get source timestamp for triggering */
  63. UA_TimestampsToReturn ts = monitoredItem->timestampsToReturn;
  64. if(ts == UA_TIMESTAMPSTORETURN_SERVER)
  65. ts = UA_TIMESTAMPSTORETURN_BOTH;
  66. else if(ts == UA_TIMESTAMPSTORETURN_NEITHER)
  67. ts = UA_TIMESTAMPSTORETURN_SOURCE;
  68. /* Read the value */
  69. UA_ReadValueId rvid;
  70. UA_ReadValueId_init(&rvid);
  71. rvid.nodeId = monitoredItem->monitoredNodeId;
  72. rvid.attributeId = monitoredItem->attributeID;
  73. rvid.indexRange = monitoredItem->indexRange;
  74. Service_Read_single(server, sub->session, ts, &rvid, &newvalue->value);
  75. /* Apply Filter */
  76. UA_Boolean hasValue = newvalue->value.hasValue;
  77. UA_Boolean hasServerTimestamp = newvalue->value.hasServerTimestamp;
  78. UA_Boolean hasServerPicoseconds = newvalue->value.hasServerPicoseconds;
  79. UA_Boolean hasSourceTimestamp = newvalue->value.hasSourceTimestamp;
  80. UA_Boolean hasSourcePicoseconds = newvalue->value.hasSourcePicoseconds;
  81. newvalue->value.hasServerTimestamp = false;
  82. newvalue->value.hasServerPicoseconds = false;
  83. if(monitoredItem->trigger == UA_DATACHANGETRIGGER_STATUS)
  84. newvalue->value.hasValue = false;
  85. if(monitoredItem->trigger < UA_DATACHANGETRIGGER_STATUSVALUETIMESTAMP) {
  86. newvalue->value.hasSourceTimestamp = false;
  87. newvalue->value.hasSourcePicoseconds = false;
  88. }
  89. /* Encode the data for comparison */
  90. size_t binsize = UA_calcSizeBinary(&newvalue->value, &UA_TYPES[UA_TYPES_DATAVALUE]);
  91. UA_ByteString newValueAsByteString;
  92. UA_StatusCode retval = UA_ByteString_allocBuffer(&newValueAsByteString, binsize);
  93. if(retval != UA_STATUSCODE_GOOD) {
  94. UA_DataValue_deleteMembers(&newvalue->value);
  95. UA_free(newvalue);
  96. return;
  97. }
  98. size_t encodingOffset = 0;
  99. retval = UA_encodeBinary(&newvalue->value, &UA_TYPES[UA_TYPES_DATAVALUE],
  100. NULL, NULL, &newValueAsByteString, &encodingOffset);
  101. /* Restore the settings changed for the filter */
  102. newvalue->value.hasValue = hasValue;
  103. newvalue->value.hasServerTimestamp = hasServerTimestamp;
  104. newvalue->value.hasServerPicoseconds = hasServerPicoseconds;
  105. if(monitoredItem->timestampsToReturn == UA_TIMESTAMPSTORETURN_SERVER ||
  106. monitoredItem->timestampsToReturn == UA_TIMESTAMPSTORETURN_NEITHER) {
  107. newvalue->value.hasSourceTimestamp = false;
  108. newvalue->value.hasSourcePicoseconds = false;
  109. } else {
  110. newvalue->value.hasSourceTimestamp = hasSourceTimestamp;
  111. newvalue->value.hasSourcePicoseconds = hasSourcePicoseconds;
  112. }
  113. /* Error or the value has not changed */
  114. if(retval != UA_STATUSCODE_GOOD ||
  115. (monitoredItem->lastSampledValue.data &&
  116. UA_String_equal(&newValueAsByteString, &monitoredItem->lastSampledValue))) {
  117. UA_LOG_TRACE_SESSION(server->config.logger, sub->session, "Subscription %u | "
  118. "MonitoredItem %u | Do not sample an unchanged value",
  119. sub->subscriptionID, monitoredItem->itemId);
  120. goto cleanup;
  121. }
  122. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  123. "Subscription %u | MonitoredItem %u | "
  124. "Sampling the value", sub->subscriptionID, monitoredItem->itemId);
  125. /* Is enough space in the queue? */
  126. if(monitoredItem->currentQueueSize >= monitoredItem->maxQueueSize) {
  127. MonitoredItem_queuedValue *queueItem;
  128. if(monitoredItem->discardOldest)
  129. queueItem = TAILQ_FIRST(&monitoredItem->queue);
  130. else
  131. queueItem = TAILQ_LAST(&monitoredItem->queue, QueueOfQueueDataValues);
  132. if(!queueItem) {
  133. UA_LOG_WARNING_SESSION(server->config.logger, sub->session, "Subscription %u | "
  134. "MonitoredItem %u | Cannot remove an element from the full "
  135. "queue. Internal error!", sub->subscriptionID,
  136. monitoredItem->itemId);
  137. goto cleanup;
  138. }
  139. TAILQ_REMOVE(&monitoredItem->queue, queueItem, listEntry);
  140. UA_DataValue_deleteMembers(&queueItem->value);
  141. UA_free(queueItem);
  142. monitoredItem->currentQueueSize--;
  143. }
  144. /* If the read request returned a datavalue pointing into the nodestore, we
  145. * must make a deep copy to keep the datavalue across mainloop iterations */
  146. if(newvalue->value.hasValue &&
  147. newvalue->value.value.storageType == UA_VARIANT_DATA_NODELETE) {
  148. UA_Variant tempv = newvalue->value.value;
  149. UA_Variant_copy(&tempv, &newvalue->value.value);
  150. }
  151. /* Replace the comparison bytestring with the current sample */
  152. UA_ByteString_deleteMembers(&monitoredItem->lastSampledValue);
  153. monitoredItem->lastSampledValue = newValueAsByteString;
  154. /* Add the sample to the queue for publication */
  155. TAILQ_INSERT_TAIL(&monitoredItem->queue, newvalue, listEntry);
  156. monitoredItem->currentQueueSize++;
  157. return;
  158. cleanup:
  159. UA_ByteString_deleteMembers(&newValueAsByteString);
  160. UA_DataValue_deleteMembers(&newvalue->value);
  161. UA_free(newvalue);
  162. }
  163. UA_StatusCode
  164. MonitoredItem_registerSampleJob(UA_Server *server, UA_MonitoredItem *mon) {
  165. UA_Job job;
  166. job.type = UA_JOBTYPE_METHODCALL;
  167. job.job.methodCall.method = (UA_ServerCallback)UA_MoniteredItem_SampleCallback;
  168. job.job.methodCall.data = mon;
  169. UA_StatusCode retval = UA_Server_addRepeatedJob(server, job,
  170. (UA_UInt32)mon->samplingInterval,
  171. &mon->sampleJobGuid);
  172. if(retval == UA_STATUSCODE_GOOD)
  173. mon->sampleJobIsRegistered = true;
  174. return retval;
  175. }
  176. UA_StatusCode MonitoredItem_unregisterSampleJob(UA_Server *server, UA_MonitoredItem *mon) {
  177. if(!mon->sampleJobIsRegistered)
  178. return UA_STATUSCODE_GOOD;
  179. mon->sampleJobIsRegistered = false;
  180. return UA_Server_removeRepeatedJob(server, mon->sampleJobGuid);
  181. }
  182. /****************/
  183. /* Subscription */
  184. /****************/
  185. UA_Subscription * UA_Subscription_new(UA_Session *session, UA_UInt32 subscriptionID) {
  186. UA_Subscription *new = UA_malloc(sizeof(UA_Subscription));
  187. if(!new)
  188. return NULL;
  189. new->session = session;
  190. new->subscriptionID = subscriptionID;
  191. new->sequenceNumber = 0;
  192. new->maxKeepAliveCount = 0;
  193. new->publishingEnabled = false;
  194. memset(&new->publishJobGuid, 0, sizeof(UA_Guid));
  195. new->publishJobIsRegistered = false;
  196. new->currentKeepAliveCount = 0;
  197. new->currentLifetimeCount = 0;
  198. new->lastMonitoredItemId = 0;
  199. new->state = UA_SUBSCRIPTIONSTATE_NORMAL; /* The first publish response is sent immediately */
  200. LIST_INIT(&new->retransmissionQueue);
  201. LIST_INIT(&new->MonitoredItems);
  202. return new;
  203. }
  204. void UA_Subscription_deleteMembers(UA_Subscription *subscription, UA_Server *server) {
  205. Subscription_unregisterPublishJob(server, subscription);
  206. /* Delete monitored Items */
  207. UA_MonitoredItem *mon, *tmp_mon;
  208. LIST_FOREACH_SAFE(mon, &subscription->MonitoredItems, listEntry, tmp_mon) {
  209. LIST_REMOVE(mon, listEntry);
  210. MonitoredItem_delete(server, mon);
  211. }
  212. /* Delete Retransmission Queue */
  213. UA_NotificationMessageEntry *nme, *nme_tmp;
  214. LIST_FOREACH_SAFE(nme, &subscription->retransmissionQueue, listEntry, nme_tmp) {
  215. LIST_REMOVE(nme, listEntry);
  216. UA_NotificationMessage_deleteMembers(&nme->message);
  217. UA_free(nme);
  218. }
  219. }
  220. UA_MonitoredItem *
  221. UA_Subscription_getMonitoredItem(UA_Subscription *sub, UA_UInt32 monitoredItemID) {
  222. UA_MonitoredItem *mon;
  223. LIST_FOREACH(mon, &sub->MonitoredItems, listEntry) {
  224. if(mon->itemId == monitoredItemID)
  225. break;
  226. }
  227. return mon;
  228. }
  229. UA_StatusCode
  230. UA_Subscription_deleteMonitoredItem(UA_Server *server, UA_Subscription *sub,
  231. UA_UInt32 monitoredItemID) {
  232. UA_MonitoredItem *mon;
  233. LIST_FOREACH(mon, &sub->MonitoredItems, listEntry) {
  234. if(mon->itemId == monitoredItemID) {
  235. LIST_REMOVE(mon, listEntry);
  236. MonitoredItem_delete(server, mon);
  237. return UA_STATUSCODE_GOOD;
  238. }
  239. }
  240. return UA_STATUSCODE_BADMONITOREDITEMIDINVALID;
  241. }
  242. static size_t
  243. countQueuedNotifications(UA_Subscription *sub, UA_Boolean *moreNotifications) {
  244. size_t notifications = 0;
  245. if(sub->publishingEnabled) {
  246. UA_MonitoredItem *mon;
  247. LIST_FOREACH(mon, &sub->MonitoredItems, listEntry) {
  248. MonitoredItem_queuedValue *qv;
  249. TAILQ_FOREACH(qv, &mon->queue, listEntry) {
  250. if(notifications >= sub->notificationsPerPublish) {
  251. *moreNotifications = true;
  252. break;
  253. }
  254. notifications++;
  255. }
  256. }
  257. }
  258. return notifications;
  259. }
  260. static UA_StatusCode
  261. prepareNotificationMessage(UA_Subscription *sub, UA_NotificationMessage *message,
  262. size_t notifications) {
  263. /* Array of ExtensionObject to hold different kinds of notifications
  264. (currently only DataChangeNotifications) */
  265. message->notificationData = UA_Array_new(1, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT]);
  266. if(!message->notificationData)
  267. return UA_STATUSCODE_BADOUTOFMEMORY;
  268. message->notificationDataSize = 1;
  269. /* Allocate Notification */
  270. UA_DataChangeNotification *dcn = UA_DataChangeNotification_new();
  271. if(!dcn)
  272. goto cleanup;
  273. UA_ExtensionObject *data = message->notificationData;
  274. data->encoding = UA_EXTENSIONOBJECT_DECODED;
  275. data->content.decoded.data = dcn;
  276. data->content.decoded.type = &UA_TYPES[UA_TYPES_DATACHANGENOTIFICATION];
  277. /* Allocate array of notifications */
  278. dcn->monitoredItems =
  279. UA_Array_new(notifications, &UA_TYPES[UA_TYPES_MONITOREDITEMNOTIFICATION]);
  280. if(!dcn->monitoredItems)
  281. goto cleanup;
  282. dcn->monitoredItemsSize = notifications;
  283. /* Move notifications into the response .. the point of no return */
  284. size_t l = 0;
  285. UA_MonitoredItem *mon;
  286. LIST_FOREACH(mon, &sub->MonitoredItems, listEntry) {
  287. MonitoredItem_queuedValue *qv, *qv_tmp;
  288. TAILQ_FOREACH_SAFE(qv, &mon->queue, listEntry, qv_tmp) {
  289. if(l >= notifications)
  290. return UA_STATUSCODE_GOOD;
  291. UA_MonitoredItemNotification *min = &dcn->monitoredItems[l];
  292. min->clientHandle = qv->clientHandle;
  293. min->value = qv->value;
  294. TAILQ_REMOVE(&mon->queue, qv, listEntry);
  295. UA_free(qv);
  296. mon->currentQueueSize--;
  297. l++;
  298. }
  299. }
  300. return UA_STATUSCODE_GOOD;
  301. cleanup:
  302. UA_NotificationMessage_deleteMembers(message);
  303. return UA_STATUSCODE_BADOUTOFMEMORY;
  304. }
  305. void UA_Subscription_publishCallback(UA_Server *server, UA_Subscription *sub) {
  306. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session, "Subscription %u | "
  307. "Publish Callback", sub->subscriptionID);
  308. /* Count the available notifications */
  309. UA_Boolean moreNotifications = false;
  310. size_t notifications = countQueuedNotifications(sub, &moreNotifications);
  311. /* Return if nothing to do */
  312. if(notifications == 0) {
  313. sub->currentKeepAliveCount++;
  314. if(sub->currentKeepAliveCount < sub->maxKeepAliveCount)
  315. return;
  316. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  317. "Sending a keepalive on subscription %u",
  318. sub->subscriptionID)
  319. }
  320. /* Check if the securechannel is valid */
  321. UA_SecureChannel *channel = sub->session->channel;
  322. if(!channel)
  323. return;
  324. /* Dequeue a response */
  325. UA_PublishResponseEntry *pre = SIMPLEQ_FIRST(&sub->session->responseQueue);
  326. /* Cannot publish without a response */
  327. if(!pre) {
  328. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  329. "Cannot send a publish response on subscription %u, "
  330. "since the publish queue is empty", sub->subscriptionID)
  331. if(sub->state != UA_SUBSCRIPTIONSTATE_LATE) {
  332. sub->state = UA_SUBSCRIPTIONSTATE_LATE;
  333. } else {
  334. sub->currentLifetimeCount++;
  335. if(sub->currentLifetimeCount > sub->lifeTimeCount) {
  336. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session, "Subscription %u | "
  337. "End of lifetime for subscription", sub->subscriptionID);
  338. UA_Session_deleteSubscription(server, sub->session, sub->subscriptionID);
  339. }
  340. }
  341. return;
  342. }
  343. UA_PublishResponse *response = &pre->response;
  344. UA_NotificationMessage *message = &response->notificationMessage;
  345. UA_NotificationMessageEntry *retransmission;
  346. if(notifications > 0) {
  347. /* Allocate the retransmission entry */
  348. retransmission = malloc(sizeof(UA_NotificationMessageEntry));
  349. if(!retransmission) {
  350. UA_LOG_WARNING_SESSION(server->config.logger, sub->session,
  351. "Subscription %u | Could not allocate memory "
  352. "for retransmission", sub->subscriptionID);
  353. return;
  354. }
  355. /* Prepare the response */
  356. UA_StatusCode retval =
  357. prepareNotificationMessage(sub, message, notifications);
  358. if(retval != UA_STATUSCODE_GOOD) {
  359. UA_LOG_WARNING_SESSION(server->config.logger, sub->session,
  360. "Subscription %u | Could not prepare the "
  361. "notification message", sub->subscriptionID);
  362. UA_free(retransmission);
  363. return;
  364. }
  365. }
  366. /* <-- The point of no return --> */
  367. /* Remove the response from the response queue */
  368. SIMPLEQ_REMOVE_HEAD(&sub->session->responseQueue, listEntry);
  369. /* Set up the response */
  370. response->responseHeader.timestamp = UA_DateTime_now();
  371. response->subscriptionId = sub->subscriptionID;
  372. response->moreNotifications = moreNotifications;
  373. message->publishTime = response->responseHeader.timestamp;
  374. if(notifications == 0) {
  375. /* Send sequence number for the next notification */
  376. message->sequenceNumber = sub->sequenceNumber + 1;
  377. } else {
  378. /* Increase the sequence number */
  379. message->sequenceNumber = ++sub->sequenceNumber;
  380. /* Put the notification message into the retransmission queue. This needs to
  381. * be done here, so that the message itself is included in the available
  382. * sequence numbers for acknowledgement. */
  383. retransmission->message = response->notificationMessage;
  384. LIST_INSERT_HEAD(&sub->retransmissionQueue, retransmission, listEntry);
  385. }
  386. /* Get the available sequence numbers from the retransmission queue */
  387. size_t available = 0;
  388. UA_NotificationMessageEntry *nme;
  389. LIST_FOREACH(nme, &sub->retransmissionQueue, listEntry)
  390. available++;
  391. if(available > 0) {
  392. response->availableSequenceNumbers = UA_alloca(available * sizeof(UA_UInt32));
  393. response->availableSequenceNumbersSize = available;
  394. }
  395. size_t i = 0;
  396. LIST_FOREACH(nme, &sub->retransmissionQueue, listEntry) {
  397. response->availableSequenceNumbers[i] = nme->message.sequenceNumber;
  398. i++;
  399. }
  400. /* Send the response */
  401. UA_LOG_DEBUG_SESSION(server->config.logger, sub->session,
  402. "Subscription %u | Sending out a publish response with %u "
  403. "notifications", sub->subscriptionID, (UA_UInt32)notifications);
  404. UA_SecureChannel_sendBinaryMessage(sub->session->channel, pre->requestId, response,
  405. &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  406. /* Reset subscription state to normal. */
  407. sub->state = UA_SUBSCRIPTIONSTATE_NORMAL;
  408. sub->currentKeepAliveCount = 0;
  409. sub->currentLifetimeCount = 0;
  410. /* Free the response */
  411. UA_Array_delete(response->results, response->resultsSize,
  412. &UA_TYPES[UA_TYPES_UINT32]);
  413. UA_free(pre); /* no need for UA_PublishResponse_deleteMembers */
  414. /* Repeat if there are more notifications to send */
  415. if(moreNotifications)
  416. UA_Subscription_publishCallback(server, sub);
  417. }
  418. UA_StatusCode
  419. Subscription_registerPublishJob(UA_Server *server, UA_Subscription *sub) {
  420. if(sub->publishJobIsRegistered)
  421. return UA_STATUSCODE_GOOD;
  422. UA_Job job;
  423. job.type = UA_JOBTYPE_METHODCALL;
  424. job.job.methodCall.method = (UA_ServerCallback)UA_Subscription_publishCallback;
  425. job.job.methodCall.data = sub;
  426. UA_StatusCode retval =
  427. UA_Server_addRepeatedJob(server, job, (UA_UInt32)sub->publishingInterval,
  428. &sub->publishJobGuid);
  429. if(retval == UA_STATUSCODE_GOOD)
  430. sub->publishJobIsRegistered = true;
  431. return retval;
  432. }
  433. UA_StatusCode
  434. Subscription_unregisterPublishJob(UA_Server *server, UA_Subscription *sub) {
  435. if(!sub->publishJobIsRegistered)
  436. return UA_STATUSCODE_GOOD;
  437. sub->publishJobIsRegistered = false;
  438. return UA_Server_removeRepeatedJob(server, sub->publishJobGuid);
  439. }
  440. /* When the session has publish requests stored but the last subscription is
  441. deleted... Send out empty responses */
  442. void
  443. UA_Subscription_answerPublishRequestsNoSubscription(UA_Server *server, UA_NodeId *sessionToken) {
  444. /* Get session */
  445. UA_Session *session = UA_SessionManager_getSession(&server->sessionManager, sessionToken);
  446. UA_NodeId_delete(sessionToken);
  447. /* No session or there are remaining subscriptions */
  448. if(!session || LIST_FIRST(&session->serverSubscriptions))
  449. return;
  450. /* Send a response for every queued request */
  451. UA_PublishResponseEntry *pre;
  452. while((pre = SIMPLEQ_FIRST(&session->responseQueue))) {
  453. SIMPLEQ_REMOVE_HEAD(&session->responseQueue, listEntry);
  454. UA_PublishResponse *response = &pre->response;
  455. response->responseHeader.serviceResult = UA_STATUSCODE_BADNOSUBSCRIPTION;
  456. response->responseHeader.timestamp = UA_DateTime_now();
  457. UA_SecureChannel_sendBinaryMessage(session->channel, pre->requestId, response,
  458. &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  459. UA_PublishResponse_deleteMembers(response);
  460. UA_free(pre);
  461. }
  462. }
  463. #endif /* UA_ENABLE_SUBSCRIPTIONS */