Search in sources :

Example 1 with Transport

use of org.apache.plc4x.java.spi.transport.Transport in project plc4x by apache.

the class OpcuaPlcDriver method getConnection.

@Override
public PlcConnection getConnection(String connectionString) throws PlcConnectionException {
    // Split up the connection string into it's 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-host}(:{transport-port})(/{transport-endpoint})(?{parameter-string)?'");
    }
    final String protocolCode = matcher.group("protocolCode");
    final String transportCode = (matcher.group("transportCode") != null) ? matcher.group("transportCode") : getDefaultTransport();
    final String transportHost = matcher.group("transportHost");
    final String transportPort = matcher.group("transportPort");
    final String transportEndpoint = matcher.group("transportEndpoint");
    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.
    OpcuaConfiguration configuration = (OpcuaConfiguration) new ConfigurationFactory().createConfiguration(getConfigurationType(), paramString);
    if (configuration == null) {
        throw new PlcConnectionException("Unsupported configuration");
    }
    configuration.setTransportCode(transportCode);
    configuration.setHost(transportHost);
    configuration.setPort(transportPort);
    configuration.setTransportEndpoint(transportEndpoint);
    configuration.setEndpoint("opc." + transportCode + "://" + transportHost + ":" + transportPort + "" + transportEndpoint);
    // 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(transportHost + ":" + transportPort);
    if (channelFactory == null) {
        throw new PlcConnectionException("Unable to get channel factory from url " + transportHost + ":" + transportPort);
    }
    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));
    }
    if (configuration.getSecurityPolicy() != null && !(configuration.getSecurityPolicy().equals("None"))) {
        try {
            configuration.openKeyStore();
        } catch (Exception e) {
            throw new PlcConnectionException("Unable to open keystore, please confirm you have the correct permissions");
        }
    }
    this.isEncrypted = configuration.isEncrypted();
    // 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(), getOptimizer());
}
Also used : 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) PlcConnectionException(org.apache.plc4x.java.api.exceptions.PlcConnectionException)

Example 2 with Transport

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

Example 3 with Transport

use of org.apache.plc4x.java.spi.transport.Transport in project plc4x by apache.

the class GenericCANDriver method getStackConfigurer.

@Override
protected ProtocolStackConfigurer<Message> getStackConfigurer(Transport transport) {
    if (!(transport instanceof CANTransport)) {
        throw new PlcRuntimeException("Generic CAN Driver requires CAN Transport instance");
    }
    CANTransport<Message> canTransport = (CANTransport<Message>) transport;
    return CustomProtocolStackConfigurer.builder(canTransport.getMessageType(), canTransport::getMessageInput).withProtocol(cfg -> {
        GenericCANProtocolLogic protocolLogic = new GenericCANProtocolLogic();
        ConfigurationFactory.configure(cfg, protocolLogic);
        return new CANDriverAdapter<>(protocolLogic, canTransport.getMessageType(), canTransport.adapter(), new GenericCANFrameDataHandler(canTransport::getTransportFrameBuilder));
    }).withDriverContext(cfg -> new GenericCANDriverContext()).withPacketSizeEstimator(cfg -> canTransport.getEstimator()).littleEndian().build();
}
Also used : GeneratedDriverBase(org.apache.plc4x.java.spi.connection.GeneratedDriverBase) Configuration(org.apache.plc4x.java.spi.configuration.Configuration) CANTransport(org.apache.plc4x.java.transport.can.CANTransport) CANDriverAdapter(org.apache.plc4x.java.can.adapter.CANDriverAdapter) GenericCANFrameDataHandler(org.apache.plc4x.java.can.generic.transport.GenericCANFrameDataHandler) GenericCANDriverContext(org.apache.plc4x.java.can.generic.context.GenericCANDriverContext) PlcValueHandler(org.apache.plc4x.java.api.value.PlcValueHandler) Message(org.apache.plc4x.java.spi.generation.Message) IEC61131ValueHandler(org.apache.plc4x.java.spi.values.IEC61131ValueHandler) GenericCANProtocolLogic(org.apache.plc4x.java.can.generic.protocol.GenericCANProtocolLogic) GenericCANConfiguration(org.apache.plc4x.java.can.generic.configuration.GenericCANConfiguration) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) ConfigurationFactory(org.apache.plc4x.java.spi.configuration.ConfigurationFactory) CustomProtocolStackConfigurer(org.apache.plc4x.java.spi.connection.CustomProtocolStackConfigurer) ProtocolStackConfigurer(org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer) BaseOptimizer(org.apache.plc4x.java.spi.optimizer.BaseOptimizer) GenericCANFieldHandler(org.apache.plc4x.java.can.generic.field.GenericCANFieldHandler) Transport(org.apache.plc4x.java.spi.transport.Transport) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) Message(org.apache.plc4x.java.spi.generation.Message) GenericCANDriverContext(org.apache.plc4x.java.can.generic.context.GenericCANDriverContext) CANDriverAdapter(org.apache.plc4x.java.can.adapter.CANDriverAdapter) GenericCANProtocolLogic(org.apache.plc4x.java.can.generic.protocol.GenericCANProtocolLogic) CANTransport(org.apache.plc4x.java.transport.can.CANTransport) GenericCANFrameDataHandler(org.apache.plc4x.java.can.generic.transport.GenericCANFrameDataHandler)

Example 4 with Transport

use of org.apache.plc4x.java.spi.transport.Transport in project plc4x by apache.

the class TransportActivator method start.

@Override
public void start(BundleContext context) throws Exception {
    ServiceLoader<Transport> transports = ServiceLoader.load(Transport.class, context.getBundle().adapt(BundleWiring.class).getClassLoader());
    for (Transport transport : transports) {
        Hashtable<String, String> props = new Hashtable<String, String>();
        props.put(TRANSPORT_CODE, transport.getTransportCode());
        props.put(TRANSPORT_NAME, transport.getTransportName());
        reg = context.registerService(Transport.class, transport, props);
    }
}
Also used : Hashtable(java.util.Hashtable) Transport(org.apache.plc4x.java.spi.transport.Transport)

Example 5 with Transport

use of org.apache.plc4x.java.spi.transport.Transport in project plc4x by apache.

the class CANOpenPlcDriver method getStackConfigurer.

@Override
protected ProtocolStackConfigurer<Message> getStackConfigurer(Transport transport) {
    if (!(transport instanceof CANTransport)) {
        throw new PlcRuntimeException("CANopen driver requires a CAN transport instance");
    }
    final CANTransport<Message> canTransport = (CANTransport<Message>) transport;
    return CustomProtocolStackConfigurer.builder(canTransport.getMessageType(), canTransport::getMessageInput).withProtocol(cfg -> {
        CANOpenProtocolLogic protocolLogic = new CANOpenProtocolLogic();
        ConfigurationFactory.configure(cfg, protocolLogic);
        return new CANDriverAdapter<>(protocolLogic, canTransport.getMessageType(), canTransport.adapter(), new CANOpenFrameDataHandler(canTransport::getTransportFrameBuilder));
    }).withDriverContext(cfg -> new CANOpenDriverContext()).withPacketSizeEstimator(configuration1 -> canTransport.getEstimator()).littleEndian().build();
}
Also used : CANOpenFrameDataHandler(org.apache.plc4x.java.canopen.transport.CANOpenFrameDataHandler) SingleFieldOptimizer(org.apache.plc4x.java.spi.optimizer.SingleFieldOptimizer) PlcList(org.apache.plc4x.java.spi.values.PlcList) GeneratedDriverBase(org.apache.plc4x.java.spi.connection.GeneratedDriverBase) CANOpenDriverContext(org.apache.plc4x.java.canopen.context.CANOpenDriverContext) Configuration(org.apache.plc4x.java.spi.configuration.Configuration) CANTransport(org.apache.plc4x.java.transport.can.CANTransport) PlcValue(org.apache.plc4x.java.api.value.PlcValue) CANDriverAdapter(org.apache.plc4x.java.can.adapter.CANDriverAdapter) CANOpenFieldHandler(org.apache.plc4x.java.canopen.field.CANOpenFieldHandler) PlcValueHandler(org.apache.plc4x.java.api.value.PlcValueHandler) Message(org.apache.plc4x.java.spi.generation.Message) PlcField(org.apache.plc4x.java.api.model.PlcField) IEC61131ValueHandler(org.apache.plc4x.java.spi.values.IEC61131ValueHandler) CANOpenProtocolLogic(org.apache.plc4x.java.canopen.protocol.CANOpenProtocolLogic) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) CANOpenConfiguration(org.apache.plc4x.java.canopen.configuration.CANOpenConfiguration) ConfigurationFactory(org.apache.plc4x.java.spi.configuration.ConfigurationFactory) CustomProtocolStackConfigurer(org.apache.plc4x.java.spi.connection.CustomProtocolStackConfigurer) ProtocolStackConfigurer(org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer) BaseOptimizer(org.apache.plc4x.java.spi.optimizer.BaseOptimizer) Transport(org.apache.plc4x.java.spi.transport.Transport) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) CANOpenFrameDataHandler(org.apache.plc4x.java.canopen.transport.CANOpenFrameDataHandler) Message(org.apache.plc4x.java.spi.generation.Message) CANDriverAdapter(org.apache.plc4x.java.can.adapter.CANDriverAdapter) CANOpenProtocolLogic(org.apache.plc4x.java.canopen.protocol.CANOpenProtocolLogic) CANTransport(org.apache.plc4x.java.transport.can.CANTransport) CANOpenDriverContext(org.apache.plc4x.java.canopen.context.CANOpenDriverContext)

Aggregations

Transport (org.apache.plc4x.java.spi.transport.Transport)5 ConfigurationFactory (org.apache.plc4x.java.spi.configuration.ConfigurationFactory)4 Configuration (org.apache.plc4x.java.spi.configuration.Configuration)3 Matcher (java.util.regex.Matcher)2 PlcConnectionException (org.apache.plc4x.java.api.exceptions.PlcConnectionException)2 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)2 PlcValueHandler (org.apache.plc4x.java.api.value.PlcValueHandler)2 CANDriverAdapter (org.apache.plc4x.java.can.adapter.CANDriverAdapter)2 CustomProtocolStackConfigurer (org.apache.plc4x.java.spi.connection.CustomProtocolStackConfigurer)2 GeneratedDriverBase (org.apache.plc4x.java.spi.connection.GeneratedDriverBase)2 ProtocolStackConfigurer (org.apache.plc4x.java.spi.connection.ProtocolStackConfigurer)2 Message (org.apache.plc4x.java.spi.generation.Message)2 BaseOptimizer (org.apache.plc4x.java.spi.optimizer.BaseOptimizer)2 IEC61131ValueHandler (org.apache.plc4x.java.spi.values.IEC61131ValueHandler)2 CANTransport (org.apache.plc4x.java.transport.can.CANTransport)2 Hashtable (java.util.Hashtable)1 PlcField (org.apache.plc4x.java.api.model.PlcField)1 PlcValue (org.apache.plc4x.java.api.value.PlcValue)1 GenericCANConfiguration (org.apache.plc4x.java.can.generic.configuration.GenericCANConfiguration)1 GenericCANDriverContext (org.apache.plc4x.java.can.generic.context.GenericCANDriverContext)1