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. UA_Boolean running = true;
  14. UA_Logger logger = UA_Log_Stdout;
  15. static void stopHandler(int sign) {
  16. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  17. running = false;
  18. }
  19. static void testCallback(UA_Server *server, void *data) {
  20. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "testcallback");
  21. }
  22. int main(int argc, char** argv) {
  23. signal(SIGINT, stopHandler); /* catches ctrl-c */
  24. UA_ServerConfig config = UA_ServerConfig_standard;
  25. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  26. config.networkLayers = &nl;
  27. config.networkLayersSize = 1;
  28. UA_Server *server = UA_Server_new(config);
  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, &running);
  34. UA_Server_delete(server);
  35. nl.deleteMembers(&nl);
  36. return (int)retval;
  37. }