server_repeated_job.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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;
  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. logger = Logger_Stdout_new();
  27. UA_Server_setLogger(server, logger);
  28. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  29. /* add a repeated job to the server */
  30. UA_Job job = {.type = UA_JOBTYPE_METHODCALL,
  31. .job.methodCall = {.method = testCallback, .data = NULL} };
  32. UA_Server_addRepeatedJob(server, job, 2000, NULL); // call every 2 sec
  33. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  34. UA_Server_delete(server);
  35. return retval;
  36. }