client_connect_loop.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /* Enable POSIX features */
  4. #ifndef _XOPEN_SOURCE
  5. # define _XOPEN_SOURCE 600
  6. #endif
  7. #ifndef _DEFAULT_SOURCE
  8. # define _DEFAULT_SOURCE
  9. #endif
  10. /* On older systems we need to define _BSD_SOURCE.
  11. * _DEFAULT_SOURCE is an alias for that. */
  12. #ifndef _BSD_SOURCE
  13. # define _BSD_SOURCE
  14. #endif
  15. /**
  16. * Client disconnect handling
  17. * --------------------------
  18. * This example shows you how to handle a client disconnect, e.g., if the server
  19. * is shut down while the client is connected. You just need to call connect
  20. * again and the client will automatically reconnect.
  21. *
  22. * This example is very similar to the tutorial_client_firststeps.c. */
  23. #include "open62541.h"
  24. #include <signal.h>
  25. #ifdef _WIN32
  26. # include <windows.h>
  27. # define UA_sleep_ms(X) Sleep(X)
  28. #else
  29. # include <unistd.h>
  30. # define UA_sleep_ms(X) usleep(X * 1000)
  31. #endif
  32. UA_Boolean running = true;
  33. UA_Logger logger = UA_Log_Stdout;
  34. static void stopHandler(int sign) {
  35. UA_LOG_INFO(logger, UA_LOGCATEGORY_CLIENT, "Received Ctrl-C");
  36. running = 0;
  37. }
  38. int main(void) {
  39. signal(SIGINT, stopHandler); /* catches ctrl-c */
  40. UA_ClientConfig config = UA_ClientConfig_default;
  41. /* default timeout is 5 seconds. Set it to 1 second here for demo */
  42. config.timeout = 1000;
  43. UA_Client *client = UA_Client_new(config);
  44. /* Read the value attribute of the node. UA_Client_readValueAttribute is a
  45. * wrapper for the raw read service available as UA_Client_Service_read. */
  46. UA_Variant value; /* Variants can hold scalar values and arrays of any type */
  47. UA_Variant_init(&value);
  48. /* Endless loop reading the server time */
  49. while (running) {
  50. /* if already connected, this will return GOOD and do nothing */
  51. /* if the connection is closed/errored, the connection will be reset and then reconnected */
  52. /* Alternatively you can also use UA_Client_getState to get the current state */
  53. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  54. if (retval != UA_STATUSCODE_GOOD) {
  55. UA_LOG_ERROR(logger, UA_LOGCATEGORY_CLIENT, "Not connected. Retrying to connect in 1 second");
  56. /* The connect may timeout after 1 second (see above) or it may fail immediately on network errors */
  57. /* E.g. name resolution errors or unreachable network. Thus there should be a small sleep here */
  58. UA_sleep_ms(1000);
  59. continue;
  60. }
  61. /* NodeId of the variable holding the current time */
  62. const UA_NodeId nodeId =
  63. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  64. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  65. if (retval == UA_STATUSCODE_BADCONNECTIONCLOSED) {
  66. UA_LOG_ERROR(logger, UA_LOGCATEGORY_CLIENT, "Connection was closed. Reconnecting ...");
  67. continue;
  68. }
  69. if (retval == UA_STATUSCODE_GOOD &&
  70. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  71. UA_DateTime raw_date = *(UA_DateTime *) value.data;
  72. UA_String string_date = UA_DateTime_toString(raw_date);
  73. UA_LOG_INFO(logger, UA_LOGCATEGORY_CLIENT, "string date is: %.*s", (int) string_date.length, string_date.data);
  74. UA_String_deleteMembers(&string_date);
  75. }
  76. UA_sleep_ms(1000);
  77. };
  78. /* Clean up */
  79. UA_Variant_deleteMembers(&value);
  80. UA_Client_delete(client); /* Disconnects the client internally */
  81. return UA_STATUSCODE_GOOD;
  82. }