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