From 9856bb75c7a60a85a847b10707b0fab3f4b29e46 Mon Sep 17 00:00:00 2001 From: jmeierhofer <46316621+jmeierhofer@users.noreply.github.com> Date: Mon, 11 Nov 2024 13:01:58 +0100 Subject: [PATCH] fix(plc4j/drivers/s7): fix NoSuchElementException when watchdog ChannelHandler was not added yet & fix exception "MessageToMessageCodec$1 must produce at least one message." & fix some typos (#1873) * fix(plc4j/drivers/s7): fix NoSuchElementException when watchdog ChannelHandler was not added yet & fix unnecessary warning in logfile/console due to exception "MessageToMessageCodec$1 must produce at least one message." * fix(plc4j/drivers/s7): fix some typos * fix(plc4j/drivers/s7): delete 2 duplicate classes, the used ones are located in org.apache.plc4x.java.s7.readwrite.protocol * Revert "fix(plc4j/drivers/s7): delete 2 duplicate classes, the used ones are located in org.apache.plc4x.java.s7.readwrite.protocol" This reverts commit 5b1cc09e642745a7f7324be38ad6268233efb5ff. --------- Co-authored-by: Jochen Meierhofer --- .../java/s7/readwrite/protocol/S7HMuxImpl.java | 16 ++++++++-------- .../s7/readwrite/protocol/S7HPlcConnection.java | 8 ++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java index d58b8e32c4..d59eb17048 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HMuxImpl.java @@ -20,6 +20,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; +import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.embedded.EmbeddedChannel; @@ -130,9 +131,8 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf outBB, List lis if ((embedCtx == null) && (ctx.channel() instanceof EmbeddedChannel)) embedCtx = ctx; if ((tcpChannel != null) && (embedCtx == ctx)) { tcpChannel.writeAndFlush(outBB.copy()); - } else { - list.add(outBB.copy()); } + list.add(outBB.copy()); } /* @@ -175,9 +175,14 @@ public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exceptio public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { super.userEventTriggered(ctx, evt); logger.info(LocalTime.now() + " userEventTriggered: " + ctx.name() + " Event: " + evt); + if (evt instanceof ConnectedEvent) { try { - tcpChannel.pipeline().remove("watchdog"); + ChannelHandler watchdog = tcpChannel.pipeline().get("watchdog"); + if (watchdog != null) { + tcpChannel.pipeline().remove(watchdog); + } + } catch (Exception ex) { logger.info(ex.toString()); } @@ -199,7 +204,6 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc if (evt instanceof DisconnectEvent) { logger.info("DisconnectEvent"); } - } @Override @@ -250,7 +254,6 @@ public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { embededChannel.pipeline().fireUserEventTriggered(new ConnectEvent()); } } - ; if ((tcpChannel == secondaryChannel) && @@ -265,10 +268,8 @@ public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { embededChannel.pipeline().fireUserEventTriggered(new ConnectEvent()); } } - } - @Override public void setEmbededhannel(Channel embeded_channel, PlcConnectionConfiguration configuration) { final S7Configuration conf = (S7Configuration) configuration; @@ -351,5 +352,4 @@ public Channel getTCPChannel() { return tcpChannel; } - } diff --git a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HPlcConnection.java b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HPlcConnection.java index ea0a1e258b..597dfdb8b7 100644 --- a/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HPlcConnection.java +++ b/plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/readwrite/protocol/S7HPlcConnection.java @@ -301,7 +301,7 @@ public void doSecondaryTcpConnections() { * The user application must take the measures to make the connection again. */ protected void sendChannelDisconectEvent() { - logger.trace("Channels was not created, firing DisconnectEvent Event"); + logger.trace("Channel was not created, firing DisconnectEvent Event"); // Send an event to the pipeline telling the Protocol filters what's going on. channel.pipeline().fireUserEventTriggered(new DisconnectEvent()); } @@ -351,7 +351,7 @@ public void run() { if (primaryChannel != null) { if (!primaryChannel.isActive()) { - logger.info("Creating prymary connection."); + logger.info("Creating primary connection."); primaryChannel.eventLoop().shutdownGracefully(); doPrimaryTcpConnections(); } else if (null == secondaryChannel) { @@ -364,7 +364,7 @@ public void run() { } } } else { - logger.info("Creating firts prymary connection."); + logger.info("Creating first primary connection."); doPrimaryTcpConnections(); } @@ -384,7 +384,7 @@ public void run() { } } else { if (secondaryChannelFactory != null) { - logger.info("Creating firts secondary connection."); + logger.info("Creating first secondary connection."); doSecondaryTcpConnections(); } }