client_find_servers.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_default);
  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,
  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(UA_ClientConfig_default);
  54. retval = UA_Client_findServers(client, DISCOVERY_SERVER_ENDPOINT, 0, NULL, 0, NULL,
  55. &applicationDescriptionArraySize, &applicationDescriptionArray);
  56. UA_Client_delete(client);
  57. }
  58. if(retval != UA_STATUSCODE_GOOD) {
  59. UA_LOG_ERROR(logger, UA_LOGCATEGORY_SERVER, "Could not call FindServers service. "
  60. "Is the discovery server started? StatusCode %s", UA_StatusCode_name(retval));
  61. return (int) retval;
  62. }
  63. // output all the returned/registered servers
  64. for(size_t i = 0; i < applicationDescriptionArraySize; i++) {
  65. UA_ApplicationDescription *description = &applicationDescriptionArray[i];
  66. printf("Server[%lu]: %.*s", (unsigned long) i, (int) description->applicationUri.length,
  67. description->applicationUri.data);
  68. printf("\n\tName: %.*s", (int) description->applicationName.text.length,
  69. description->applicationName.text.data);
  70. printf("\n\tApplication URI: %.*s", (int) description->applicationUri.length,
  71. description->applicationUri.data);
  72. printf("\n\tProduct URI: %.*s", (int) description->productUri.length,
  73. description->productUri.data);
  74. printf("\n\tType: ");
  75. switch(description->applicationType) {
  76. case UA_APPLICATIONTYPE_SERVER:
  77. printf("Server");
  78. break;
  79. case UA_APPLICATIONTYPE_CLIENT:
  80. printf("Client");
  81. break;
  82. case UA_APPLICATIONTYPE_CLIENTANDSERVER:
  83. printf("Client and Server");
  84. break;
  85. case UA_APPLICATIONTYPE_DISCOVERYSERVER:
  86. printf("Discovery Server");
  87. break;
  88. default:
  89. printf("Unknown");
  90. }
  91. printf("\n\tDiscovery URLs:");
  92. for(size_t j = 0; j < description->discoveryUrlsSize; j++) {
  93. printf("\n\t\t[%lu]: %.*s", (unsigned long) j,
  94. (int) description->discoveryUrls[j].length,
  95. description->discoveryUrls[j].data);
  96. }
  97. printf("\n\n");
  98. }
  99. /*
  100. * Now that we have the list of available servers, call get endpoints on all of them
  101. */
  102. printf("-------- Server Endpoints --------\n");
  103. for(size_t i = 0; i < applicationDescriptionArraySize; i++) {
  104. UA_ApplicationDescription *description = &applicationDescriptionArray[i];
  105. if(description->discoveryUrlsSize == 0) {
  106. UA_LOG_INFO(logger, UA_LOGCATEGORY_CLIENT,
  107. "[GetEndpoints] Server %.*s did not provide any discovery urls. Skipping.",
  108. (int)description->applicationUri.length, description->applicationUri.data);
  109. continue;
  110. }
  111. printf("\nEndpoints for Server[%lu]: %.*s\n", (unsigned long) i,
  112. (int) description->applicationUri.length, description->applicationUri.data);
  113. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  114. char *discoveryUrl = (char *) UA_malloc(sizeof(char) * description->discoveryUrls[0].length + 1);
  115. memcpy(discoveryUrl, description->discoveryUrls[0].data, description->discoveryUrls[0].length);
  116. discoveryUrl[description->discoveryUrls[0].length] = '\0';
  117. UA_EndpointDescription *endpointArray = NULL;
  118. size_t endpointArraySize = 0;
  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. }