From 8c93bd1e582a05bd29b96aa45525895f79b65d9d Mon Sep 17 00:00:00 2001 From: Jochen Meierhofer Date: Thu, 7 Nov 2024 17:45:50 +0100 Subject: [PATCH] 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." --- .../java/s7/readwrite/protocol/S7HMuxImpl.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 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; } - }