Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / transport / UniformRandomTransport.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  */
18
19 package peersim.transport;
20
21 import peersim.config.*;
22 import peersim.core.*;
23 import peersim.edsim.*;
24
25
26 /**
27  * Implement a transport layer that reliably delivers messages with a random
28  * delay, that is drawn from the configured interval according to the uniform
29  * distribution.
30  *
31  * @author Alberto Montresor
32  * @version $Revision: 1.14 $
33  */
34 public final class UniformRandomTransport implements Transport
35 {
36
37 //---------------------------------------------------------------------
38 //Parameters
39 //---------------------------------------------------------------------
40
41 /** 
42  * String name of the parameter used to configure the minimum latency.
43  * @config
44  */     
45 private static final String PAR_MINDELAY = "mindelay";  
46         
47 /** 
48  * String name of the parameter used to configure the maximum latency.
49  * Defaults to {@value #PAR_MINDELAY}, which results in a constant delay.
50  * @config 
51  */     
52 private static final String PAR_MAXDELAY = "maxdelay";  
53         
54 //---------------------------------------------------------------------
55 //Fields
56 //---------------------------------------------------------------------
57
58 /** Minimum delay for message sending */
59 private final long min;
60         
61 /** Difference between the max and min delay plus one. That is, max delay is
62 * min+range-1.
63 */
64 private final long range;
65
66         
67 //---------------------------------------------------------------------
68 //Initialization
69 //---------------------------------------------------------------------
70
71 /**
72  * Reads configuration parameter.
73  */
74 public UniformRandomTransport(String prefix)
75 {
76         min = Configuration.getLong(prefix + "." + PAR_MINDELAY);
77         long max = Configuration.getLong(prefix + "." + PAR_MAXDELAY,min);
78         if (max < min) 
79            throw new IllegalParameterException(prefix+"."+PAR_MAXDELAY, 
80            "The maximum latency cannot be smaller than the minimum latency");
81         range = max-min+1;
82 }
83
84 //---------------------------------------------------------------------
85
86 /**
87 * Returns <code>this</code>. This way only one instance exists in the system
88 * that is linked from all the nodes. This is because this protocol has no
89 * node specific state.
90 */
91 public Object clone()
92 {
93         return this;
94 }
95
96 //---------------------------------------------------------------------
97 //Methods
98 //---------------------------------------------------------------------
99
100 /**
101  * Delivers the message with a random
102  * delay, that is drawn from the configured interval according to the uniform
103  * distribution.
104 */
105 public void send(Node src, Node dest, Object msg, int pid)
106 {
107         // avoid calling nextLong if possible
108         long delay = (range==1?min:min + CommonState.r.nextLong(range));
109         EDSimulator.add(delay, msg, dest, pid);
110 }
111
112 /**
113  * Returns a random
114  * delay, that is drawn from the configured interval according to the uniform
115  * distribution.
116 */
117 public long getLatency(Node src, Node dest)
118 {
119         return (range==1?min:min + CommonState.r.nextLong(range));
120 }
121
122
123 }