client_connect_loop.c 3.3 KB

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