server_repeated_job.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 "ua_config_standard.h"
  10. # include "networklayer_tcp.h"
  11. #else
  12. # include "open62541.h"
  13. #endif
  14. UA_Boolean running = true;
  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 = false;
  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_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_StatusCode retval = UA_Server_run(server, &running);
  35. UA_Server_delete(server);
  36. nl.deleteMembers(&nl);
  37. return (int)retval;
  38. }