server_discovery.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 <stdio.h>
  9. #include <signal.h>
  10. #ifdef UA_NO_AMALGAMATION
  11. # include "ua_types.h"
  12. # include "ua_server.h"
  13. # include "ua_config_standard.h"
  14. # include "ua_network_tcp.h"
  15. #else
  16. # include "open62541.h"
  17. #endif
  18. UA_Boolean running = true;
  19. static void stopHandler(int sig) {
  20. running = false;
  21. }
  22. int main(void) {
  23. signal(SIGINT, stopHandler);
  24. signal(SIGTERM, stopHandler);
  25. UA_ServerConfig config = UA_ServerConfig_standard;
  26. config.applicationDescription.applicationType = UA_APPLICATIONTYPE_DISCOVERYSERVER;
  27. config.applicationDescription.applicationUri = UA_String_fromChars("open62541.example.local_discovery_server");
  28. // timeout in seconds when to automatically remove a registered server from the list,
  29. // if it doesn't re-register within the given time frame. A value of 0 disables automatic removal.
  30. // Default is 60 Minutes (60*60). Must be bigger than 10 seconds, because cleanup is only triggered approximately
  31. // ervery 10 seconds.
  32. // The server will still be removed depending on the state of the semaphore file.
  33. // config.discoveryCleanupTimeout = 60*60;
  34. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 4840);
  35. config.networkLayers = &nl;
  36. config.networkLayersSize = 1;
  37. UA_Server *server = UA_Server_new(config);
  38. UA_StatusCode retval = UA_Server_run(server, &running);
  39. UA_String_deleteMembers(&config.applicationDescription.applicationUri);
  40. UA_Server_delete(server);
  41. nl.deleteMembers(&nl);
  42. return (int)retval;
  43. }