Search in sources :

Example 1 with PlcIoException

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);
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Configuration(org.apache.plc4x.java.spi.configuration.Configuration) PlcIoException(org.apache.plc4x.java.api.exceptions.PlcIoException) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) ExecutionException(java.util.concurrent.ExecutionException) PlcIoException(org.apache.plc4x.java.api.exceptions.PlcIoException) ExecutionException(java.util.concurrent.ExecutionException) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)1 PlcIoException (org.apache.plc4x.java.api.exceptions.PlcIoException)1 Configuration (org.apache.plc4x.java.spi.configuration.Configuration)1