Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / psgsim / PSGTransport.java
1 package psgsim;
2
3 import java.util.LinkedHashMap;
4 import java.util.Map;
5
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;
11
12 /**
13  * PSGTransport is the transport layer. it is responsible for sending messages.
14  * 
15  * @author Khaled Baati 28/10/2014
16  * @version 1.1
17  */
18 public class PSGTransport implements Transport {
19
20         private static double computeDuration = 0;
21         private PSGTask task;
22         private static Map<PSGTask, String> taskToSend = new LinkedHashMap<PSGTask, String>();
23
24         /**
25          * String name of the parameter used to configure the minimum latency. * @config
26          */
27         private static final String PAR_MINDELAY = "mindelay";
28
29         /**
30          * String name of the parameter used to configure the maximum latency.
31          * Defaults to {@value #PAR_MINDELAY}, which results in a constant delay.
32          * 
33          * @config
34          */
35         private static final String PAR_MAXDELAY = "maxdelay";
36
37         /** Minimum delay for message sending */
38         private long min;
39         /** Maximum delay for message sending */
40         private long max;
41
42         /**
43          * Difference between the max and min delay plus one. That is, max delay is
44          * min+range-1.
45          */
46         private long range;
47
48         public PSGTransport() {
49
50         }
51
52         public PSGTransport(String prefix) {
53                 min = Configuration.getLong(prefix + "." + PAR_MINDELAY);
54                 max = Configuration.getLong(prefix + "." + PAR_MAXDELAY, min);
55                 if (max < min)
56                         throw new IllegalParameterException(prefix + "." + PAR_MAXDELAY,
57                                         "The maximum latency cannot be smaller than the minimum latency");
58                 range = max - min + 1;
59         }
60
61         /**
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.
65          */
66         public Object clone() {
67                 return this;
68         }
69
70         @Override
71         public void send(Node src, Node dest, Object msg, int pid) {
72                 double commSizeLat = 0;
73                 /**
74                  * random instruction associated to UniformRandomTransport.send(...)
75                  * method in peersim.transport
76                  **/
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();
81                 }
82
83                 task = new PSGTask("task sender_" + src.getID(), computeDuration,
84                                 commSizeLat, msg, pid);
85                 taskToSend.put(this.task, NodeHost.getHost(dest).getName());
86
87         }
88
89         /**
90          * Process for sending all messages in the queue.
91          */
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());
96                 }
97                 taskToSend.clear();
98                 for (Map.Entry<PSGTask, String> entry : taskToSendCopy.entrySet()) {
99                         PSGTask task = entry.getKey();
100                         String dest = entry.getValue();
101                         task.dsend(dest);
102                 }
103                 taskToSendCopy.clear();
104
105         }
106
107         @Override
108         public long getLatency(Node src, Node dest) {
109                 /**
110                  * random instruction associated to
111                  * UniformRandomTransport.getLatency(...) method in peersim.transport
112                  **/
113                 return (range == 1 ? min : min + CommonState.r.nextLong(range));
114         }
115 }