client_find_servers.c 7.9 KB

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