client_find_servers.c 7.7 KB

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