client_find_servers.c 7.7 KB

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