You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by hu...@apache.org on 2023/10/14 11:13:23 UTC
[plc4x] branch develop updated: plc4j-driver-opcua: Move back to using session lifetime * 75% as keepalive period (#1148)
This is an automated email from the ASF dual-hosted git repository.
hutcheb pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new 86d9db1ff7 plc4j-driver-opcua: Move back to using session lifetime * 75% as keepalive period (#1148)
86d9db1ff7 is described below
commit 86d9db1ff71a59425018bca477aea07402000f7e
Author: Rajmund Takács <ta...@gmail.com>
AuthorDate: Sat Oct 14 13:13:18 2023 +0200
plc4j-driver-opcua: Move back to using session lifetime * 75% as keepalive period (#1148)
* plc4j-driver-opcua: Move back to using session lifetime * 75% as keepalive period
* plc4j-driver-opcua: Do not send keepalive right away, and continously check exit condition
---
.../plc4x/java/opcua/context/SecureChannel.java | 27 ++++++++++++++++------
1 file changed, 20 insertions(+), 7 deletions(-)
diff --git a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannel.java b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannel.java
index 9d80a7b4e6..df9ba53a99 100644
--- a/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannel.java
+++ b/plc4j/drivers/opcua/src/main/java/org/apache/plc4x/java/opcua/context/SecureChannel.java
@@ -21,6 +21,7 @@ package org.apache.plc4x.java.opcua.context;
import static java.lang.Thread.currentThread;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import java.time.Instant;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.plc4x.java.api.authentication.PlcAuthentication;
@@ -365,6 +366,7 @@ public class SecureChannel {
ChannelSecurityToken securityToken = (ChannelSecurityToken) openSecureChannelResponse.getSecurityToken();
tokenId.set((int) securityToken.getTokenId());
channelId.set((int) securityToken.getChannelId());
+ lifetime = securityToken.getRevisedLifetime();
onConnectCreateSessionRequest(context);
} catch (PlcConnectionException e) {
LOGGER.error("Error occurred while connecting to OPC UA server", e);
@@ -1003,6 +1005,24 @@ public class SecureChannel {
private void keepAlive() {
keepAlive = CompletableFuture.supplyAsync(() -> {
while (enableKeepalive.get()) {
+
+ final Instant sendNextKeepaliveAt = Instant.now()
+ .plus(Duration.ofMillis((long) Math.ceil(this.lifetime * 0.75f)));
+
+ while (Instant.now().isBefore(sendNextKeepaliveAt)) {
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ LOGGER.trace("Interrupted Exception");
+ currentThread().interrupt();
+ }
+
+ // Do not attempt to send keepalive, if the thread has already been shut down.
+ if (!enableKeepalive.get()) {
+ return null; // exit from keepalive loop
+ }
+ }
+
int transactionId = channelTransactionManager.getTransactionIdentifier();
RequestHeader requestHeader = new RequestHeader(new NodeId(authenticationToken),
@@ -1103,13 +1123,6 @@ public class SecureChannel {
} catch (SerializationException | ParseException e) {
LOGGER.error("Unable to to Parse Open Secure Request");
}
-
- try {
- Thread.sleep((long) Math.ceil(this.sessionTimeout * 0.25f));
- } catch (InterruptedException e) {
- LOGGER.trace("Interrupted Exception");
- currentThread().interrupt();
- }
}
return null;
},