client_connect_loop.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #include "open62541.h"
  12. #include <signal.h>
  13. UA_Boolean running = true;
  14. static void stopHandler(int sign) {
  15. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Received Ctrl-C");
  16. running = 0;
  17. }
  18. int main(void) {
  19. signal(SIGINT, stopHandler); /* catches ctrl-c */
  20. UA_ClientConfig config = UA_ClientConfig_default;
  21. /* default timeout is 5 seconds. Set it to 1 second here for demo */
  22. config.timeout = 1000;
  23. UA_Client *client = UA_Client_new(config);
  24. /* Read the value attribute of the node. UA_Client_readValueAttribute is a
  25. * wrapper for the raw read service available as UA_Client_Service_read. */
  26. UA_Variant value; /* Variants can hold scalar values and arrays of any type */
  27. UA_Variant_init(&value);
  28. /* Endless loop reading the server time */
  29. while(running) {
  30. /* if already connected, this will return GOOD and do nothing */
  31. /* if the connection is closed/errored, the connection will be reset and then reconnected */
  32. /* Alternatively you can also use UA_Client_getState to get the current state */
  33. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  34. if(retval != UA_STATUSCODE_GOOD) {
  35. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "Not connected. Retrying to connect in 1 second");
  36. /* The connect may timeout after 1 second (see above) or it may fail immediately on network errors */
  37. /* E.g. name resolution errors or unreachable network. Thus there should be a small sleep here */
  38. UA_sleep_ms(1000);
  39. continue;
  40. }
  41. /* NodeId of the variable holding the current time */
  42. const UA_NodeId nodeId =
  43. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  44. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  45. if(retval == UA_STATUSCODE_BADCONNECTIONCLOSED) {
  46. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT,
  47. "Connection was closed. Reconnecting ...");
  48. continue;
  49. }
  50. if(retval == UA_STATUSCODE_GOOD &&
  51. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  52. UA_DateTime raw_date = *(UA_DateTime *) value.data;
  53. UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
  54. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  55. "date is: %02u-%02u-%04u %02u:%02u:%02u.%03u",
  56. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  57. }
  58. UA_Variant_clear(&value);
  59. UA_sleep_ms(1000);
  60. };
  61. /* Clean up */
  62. UA_Variant_clear(&value);
  63. UA_Client_delete(client); /* Disconnects the client internally */
  64. return UA_STATUSCODE_GOOD;
  65. }