server_lds.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. * Server representing a local discovery server as a central instance.
  5. * Any other server can register with this server (see server_register.c). Clients can then call the
  6. * find servers service to get all registered servers (see client_find_servers.c).
  7. */
  8. #include <open62541/server.h>
  9. #include <open62541/server_config_default.h>
  10. #include <signal.h>
  11. #include <stdlib.h>
  12. UA_Boolean running = true;
  13. static void stopHandler(int sig) {
  14. running = false;
  15. }
  16. int main(void) {
  17. signal(SIGINT, stopHandler);
  18. signal(SIGTERM, stopHandler);
  19. UA_Server *server = UA_Server_new();
  20. UA_ServerConfig *config = UA_Server_getConfig(server);
  21. UA_ServerConfig_setDefault(config);
  22. // This is an LDS server only. Set the application type to DISCOVERYSERVER.
  23. // NOTE: This will cause UaExpert to not show this instance in the server list.
  24. // See also: https://forum.unified-automation.com/topic1987.html
  25. config->applicationDescription.applicationType = UA_APPLICATIONTYPE_DISCOVERYSERVER;
  26. UA_String_clear(&config->applicationDescription.applicationUri);
  27. config->applicationDescription.applicationUri =
  28. UA_String_fromChars("urn:open62541.example.local_discovery_server");
  29. // Enable the mDNS announce and response functionality
  30. config->discovery.mdnsEnable = true;
  31. config->discovery.mdns.mdnsServerName = UA_String_fromChars("LDS");
  32. // See http://www.opcfoundation.org/UA/schemas/1.03/ServerCapabilities.csv
  33. // For a LDS server, you should only indicate the LDS capability.
  34. // If this instance is an LDS and at the same time a normal OPC UA server, you also have to indicate
  35. // the additional capabilities.
  36. // NOTE: UaExpert does not show LDS-only servers in the list.
  37. // See also: https://forum.unified-automation.com/topic1987.html
  38. // E.g. here we only set LDS, and you will not see it in UaExpert
  39. config->discovery.mdns.serverCapabilitiesSize = 1;
  40. UA_String *caps = (UA_String *) UA_Array_new(1, &UA_TYPES[UA_TYPES_STRING]);
  41. caps[0] = UA_String_fromChars("LDS");
  42. config->discovery.mdns.serverCapabilities = caps;
  43. /* timeout in seconds when to automatically remove a registered server from
  44. * the list, if it doesn't re-register within the given time frame. A value
  45. * of 0 disables automatic removal. Default is 60 Minutes (60*60). Must be
  46. * bigger than 10 seconds, because cleanup is only triggered approximately
  47. * every 10 seconds. The server will still be removed depending on the
  48. * state of the semaphore file. */
  49. // config->discoveryCleanupTimeout = 60*60;
  50. UA_StatusCode retval = UA_Server_run(server, &running);
  51. UA_Server_delete(server);
  52. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  53. }