Search in sources :

Example 1 with Configuration

use of org.apache.plc4x.java.spi.configuration.Configuration 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)

Example 2 with Configuration

use of org.apache.plc4x.java.spi.configuration.Configuration in project plc4x by apache.

the class GeneratedDriverBase method getConnection.

@Override
public PlcConnection getConnection(String connectionString) throws PlcConnectionException {
    // Split up the connection string into its individual segments.
    Matcher matcher = URI_PATTERN.matcher(connectionString);
    if (!matcher.matches()) {
        throw new PlcConnectionException("Connection string doesn't match the format '{protocol-code}:({transport-code})?//{transport-address}(?{parameter-string)?'");
    }
    final String protocolCode = matcher.group("protocolCode");
    final String transportCode = (matcher.group("transportCode") != null) ? matcher.group("transportCode") : getDefaultTransport();
    final String transportConfig = matcher.group("transportConfig");
    final String paramString = matcher.group("paramString");
    // Check if the protocol code matches this driver.
    if (!protocolCode.equals(getProtocolCode())) {
        // Actually this shouldn't happen as the DriverManager should have not used this driver in the first place.
        throw new PlcConnectionException("This driver is not suited to handle this connection string");
    }
    // Create the configuration object.
    Configuration configuration = new ConfigurationFactory().createConfiguration(getConfigurationType(), paramString);
    if (configuration == null) {
        throw new PlcConnectionException("Unsupported configuration");
    }
    // Try to find a transport in order to create a communication channel.
    Transport transport = null;
    ServiceLoader<Transport> transportLoader = ServiceLoader.load(Transport.class, Thread.currentThread().getContextClassLoader());
    for (Transport curTransport : transportLoader) {
        if (curTransport.getTransportCode().equals(transportCode)) {
            transport = curTransport;
            break;
        }
    }
    if (transport == null) {
        throw new PlcConnectionException("Unsupported transport " + transportCode);
    }
    // Inject the configuration into the transport.
    configure(configuration, transport);
    // Create an instance of the communication channel which the driver should use.
    ChannelFactory channelFactory = transport.createChannelFactory(transportConfig);
    if (channelFactory == null) {
        throw new PlcConnectionException("Unable to get channel factory from url " + transportConfig);
    }
    configure(configuration, channelFactory);
    // Give drivers the option to customize the channel.
    initializePipeline(channelFactory);
    // Make the "await setup complete" overridable via system property.
    boolean awaitSetupComplete = awaitSetupComplete();
    if (System.getProperty(PROPERTY_PLC4X_FORCE_AWAIT_SETUP_COMPLETE) != null) {
        awaitSetupComplete = Boolean.parseBoolean(System.getProperty(PROPERTY_PLC4X_FORCE_AWAIT_SETUP_COMPLETE));
    }
    // Make the "await disconnect complete" overridable via system property.
    boolean awaitDisconnectComplete = awaitDisconnectComplete();
    if (System.getProperty(PROPERTY_PLC4X_FORCE_AWAIT_DISCONNECT_COMPLETE) != null) {
        awaitDisconnectComplete = Boolean.parseBoolean(System.getProperty(PROPERTY_PLC4X_FORCE_AWAIT_DISCONNECT_COMPLETE));
    }
    // Make the "await disconnect complete" overridable via system property.
    boolean awaitDiscoverComplete = awaitDiscoverComplete();
    if (System.getProperty(PROPERTY_PLC4X_FORCE_AWAIT_DISCOVER_COMPLETE) != null) {
        awaitDiscoverComplete = Boolean.parseBoolean(System.getProperty(PROPERTY_PLC4X_FORCE_AWAIT_DISCOVER_COMPLETE));
    }
    return new DefaultNettyPlcConnection(canRead(), canWrite(), canSubscribe(), getFieldHandler(), getValueHandler(), configuration, channelFactory, awaitSetupComplete, awaitDisconnectComplete, awaitDiscoverComplete, getStackConfigurer(transport), getOptimizer());
}
Also used : Configuration(org.apache.plc4x.java.spi.configuration.Configuration) Matcher(java.util.regex.Matcher) ConfigurationFactory(org.apache.plc4x.java.spi.configuration.ConfigurationFactory) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException) Transport(org.apache.plc4x.java.spi.transport.Transport)

Aggregations

PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)2 Configuration (org.apache.plc4x.java.spi.configuration.Configuration)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 Matcher (java.util.regex.Matcher)1 PlcIoException (org.apache.plc4x.java.api.exceptions.PlcIoException)1 ConfigurationFactory (org.apache.plc4x.java.spi.configuration.ConfigurationFactory)1 Transport (org.apache.plc4x.java.spi.transport.Transport)1