server_repeated_job.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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_ServerConfig config = UA_ServerConfig_standard;
  26. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  27. config.logger = Logger_Stdout;
  28. config.networkLayers = &nl;
  29. config.networkLayersSize = 1;
  30. UA_Server *server = UA_Server_new(config);
  31. /* add a repeated job to the server */
  32. UA_Job job = {.type = UA_JOBTYPE_METHODCALL,
  33. .job.methodCall = {.method = testCallback, .data = NULL} };
  34. UA_Server_addRepeatedJob(server, job, 2000, NULL); // call every 2 sec
  35. UA_StatusCode retval = UA_Server_run(server, &running);
  36. UA_Server_delete(server);
  37. nl.deleteMembers(&nl);
  38. return retval;
  39. }