-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plc4j) Better handling of timeouts in plc4j (#821).
Signed-off-by: Łukasz Dywicki <luke@code-house.org>
- Loading branch information
Showing
9 changed files
with
258 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultConversationContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.plc4x.java.spi.internal; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import java.time.Duration; | ||
import java.util.function.Consumer; | ||
import org.apache.plc4x.java.api.authentication.PlcAuthentication; | ||
import org.apache.plc4x.java.spi.ConversationContext; | ||
import org.apache.plc4x.java.spi.configuration.Configuration; | ||
import org.apache.plc4x.java.spi.events.ConnectedEvent; | ||
import org.apache.plc4x.java.spi.events.DisconnectedEvent; | ||
import org.apache.plc4x.java.spi.events.DiscoveredEvent; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class DefaultConversationContext<T1> implements ConversationContext<T1> { | ||
private final Logger logger = LoggerFactory.getLogger(DefaultConversationContext.class); | ||
|
||
private final Consumer<HandlerRegistration> handlerRegistrar; | ||
|
||
private final ChannelHandlerContext channelHandlerContext; | ||
private final PlcAuthentication authentication; | ||
private final boolean passive; | ||
|
||
public DefaultConversationContext(Consumer<HandlerRegistration> handlerRegistrar, | ||
ChannelHandlerContext channelHandlerContext, | ||
PlcAuthentication authentication, | ||
boolean passive) { | ||
this.handlerRegistrar = handlerRegistrar; | ||
this.channelHandlerContext = channelHandlerContext; | ||
this.authentication = authentication; | ||
this.passive = passive; | ||
} | ||
@Override | ||
public Channel getChannel() { | ||
return channelHandlerContext.channel(); | ||
} | ||
|
||
public PlcAuthentication getAuthentication() { | ||
return authentication; | ||
} | ||
|
||
@Override | ||
public boolean isPassive() { | ||
return passive; | ||
} | ||
|
||
@Override | ||
public void sendToWire(T1 msg) { | ||
logger.trace("Sending to wire {}", msg); | ||
channelHandlerContext.channel().writeAndFlush(msg); | ||
} | ||
|
||
@Override | ||
public void fireConnected() { | ||
logger.trace("Firing Connected!"); | ||
channelHandlerContext.pipeline().fireUserEventTriggered(new ConnectedEvent()); | ||
} | ||
|
||
@Override | ||
public void fireDisconnected() { | ||
logger.trace("Firing Disconnected!"); | ||
channelHandlerContext.pipeline().fireUserEventTriggered(new DisconnectedEvent()); | ||
} | ||
|
||
@Override | ||
public void fireDiscovered(Configuration c) { | ||
logger.trace("Firing Discovered!"); | ||
channelHandlerContext.pipeline().fireUserEventTriggered(new DiscoveredEvent(c)); | ||
} | ||
|
||
@Override | ||
public SendRequestContext<T1> sendRequest(T1 packet) { | ||
return new DefaultSendRequestContext<>(handler -> { | ||
logger.trace("Adding Response Handler ..."); | ||
handlerRegistrar.accept(handler); | ||
}, packet, this); | ||
} | ||
|
||
@Override | ||
public ExpectRequestContext<T1> expectRequest(Class<T1> clazz, Duration timeout) { | ||
return new DefaultExpectRequestContext<>(handler -> { | ||
logger.trace("Adding Request Handler ..."); | ||
handlerRegistrar.accept(handler); | ||
}, clazz, timeout, this); | ||
} | ||
} |
Oops, something went wrong.