initial commit
This commit is contained in:
commit
ce1bbc047a
7 changed files with 260 additions and 0 deletions
40
.classpath
Normal file
40
.classpath
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="test" value="true"/>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="test" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
/target/
|
||||||
|
/.settings/
|
23
.project
Normal file
23
.project
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>netsim</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
21
pom.xml
Normal file
21
pom.xml
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>eu.m724</groupId>
|
||||||
|
<artifactId>netsim</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
134
src/main/java/eu/m724/netsim/Machine.java
Normal file
134
src/main/java/eu/m724/netsim/Machine.java
Normal file
|
@ -0,0 +1,134 @@
|
||||||
|
package eu.m724.netsim;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Machine {
|
||||||
|
public final String id;
|
||||||
|
|
||||||
|
private List<Machine> peers = new ArrayList<>();
|
||||||
|
// key - destination
|
||||||
|
private HashMap<Machine, RoutingInfo> routingTable = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
public Machine(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get route to machine
|
||||||
|
* @param machine the machine to get route to
|
||||||
|
* @return the route to machine
|
||||||
|
*/
|
||||||
|
public RoutingInfo getRouteTo(Machine machine) {
|
||||||
|
return routingTable.get(machine);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* import a route
|
||||||
|
* @param from the machine the route is imported from
|
||||||
|
* @param route
|
||||||
|
* @return if I accepted the route
|
||||||
|
*/
|
||||||
|
public boolean importRoute(Machine from, RoutingInfo route) {
|
||||||
|
Machine dest = route.dest();
|
||||||
|
int pathLength = route.pathLength();
|
||||||
|
|
||||||
|
// if this route leads to this machine
|
||||||
|
// or via this machine or the route is from an unknown machine
|
||||||
|
if (dest.equals(this) || route.from().equals(this) || !route.from().equals(from)) {
|
||||||
|
System.out.printf("%s rejected from %s route %s\n", id, from.id, route.toString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
RoutingInfo existingInfo = routingTable.get(dest);
|
||||||
|
|
||||||
|
// reject if destination already exists and is faster
|
||||||
|
if (existingInfo != null && existingInfo.pathLength() < pathLength) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO other import filtering here
|
||||||
|
|
||||||
|
routingTable.put(route.dest(), route);
|
||||||
|
|
||||||
|
if (from.equals(dest) && pathLength == 1)
|
||||||
|
System.out.printf("%s accepted direct from %s\n", id, from.id);
|
||||||
|
else
|
||||||
|
System.out.printf("%s accepted from %s route %s\n", id, from.id, route.toString());
|
||||||
|
|
||||||
|
for (Machine peer : peers) {
|
||||||
|
if (peer.equals(from) || peer.equals(dest)) continue;
|
||||||
|
exportRoute(peer, route);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* export a route
|
||||||
|
* @param to the machine to export a route to
|
||||||
|
* @param route
|
||||||
|
* @return if the machine accepted the route
|
||||||
|
*/
|
||||||
|
public boolean exportRoute(Machine to, RoutingInfo route) {
|
||||||
|
if (route == null) return false;
|
||||||
|
if (to.equals(route.dest())) return false;
|
||||||
|
if (to.equals(route.from())) return false;
|
||||||
|
// TODO export filtering here
|
||||||
|
System.out.printf("%s exports to %s route %s\n", id, to.id, route.toString());
|
||||||
|
return to.importRoute(this, route.nextHop(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param to the machine to export a route to
|
||||||
|
* @param dest the destination of exported route
|
||||||
|
* @return if the machine accepted the route
|
||||||
|
*/
|
||||||
|
public boolean exportRouteTo(Machine to, Machine dest) {
|
||||||
|
return exportRoute(to, routingTable.get(dest));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* peer with somebody
|
||||||
|
* all tables are exported
|
||||||
|
* @param machine the machine to peer with
|
||||||
|
* @return peering successful (if not already peering)
|
||||||
|
*/
|
||||||
|
public boolean peer(Machine machine) {
|
||||||
|
System.out.printf("%s peering with %s\n", id, machine.id);
|
||||||
|
boolean success = peers.add(machine);
|
||||||
|
importRoute(machine, RoutingInfo.direct(machine));
|
||||||
|
for (RoutingInfo route : routingTable.values()) {
|
||||||
|
exportRoute(machine, route);
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* peer with somebody and make that somebody peer with me
|
||||||
|
* all tables are exported
|
||||||
|
* @param machine the machine to peer with
|
||||||
|
* @return peering successful (if not already peering)
|
||||||
|
*/
|
||||||
|
public boolean peerWith(Machine machine) {
|
||||||
|
if (peer(machine)) return machine.peer(this);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object object) {
|
||||||
|
if (object == this) return true;
|
||||||
|
if (object == null) return false;
|
||||||
|
if (!(object instanceof Machine)) return false;
|
||||||
|
Machine machine = (Machine) object;
|
||||||
|
if (!machine.id.equals(this.id)) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
15
src/main/java/eu/m724/netsim/RoutingInfo.java
Normal file
15
src/main/java/eu/m724/netsim/RoutingInfo.java
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package eu.m724.netsim;
|
||||||
|
|
||||||
|
public record RoutingInfo(Machine dest, Machine from, int pathLength) {
|
||||||
|
public String toString() {
|
||||||
|
return "%s via %s len %d".formatted(dest.id, from.id, pathLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
public RoutingInfo nextHop(Machine from) {
|
||||||
|
return new RoutingInfo(dest, from, pathLength + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static RoutingInfo direct(Machine machine) {
|
||||||
|
return new RoutingInfo(machine, machine, 1);
|
||||||
|
}
|
||||||
|
}
|
25
src/test/java/netsim/PeeringTest.java
Normal file
25
src/test/java/netsim/PeeringTest.java
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package netsim;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import eu.m724.netsim.Machine;
|
||||||
|
|
||||||
|
public class PeeringTest {
|
||||||
|
@Test
|
||||||
|
public void testPeering() {
|
||||||
|
// machine1 - machine2 - machine3
|
||||||
|
// so machine1 goes to machine3 through machine2
|
||||||
|
|
||||||
|
Machine machine1 = new Machine("machine1");
|
||||||
|
Machine machine2 = new Machine("machine2");
|
||||||
|
Machine machine3 = new Machine("machine3");
|
||||||
|
|
||||||
|
machine1.peerWith(machine2);
|
||||||
|
machine2.peerWith(machine3);
|
||||||
|
|
||||||
|
System.out.println("");
|
||||||
|
System.out.println(machine1.getRouteTo(machine2).toString());
|
||||||
|
System.out.println(machine2.getRouteTo(machine3).toString());
|
||||||
|
System.out.println(machine1.getRouteTo(machine3).toString());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue