ua_transport_connection_manager.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 == UA_NULL)
  20. {
  21. retval |= UA_alloc((void**)&connectionManager,sizeof(UA_TL_ConnectionManager));
  22. connectionManager->maxConnectionCount = maxConnectionCount;
  23. connectionManager->currentConnectionCount = 0;
  24. retval |= UA_indexedList_init(&connectionManager->connections);
  25. return UA_SUCCESS;
  26. }
  27. return UA_ERROR; //connection Manager already exists
  28. }
  29. UA_Int32 UA_TL_ConnectionManager_addConnection(UA_TL_Connection *connection)
  30. {
  31. UA_UInt32 connectionId;
  32. UA_TL_Connection_getHandle(*connection, &connectionId);
  33. printf("UA_TL_ConnectionManager_addConnection - added connection with handle = %d \n", connectionId);
  34. return UA_list_addPayloadToBack(&(connectionManager->connections), (void*)connection);
  35. }
  36. UA_Int32 UA_TL_ConnectionManager_removeConnection(UA_TL_Connection connection)
  37. {
  38. UA_list_Element *element = UA_list_find(&connectionManager->connections, (UA_list_PayloadMatcher)UA_TL_Connection_compare);
  39. if(element)
  40. {
  41. UA_list_removeElement(element, (UA_list_PayloadVisitor)UA_TL_Connection_delete);
  42. }
  43. return UA_SUCCESS;
  44. }
  45. UA_Int32 UA_TL_ConnectionManager_getConnectionByHandle(UA_UInt32 connectionId, UA_TL_Connection *connection)
  46. {
  47. UA_UInt32 tmpConnectionHandle;
  48. if(connectionManager)
  49. {
  50. UA_list_Element* current = connectionManager->connections.first;
  51. while (current)
  52. {
  53. if (current->payload)
  54. {
  55. UA_list_Element* elem = (UA_list_Element*) current;
  56. *connection = *((UA_TL_Connection*) (elem->payload));
  57. UA_TL_Connection_getHandle(*connection, &tmpConnectionHandle);
  58. if(tmpConnectionHandle == connectionId)
  59. {
  60. return UA_SUCCESS;
  61. }
  62. }
  63. current = current->next;
  64. }
  65. }
  66. *connection = UA_NULL;
  67. return UA_ERROR;
  68. }