You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2019/08/19 12:40:40 UTC

[plc4x] branch develop updated: Tried to make the test run on the nodes without "en0" device by going through the list of devices looking for the loopback device and using this.

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 8ea7c12  Tried to make the test run on the nodes without "en0" device by going through the list of devices looking for the loopback device and using this.
8ea7c12 is described below

commit 8ea7c12b050ffa1a3d78d2f6a9fd7bf1e1d05508
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon Aug 19 14:40:32 2019 +0200

    Tried to make the test run on the nodes without "en0" device by going through the list of devices looking for the loopback device and using this.
---
 .../utils/rawsockets/netty/RawSocketAddress.java   | 20 ++++++++++++++++---
 .../utils/rawsockets/netty2/RawSocketChannel.java  | 23 ++++++++++++++++++++--
 .../rawsockets/netty2/RawSocketChannelTest.java    | 14 +++++++++----
 3 files changed, 48 insertions(+), 9 deletions(-)

diff --git a/plc4j/utils/raw-sockets/src/main/java/org/apache/plc4x/java/utils/rawsockets/netty/RawSocketAddress.java b/plc4j/utils/raw-sockets/src/main/java/org/apache/plc4x/java/utils/rawsockets/netty/RawSocketAddress.java
index b602e51..dc8edca 100644
--- a/plc4j/utils/raw-sockets/src/main/java/org/apache/plc4x/java/utils/rawsockets/netty/RawSocketAddress.java
+++ b/plc4j/utils/raw-sockets/src/main/java/org/apache/plc4x/java/utils/rawsockets/netty/RawSocketAddress.java
@@ -22,15 +22,29 @@ import java.net.SocketAddress;
 
 public class RawSocketAddress extends SocketAddress {
     private static final long serialVersionUID = 1L;
-    
+
+    private String deviceName;
+
     private String hostName;
 
-    public RawSocketAddress(String hostName) {
+    private int port;
+
+    public RawSocketAddress(String deviceName, String hostName, int port) {
+        this.deviceName = deviceName;
         this.hostName = hostName;
+        this.port = port;
     }
 
-    String getHostName() {
+    public String getDeviceName() {
+        return deviceName;
+    }
+
+    public String getHostName() {
         return hostName;
     }
 
+    public int getPort() {
+        return port;
+    }
+
 }
diff --git a/plc4j/utils/raw-sockets/src/main/java/org/apache/plc4x/java/utils/rawsockets/netty2/RawSocketChannel.java b/plc4j/utils/raw-sockets/src/main/java/org/apache/plc4x/java/utils/rawsockets/netty2/RawSocketChannel.java
index bc26ffc..e603de6 100644
--- a/plc4j/utils/raw-sockets/src/main/java/org/apache/plc4x/java/utils/rawsockets/netty2/RawSocketChannel.java
+++ b/plc4j/utils/raw-sockets/src/main/java/org/apache/plc4x/java/utils/rawsockets/netty2/RawSocketChannel.java
@@ -26,6 +26,8 @@ import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelPromise;
 import io.netty.channel.oio.OioByteStreamChannel;
 import org.apache.commons.lang3.NotImplementedException;
+import org.apache.plc4x.java.utils.rawsockets.RawSocketException;
+import org.apache.plc4x.java.utils.rawsockets.netty.RawSocketAddress;
 import org.apache.plc4x.java.utils.rawsockets.netty.RawSocketChannelConfig;
 import org.pcap4j.core.NotOpenException;
 import org.pcap4j.core.PacketListener;
@@ -42,7 +44,6 @@ import java.io.OutputStream;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
 import java.net.SocketTimeoutException;
-import java.util.concurrent.TimeoutException;
 
 /**
  * TODO write comment
@@ -77,8 +78,26 @@ public class RawSocketChannel extends OioByteStreamChannel {
 
     @Override
     protected void doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
+        if(!(remoteAddress instanceof RawSocketAddress)) {
+            logger.error("Expecting remote address of type RawSocketAddress");
+            pipeline().fireExceptionCaught(new RawSocketException("Expecting remote address of type RawSocketAddress"));
+            return;
+        }
+        RawSocketAddress rawSocketAddress = (RawSocketAddress) remoteAddress;
+        // If no device name was provided, try to find out which device would be able
+        // to connect to the given hostname.
+        if(rawSocketAddress.getDeviceName() == null) {
+            if(rawSocketAddress.getHostName() == null) {
+                logger.error("At least one of 'device name' or 'host name' has to be set.");
+                pipeline().fireExceptionCaught(new RawSocketException(
+                    "At least one of 'device name' or 'host name' has to be set."));
+                return;
+            }
+            // TODO: Implement this ...
+        }
+
         logger.debug("Connecting...");
-        nif = Pcaps.getDevByName("en0");
+        nif = Pcaps.getDevByName(rawSocketAddress.getDeviceName());
         handle = nif.openLive(65535, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, 10);
         buffer = Unpooled.buffer();
         // Start loop in another Thread
diff --git a/plc4j/utils/raw-sockets/src/test/java/org/apache/plc4x/java/utils/rawsockets/netty2/RawSocketChannelTest.java b/plc4j/utils/raw-sockets/src/test/java/org/apache/plc4x/java/utils/rawsockets/netty2/RawSocketChannelTest.java
index 54029d0..fb5a8dc 100644
--- a/plc4j/utils/raw-sockets/src/test/java/org/apache/plc4x/java/utils/rawsockets/netty2/RawSocketChannelTest.java
+++ b/plc4j/utils/raw-sockets/src/test/java/org/apache/plc4x/java/utils/rawsockets/netty2/RawSocketChannelTest.java
@@ -25,9 +25,12 @@ import io.netty.buffer.ByteBufUtil;
 import io.netty.buffer.Unpooled;
 import io.netty.channel.*;
 import io.netty.channel.oio.OioEventLoopGroup;
-import io.netty.util.concurrent.Future;
-import io.netty.util.concurrent.GenericFutureListener;
+import org.apache.plc4x.java.utils.rawsockets.netty.RawSocketAddress;
+import org.junit.Assert;
 import org.junit.Test;
+import org.pcap4j.core.PcapNativeException;
+import org.pcap4j.core.PcapNetworkInterface;
+import org.pcap4j.core.Pcaps;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,7 +45,7 @@ public class RawSocketChannelTest {
     private static final Logger logger = LoggerFactory.getLogger(RawSocketChannelTest.class);
 
     @Test
-    public void doConnect() throws InterruptedException {
+    public void doConnect() throws Exception {
         Channel channel = null;
         final EventLoopGroup workerGroup = new OioEventLoopGroup();
         try {
@@ -72,7 +75,10 @@ public class RawSocketChannelTest {
                 }
             });
             // Start the client.
-            final ChannelFuture f = bootstrap.connect("127.0.0.1", 1234);
+            PcapNetworkInterface loopbackDevice = Pcaps.findAllDevs().stream().filter(
+                pcapNetworkInterface -> pcapNetworkInterface.isLoopBack()).findFirst().orElse(null);
+            Assert.assertNotNull("Couldn't find loopback device", loopbackDevice);
+            final ChannelFuture f = bootstrap.connect(new RawSocketAddress(loopbackDevice.getName(), "127.0.0.1", 1234));
             // Wait for sync
             f.sync();
             // Wait till the session is finished initializing.