Commit cff5d31b authored by Björn Richerzhagen's avatar Björn Richerzhagen
Browse files

Prevent duplicate TransMessageListeners on the same port

parent 679a6f50
......@@ -19,73 +19,61 @@
*/
package de.tud.kom.p2psim.api.transport;
/**
* Instances of this type describe the service properties chosen for message
* transmission.
*
* @author Sebastian Kaune
* @author Konstantin Pussep
* @version 3.0, 11/29/2007
*
*/
public enum TransProtocol {
/**
* User Datagram Protocol
*/
UDP(false, false, 8),
/**
* Transmission Control Protocol
*/
TCP(true, true, 20),
package de.tud.kom.p2psim.api.transport;
/**
* Instances of this type describe the service properties chosen for message
* transmission.
*
* @author Sebastian Kaune
* @author Konstantin Pussep
* @version 3.0, 11/29/2007
*
*/
public enum TransProtocol {
/**
* Mobile Communication, one hop (this will be changed to UDP/TCP on the
* BaseStation or one of its backbone nodes). Only available, if an
* UMTS-transceiver is configured in the LinkLayer. TODO check Header Size
* User Datagram Protocol
*/
UMTS(true, true, 20),
UDP(false, false, 8),
/**
* Bluetooth (this will only allow one-hop communication to short-ranged
* neighbors). Only available, if a BT-transceiver is configured in the
* LinkLayer. TODO check Header Size
* Transmission Control Protocol
*/
BLUETOOTH(true, true, 20);
private boolean isConOriented;
TCP(true, true, 20);
private boolean isConOriented;
private boolean isReliable;
private int headerSize;
private int headerSize;
private TransProtocol(boolean isConOriented, boolean isReliable,
int headerSize) {
this.isConOriented = isConOriented;
int headerSize) {
this.isConOriented = isConOriented;
this.isReliable = isReliable;
this.headerSize = headerSize;
}
/**
* If the used service is connection-oriented the in-order delivery of
* messages is guaranteed.
*
* @return whether the applied service should be connection-oriented
*/
public boolean isConnectionOriented() {
return this.isConOriented;
}
/**
* Whether the used connection is reliable, i.e. the network wrapper tries
* to deliver the message in case of loss or and if the message cannot be
* delivered an exception is reported to the upper layer.
*
* @return whether the applied service should be reliable
*/
public boolean isReliable() {
return this.isReliable;
}
this.headerSize = headerSize;
}
/**
* If the used service is connection-oriented the in-order delivery of
* messages is guaranteed.
*
* @return whether the applied service should be connection-oriented
*/
public boolean isConnectionOriented() {
return this.isConOriented;
}
/**
* Whether the used connection is reliable, i.e. the network wrapper tries
* to deliver the message in case of loss or and if the message cannot be
* delivered an exception is reported to the upper layer.
*
* @return whether the applied service should be reliable
*/
public boolean isReliable() {
return this.isReliable;
}
/**
* Size of the headers of this TransMessage in byte.
......@@ -95,5 +83,5 @@ public enum TransProtocol {
public int getHeaderSize() {
return this.headerSize;
}
}
}
......@@ -21,6 +21,7 @@
package de.tud.kom.p2psim.impl.transport.modular;
import de.tud.kom.p2psim.api.transport.TransMessage;
import de.tud.kom.p2psim.api.transport.TransProtocol;
import de.tudarmstadt.maki.simonstrator.api.Message;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetID;
import de.tudarmstadt.maki.simonstrator.api.component.transport.TransInfo;
......@@ -63,5 +64,11 @@ public interface ITransProtocol {
* @return
*/
public int getHeaderSize();
/**
* Protocol type of this instance.
* @return
*/
public TransProtocol getProtocol();
}
......@@ -253,7 +253,7 @@ public class ModularTransLayer implements SimHostComponent, TransportComponent,
@Override
public void setTransportMessageListener(TransMessageListener listener) {
this.listener = listener;
addTransMsgListener(listener, localPort);
addTransMsgListener(transport.getProtocol(), listener, localPort);
}
/*
......@@ -265,7 +265,7 @@ public class ModularTransLayer implements SimHostComponent, TransportComponent,
Monitor.log(ModularTransLayer.class, Level.DEBUG, "Trying tor remove a non-existing transport message listener.");
return;
}
removeTransMsgListener(listener, localPort);
removeTransMsgListener(transport.getProtocol(), localPort);
listener = null;
}
......@@ -403,18 +403,18 @@ public class ModularTransLayer implements SimHostComponent, TransportComponent,
}
protected void addTransMsgListener(TransMessageListener receiver, int port) {
protected void addTransMsgListener(TransProtocol protocol, TransMessageListener receiver, int port) {
PortListener pl = portListeners.get(port);
if (pl == null) {
pl = new PortListener();
portListeners.put(port, pl);
}
pl.addListener(receiver);
pl.setListener(protocol, receiver);
}
protected void removeTransMsgListener(TransMessageListener listener, int port) {
protected void removeTransMsgListener(TransProtocol protocol, int port) {
PortListener pl = portListeners.get(port);
pl.removeListener(listener);
pl.removeListener(protocol);
if (pl.isEmpty()) {
portListeners.remove(port);
}
......@@ -445,15 +445,12 @@ public class ModularTransLayer implements SimHostComponent, TransportComponent,
* Notify the Protocol of a received Message
*/
TransMessage receivedMessage = null;
Class<? extends MessageBasedTransport> protocol = null;
if (msg.getProtocol() == TransProtocol.TCP) {
receivedMessage = tcp.get(nme.getReceiver()).receive(msg,
senderInfo);
protocol = UDP.class;
} else if (msg.getProtocol() == TransProtocol.UDP) {
receivedMessage = udp.get(nme.getReceiver()).receive(msg,
senderInfo);
protocol = TCPMessageBased.class;
} else {
throw new AssertionError("No Protocol " + msg.getProtocol());
}
......@@ -519,10 +516,10 @@ public class ModularTransLayer implements SimHostComponent, TransportComponent,
}
}
// deliver real message
pl.dispatch(pMsg.getPayload(), protocol, senderInfo,
pl.dispatch(pMsg.getPayload(), msg.getProtocol(), senderInfo,
receivedMessage.getCommId());
} else {
pl.dispatch(receivedMessage.getPayload(), protocol,
pl.dispatch(receivedMessage.getPayload(), msg.getProtocol(),
senderInfo, receivedMessage.getCommId());
}
} else {
......@@ -547,27 +544,34 @@ public class ModularTransLayer implements SimHostComponent, TransportComponent,
*/
public class PortListener {
private List<TransMessageListener> transListeners;
private Map<TransProtocol, TransMessageListener> transListeners;
public PortListener() {
transListeners = new LinkedList<TransMessageListener>();
transListeners = new LinkedHashMap<TransProtocol, TransMessageListener>();
}
public void addListener(TransMessageListener listener) {
if (!transListeners.contains(listener)) {
transListeners.add(listener);
public void setListener(TransProtocol protocol, TransMessageListener listener) {
if (transListeners.containsKey(protocol)) {
Monitor.log(ModularTransLayer.class, Level.WARN, "Overwriting an existing TransMessageListener for protocol %s.", protocol.toString());
}
transListeners.put(protocol, listener);
}
public void removeListener(TransMessageListener listener) {
transListeners.remove(listener);
public void removeListener(TransProtocol protocol) {
if (!transListeners.containsKey(protocol)) {
Monitor.log(ModularTransLayer.class, Level.WARN, "Trying to delete an non-existing TransMessageListener for protocol %s.", protocol.toString());
}
transListeners.remove(protocol);
}
public <T extends MessageBasedTransport> void dispatch(Message msg,
Class<T> protocol, TransInfo sender, int commId) {
for (TransMessageListener listener : transListeners) {
listener.messageArrived(msg, sender, commId);
TransProtocol protocol, TransInfo sender, int commId) {
if (transListeners.containsKey(protocol)) {
transListeners.get(protocol).messageArrived(msg, sender, commId);
} else {
Monitor.log(ModularTransLayer.class, Level.WARN, "Message arrived, but no TransMessageListener active for %s.", protocol.toString());
}
}
public boolean isEmpty() {
......
......@@ -129,6 +129,11 @@ public class TransmissionControlProtocol extends AbstractTransProtocol {
public int getHeaderSize() {
return 20;
}
@Override
public TransProtocol getProtocol() {
return TransProtocol.TCP;
}
@Override
public void send(Message msg, NetID receiverNet, int receiverPort,
......
......@@ -65,5 +65,10 @@ public class TransmissionControlProtocolDummy extends AbstractTransProtocol {
public int getHeaderSize() {
return TransProtocol.TCP.getHeaderSize();
}
@Override
public TransProtocol getProtocol() {
return TransProtocol.TCP;
}
}
......@@ -24,6 +24,7 @@ import de.tud.kom.p2psim.api.analyzer.MessageAnalyzer.Reason;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.network.NetProtocol;
import de.tud.kom.p2psim.api.network.SimNetInterface;
import de.tud.kom.p2psim.api.transport.TransProtocol;
import de.tud.kom.p2psim.impl.transport.UDPMessage;
import de.tud.kom.p2psim.impl.transport.modular.AbstractTransProtocol;
import de.tudarmstadt.maki.simonstrator.api.Message;
......@@ -65,4 +66,9 @@ public class UserDatagramProtocol extends AbstractTransProtocol {
return 8;
}
@Override
public TransProtocol getProtocol() {
return TransProtocol.UDP;
}
}
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