3 import java.util.LinkedHashMap;
6 import peersim.config.Configuration;
7 import peersim.config.IllegalParameterException;
8 import peersim.core.CommonState;
9 import peersim.core.Node;
10 import peersim.transport.Transport;
13 * PSGTransport is the transport layer. it is responsible for sending messages.
15 * @author Khaled Baati 28/10/2014
18 public class PSGTransport implements Transport {
20 private static double computeDuration = 0;
22 private static Map<PSGTask, String> taskToSend = new LinkedHashMap<PSGTask, String>();
25 * String name of the parameter used to configure the minimum latency. * @config
27 private static final String PAR_MINDELAY = "mindelay";
30 * String name of the parameter used to configure the maximum latency.
31 * Defaults to {@value #PAR_MINDELAY}, which results in a constant delay.
35 private static final String PAR_MAXDELAY = "maxdelay";
37 /** Minimum delay for message sending */
39 /** Maximum delay for message sending */
43 * Difference between the max and min delay plus one. That is, max delay is
48 public PSGTransport() {
52 public PSGTransport(String prefix) {
53 min = Configuration.getLong(prefix + "." + PAR_MINDELAY);
54 max = Configuration.getLong(prefix + "." + PAR_MAXDELAY, min);
56 throw new IllegalParameterException(prefix + "." + PAR_MAXDELAY,
57 "The maximum latency cannot be smaller than the minimum latency");
58 range = max - min + 1;
62 * Returns <code>this</code>. This way only one instance exists in the
63 * system that is linked from all the nodes. This is because this protocol
64 * has no node specific state.
66 public Object clone() {
71 public void send(Node src, Node dest, Object msg, int pid) {
72 double commSizeLat = 0;
74 * random instruction associated to UniformRandomTransport.send(...)
75 * method in peersim.transport
77 long delay = (range == 1 ? min : min + CommonState.r.nextLong(range));
78 CommonState.r.nextInt(1 << 8);
79 if (msg instanceof Sizable) {
80 commSizeLat = ((Sizable) msg).getSize();
83 task = new PSGTask("task sender_" + src.getID(), computeDuration,
84 commSizeLat, msg, pid);
85 taskToSend.put(this.task, NodeHost.getHost(dest).getName());
90 * Process for sending all messages in the queue.
92 public static void flush() {
93 Map<PSGTask, String> taskToSendCopy = new LinkedHashMap<PSGTask, String>();
94 for (Map.Entry<PSGTask, String> entry : taskToSend.entrySet()) {
95 taskToSendCopy.put(entry.getKey(), entry.getValue());
98 for (Map.Entry<PSGTask, String> entry : taskToSendCopy.entrySet()) {
99 PSGTask task = entry.getKey();
100 String dest = entry.getValue();
103 taskToSendCopy.clear();
108 public long getLatency(Node src, Node dest) {
110 * random instruction associated to
111 * UniformRandomTransport.getLatency(...) method in peersim.transport
113 return (range == 1 ? min : min + CommonState.r.nextLong(range));