changeset 996:14489dda952a

MACAddressMapper, StatelessTasks
author Devel 2
date Sat, 28 Sep 2019 17:20:25 +0200
parents c5adbd85528f
children c43894f26e50
files stress-tester/src/main/java/com/passus/st/emitter/DynamicSessionMapper.java stress-tester/src/main/java/com/passus/st/emitter/MACAddressMapper.java stress-tester/src/main/java/com/passus/st/emitter/MapBasedMACAddressAddress.java stress-tester/src/main/java/com/passus/st/emitter/PassThroughMACAddressMapper.java stress-tester/src/main/java/com/passus/st/emitter/StatelessTasks.java stress-tester/src/test/java/com/passus/st/emitter/MapBasedMACAddressAddressTest.java
diffstat 6 files changed, 128 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/emitter/DynamicSessionMapper.java	Thu Sep 26 14:54:18 2019 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/DynamicSessionMapper.java	Sat Sep 28 17:20:25 2019 +0200
@@ -1,9 +1,7 @@
 package com.passus.st.emitter;
 
-import com.passus.net.IpAddress;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Set;
 
 /**
  * TODO Dokonczyc
@@ -14,8 +12,6 @@
 
     public static final int DEFAULT_SESSIONS_CAPACITY = 1024;
 
-    private Set<IpAddress> localAdresses;
-
     private Map<SessionInfo, ConnectionParams> map = new HashMap<>(DEFAULT_SESSIONS_CAPACITY);
 
     @Override
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/emitter/MACAddressMapper.java	Sat Sep 28 17:20:25 2019 +0200
@@ -0,0 +1,9 @@
+package com.passus.st.emitter;
+
+import com.passus.net.MACAddress;
+
+public interface MACAddressMapper {
+
+    MACAddress map(MACAddress mac);
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/emitter/MapBasedMACAddressAddress.java	Sat Sep 28 17:20:25 2019 +0200
@@ -0,0 +1,52 @@
+package com.passus.st.emitter;
+
+import com.passus.net.MACAddress;
+import com.passus.st.Pair;
+import org.apache.commons.lang3.StringUtils;
+
+import java.text.ParseException;
+import java.util.HashMap;
+import java.util.Map;
+
+public class MapBasedMACAddressAddress implements MACAddressMapper {
+
+    private static final String RULE_SEPARATOR = "->";
+
+    public static final MACAddress ALL_MAC = new MACAddress(new byte[]{0, 0, 0, 0, 0, 0});
+
+    private final Map<MACAddress, MACAddress> macMap = new HashMap<>();
+
+    @Override
+    public MACAddress map(MACAddress mac) {
+        return macMap.get(mac);
+    }
+
+    public static Pair<MACAddress, MACAddress> parseRule(String rule) throws ParseException {
+        String[] parts = StringUtils.splitByWholeSeparator(rule, RULE_SEPARATOR);
+        if (parts.length != 2) {
+            throw new ParseException("Invalid rule.", 0);
+        }
+
+        MACAddress srcMac;
+        String macStr = parts[0].trim();
+        if ("*".equals(macStr)) {
+            srcMac = ALL_MAC;
+        } else {
+            try {
+                srcMac = new MACAddress(macStr);
+            } catch (Exception e) {
+                throw new ParseException("Invalid rule.", 0);
+            }
+        }
+
+        MACAddress dstMac;
+        macStr = parts[1].trim();
+        try {
+            dstMac = new MACAddress(macStr);
+        } catch (Exception e) {
+            throw new ParseException("Invalid rule.", 0);
+        }
+
+        return new Pair<>(srcMac, dstMac);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/emitter/PassThroughMACAddressMapper.java	Sat Sep 28 17:20:25 2019 +0200
@@ -0,0 +1,12 @@
+package com.passus.st.emitter;
+
+import com.passus.net.MACAddress;
+
+public class PassThroughMACAddressMapper implements MACAddressMapper {
+
+    @Override
+    public MACAddress map(MACAddress mac) {
+        return mac;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/emitter/StatelessTasks.java	Sat Sep 28 17:20:25 2019 +0200
@@ -0,0 +1,27 @@
+package com.passus.st.emitter;
+
+public class StatelessTasks {
+
+    protected static class Task {
+
+        public static final int CONNECT = 1;
+        public static final int READ = 2;
+        public static final int FLUSH = 3;
+        public static final int CLOSE = 4;
+
+        final int code;
+
+        public Task(int code) {
+            this.code = code;
+        }
+
+    }
+
+    public static final Task CONNECT_TASK = new Task(Task.CONNECT);
+    public static final Task READ_TASK = new Task(Task.READ);
+    public static final Task FLUSH_TASK = new Task(Task.FLUSH);
+    public static final Task CLOSE_TASK = new Task(Task.CLOSE);
+
+    private StatelessTasks() {
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/emitter/MapBasedMACAddressAddressTest.java	Sat Sep 28 17:20:25 2019 +0200
@@ -0,0 +1,28 @@
+package com.passus.st.emitter;
+
+import com.passus.net.MACAddress;
+import com.passus.st.Pair;
+import org.testng.annotations.Test;
+
+import java.text.ParseException;
+
+import static com.passus.st.emitter.MapBasedMACAddressAddress.parseRule;
+import static org.testng.Assert.assertEquals;
+
+public class MapBasedMACAddressAddressTest {
+
+    private void assertRule(String rule, MACAddress srcMac, MACAddress dstMac) throws ParseException {
+        Pair<MACAddress, MACAddress> pair = parseRule(rule);
+        assertEquals(pair.getValue1(), srcMac);
+        assertEquals(pair.getValue2(), dstMac);
+    }
+
+    @Test
+    public void testParseRule() throws ParseException {
+        MACAddress mac1 = new MACAddress("4C-4C-4C-4C-4C-4C");
+        MACAddress mac2 = new MACAddress("4E-4E-4E-4E-4E-4E");
+        assertRule("4C-4C-4C-4C-4C-4C -> 4E-4E-4E-4E-4E-4E", mac1, mac2);
+        assertRule("4C:4C:4C:4C:4C:4C -> 4E-4E-4E-4E-4E-4E", mac1, mac2);
+        assertRule("* -> 4E-4E-4E-4E-4E-4E", MapBasedMACAddressAddress.ALL_MAC, mac2);
+    }
+}
\ No newline at end of file