ua_transport_connection_manager.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * ua_connection_manager.c
  3. *
  4. * Created on: 11.05.2014
  5. * Author: open62541
  6. */
  7. #include "ua_transport_connection_manager.h"
  8. #include "ua_indexedList.h"
  9. typedef struct UA_TL_ConnectionManager
  10. {
  11. UA_list_List connections;
  12. UA_UInt32 maxConnectionCount;
  13. UA_UInt32 currentConnectionCount;
  14. }UA_TL_ConnectionManager;
  15. static UA_TL_ConnectionManager *connectionManager = UA_NULL;
  16. UA_Int32 UA_TL_ConnectionManager_init(UA_UInt32 maxConnectionCount)
  17. {
  18. UA_Int32 retval = UA_SUCCESS;
  19. if(connectionManager)
  20. {
  21. //connectionManager already exists;
  22. }
  23. else
  24. {
  25. retval |= UA_alloc((void**)connectionManager,sizeof(UA_TL_ConnectionManager));
  26. connectionManager->maxConnectionCount = maxConnectionCount;
  27. connectionManager->currentConnectionCount = 0;
  28. retval |= UA_indexedList_init(&connectionManager->connections);
  29. }
  30. return UA_SUCCESS;
  31. }
  32. UA_Int32 UA_TL_ConnectionManager_addConnection(UA_TL_Connection1 *connection)
  33. {
  34. UA_UInt32 connectionId;
  35. UA_TL_Connection_getHandle(*connection, &connectionId);
  36. printf("UA_TL_ConnectionManager_addConnection - added connection with handle = %d \n", connectionId);
  37. return UA_list_addPayloadToBack(&(connectionManager->connections), (void*)connection);
  38. }
  39. UA_Int32 UA_TL_ConnectionManager_removeConnection(UA_TL_Connection1 connection)
  40. {
  41. UA_list_Element *element = UA_list_find(&connectionManager->connections, (UA_list_PayloadMatcher)UA_TL_Connection_compare);
  42. if(element)
  43. {
  44. UA_list_removeElement(element, (UA_list_PayloadVisitor)UA_TL_Connection_delete);
  45. }
  46. return UA_SUCCESS;
  47. }
  48. UA_Int32 UA_TL_ConnectionManager_getConnectionByHandle(UA_UInt32 connectionId, UA_TL_Connection1 *connection)
  49. {
  50. UA_UInt32 tmpConnectionHandle;
  51. if(connectionManager)
  52. {
  53. UA_list_Element* current = connectionManager->connections.first;
  54. while (current)
  55. {
  56. if (current->payload)
  57. {
  58. UA_list_Element* elem = (UA_list_Element*) current;
  59. *connection = *((UA_TL_Connection1*) (elem->payload));
  60. UA_TL_Connection_getHandle(*connection, &tmpConnectionHandle);
  61. if(tmpConnectionHandle == connectionId)
  62. {
  63. return UA_SUCCESS;
  64. }
  65. }
  66. current = current->next;
  67. }
  68. }
  69. *connection = UA_NULL;
  70. return UA_ERROR;
  71. }