ua_stack_session_manager.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * ua_stack_session_manager.c
  3. *
  4. * Created on: 05.06.2014
  5. * Author: root
  6. */
  7. #include "ua_stack_session_manager.h"
  8. typedef struct UA_SessionManagerType
  9. {
  10. UA_list_List sessions;
  11. UA_UInt32 maxSessionCount;
  12. UA_Int32 lastSessionId;
  13. UA_UInt32 currentSessionCount;
  14. UA_DateTime maxSessionLifeTime;
  15. UA_DateTime sessionTimeout;
  16. }UA_SessionManagerType;
  17. static UA_SessionManagerType *sessionManager;
  18. UA_Int32 UA_SessionManager_generateSessionId(UA_NodeId *sessionId)
  19. {
  20. sessionId->encodingByte = 0x00;
  21. sessionId->namespace = 0;
  22. sessionId->identifier.numeric = sessionManager->lastSessionId++;
  23. return UA_SUCCESS;
  24. }
  25. UA_Int32 UA_SessionManager_init(UA_UInt32 maxSessionCount,UA_UInt32 sessionTimeout, UA_UInt32 startSessionId)
  26. {
  27. UA_Int32 retval = UA_SUCCESS;
  28. retval |= UA_alloc((void**)&sessionManager,sizeof(UA_SessionManagerType));
  29. retval |= UA_list_init(&sessionManager->sessions);
  30. sessionManager->maxSessionCount = maxSessionCount;
  31. sessionManager->lastSessionId = startSessionId;
  32. sessionManager->sessionTimeout = sessionTimeout;
  33. return retval;
  34. }
  35. UA_Boolean UA_SessionManager_sessionExists(UA_Session *session)
  36. {
  37. if(sessionManager == UA_NULL)
  38. {
  39. return UA_FALSE;
  40. }
  41. if(UA_list_search(&sessionManager->sessions,(UA_list_PayloadComparer)UA_Session_compare,(void*)session))
  42. {
  43. UA_Double pendingLifetime;
  44. UA_Session_getPendingLifetime(*session,&pendingLifetime);
  45. if(pendingLifetime>0){
  46. return UA_TRUE;
  47. }
  48. //timeout of session reached so remove it
  49. UA_NodeId sessionId;
  50. UA_Session_getId(*session,&sessionId);
  51. UA_SessionManager_removeSession(&sessionId);
  52. }
  53. return UA_FALSE;
  54. }
  55. UA_Int32 UA_SessionManager_getSessionById(UA_NodeId *sessionId, UA_Session *session)
  56. {
  57. if(sessionManager == UA_NULL)
  58. {
  59. *session = UA_NULL;
  60. return UA_ERROR;
  61. }
  62. UA_list_Element* current = sessionManager->sessions.first;
  63. while (current)
  64. {
  65. if (current->payload)
  66. {
  67. UA_list_Element* elem = (UA_list_Element*) current;
  68. *session = *((UA_Session*) (elem->payload));
  69. if(UA_Session_compareById(*session,sessionId) == UA_EQUAL){
  70. UA_Double pendingLifetime;
  71. UA_Session_getPendingLifetime(*session, &pendingLifetime);
  72. if(pendingLifetime > 0){
  73. return UA_SUCCESS;
  74. }
  75. //session not valid anymore -> remove it
  76. UA_list_removeElement(elem, (UA_list_PayloadVisitor)UA_Session_delete);
  77. *session = UA_NULL;
  78. return UA_ERROR;
  79. }
  80. }
  81. current = current->next;
  82. }
  83. *session = UA_NULL;
  84. return UA_ERROR;
  85. }
  86. UA_Int32 UA_SessionManager_getSessionByToken(UA_NodeId *token, UA_Session *session)
  87. {
  88. if(sessionManager == UA_NULL)
  89. {
  90. *session = UA_NULL;
  91. return UA_ERROR;
  92. }
  93. UA_list_Element* current = sessionManager->sessions.first;
  94. while (current)
  95. {
  96. if (current->payload)
  97. {
  98. UA_list_Element* elem = (UA_list_Element*) current;
  99. *session = *((UA_Session*) (elem->payload));
  100. if(UA_Session_compareByToken(*session,token) == UA_EQUAL){
  101. UA_Double pendingLifetime;
  102. UA_Session_getPendingLifetime(*session, &pendingLifetime);
  103. if(pendingLifetime > 0){
  104. return UA_SUCCESS;
  105. }
  106. //session not valid anymore -> remove it
  107. UA_list_removeElement(elem, (UA_list_PayloadVisitor)UA_Session_delete);
  108. *session = UA_NULL;
  109. return UA_ERROR;
  110. }
  111. }
  112. current = current->next;
  113. }
  114. *session = UA_NULL;
  115. return UA_ERROR;
  116. }
  117. UA_Int32 UA_SessionManager_addSession(UA_Session *session)
  118. {
  119. UA_Int32 retval = UA_SUCCESS;
  120. UA_NodeId sessionId;
  121. if(!UA_SessionManager_sessionExists(session))
  122. {
  123. retval |= UA_list_addPayloadToBack(&sessionManager->sessions,(void*)session);
  124. UA_Session_getId(*session, &sessionId);
  125. printf("UA_SessionManager_addSession - added session with id = %d \n",sessionId.identifier.numeric);
  126. printf("UA_SessionManager_addSession - current session count: %i \n",sessionManager->sessions.size);
  127. return retval;
  128. }
  129. printf("UA_SessionManager_addSession - session already in list");
  130. return UA_ERROR;
  131. }
  132. UA_Int32 UA_SessionManager_removeSession(UA_NodeId *sessionId)
  133. {
  134. UA_Int32 retval = UA_SUCCESS;
  135. UA_list_Element *element = UA_list_search(&sessionManager->sessions,(UA_list_PayloadComparer)UA_Session_compare,sessionId);
  136. if(element)
  137. {
  138. retval |= UA_list_removeElement(element,(UA_list_PayloadVisitor)UA_Session_delete);
  139. printf("UA_SessionManager_removeSession - session removed, current count: %i \n",sessionManager->sessions.size);
  140. }
  141. return retval;
  142. }
  143. UA_Int32 UA_SessionManager_updateSessions()
  144. {
  145. if(sessionManager == UA_NULL)
  146. {
  147. return UA_ERROR;
  148. }
  149. UA_list_Element* current = sessionManager->sessions.first;
  150. while (current)
  151. {
  152. if (current->payload)
  153. {
  154. UA_list_Element* elem = (UA_list_Element*) current;
  155. UA_Session session = *((UA_Session*) (elem->payload));
  156. UA_Double pendingLifetime;
  157. UA_Session_getPendingLifetime(session, &pendingLifetime);
  158. if(pendingLifetime <= 0){
  159. UA_NodeId sessionId;
  160. UA_Session_getId(session,&sessionId);
  161. UA_SessionManager_removeSession(&sessionId);
  162. }
  163. }
  164. current = current->next;
  165. }
  166. return UA_SUCCESS;
  167. }
  168. UA_Int32 UA_SessionManager_getSessionTimeout(UA_Int64 *timeout_ms)
  169. {
  170. if(sessionManager)
  171. {
  172. *timeout_ms = sessionManager->sessionTimeout;
  173. return UA_SUCCESS;
  174. }
  175. *timeout_ms = 0;
  176. return UA_ERROR;
  177. }