server_multicast.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. (int)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. (int)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. (int)serverOnNetwork->discoveryUrl.length, serverOnNetwork->discoveryUrl.data);
  66. if (discovery_url != NULL)
  67. UA_free(discovery_url);
  68. discovery_url = (char*)UA_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_new_default();
  76. // To enable mDNS discovery, set application type to discovery server.
  77. config->applicationDescription.applicationType = UA_APPLICATIONTYPE_DISCOVERYSERVER;
  78. UA_String_deleteMembers(&config->applicationDescription.applicationUri);
  79. config->applicationDescription.applicationUri =
  80. UA_String_fromChars("urn:open62541.example.server_multicast");
  81. config->mdnsServerName = UA_String_fromChars("Sample Multicast Server");
  82. // See http://www.opcfoundation.org/UA/schemas/1.03/ServerCapabilities.csv
  83. //config.serverCapabilitiesSize = 1;
  84. //UA_String caps = UA_String_fromChars("LDS");
  85. //config.serverCapabilities = &caps;
  86. UA_Server *server = UA_Server_new(config);
  87. self_discovery_url = &config->networkLayers[0].discoveryUrl;
  88. /* add a variable node to the address space */
  89. UA_Int32 myInteger = 42;
  90. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  91. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  92. UA_DataSource dateDataSource;
  93. dateDataSource.handle = &myInteger;
  94. dateDataSource.read = readInteger;
  95. dateDataSource.write = writeInteger;
  96. UA_VariableAttributes attr;
  97. UA_VariableAttributes_init(&attr);
  98. attr.description = UA_LOCALIZEDTEXT("en_US", "the answer");
  99. attr.displayName = UA_LOCALIZEDTEXT("en_US", "the answer");
  100. UA_Server_addDataSourceVariableNode(server, myIntegerNodeId,
  101. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  102. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  103. myIntegerName, UA_NODEID_NULL, attr, dateDataSource, NULL);
  104. // callback which is called when a new server is detected through mDNS
  105. UA_Server_setServerOnNetworkCallback(server, serverOnNetworkCallback, NULL);
  106. // Start the server and call iterate to wait for the multicast discovery of the LDS
  107. UA_StatusCode retval = UA_Server_run_startup(server);
  108. if (retval != UA_STATUSCODE_GOOD) {
  109. UA_LOG_ERROR(logger, UA_LOGCATEGORY_SERVER,
  110. "Could not start the server. StatusCode %s",
  111. UA_StatusCode_name(retval));
  112. UA_Server_delete(server);
  113. UA_ServerConfig_delete(config);
  114. UA_free(discovery_url);
  115. return 1;
  116. }
  117. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER,
  118. "Server started. Waiting for announce of LDS Server.");
  119. while (running && discovery_url == NULL)
  120. UA_Server_run_iterate(server, true);
  121. if (!running) {
  122. UA_Server_delete(server);
  123. UA_ServerConfig_delete(config);
  124. UA_free(discovery_url);
  125. return 1;
  126. }
  127. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "LDS-ME server found on %s", discovery_url);
  128. // periodic server register after 10 Minutes, delay first register for 500ms
  129. retval = UA_Server_addPeriodicServerRegisterCallback(server, discovery_url,
  130. 10 * 60 * 1000, 500, NULL);
  131. if (retval != UA_STATUSCODE_GOOD) {
  132. UA_LOG_ERROR(logger, UA_LOGCATEGORY_SERVER,
  133. "Could not create periodic job for server register. StatusCode %s",
  134. UA_StatusCode_name(retval));
  135. UA_Server_delete(server);
  136. UA_ServerConfig_delete(config);
  137. return 1;
  138. }
  139. while (running)
  140. UA_Server_run_iterate(server, true);
  141. UA_Server_run_shutdown(server);
  142. // UNregister the server from the discovery server.
  143. retval = UA_Server_unregister_discovery(server, discovery_url);
  144. //retval = UA_Server_unregister_discovery(server, "opc.tcp://localhost:4840" );
  145. if(retval != UA_STATUSCODE_GOOD)
  146. UA_LOG_ERROR(logger, UA_LOGCATEGORY_SERVER,
  147. "Could not unregister server from discovery server. "
  148. "StatusCode %s", UA_StatusCode_name(retval));
  149. UA_Server_delete(server);
  150. UA_ServerConfig_delete(config);
  151. UA_free(discovery_url);
  152. return (int)retval;
  153. }