client_find_servers.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <open62541/client_config_default.h>
  8. #include <open62541/client_highlevel.h>
  9. #include <open62541/plugin/log_stdout.h>
  10. #include <stdlib.h>
  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();
  20. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  21. UA_StatusCode retval = UA_Client_findServersOnNetwork(client, DISCOVERY_SERVER_ENDPOINT, 0, 0,
  22. 0, NULL, &serverOnNetworkSize, &serverOnNetwork);
  23. if(retval != UA_STATUSCODE_GOOD) {
  24. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
  25. "Could not call FindServersOnNetwork service. "
  26. "Is the discovery server started? StatusCode %s",
  27. UA_StatusCode_name(retval));
  28. UA_Client_delete(client);
  29. return EXIT_FAILURE;
  30. }
  31. // output all the returned/registered servers
  32. for(size_t i = 0; i < serverOnNetworkSize; i++) {
  33. UA_ServerOnNetwork *server = &serverOnNetwork[i];
  34. printf("Server[%lu]: %.*s", (unsigned long) i,
  35. (int) server->serverName.length, server->serverName.data);
  36. printf("\n\tRecordID: %d", server->recordId);
  37. printf("\n\tDiscovery URL: %.*s", (int) server->discoveryUrl.length,
  38. server->discoveryUrl.data);
  39. printf("\n\tCapabilities: ");
  40. for(size_t j = 0; j < server->serverCapabilitiesSize; j++) {
  41. printf("%.*s,", (int) server->serverCapabilities[j].length,
  42. server->serverCapabilities[j].data);
  43. }
  44. printf("\n\n");
  45. }
  46. UA_Array_delete(serverOnNetwork, serverOnNetworkSize,
  47. &UA_TYPES[UA_TYPES_SERVERONNETWORK]);
  48. }
  49. /* Example for calling FindServers */
  50. UA_ApplicationDescription *applicationDescriptionArray = NULL;
  51. size_t applicationDescriptionArraySize = 0;
  52. UA_StatusCode retval;
  53. {
  54. UA_Client *client = UA_Client_new();
  55. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  56. retval = UA_Client_findServers(client, DISCOVERY_SERVER_ENDPOINT, 0, NULL, 0, NULL,
  57. &applicationDescriptionArraySize, &applicationDescriptionArray);
  58. UA_Client_delete(client);
  59. }
  60. if(retval != UA_STATUSCODE_GOOD) {
  61. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Could not call FindServers service. "
  62. "Is the discovery server started? StatusCode %s", UA_StatusCode_name(retval));
  63. return EXIT_FAILURE;
  64. }
  65. // output all the returned/registered servers
  66. for(size_t i = 0; i < applicationDescriptionArraySize; i++) {
  67. UA_ApplicationDescription *description = &applicationDescriptionArray[i];
  68. printf("Server[%lu]: %.*s", (unsigned long) i, (int) description->applicationUri.length,
  69. description->applicationUri.data);
  70. printf("\n\tName: %.*s", (int) description->applicationName.text.length,
  71. description->applicationName.text.data);
  72. printf("\n\tApplication URI: %.*s", (int) description->applicationUri.length,
  73. description->applicationUri.data);
  74. printf("\n\tProduct URI: %.*s", (int) description->productUri.length,
  75. description->productUri.data);
  76. printf("\n\tType: ");
  77. switch(description->applicationType) {
  78. case UA_APPLICATIONTYPE_SERVER:
  79. printf("Server");
  80. break;
  81. case UA_APPLICATIONTYPE_CLIENT:
  82. printf("Client");
  83. break;
  84. case UA_APPLICATIONTYPE_CLIENTANDSERVER:
  85. printf("Client and Server");
  86. break;
  87. case UA_APPLICATIONTYPE_DISCOVERYSERVER:
  88. printf("Discovery Server");
  89. break;
  90. default:
  91. printf("Unknown");
  92. }
  93. printf("\n\tDiscovery URLs:");
  94. for(size_t j = 0; j < description->discoveryUrlsSize; j++) {
  95. printf("\n\t\t[%lu]: %.*s", (unsigned long) j,
  96. (int) description->discoveryUrls[j].length,
  97. description->discoveryUrls[j].data);
  98. }
  99. printf("\n\n");
  100. }
  101. /*
  102. * Now that we have the list of available servers, call get endpoints on all of them
  103. */
  104. printf("-------- Server Endpoints --------\n");
  105. for(size_t i = 0; i < applicationDescriptionArraySize; i++) {
  106. UA_ApplicationDescription *description = &applicationDescriptionArray[i];
  107. if(description->discoveryUrlsSize == 0) {
  108. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT,
  109. "[GetEndpoints] Server %.*s did not provide any discovery urls. Skipping.",
  110. (int)description->applicationUri.length, description->applicationUri.data);
  111. continue;
  112. }
  113. printf("\nEndpoints for Server[%lu]: %.*s\n", (unsigned long) i,
  114. (int) description->applicationUri.length, description->applicationUri.data);
  115. UA_Client *client = UA_Client_new();
  116. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  117. char *discoveryUrl = (char *) UA_malloc(sizeof(char) * description->discoveryUrls[0].length + 1);
  118. memcpy(discoveryUrl, description->discoveryUrls[0].data, description->discoveryUrls[0].length);
  119. discoveryUrl[description->discoveryUrls[0].length] = '\0';
  120. UA_EndpointDescription *endpointArray = NULL;
  121. size_t endpointArraySize = 0;
  122. //TODO: adapt to the new async getEndpoint
  123. retval = UA_Client_getEndpoints(client, discoveryUrl, &endpointArraySize, &endpointArray);
  124. UA_free(discoveryUrl);
  125. if(retval != UA_STATUSCODE_GOOD) {
  126. UA_Client_disconnect(client);
  127. UA_Client_delete(client);
  128. break;
  129. }
  130. for(size_t j = 0; j < endpointArraySize; j++) {
  131. UA_EndpointDescription *endpoint = &endpointArray[j];
  132. printf("\n\tEndpoint[%lu]:", (unsigned long) j);
  133. printf("\n\t\tEndpoint URL: %.*s", (int) endpoint->endpointUrl.length, endpoint->endpointUrl.data);
  134. printf("\n\t\tTransport profile URI: %.*s", (int) endpoint->transportProfileUri.length,
  135. endpoint->transportProfileUri.data);
  136. printf("\n\t\tSecurity Mode: ");
  137. switch(endpoint->securityMode) {
  138. case UA_MESSAGESECURITYMODE_INVALID:
  139. printf("Invalid");
  140. break;
  141. case UA_MESSAGESECURITYMODE_NONE:
  142. printf("None");
  143. break;
  144. case UA_MESSAGESECURITYMODE_SIGN:
  145. printf("Sign");
  146. break;
  147. case UA_MESSAGESECURITYMODE_SIGNANDENCRYPT:
  148. printf("Sign and Encrypt");
  149. break;
  150. default:
  151. printf("No valid security mode");
  152. break;
  153. }
  154. printf("\n\t\tSecurity profile URI: %.*s", (int) endpoint->securityPolicyUri.length,
  155. endpoint->securityPolicyUri.data);
  156. printf("\n\t\tSecurity Level: %d", endpoint->securityLevel);
  157. }
  158. UA_Array_delete(endpointArray, endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  159. UA_Client_delete(client);
  160. }
  161. printf("\n");
  162. UA_Array_delete(applicationDescriptionArray, applicationDescriptionArraySize,
  163. &UA_TYPES[UA_TYPES_APPLICATIONDESCRIPTION]);
  164. return EXIT_SUCCESS;
  165. }