use of org.apache.plc4x.java.api.exceptions.PlcIoException in project plc4x by apache.
the class DefaultNettyPlcConnection method connect.
@Override
public void connect() throws PlcConnectionException {
try {
// As we don't just want to wait till the connection is established,
// define a future we can use to signal back that the s7 session is
// finished initializing.
CompletableFuture<Void> sessionSetupCompleteFuture = new CompletableFuture<>();
CompletableFuture<Configuration> sessionDiscoveredCompleteFuture = new CompletableFuture<>();
if (channelFactory == null) {
throw new PlcConnectionException("No channel factory provided");
}
// Inject the configuration
ConfigurationFactory.configure(configuration, channelFactory);
// Have the channel factory create a new channel instance.
if (awaitSessionDiscoverComplete) {
channel = channelFactory.createChannel(getChannelHandler(sessionSetupCompleteFuture, sessionDisconnectCompleteFuture, sessionDiscoveredCompleteFuture));
channel.closeFuture().addListener(future -> {
if (!sessionDiscoveredCompleteFuture.isDone()) {
// Do Nothing
try {
sessionDiscoveredCompleteFuture.complete(null);
} catch (Exception e) {
// Do Nothing
}
}
});
channel.pipeline().fireUserEventTriggered(new DiscoverEvent());
// Wait till the connection is established.
sessionDiscoveredCompleteFuture.get();
}
channel = channelFactory.createChannel(getChannelHandler(sessionSetupCompleteFuture, sessionDisconnectCompleteFuture, sessionDiscoveredCompleteFuture));
channel.closeFuture().addListener(future -> {
if (!sessionSetupCompleteFuture.isDone()) {
sessionSetupCompleteFuture.completeExceptionally(new PlcIoException("Connection terminated by remote"));
}
});
// Send an event to the pipeline telling the Protocol filters what's going on.
sendChannelCreatedEvent();
// Wait till the connection is established.
if (awaitSessionSetupComplete) {
sessionSetupCompleteFuture.get();
}
// Set the connection to "connected"
connected = true;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new PlcConnectionException(e);
} catch (ExecutionException e) {
throw new PlcConnectionException(e);
}
}
Aggregations