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 2018/02/07 13:27:57 UTC

[incubator-plc4x] 04/06: Some more work on the dummy driver - Should now use the "dummy" connection prefix - Should use a raw socket to send an ICMP (Ping) packet

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

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

commit d9b993f1bf180cddca66e74cd534a4f5150bae27
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Wed Feb 7 13:52:39 2018 +0100

    Some more work on the dummy driver
    - Should now use the "dummy" connection prefix
    - Should use a raw socket to send an ICMP (Ping) packet
---
 .../java/examples/dummydriver/DummyDriver.java     | 12 +++++-----
 .../examples/dummydriver/netty/DummyProtocol.java  | 28 ++++++++++++++--------
 2 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/examples/dummy-driver/src/main/java/org/apache/plc4x/java/examples/dummydriver/DummyDriver.java b/examples/dummy-driver/src/main/java/org/apache/plc4x/java/examples/dummydriver/DummyDriver.java
index 0e62561..efc42c2 100644
--- a/examples/dummy-driver/src/main/java/org/apache/plc4x/java/examples/dummydriver/DummyDriver.java
+++ b/examples/dummy-driver/src/main/java/org/apache/plc4x/java/examples/dummydriver/DummyDriver.java
@@ -29,24 +29,24 @@ import java.util.regex.Pattern;
 
 public class DummyDriver implements PlcDriver {
 
-    private static final Pattern RAW_URI_PATTERN = Pattern.compile("^raw://(?<host>.*)");
+    private static final Pattern DUMMY_URI_PATTERN = Pattern.compile("^dummy://(?<host>.*)");
 
     @Override
     public String getProtocolCode() {
-        return "raw";
+        return "dummy";
     }
 
     @Override
     public String getProtocolName() {
-        return "RAW";
+        return "Dummy";
     }
 
     @Override
     public PlcConnection connect(String url) throws PlcConnectionException {
-        Matcher matcher = RAW_URI_PATTERN.matcher(url);
+        Matcher matcher = DUMMY_URI_PATTERN.matcher(url);
         if (!matcher.matches()) {
             throw new PlcConnectionException(
-                "Connection url doesn't match the format 'raw://{host|ip}'");
+                "Connection url doesn't match the format 'dummy://{host|ip}'");
         }
         String host = matcher.group("host");
         return new DummyConnection(host);
@@ -54,7 +54,7 @@ public class DummyDriver implements PlcDriver {
 
     @Override
     public PlcConnection connect(String url, PlcAuthentication authentication) throws PlcConnectionException {
-        throw new PlcConnectionException("RAW connections don't support authentication.");
+        throw new PlcConnectionException("Dummy connections don't support authentication.");
     }
 
 }
diff --git a/examples/dummy-driver/src/main/java/org/apache/plc4x/java/examples/dummydriver/netty/DummyProtocol.java b/examples/dummy-driver/src/main/java/org/apache/plc4x/java/examples/dummydriver/netty/DummyProtocol.java
index 458074d..bf448ea 100644
--- a/examples/dummy-driver/src/main/java/org/apache/plc4x/java/examples/dummydriver/netty/DummyProtocol.java
+++ b/examples/dummy-driver/src/main/java/org/apache/plc4x/java/examples/dummydriver/netty/DummyProtocol.java
@@ -20,13 +20,12 @@ package org.apache.plc4x.java.examples.dummydriver.netty;
 
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufUtil;
+import io.netty.buffer.Unpooled;
 import io.netty.channel.ChannelHandlerContext;
 import io.netty.handler.codec.MessageToMessageCodec;
-import org.apache.plc4x.java.api.exceptions.PlcException;
 import org.apache.plc4x.java.api.messages.PlcReadRequest;
 import org.apache.plc4x.java.api.messages.PlcRequest;
 import org.apache.plc4x.java.api.messages.PlcRequestContainer;
-import org.apache.plc4x.java.api.messages.PlcWriteRequest;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -40,16 +39,25 @@ public class DummyProtocol extends MessageToMessageCodec<ByteBuf, PlcRequestCont
     protected void encode(ChannelHandlerContext ctx, PlcRequestContainer in, List<Object> out) throws Exception {
         PlcRequest request = in.getRequest();
         if (request instanceof PlcReadRequest) {
-            encodeReadRequest(in, out);
-        } else if (request instanceof PlcWriteRequest) {
-            encodeWriteRequest(in, out);
-        }
-    }
 
-    private void encodeWriteRequest(PlcRequestContainer msg, List<Object> out) throws PlcException {
-    }
+            // Simple ICMP (Ping packet)
+            byte[] rawData = new byte[] {
+                // Type (ICMP Ping Request) & Code (just 0)
+                (byte) 0x08, (byte) 0x00,
+                // Checksum
+                (byte) 0xe3, (byte) 0xe5,
+                // Identifier
+                (byte) 0x00, (byte) 0x01,
+                // Sequence Number
+                (byte) 0x00, (byte) 0x00,
+                // Payload (Just random data that was used to fit to the checksum)
+                (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, (byte) 0x08, (byte) 0x09};
 
-    private void encodeReadRequest(PlcRequestContainer msg, List<Object> out) throws PlcException {
+            ByteBuf buf = Unpooled.buffer();
+            buf.writeBytes(rawData);
+
+            out.add(buf);
+        }
     }
 
     @Override

-- 
To stop receiving notification emails like this one, please contact
cdutz@apache.org.