123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- /*
- * ua_stack_session_manager.c
- *
- * Created on: 05.06.2014
- * Author: root
- */
- #include "ua_stack_session_manager.h"
- typedef struct UA_SessionManagerType
- {
- UA_list_List sessions;
- UA_UInt32 maxSessionCount;
- UA_Int32 lastSessionId;
- UA_UInt32 currentSessionCount;
- UA_DateTime maxSessionLifeTime;
- UA_DateTime sessionTimeout;
- }UA_SessionManagerType;
- static UA_SessionManagerType *sessionManager;
- UA_Int32 UA_SessionManager_generateSessionId(UA_NodeId *sessionId)
- {
- sessionId->encodingByte = 0x00;
- sessionId->namespace = 0;
- sessionId->identifier.numeric = sessionManager->lastSessionId++;
- return UA_SUCCESS;
- }
- UA_Int32 UA_SessionManager_init(UA_UInt32 maxSessionCount,UA_UInt32 sessionTimeout, UA_UInt32 startSessionId)
- {
- UA_Int32 retval = UA_SUCCESS;
- retval |= UA_alloc((void**)&sessionManager,sizeof(UA_SessionManagerType));
- retval |= UA_list_init(&sessionManager->sessions);
- sessionManager->maxSessionCount = maxSessionCount;
- sessionManager->lastSessionId = startSessionId;
- sessionManager->sessionTimeout = sessionTimeout;
- return retval;
- }
- UA_Boolean UA_SessionManager_sessionExists(UA_Session *session)
- {
- if(sessionManager == UA_NULL)
- {
- return UA_FALSE;
- }
- if(UA_list_search(&sessionManager->sessions,(UA_list_PayloadComparer)UA_Session_compare,(void*)session))
- {
- UA_Double pendingLifetime;
- UA_Session_getPendingLifetime(*session,&pendingLifetime);
- if(pendingLifetime>0){
- return UA_TRUE;
- }
- //timeout of session reached so remove it
- UA_NodeId sessionId;
- UA_Session_getId(*session,&sessionId);
- UA_SessionManager_removeSession(&sessionId);
- }
- return UA_FALSE;
- }
- UA_Int32 UA_SessionManager_getSessionById(UA_NodeId *sessionId, UA_Session *session)
- {
- if(sessionManager == UA_NULL)
- {
- *session = UA_NULL;
- return UA_ERROR;
- }
- UA_list_Element* current = sessionManager->sessions.first;
- while (current)
- {
- if (current->payload)
- {
- UA_list_Element* elem = (UA_list_Element*) current;
- *session = *((UA_Session*) (elem->payload));
- if(UA_Session_compareById(*session,sessionId) == UA_EQUAL){
- UA_Double pendingLifetime;
- UA_Session_getPendingLifetime(*session, &pendingLifetime);
- if(pendingLifetime > 0){
- return UA_SUCCESS;
- }
- //session not valid anymore -> remove it
- UA_list_removeElement(elem, (UA_list_PayloadVisitor)UA_Session_delete);
- *session = UA_NULL;
- return UA_ERROR;
- }
- }
- current = current->next;
- }
- *session = UA_NULL;
- return UA_ERROR;
- }
- UA_Int32 UA_SessionManager_getSessionByToken(UA_NodeId *token, UA_Session *session)
- {
- if(sessionManager == UA_NULL)
- {
- *session = UA_NULL;
- return UA_ERROR;
- }
- UA_list_Element* current = sessionManager->sessions.first;
- while (current)
- {
- if (current->payload)
- {
- UA_list_Element* elem = (UA_list_Element*) current;
- *session = *((UA_Session*) (elem->payload));
- if(UA_Session_compareByToken(*session,token) == UA_EQUAL){
- UA_Double pendingLifetime;
- UA_Session_getPendingLifetime(*session, &pendingLifetime);
- if(pendingLifetime > 0){
- return UA_SUCCESS;
- }
- //session not valid anymore -> remove it
- UA_list_removeElement(elem, (UA_list_PayloadVisitor)UA_Session_delete);
- *session = UA_NULL;
- return UA_ERROR;
- }
- }
- current = current->next;
- }
- *session = UA_NULL;
- return UA_ERROR;
- }
- UA_Int32 UA_SessionManager_addSession(UA_Session *session)
- {
- UA_Int32 retval = UA_SUCCESS;
- UA_NodeId sessionId;
- if(!UA_SessionManager_sessionExists(session))
- {
- retval |= UA_list_addPayloadToBack(&sessionManager->sessions,(void*)session);
- UA_Session_getId(*session, &sessionId);
- printf("UA_SessionManager_addSession - added session with id = %d \n",sessionId.identifier.numeric);
- printf("UA_SessionManager_addSession - current session count: %i \n",sessionManager->sessions.size);
- return retval;
- }
- printf("UA_SessionManager_addSession - session already in list");
- return UA_ERROR;
- }
- UA_Int32 UA_SessionManager_removeSession(UA_NodeId *sessionId)
- {
- UA_Int32 retval = UA_SUCCESS;
- UA_list_Element *element = UA_list_search(&sessionManager->sessions,(UA_list_PayloadComparer)UA_Session_compare,sessionId);
- if(element)
- {
- retval |= UA_list_removeElement(element,(UA_list_PayloadVisitor)UA_Session_delete);
- printf("UA_SessionManager_removeSession - session removed, current count: %i \n",sessionManager->sessions.size);
- }
- return retval;
- }
- UA_Int32 UA_SessionManager_updateSessions()
- {
- if(sessionManager == UA_NULL)
- {
- return UA_ERROR;
- }
- UA_list_Element* current = sessionManager->sessions.first;
- while (current)
- {
- if (current->payload)
- {
- UA_list_Element* elem = (UA_list_Element*) current;
- UA_Session session = *((UA_Session*) (elem->payload));
- UA_Double pendingLifetime;
- UA_Session_getPendingLifetime(session, &pendingLifetime);
- if(pendingLifetime <= 0){
- UA_NodeId sessionId;
- UA_Session_getId(session,&sessionId);
- UA_SessionManager_removeSession(&sessionId);
- }
- }
- current = current->next;
- }
- return UA_SUCCESS;
- }
- UA_Int32 UA_SessionManager_getSessionTimeout(UA_Int64 *timeout_ms)
- {
- if(sessionManager)
- {
- *timeout_ms = sessionManager->sessionTimeout;
- return UA_SUCCESS;
- }
- *timeout_ms = 0;
- return UA_ERROR;
- }
|