server_repeated_job.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. #include <signal.h>
  4. #ifdef UA_NO_AMALGAMATION
  5. # include "ua_types.h"
  6. # include "ua_server.h"
  7. # include "ua_config_standard.h"
  8. # include "ua_network_tcp.h"
  9. # include "ua_log_stdout.h"
  10. #else
  11. # include "open62541.h"
  12. #endif
  13. static void
  14. testCallback(UA_Server *server, void *data) {
  15. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "testcallback");
  16. }
  17. UA_Boolean running = true;
  18. static void stopHandler(int sign) {
  19. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  20. running = false;
  21. }
  22. int main(void) {
  23. signal(SIGINT, stopHandler);
  24. signal(SIGTERM, stopHandler);
  25. UA_ServerConfig config = UA_ServerConfig_standard;
  26. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  27. config.networkLayers = &nl;
  28. config.networkLayersSize = 1;
  29. UA_Server *server = UA_Server_new(config);
  30. /* add a repeated job to the server */
  31. UA_Job job = {.type = UA_JOBTYPE_METHODCALL,
  32. .job.methodCall = {.method = testCallback, .data = NULL} };
  33. UA_Server_addRepeatedJob(server, job, 2000, NULL); /* call every 2 sec */
  34. UA_Server_run(server, &running);
  35. UA_Server_delete(server);
  36. nl.deleteMembers(&nl);
  37. return 0;
  38. }