Commit 2574e8cf authored by Lukas Bram's avatar Lukas Bram
Browse files

Merge branch 'master' into dev_ema

parents 514c6abd e0a26ade
Pipeline #403493 passed with stage
in 55 seconds
......@@ -2,8 +2,11 @@ package de.rwth.montisim.simulation.eecomponents.simple_network;
public class IPV6Address {
public static final IPV6Address BROADCAST_ADDR = new IPV6Address("ff02::1");
public String addr;
private String addr;
public IPV6Address(String addr) {
this.addr = addr;
}
public String getString() {
return addr;
}
}
......@@ -70,7 +70,7 @@ public class SimpleCommunicationGateway extends EEComponent implements PortTagUs
if (msgInfo == null) return; // The vehicle does not use this message
// Resolve local MessageInformation
Object newData[] = new Object[2];
newData[0] = event.msg.sender.addr;
newData[0] = event.msg.sender.getString();
newData[1] = event.msg.message;
Message newMsg = new Message(msgInfo, newData, event.msg.msgLen);
eesystem.simulator.addEvent(new MessageSendEvent(this, event.getEventTime().plus(properties.processing_time), newMsg));
......
......@@ -16,12 +16,19 @@ import de.rwth.montisim.simulation.eecomponents.simple_network.events.*;
public class SimpleNetwork implements EventTarget, SimulatorModule {
public static final String CONTEXT_KEY = "simple_network";
// Used to address 'other cars' in the simulation:
// '2::1' is the 'first other car' (other than sender)
// '2::2' is the 'second other car'
public static final String N_TO_N_PREFIX = "2::";
public static final String N_TO_N_BROADCAST_ADDR = "ff02::2";
public final SimpleNetworkProperties properties;
final DiscreteEventSimulator simulator;
Vector<SimpleNetworkNodeInfo> nodes = new Vector<>();
HashMap<String, SimpleNetworkNodeInfo> ipToNode = new HashMap<>();
HashMap<String, Integer> ipToId = new HashMap<>();
int addrCounter = 1;
......@@ -36,8 +43,10 @@ public class SimpleNetwork implements EventTarget, SimulatorModule {
String addrStr = "1::"+Integer.toHexString(addrCounter);
IPV6Address addr = new IPV6Address(addrStr);
SimpleNetworkNodeInfo nodeInfo = new SimpleNetworkNodeInfo(nodeName, addr, physicalObject, component);
int id = nodes.size();
nodes.add(nodeInfo);
ipToNode.put(addr.addr, nodeInfo);
ipToNode.put(addrStr, nodeInfo);
ipToId.put(addrStr, id);
addrCounter++;
return addr;
}
......@@ -48,8 +57,8 @@ public class SimpleNetwork implements EventTarget, SimulatorModule {
msgTypes.put(msgName, new Pair<>(msgType, addr));
} else {
if (!prev.getKey().equals(msgType)) {
SimpleNetworkNodeInfo prevNode = ipToNode.get(prev.getValue().addr);
SimpleNetworkNodeInfo newNode = ipToNode.get(addr.addr);
SimpleNetworkNodeInfo prevNode = ipToNode.get(prev.getValue().getClass());
SimpleNetworkNodeInfo newNode = ipToNode.get(addr.getString());
throw new IllegalArgumentException(
"Network Msg type error: Node "+newNode.nodeName + " registered msg '"+msgName+"' with type '"+msgType
+" which was already registered with type '"+prev.getKey()+"' by node "+prevNode.nodeName);
......@@ -68,16 +77,41 @@ public class SimpleNetwork implements EventTarget, SimulatorModule {
}
private void dispatch(SimpleNetworkSendEvent event) {
SimpleNetworkNodeInfo sender = ipToNode.get(event.msg.sender.addr);
SimpleNetworkNodeInfo sender = ipToNode.get(event.msg.sender.getString());
SimpleNetworkRecvEvent recvEvent = new SimpleNetworkRecvEvent(null, event.getEventTime(), event.msg);
if (event.msg.target.addr.equals(IPV6Address.BROADCAST_ADDR.addr)) {
if (event.msg.target.getString().equals(IPV6Address.BROADCAST_ADDR.getString())) {
nodesInRange(sender).forEach(v -> {
if (v != sender)
v.component.process(recvEvent);
});
} else if (event.msg.target.getString().equals(N_TO_N_BROADCAST_ADDR)) { // TODO this is hacky
// N-to-N broadcast
int senderId = ipToId.get(event.msg.sender.getString()) - 1; // When the sender is after the target in the 'nodes' table, the relative id is one smaller
for (SimpleNetworkNodeInfo node : nodes) {
if (node == sender) {
++senderId; // Now target is after sender: id of sender is position in 'nodes' table.
continue;
}
recvEvent.msg.sender = new IPV6Address(N_TO_N_PREFIX + Integer.toString(senderId+1)); // Convert sender ip to relative n-to-n address
node.component.process(recvEvent);
}
} else if (event.msg.target.getString().startsWith(N_TO_N_PREFIX)) { // TODO this is hacky
// N-to-N message
int senderIndex = ipToId.get(event.msg.sender.getString());
int targetId = Integer.parseInt(event.msg.target.getString().substring(N_TO_N_PREFIX.length())) - 1;
if (targetId+1 >= nodes.size()) {
throw new IllegalArgumentException("Trying to send N-to-N message to invalid IP: "+event.msg.target.getString());
}
// Get 'other' target
int targetIndex = targetId >= senderIndex ? targetId+1 : targetId;
SimpleNetworkNodeInfo target = nodes.elementAt(targetIndex);
int senderId = senderIndex < targetId ? senderIndex : senderIndex - 1;
recvEvent.msg.sender = new IPV6Address(N_TO_N_PREFIX + Integer.toString(senderId+1)); // Convert sender ip to relative n-to-n address
target.component.process(recvEvent);
return;
} else {
SimpleNetworkNodeInfo target = ipToNode.get(event.msg.target.addr);
if (target == null) throw new IllegalArgumentException("Sending to Unknown IP: "+event.msg.target.addr);
SimpleNetworkNodeInfo target = ipToNode.get(event.msg.target.getString());
if (target == null) throw new IllegalArgumentException("Sending to Unknown IP: "+event.msg.target.getString());
if (inRange(target, sender)) {
target.component.process(recvEvent);
}
......
......@@ -21,6 +21,6 @@ public class SimpleNetworkMessage extends Message {
}
public static Object newMessage(IPV6Address target, Object payload) {
return new Object[]{target.addr, payload};
return new Object[]{target.getString(), payload};
}
}
......@@ -25,10 +25,10 @@
<properties>
<!-- USE THIS VARIABLE TO SET THE VERSION OF THIS PROJECT AND ALL SUB PROJECTS -->
<revision>3.1.4</revision>
<revision>3.1.5</revision>
<!-- USE THESE TO SET THE VERSIONS OF THE DEPENDENCIES FOR ALL THE SUB-PROJECTS -->
<commons.version>2.0.13</commons.version>
<commons.version>2.0.14</commons.version>
<controller.version>1.1.0</controller.version>
<assembly.plugin>2.5.4</assembly.plugin>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment