Skip to content

Commit

Permalink
refactor(plc4j/udp-transport): Made it generally possible to open a U…
Browse files Browse the repository at this point in the history
…DP transport with a fixed local port
  • Loading branch information
chrisdutz committed Jun 15, 2023
1 parent 255ddc3 commit 23eee0a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
Expand Up @@ -44,13 +44,16 @@ public abstract class NettyChannelFactory implements ChannelFactory {

private final Map<Channel, EventLoopGroup> eventLoops = new ConcurrentHashMap<>();

/**
* TODO should be removed together with the Constructor.
*/
private final SocketAddress address;
private final SocketAddress localAddress;
private final SocketAddress remoteAddress;

protected NettyChannelFactory(SocketAddress remoteAddress) {
this(null, remoteAddress);
}

protected NettyChannelFactory(SocketAddress address) {
this.address = address;
protected NettyChannelFactory(SocketAddress localAddress, SocketAddress remoteAddress) {
this.localAddress = localAddress;
this.remoteAddress = remoteAddress;
}

/**
Expand Down Expand Up @@ -83,7 +86,7 @@ protected Bootstrap createBootstrap() {
* Has to be in accordance with {@link #getChannel()}
* otherwise a Runtime Exception will be produced by Netty
* <p>
* By Default Nettys {@link NioEventLoopGroup} is used.
* By Default Netty's {@link NioEventLoopGroup} is used.
* Transports which have to use a different EventLoopGroup have to override {#getEventLoopGroup()}.
*/
public EventLoopGroup getEventLoopGroup() {
Expand All @@ -106,7 +109,8 @@ public Channel createChannel(ChannelHandler channelHandler) throws PlcConnection
configureBootstrap(bootstrap);
bootstrap.handler(channelHandler);
// Start the client.
final ChannelFuture f = bootstrap.connect(address);
final ChannelFuture f = (localAddress == null) ?
bootstrap.connect(remoteAddress) : bootstrap.connect(remoteAddress, localAddress);
f.addListener(future -> {
if (!future.isSuccess()) {
logger.info("Unable to connect, shutting down worker thread.");
Expand Down
Expand Up @@ -36,8 +36,12 @@ public class UdpChannelFactory extends NettyChannelFactory implements HasConfigu

private UdpTransportConfiguration configuration;

public UdpChannelFactory(SocketAddress address) {
super(address);
public UdpChannelFactory(SocketAddress remoteAddress) {
super(remoteAddress);
}

public UdpChannelFactory(SocketAddress localAddress, SocketAddress remoteAddress) {
super(localAddress, remoteAddress);
}

@Override
Expand Down
Expand Up @@ -62,20 +62,27 @@ public ChannelFactory createChannelFactory(String transportConfig) {

// If the port wasn't specified, try to get a default port from the configuration.
int port;
int localPort = UdpTransportConfiguration.NO_DEFAULT_PORT;
if(portString != null) {
port = Integer.parseInt(portString);
} else if ((configuration != null) &&
(configuration.getDefaultPort() != UdpTransportConfiguration.NO_DEFAULT_PORT)) {
} else if ((configuration != null) && (configuration.getDefaultPort() != UdpTransportConfiguration.NO_DEFAULT_PORT)) {
port = configuration.getDefaultPort();
} else {
throw new PlcRuntimeException("No port defined");
}
if (configuration != null) {
localPort = configuration.getLocalPort();
}

// Create the fully qualified remote socket address which we should connect to.
SocketAddress address = new InetSocketAddress((ip == null) ? hostname : ip, port);
SocketAddress remoteAddress = new InetSocketAddress((ip == null) ? hostname : ip, port);
if(localPort != UdpTransportConfiguration.NO_DEFAULT_PORT) {
SocketAddress localAddress = new InetSocketAddress(localPort);
return new UdpChannelFactory(localAddress, remoteAddress);
}

// Initialize the channel factory with the default socket address we want to connect to.
return new UdpChannelFactory(address);
return new UdpChannelFactory(remoteAddress);
}

}
Expand Up @@ -28,4 +28,13 @@ default int getDefaultPort() {
return NO_DEFAULT_PORT;
}

/**
* Most transports don't care about the local port, but some do.
* This option allows forcing a local port number to be used.
* @return local port number
*/
default int getLocalPort() {
return NO_DEFAULT_PORT;
}

}

0 comments on commit 23eee0a

Please sign in to comment.