client_find_servers.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. /**
  4. * This client requests all the available servers from the discovery server (see server_lds.c)
  5. * and then calls GetEndpoints on the returned list of servers.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <open62541.h>
  10. UA_Logger logger = UA_Log_Stdout;
  11. #define DISCOVERY_SERVER_ENDPOINT "opc.tcp://localhost:4840"
  12. int main(void) {
  13. /*
  14. * Example for calling FindServersOnNetwork
  15. */
  16. {
  17. UA_ServerOnNetwork *serverOnNetwork = NULL;
  18. size_t serverOnNetworkSize = 0;
  19. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  20. UA_StatusCode retval = UA_Client_findServersOnNetwork(client, DISCOVERY_SERVER_ENDPOINT, 0, 0,
  21. 0, NULL, &serverOnNetworkSize, &serverOnNetwork);
  22. if (retval != UA_STATUSCODE_GOOD) {
  23. UA_LOG_ERROR(logger, UA_LOGCATEGORY_SERVER, "Could not call FindServersOnNetwork service. Is the discovery server started? StatusCode %s",
  24. UA_StatusCode_name(retval));
  25. UA_Client_delete(client);
  26. return (int) retval;
  27. }
  28. // output all the returned/registered servers
  29. for (size_t i = 0; i < serverOnNetworkSize; i++) {
  30. UA_ServerOnNetwork *server = &serverOnNetwork[i];
  31. printf("Server[%lu]: %.*s", (unsigned long) i, (int) server->serverName.length, server->serverName.data);
  32. printf("\n\tRecordID: %d", server->recordId);
  33. printf("\n\tDiscovery URL: %.*s", (int) server->discoveryUrl.length, server->discoveryUrl.data);
  34. printf("\n\tCapabilities: ");
  35. for (size_t j = 0; j < server->serverCapabilitiesSize; j++) {
  36. printf("%.*s,", (int) server->serverCapabilities[j].length, server->serverCapabilities[j].data);
  37. }
  38. printf("\n\n");
  39. }
  40. UA_Array_delete(serverOnNetwork, serverOnNetworkSize, &UA_TYPES[UA_TYPES_SERVERONNETWORK]);
  41. }
  42. /*
  43. * Example for calling FindServers
  44. */
  45. UA_ApplicationDescription *applicationDescriptionArray = NULL;
  46. size_t applicationDescriptionArraySize = 0;
  47. UA_StatusCode retval;
  48. {
  49. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  50. retval = UA_Client_findServers(client, DISCOVERY_SERVER_ENDPOINT, 0, NULL, 0, NULL,
  51. &applicationDescriptionArraySize, &applicationDescriptionArray);
  52. UA_Client_delete(client);
  53. }
  54. if (retval != UA_STATUSCODE_GOOD) {
  55. UA_LOG_ERROR(logger, UA_LOGCATEGORY_SERVER, "Could not call FindServers service. "
  56. "Is the discovery server started? StatusCode %s", UA_StatusCode_name(retval));
  57. return (int) retval;
  58. }
  59. // output all the returned/registered servers
  60. for (size_t i = 0; i < applicationDescriptionArraySize; i++) {
  61. UA_ApplicationDescription *description = &applicationDescriptionArray[i];
  62. printf("Server[%lu]: %.*s", (unsigned long) i, (int) description->applicationUri.length,
  63. description->applicationUri.data);
  64. printf("\n\tName: %.*s", (int) description->applicationName.text.length,
  65. description->applicationName.text.data);
  66. printf("\n\tApplication URI: %.*s", (int) description->applicationUri.length,
  67. description->applicationUri.data);
  68. printf("\n\tProduct URI: %.*s", (int) description->productUri.length,
  69. description->productUri.data);
  70. printf("\n\tType: ");
  71. switch (description->applicationType) {
  72. case UA_APPLICATIONTYPE_SERVER:
  73. printf("Server");
  74. break;
  75. case UA_APPLICATIONTYPE_CLIENT:
  76. printf("Client");
  77. break;
  78. case UA_APPLICATIONTYPE_CLIENTANDSERVER:
  79. printf("Client and Server");
  80. break;
  81. case UA_APPLICATIONTYPE_DISCOVERYSERVER:
  82. printf("Discovery Server");
  83. break;
  84. default:
  85. printf("Unknown");
  86. }
  87. printf("\n\tDiscovery URLs:");
  88. for (size_t j = 0; j < description->discoveryUrlsSize; j++) {
  89. printf("\n\t\t[%lu]: %.*s", (unsigned long) j,
  90. (int) description->discoveryUrls[j].length,
  91. description->discoveryUrls[j].data);
  92. }
  93. printf("\n\n");
  94. }
  95. /*
  96. * Now that we have the list of available servers, call get endpoints on all of them
  97. */
  98. printf("-------- Server Endpoints --------\n");
  99. for (size_t i = 0; i < applicationDescriptionArraySize; i++) {
  100. UA_ApplicationDescription *description = &applicationDescriptionArray[i];
  101. if (description->discoveryUrlsSize == 0) {
  102. UA_LOG_INFO(logger, UA_LOGCATEGORY_CLIENT,
  103. "[GetEndpoints] Server %.*s did not provide any discovery urls. Skipping.",
  104. description->applicationUri);
  105. continue;
  106. }
  107. printf("\nEndpoints for Server[%lu]: %.*s\n", (unsigned long) i,
  108. (int) description->applicationUri.length, description->applicationUri.data);
  109. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  110. char *discoveryUrl = (char *) UA_malloc(sizeof(char) * description->discoveryUrls[0].length + 1);
  111. memcpy(discoveryUrl, description->discoveryUrls[0].data, description->discoveryUrls[0].length);
  112. discoveryUrl[description->discoveryUrls[0].length] = '\0';
  113. UA_EndpointDescription *endpointArray = NULL;
  114. size_t endpointArraySize = 0;
  115. retval = UA_Client_getEndpoints(client, discoveryUrl, &endpointArraySize, &endpointArray);
  116. UA_free(discoveryUrl);
  117. if (retval != UA_STATUSCODE_GOOD) {
  118. UA_Client_disconnect(client);
  119. UA_Client_delete(client);
  120. break;
  121. }
  122. for (size_t j = 0; j < endpointArraySize; j++) {
  123. UA_EndpointDescription *endpoint = &endpointArray[j];
  124. printf("\n\tEndpoint[%lu]:", (unsigned long) j);
  125. printf("\n\t\tEndpoint URL: %.*s", (int) endpoint->endpointUrl.length, endpoint->endpointUrl.data);
  126. printf("\n\t\tTransport profile URI: %.*s", (int) endpoint->transportProfileUri.length,
  127. endpoint->transportProfileUri.data);
  128. printf("\n\t\tSecurity Mode: ");
  129. switch (endpoint->securityMode) {
  130. case UA_MESSAGESECURITYMODE_INVALID:
  131. printf("Invalid");
  132. case UA_MESSAGESECURITYMODE_NONE:
  133. printf("None");
  134. case UA_MESSAGESECURITYMODE_SIGN:
  135. printf("Sign");
  136. case UA_MESSAGESECURITYMODE_SIGNANDENCRYPT:
  137. printf("Sign and Encrypt");
  138. }
  139. printf("\n\t\tSecurity profile URI: %.*s", (int) endpoint->securityPolicyUri.length,
  140. endpoint->securityPolicyUri.data);
  141. printf("\n\t\tSecurity Level: %d", endpoint->securityLevel);
  142. }
  143. UA_Array_delete(endpointArray, endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  144. UA_Client_delete(client);
  145. }
  146. printf("\n");
  147. UA_Array_delete(applicationDescriptionArray, applicationDescriptionArraySize,
  148. &UA_TYPES[UA_TYPES_APPLICATIONDESCRIPTION]);
  149. return (int) UA_STATUSCODE_GOOD;
  150. }