Skip to content

Commit

Permalink
refactoring(plc4j): split up the raw socket transport into a "raw" an…
Browse files Browse the repository at this point in the history
…d "raw-passive" transport, where the raw-passive is equivalent to the previous raw transport.
  • Loading branch information
chrisdutz committed Jan 10, 2022
1 parent 80307c2 commit 27442e6
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 7 deletions.
5 changes: 5 additions & 0 deletions plc4j/transports/raw-socket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<description>Base classes needed to implement plc4x drivers based on Raw Socket connections.</description>

<dependencies>
<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-api</artifactId>
<version>0.10.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.plc4x</groupId>
<artifactId>plc4j-spi</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@
import org.apache.plc4x.java.spi.configuration.HasConfiguration;
import org.apache.plc4x.java.spi.connection.NettyChannelFactory;
import org.apache.plc4x.java.utils.pcap.netty.config.PcapChannelOption;
import org.apache.plc4x.java.utils.rawsockets.netty.address.RawSocketAddress;
import org.apache.plc4x.java.utils.rawsockets.netty.RawSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.SocketAddress;

public class RawSocketChannelFactory extends NettyChannelFactory implements HasConfiguration<RawSocketTransportConfiguration> {

private static final Logger logger = LoggerFactory.getLogger(RawSocketChannelFactory.class);

private RawSocketTransportConfiguration configuration;

public RawSocketChannelFactory(RawSocketAddress address) {
public RawSocketChannelFactory(SocketAddress address) {
super(address);
}

Expand All @@ -53,7 +54,7 @@ public Class<? extends Channel> getChannel() {

@Override
public boolean isPassive() {
return true;
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,25 @@
*/
package org.apache.plc4x.java.transport.rawsocket;

import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.spi.configuration.HasConfiguration;
import org.apache.plc4x.java.spi.connection.ChannelFactory;
import org.apache.plc4x.java.spi.transport.Transport;
import org.apache.plc4x.java.utils.rawsockets.netty.address.RawSocketAddress;

public class RawSocketTransport implements Transport {
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RawSocketTransport implements Transport, HasConfiguration<RawSocketTransportConfiguration> {

private static final Pattern TRANSPORT_RAW_SOCKET_PATTERN = Pattern.compile(
"^((?<ip>[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})|(?<hostname>[a-zA-Z0-9.\\-]+))(:(?<port>[0-9]{1,5}))?");

public static final String TRANSPORT_CODE = "raw";

private RawSocketTransportConfiguration configuration;

@Override
public String getTransportCode() {
return TRANSPORT_CODE;
Expand All @@ -36,9 +47,35 @@ public String getTransportName() {
return "Raw Ethernet Transport";
}

@Override
public void setConfiguration(RawSocketTransportConfiguration configuration) {
this.configuration = configuration;
}

@Override
public ChannelFactory createChannelFactory(String transportConfig) {
RawSocketAddress address = new RawSocketAddress(transportConfig);
final Matcher matcher = TRANSPORT_RAW_SOCKET_PATTERN.matcher(transportConfig);
if(!matcher.matches()) {
throw new PlcRuntimeException("Invalid url for TCP transport");
}
String ip = matcher.group("ip");
String hostname = matcher.group("hostname");
String portString = matcher.group("port");

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

// Create the fully qualified remote socket address which we should connect to.
SocketAddress address = new InetSocketAddress((ip == null) ? hostname : ip, port);

return new RawSocketChannelFactory(address);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@

public interface RawSocketTransportConfiguration extends PcapTransportConfiguration {

int NO_DEFAULT_PORT = -1;

default int getDefaultPort() {
return NO_DEFAULT_PORT;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.plc4x.java.transport.rawsocketpassive;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.ThreadPerChannelEventLoop;
import io.netty.channel.oio.OioEventLoopGroup;
import org.apache.plc4x.java.spi.configuration.HasConfiguration;
import org.apache.plc4x.java.spi.connection.NettyChannelFactory;
import org.apache.plc4x.java.utils.pcap.netty.config.PcapChannelOption;
import org.apache.plc4x.java.utils.rawsockets.netty.RawSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.SocketAddress;

public class RawSocketPassiveChannelFactory extends NettyChannelFactory implements HasConfiguration<RawSocketPassiveTransportConfiguration> {

private static final Logger logger = LoggerFactory.getLogger(RawSocketPassiveChannelFactory.class);

private RawSocketPassiveTransportConfiguration configuration;

public RawSocketPassiveChannelFactory(SocketAddress address) {
super(address);
}

@Override
public void setConfiguration(RawSocketPassiveTransportConfiguration configuration) {
this.configuration = configuration;
}

@Override
public Class<? extends Channel> getChannel() {
return RawSocketChannel.class;
}

@Override
public boolean isPassive() {
return true;
}

@Override
public EventLoopGroup getEventLoopGroup() {
return new ThreadPerChannelEventLoop(new OioEventLoopGroup());
}

@Override
public void configureBootstrap(Bootstrap bootstrap) {
if(configuration != null) {
logger.info("Configuring Bootstrap with {}", configuration);
/*bootstrap.option(RawSocketChannelOption.PORT, configuration.getDefaultPort());
bootstrap.option(RawSocketChannelOption.PROTOCOL_ID, configuration.getProtocolId());
bootstrap.option(RawSocketChannelOption.SPEED_FACTOR, configuration.getReplaySpeedFactor());*/
if(configuration.getPcapPacketHandler() != null) {
bootstrap.option(PcapChannelOption.PACKET_HANDLER, configuration.getPcapPacketHandler());
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.plc4x.java.transport.rawsocketpassive;

import org.apache.plc4x.java.spi.connection.ChannelFactory;
import org.apache.plc4x.java.spi.transport.Transport;
import org.apache.plc4x.java.utils.rawsockets.netty.address.RawSocketPassiveAddress;

public class RawSocketPassiveTransport implements Transport {

public static final String TRANSPORT_CODE = "raw-passive";

@Override
public String getTransportCode() {
return TRANSPORT_CODE;
}

@Override
public String getTransportName() {
return "Passive Raw Ethernet Transport";
}

@Override
public ChannelFactory createChannelFactory(String transportConfig) {
RawSocketPassiveAddress address = new RawSocketPassiveAddress(transportConfig);
return new RawSocketPassiveChannelFactory(address);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.plc4x.java.transport.rawsocketpassive;

import org.apache.plc4x.java.transport.pcap.PcapTransportConfiguration;

public interface RawSocketPassiveTransportConfiguration extends PcapTransportConfiguration {

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
# specific language governing permissions and limitations
# under the License.
#
org.apache.plc4x.java.transport.rawsocket.RawSocketTransport
org.apache.plc4x.java.transport.rawsocket.RawSocketTransport
org.apache.plc4x.java.transport.rawsocketpassive.RawSocketPassiveTransport

0 comments on commit 27442e6

Please sign in to comment.