server_multicast.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. * A simple server instance which registers with the discovery server.
  5. * Compared to server_register.c this example waits until the LDS server announces
  6. * itself through mDNS. Therefore the LDS server needs to support multicast extension
  7. * (i.e., LDS-ME).
  8. */
  9. #include <signal.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "open62541.h"
  13. UA_Logger logger = UA_Log_Stdout;
  14. UA_Boolean running = true;
  15. static void stopHandler(int sign) {
  16. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  17. running = false;
  18. }
  19. static UA_StatusCode
  20. readInteger(void *handle, const UA_NodeId nodeid, UA_Boolean sourceTimeStamp,
  21. const UA_NumericRange *range, UA_DataValue *dataValue) {
  22. dataValue->hasValue = true;
  23. UA_Variant_setScalarCopy(&dataValue->value, (UA_UInt32 *) handle, &UA_TYPES[UA_TYPES_INT32]);
  24. // we know the nodeid is a string
  25. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "Node read %.*s",
  26. nodeid.identifier.string.length, nodeid.identifier.string.data);
  27. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "read value %i", *(UA_UInt32 *) handle);
  28. return UA_STATUSCODE_GOOD;
  29. }
  30. static UA_StatusCode
  31. writeInteger(void *handle, const UA_NodeId nodeid,
  32. const UA_Variant *data, const UA_NumericRange *range) {
  33. if (UA_Variant_isScalar(data) && data->type == &UA_TYPES[UA_TYPES_INT32] && data->data) {
  34. *(UA_UInt32 *) handle = *(UA_UInt32 *) data->data;
  35. }
  36. // we know the nodeid is a string
  37. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "Node written %.*s",
  38. nodeid.identifier.string.length, nodeid.identifier.string.data);
  39. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "written value %i", *(UA_UInt32 *) handle);
  40. return UA_STATUSCODE_GOOD;
  41. }
  42. char *discovery_url = NULL;
  43. UA_String *self_discovery_url = NULL;
  44. static void
  45. serverOnNetworkCallback(const UA_ServerOnNetwork *serverOnNetwork, UA_Boolean isServerAnnounce,
  46. UA_Boolean isTxtReceived, void *data) {
  47. if (discovery_url != NULL || !isServerAnnounce) {
  48. UA_LOG_DEBUG(logger, UA_LOGCATEGORY_SERVER,
  49. "serverOnNetworkCallback called, but discovery URL "
  50. "already initialized or is not announcing. Ignoring.");
  51. return; // we already have everything we need or we only want server announces
  52. }
  53. if (self_discovery_url != NULL && UA_String_equal(&serverOnNetwork->discoveryUrl, self_discovery_url)) {
  54. // skip self
  55. return;
  56. }
  57. if (!isTxtReceived)
  58. return; // we wait until the corresponding TXT record is announced.
  59. // Problem: how to handle if a Server does not announce the
  60. // optional TXT?
  61. // here you can filter for a specific LDS server, e.g. call FindServers on
  62. // the serverOnNetwork to make sure you are registering with the correct
  63. // LDS. We will ignore this for now
  64. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "Another server announced itself on %.*s",
  65. serverOnNetwork->discoveryUrl.length, serverOnNetwork->discoveryUrl.data);
  66. if (discovery_url != NULL)
  67. free(discovery_url);
  68. discovery_url = malloc(serverOnNetwork->discoveryUrl.length + 1);
  69. memcpy(discovery_url, serverOnNetwork->discoveryUrl.data, serverOnNetwork->discoveryUrl.length);
  70. discovery_url[serverOnNetwork->discoveryUrl.length] = 0;
  71. }
  72. int main(int argc, char **argv) {
  73. signal(SIGINT, stopHandler); /* catches ctrl-c */
  74. signal(SIGTERM, stopHandler);
  75. UA_ServerConfig config = UA_ServerConfig_standard;
  76. // To enable mDNS discovery, set application type to discovery server.
  77. config.applicationDescription.applicationType = UA_APPLICATIONTYPE_DISCOVERYSERVER;
  78. config.applicationDescription.applicationUri =
  79. UA_String_fromChars("urn:open62541.example.server_multicast");
  80. config.mdnsServerName = UA_String_fromChars("Sample Multicast Server");
  81. // See http://www.opcfoundation.org/UA/schemas/1.03/ServerCapabilities.csv
  82. //config.serverCapabilitiesSize = 1;
  83. //UA_String caps = UA_String_fromChars("LDS");
  84. //config.serverCapabilities = &caps;
  85. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16665);
  86. config.networkLayers = &nl;
  87. config.networkLayersSize = 1;
  88. UA_Server *server = UA_Server_new(config);
  89. self_discovery_url = &nl.discoveryUrl;
  90. /* add a variable node to the address space */
  91. UA_Int32 myInteger = 42;
  92. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  93. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  94. UA_DataSource dateDataSource;
  95. dateDataSource.handle = &myInteger;
  96. dateDataSource.read = readInteger;
  97. dateDataSource.write = writeInteger;
  98. UA_VariableAttributes attr;
  99. UA_VariableAttributes_init(&attr);
  100. attr.description = UA_LOCALIZEDTEXT("en_US", "the answer");
  101. attr.displayName = UA_LOCALIZEDTEXT("en_US", "the answer");
  102. UA_Server_addDataSourceVariableNode(server, myIntegerNodeId,
  103. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  104. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  105. myIntegerName, UA_NODEID_NULL, attr, dateDataSource, NULL);
  106. // callback which is called when a new server is detected through mDNS
  107. UA_Server_setServerOnNetworkCallback(server, serverOnNetworkCallback, NULL);
  108. // Start the server and call iterate to wait for the multicast discovery of the LDS
  109. UA_StatusCode retval = UA_Server_run_startup(server);
  110. if (retval != UA_STATUSCODE_GOOD) {
  111. UA_LOG_ERROR(logger, UA_LOGCATEGORY_SERVER,
  112. "Could not start the server. StatusCode %s",
  113. UA_StatusCode_name(retval));
  114. UA_String_deleteMembers(&config.applicationDescription.applicationUri);
  115. UA_Server_delete(server);
  116. nl.deleteMembers(&nl);
  117. free(discovery_url);
  118. return 1;
  119. }
  120. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER,
  121. "Server started. Waiting for announce of LDS Server.");
  122. while (running && discovery_url == NULL)
  123. UA_Server_run_iterate(server, true);
  124. if (!running) {
  125. UA_String_deleteMembers(&config.applicationDescription.applicationUri);
  126. UA_Server_delete(server);
  127. nl.deleteMembers(&nl);
  128. free(discovery_url);
  129. return 1;
  130. }
  131. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "LDS-ME server found on %s", discovery_url);
  132. // periodic server register after 10 Minutes, delay first register for 500ms
  133. retval = UA_Server_addPeriodicServerRegisterJob(server, discovery_url,
  134. 10 * 60 * 1000, 500, NULL);
  135. if (retval != UA_STATUSCODE_GOOD) {
  136. UA_LOG_ERROR(logger, UA_LOGCATEGORY_SERVER,
  137. "Could not create periodic job for server register. StatusCode %s",
  138. UA_StatusCode_name(retval));
  139. UA_String_deleteMembers(&config.applicationDescription.applicationUri);
  140. UA_Server_delete(server);
  141. nl.deleteMembers(&nl);
  142. free(discovery_url);
  143. return 1;
  144. }
  145. while (running)
  146. UA_Server_run_iterate(server, true);
  147. UA_Server_run_shutdown(server);
  148. // UNregister the server from the discovery server.
  149. retval = UA_Server_unregister_discovery(server, discovery_url);
  150. //retval = UA_Server_unregister_discovery(server, "opc.tcp://localhost:4840" );
  151. if (retval != UA_STATUSCODE_GOOD) {
  152. UA_LOG_ERROR(logger, UA_LOGCATEGORY_SERVER,
  153. "Could not unregister server from discovery server. "
  154. "StatusCode %s", UA_StatusCode_name(retval));
  155. UA_String_deleteMembers(&config.applicationDescription.applicationUri);
  156. UA_Server_delete(server);
  157. nl.deleteMembers(&nl);
  158. free(discovery_url);
  159. return (int) retval;
  160. }
  161. UA_String_deleteMembers(&config.applicationDescription.applicationUri);
  162. UA_String_deleteMembers(&config.mdnsServerName);
  163. //UA_Array_delete(config.serverCapabilities, config.serverCapabilitiesSize, &UA_TYPES[UA_TYPES_STRING]);
  164. UA_Server_delete(server);
  165. nl.deleteMembers(&nl);
  166. free(discovery_url);
  167. return (int) retval;
  168. }