ua_transport_connection_manager.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. UA_list_List connections;
  11. UA_UInt32 maxConnectionCount;
  12. UA_UInt32 currentConnectionCount;
  13. } UA_TL_ConnectionManager;
  14. static UA_TL_ConnectionManager *connectionManager = UA_NULL;
  15. UA_Int32 UA_TL_ConnectionManager_init(UA_UInt32 maxConnectionCount) {
  16. UA_Int32 retval = UA_SUCCESS;
  17. if(connectionManager != UA_NULL)
  18. return UA_ERROR; //connection Manager already exists
  19. retval |= UA_alloc((void**)&connectionManager,sizeof(UA_TL_ConnectionManager));
  20. connectionManager->maxConnectionCount = maxConnectionCount;
  21. connectionManager->currentConnectionCount = 0;
  22. retval |= UA_indexedList_init(&connectionManager->connections);
  23. return UA_SUCCESS;
  24. }
  25. UA_Int32 UA_TL_ConnectionManager_addConnection(UA_TL_Connection *connection) {
  26. UA_UInt32 connectionId;
  27. UA_TL_Connection_getHandle(connection, &connectionId);
  28. printf("UA_TL_ConnectionManager_addConnection - added connection with handle = %d \n", connectionId);
  29. return UA_list_addPayloadToBack(&(connectionManager->connections), (void*)connection);
  30. }
  31. UA_Int32 UA_TL_ConnectionManager_removeConnection(UA_TL_Connection *connection) {
  32. UA_list_Element *element = UA_list_find(&connectionManager->connections, (UA_list_PayloadMatcher)UA_TL_Connection_compare);
  33. if(element) {
  34. UA_list_removeElement(element, (UA_list_PayloadVisitor)UA_TL_Connection_delete);
  35. }
  36. return UA_SUCCESS;
  37. }
  38. UA_Int32 UA_TL_ConnectionManager_getConnectionByHandle(UA_UInt32 connectionId, UA_TL_Connection **connection) {
  39. UA_UInt32 tmpConnectionHandle;
  40. if(connectionManager) {
  41. UA_list_Element* current = connectionManager->connections.first;
  42. while (current) {
  43. if (current->payload) {
  44. UA_list_Element* elem = (UA_list_Element*) current;
  45. *connection = ((UA_TL_Connection*) (elem->payload));
  46. UA_TL_Connection_getHandle(*connection, &tmpConnectionHandle);
  47. if(tmpConnectionHandle == connectionId)
  48. return UA_SUCCESS;
  49. }
  50. current = current->next;
  51. }
  52. }
  53. *connection = UA_NULL;
  54. return UA_ERROR;
  55. }