123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- #include "ua_server_internal.h"
- #include "ua_subscription.h"
- #ifdef UA_ENABLE_SUBSCRIPTIONS
- #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
- static const UA_NodeId overflowEventType =
- {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_EVENTQUEUEOVERFLOWEVENTTYPE}};
- static const UA_NodeId simpleOverflowEventType =
- {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_SIMPLEOVERFLOWEVENTTYPE}};
- static UA_Boolean
- UA_Notification_isOverflowEvent(UA_Server *server, UA_Notification *n) {
- UA_MonitoredItem *mon = n->mon;
- if(mon->attributeId != UA_ATTRIBUTEID_EVENTNOTIFIER)
- return false;
- UA_EventFieldList *efl = &n->data.event.fields;
- if(efl->eventFieldsSize >= 1 &&
- efl->eventFields[0].type == &UA_TYPES[UA_TYPES_NODEID] &&
- isNodeInTree(server->nsCtx, (const UA_NodeId *)efl->eventFields[0].data,
- &overflowEventType, &subtypeId, 1)) {
- return true;
- }
- return false;
- }
- static UA_StatusCode
- createEventOverflowNotification(UA_Server *server, UA_Subscription *sub,
- UA_MonitoredItem *mon, UA_Notification *indicator) {
-
- if(UA_Notification_isOverflowEvent(server, indicator))
- return UA_STATUSCODE_GOOD;
-
-
- UA_Notification *overflowNotification = (UA_Notification *)
- UA_malloc(sizeof(UA_Notification));
- if(!overflowNotification)
- return UA_STATUSCODE_BADOUTOFMEMORY;;
-
- overflowNotification->mon = mon;
- UA_EventFieldList_init(&overflowNotification->data.event.fields);
- overflowNotification->data.event.fields.eventFields = UA_Variant_new();
- if(!overflowNotification->data.event.fields.eventFields) {
- UA_free(overflowNotification);
- return UA_STATUSCODE_BADOUTOFMEMORY;;
- }
- overflowNotification->data.event.fields.eventFieldsSize = 1;
- UA_StatusCode retval =
- UA_Variant_setScalarCopy(overflowNotification->data.event.fields.eventFields,
- &simpleOverflowEventType, &UA_TYPES[UA_TYPES_NODEID]);
- if(retval != UA_STATUSCODE_GOOD) {
- UA_EventFieldList_deleteMembers(&overflowNotification->data.event.fields);
- UA_free(overflowNotification);
- return retval;
- }
-
- TAILQ_INSERT_BEFORE(indicator, overflowNotification, listEntry);
- ++mon->eventOverflows;
- ++mon->queueSize;
- TAILQ_NEXT(overflowNotification, globalEntry) = UA_SUBSCRIPTION_QUEUE_SENTINEL;
- if(mon->monitoringMode == UA_MONITORINGMODE_REPORTING) {
- TAILQ_INSERT_BEFORE(indicator, overflowNotification, globalEntry);
- ++sub->notificationQueueSize;
- ++sub->eventNotifications;
- }
- return UA_STATUSCODE_GOOD;
- }
- #endif
- void
- UA_Notification_enqueue(UA_Server *server, UA_Subscription *sub,
- UA_MonitoredItem *mon, UA_Notification *n) {
-
- TAILQ_INSERT_TAIL(&mon->queue, n, listEntry);
- ++mon->queueSize;
- #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
- if(mon->attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER &&
- UA_Notification_isOverflowEvent(server, n))
- ++mon->eventOverflows;
- #endif
-
- TAILQ_NEXT(n, globalEntry) = UA_SUBSCRIPTION_QUEUE_SENTINEL;
- if(mon->monitoringMode == UA_MONITORINGMODE_REPORTING) {
- TAILQ_INSERT_TAIL(&sub->notificationQueue, n, globalEntry);
- ++sub->notificationQueueSize;
- #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
- if(mon->attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER) {
- ++sub->eventNotifications;
- } else
- #endif
- {
- ++sub->dataChangeNotifications;
- }
- }
-
- UA_MonitoredItem_ensureQueueSpace(server, mon);
- }
- void
- UA_Notification_dequeue(UA_Server *server, UA_Notification *n) {
- UA_MonitoredItem *mon = n->mon;
- UA_Subscription *sub = mon->subscription;
-
- #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
- if(mon->attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER &&
- UA_Notification_isOverflowEvent(server, n))
- --mon->eventOverflows;
- #endif
- TAILQ_REMOVE(&mon->queue, n, listEntry);
- --mon->queueSize;
-
- if(TAILQ_NEXT(n, globalEntry) != UA_SUBSCRIPTION_QUEUE_SENTINEL) {
- #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
- if(mon->attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER) {
- --sub->eventNotifications;
- } else
- #endif
- {
- --sub->dataChangeNotifications;
- }
- TAILQ_REMOVE(&sub->notificationQueue, n, globalEntry);
- --sub->notificationQueueSize;
- }
- }
- void
- UA_Notification_delete(UA_Notification *n) {
- #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
- UA_MonitoredItem *mon = n->mon;
- if(mon->attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER) {
- UA_EventFieldList_deleteMembers(&n->data.event.fields);
-
- } else
- #endif
- {
- UA_DataValue_deleteMembers(&n->data.value);
- }
- UA_free(n);
- }
- void
- UA_MonitoredItem_init(UA_MonitoredItem *mon, UA_Subscription *sub) {
- memset(mon, 0, sizeof(UA_MonitoredItem));
- mon->subscription = sub;
- TAILQ_INIT(&mon->queue);
- }
- void
- UA_MonitoredItem_delete(UA_Server *server, UA_MonitoredItem *monitoredItem) {
- UA_LOCK_ASSERT(server->serviceMutex, 1);
-
- UA_MonitoredItem_unregisterSampleCallback(server, monitoredItem);
-
- if(monitoredItem->subscription) {
- UA_Notification *notification, *notification_tmp;
- TAILQ_FOREACH_SAFE(notification, &monitoredItem->queue,
- listEntry, notification_tmp) {
-
- UA_Notification_dequeue(server, notification);
- UA_Notification_delete(notification);
- }
- }
- #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
- if(monitoredItem->attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER) {
-
- UA_Server_editNode(server, NULL, &monitoredItem->monitoredNodeId,
- UA_MonitoredItem_removeNodeEventCallback, monitoredItem);
- UA_EventFilter_clear(&monitoredItem->filter.eventFilter);
- } else
- #endif
- {
-
-
- }
-
- if(server->config.monitoredItemRegisterCallback && monitoredItem->registered) {
-
- UA_Session *session = NULL;
- if(monitoredItem->subscription)
- session = monitoredItem->subscription->session;
- if(!session)
- session = &server->adminSession;
-
- void *targetContext = NULL;
- getNodeContext(server, monitoredItem->monitoredNodeId, &targetContext);
-
- UA_UNLOCK(server->serviceMutex);
- server->config.monitoredItemRegisterCallback(server, &session->sessionId,
- session->sessionHandle,
- &monitoredItem->monitoredNodeId,
- targetContext, monitoredItem->attributeId, true);
- UA_LOCK(server->serviceMutex);
- }
-
- if(monitoredItem->listEntry.le_prev != NULL)
- LIST_REMOVE(monitoredItem, listEntry);
- UA_String_deleteMembers(&monitoredItem->indexRange);
- UA_ByteString_deleteMembers(&monitoredItem->lastSampledValue);
- UA_Variant_deleteMembers(&monitoredItem->lastValue);
- UA_NodeId_deleteMembers(&monitoredItem->monitoredNodeId);
-
- monitoredItem->delayedFreePointers.callback = NULL;
- UA_WorkQueue_enqueueDelayed(&server->workQueue, &monitoredItem->delayedFreePointers);
- }
- UA_StatusCode
- UA_MonitoredItem_ensureQueueSpace(UA_Server *server, UA_MonitoredItem *mon) {
-
- UA_assert(mon->queueSize >= mon->eventOverflows);
- UA_assert(mon->eventOverflows <= mon->queueSize - mon->eventOverflows + 1);
-
- if(mon->queueSize - mon->eventOverflows <= mon->maxQueueSize)
- return UA_STATUSCODE_GOOD;
- #ifdef __clang_analyzer__
- return UA_STATUSCODE_GOOD;
- #endif
-
-
- UA_Subscription *sub = mon->subscription;
- while(mon->queueSize - mon->eventOverflows > mon->maxQueueSize) {
-
- UA_assert(mon->queueSize - mon->eventOverflows >= 2);
-
- UA_Notification *del = NULL;
- if(mon->discardOldest) {
-
- del = TAILQ_FIRST(&mon->queue);
- #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
- while(UA_Notification_isOverflowEvent(server, del))
- del = TAILQ_NEXT(del, listEntry);
- #endif
- } else {
-
- del = TAILQ_LAST(&mon->queue, NotificationQueue);
- del = TAILQ_PREV(del, NotificationQueue, listEntry);
- #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
- while(UA_Notification_isOverflowEvent(server, del))
- del = TAILQ_PREV(del, NotificationQueue, listEntry);
- #endif
- }
- UA_assert(del);
-
- if(TAILQ_NEXT(del, globalEntry) != UA_SUBSCRIPTION_QUEUE_SENTINEL) {
- UA_Notification *after_del = TAILQ_NEXT(del, listEntry);
- UA_assert(after_del);
- TAILQ_REMOVE(&sub->notificationQueue, after_del, globalEntry);
- TAILQ_INSERT_AFTER(&sub->notificationQueue, del, after_del, globalEntry);
- }
-
- UA_Notification_dequeue(server, del);
- UA_Notification_delete(del);
- }
-
- UA_Notification *indicator;
- if(mon->discardOldest)
- indicator = TAILQ_FIRST(&mon->queue);
- else
- indicator = TAILQ_LAST(&mon->queue, NotificationQueue);
- UA_assert(indicator);
-
- #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
- if(mon->attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER) {
- return createEventOverflowNotification(server, sub, mon, indicator);
- } else
- #endif
- {
-
- if(mon->maxQueueSize > 1) {
-
- indicator->data.value.hasStatus = true;
- indicator->data.value.status |=
- (UA_STATUSCODE_INFOTYPE_DATAVALUE | UA_STATUSCODE_INFOBITS_OVERFLOW);
- }
- }
- return UA_STATUSCODE_GOOD;
- }
- UA_StatusCode
- UA_MonitoredItem_registerSampleCallback(UA_Server *server, UA_MonitoredItem *mon) {
- UA_LOCK_ASSERT(server->serviceMutex, 1);
- if(mon->sampleCallbackIsRegistered)
- return UA_STATUSCODE_GOOD;
-
- if(mon->attributeId == UA_ATTRIBUTEID_EVENTNOTIFIER)
- return UA_STATUSCODE_GOOD;
- UA_StatusCode retval =
- addRepeatedCallback(server, (UA_ServerCallback)UA_MonitoredItem_sampleCallback,
- mon, mon->samplingInterval, &mon->sampleCallbackId);
- if(retval == UA_STATUSCODE_GOOD)
- mon->sampleCallbackIsRegistered = true;
- return retval;
- }
- void
- UA_MonitoredItem_unregisterSampleCallback(UA_Server *server, UA_MonitoredItem *mon) {
- UA_LOCK_ASSERT(server->serviceMutex, 1);
- if(!mon->sampleCallbackIsRegistered)
- return;
- removeCallback(server, mon->sampleCallbackId);
- mon->sampleCallbackIsRegistered = false;
- }
- #endif
|