server_repeated_job.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  3. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  4. */
  5. #include <signal.h>
  6. #ifdef UA_NO_AMALGAMATION
  7. # include "ua_types.h"
  8. # include "ua_server.h"
  9. # include "logger_stdout.h"
  10. # include "networklayer_tcp.h"
  11. #else
  12. # include "open62541.h"
  13. #endif
  14. UA_Boolean running = 1;
  15. UA_Logger logger = Logger_Stdout;
  16. static void stopHandler(int sign) {
  17. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  18. running = 0;
  19. }
  20. static void testCallback(UA_Server *server, void *data) {
  21. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "testcallback");
  22. }
  23. int main(int argc, char** argv) {
  24. signal(SIGINT, stopHandler); /* catches ctrl-c */
  25. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  26. UA_Server_setLogger(server, logger);
  27. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  28. /* add a repeated job to the server */
  29. UA_Job job = {.type = UA_JOBTYPE_METHODCALL,
  30. .job.methodCall = {.method = testCallback, .data = NULL} };
  31. UA_Server_addRepeatedJob(server, job, 2000, NULL); // call every 2 sec
  32. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  33. UA_Server_delete(server);
  34. return retval;
  35. }