|
@@ -0,0 +1,84 @@
|
|
|
+
|
|
|
+ * See http:
|
|
|
+
|
|
|
+
|
|
|
+ * Client disconnect handling
|
|
|
+ * --------------------------
|
|
|
+ * This example shows you how to handle a client disconnect, e.g., if the server
|
|
|
+ * is shut down while the client is connected. You just need to call connect
|
|
|
+ * again and the client will automatically reconnect.
|
|
|
+ *
|
|
|
+ * This example is very similar to the tutorial_client_firststeps.c
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+#include "open62541.h"
|
|
|
+#include <signal.h>
|
|
|
+
|
|
|
+#ifdef _WIN32
|
|
|
+# include <windows.h>
|
|
|
+# define UA_sleep_ms(X) Sleep(X)
|
|
|
+#else
|
|
|
+# include <unistd.h>
|
|
|
+# define UA_sleep_ms(X) usleep(X * 1000)
|
|
|
+#endif
|
|
|
+
|
|
|
+UA_Boolean running = true;
|
|
|
+UA_Logger logger = UA_Log_Stdout;
|
|
|
+
|
|
|
+static void stopHandler(int sign) {
|
|
|
+ UA_LOG_INFO(logger, UA_LOGCATEGORY_CLIENT, "Received Ctrl-C");
|
|
|
+ running = 0;
|
|
|
+}
|
|
|
+
|
|
|
+int main(void) {
|
|
|
+ signal(SIGINT, stopHandler);
|
|
|
+
|
|
|
+ UA_ClientConfig config = UA_ClientConfig_default;
|
|
|
+
|
|
|
+ config.timeout = 1000;
|
|
|
+ UA_Client *client = UA_Client_new(config);
|
|
|
+
|
|
|
+
|
|
|
+ * wrapper for the raw read service available as UA_Client_Service_read. */
|
|
|
+ UA_Variant value;
|
|
|
+ UA_Variant_init(&value);
|
|
|
+
|
|
|
+
|
|
|
+ while (running) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
|
|
|
+ if (retval != UA_STATUSCODE_GOOD) {
|
|
|
+ UA_LOG_ERROR(logger, UA_LOGCATEGORY_CLIENT, "Not connected. Retrying to connect in 1 second");
|
|
|
+
|
|
|
+
|
|
|
+ UA_sleep_ms(1000);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ const UA_NodeId nodeId =
|
|
|
+ UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
|
|
|
+
|
|
|
+ retval = UA_Client_readValueAttribute(client, nodeId, &value);
|
|
|
+ if (retval == UA_STATUSCODE_BADCONNECTIONCLOSED) {
|
|
|
+ UA_LOG_ERROR(logger, UA_LOGCATEGORY_CLIENT, "Connection was closed. Reconnecting ...");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (retval == UA_STATUSCODE_GOOD &&
|
|
|
+ UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
|
|
|
+ UA_DateTime raw_date = *(UA_DateTime *) value.data;
|
|
|
+ UA_String string_date = UA_DateTime_toString(raw_date);
|
|
|
+ UA_LOG_INFO(logger, UA_LOGCATEGORY_CLIENT, "string date is: %.*s", (int) string_date.length, string_date.data);
|
|
|
+ UA_String_deleteMembers(&string_date);
|
|
|
+ }
|
|
|
+ UA_sleep_ms(1000);
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ UA_Variant_deleteMembers(&value);
|
|
|
+ UA_Client_delete(client);
|
|
|
+ return UA_STATUSCODE_GOOD;
|
|
|
+}
|