2 * Copyright (c) 2003-2005 The BISON Project
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.
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.
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.
19 package peersim.transport;
21 import peersim.config.*;
22 import peersim.core.*;
23 import peersim.edsim.*;
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
31 * @author Alberto Montresor
32 * @version $Revision: 1.14 $
34 public final class UniformRandomTransport implements Transport
37 //---------------------------------------------------------------------
39 //---------------------------------------------------------------------
42 * String name of the parameter used to configure the minimum latency.
45 private static final String PAR_MINDELAY = "mindelay";
48 * String name of the parameter used to configure the maximum latency.
49 * Defaults to {@value #PAR_MINDELAY}, which results in a constant delay.
52 private static final String PAR_MAXDELAY = "maxdelay";
54 //---------------------------------------------------------------------
56 //---------------------------------------------------------------------
58 /** Minimum delay for message sending */
59 private final long min;
61 /** Difference between the max and min delay plus one. That is, max delay is
64 private final long range;
67 //---------------------------------------------------------------------
69 //---------------------------------------------------------------------
72 * Reads configuration parameter.
74 public UniformRandomTransport(String prefix)
76 min = Configuration.getLong(prefix + "." + PAR_MINDELAY);
77 long max = Configuration.getLong(prefix + "." + PAR_MAXDELAY,min);
79 throw new IllegalParameterException(prefix+"."+PAR_MAXDELAY,
80 "The maximum latency cannot be smaller than the minimum latency");
84 //---------------------------------------------------------------------
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.
96 //---------------------------------------------------------------------
98 //---------------------------------------------------------------------
101 * Delivers the message with a random
102 * delay, that is drawn from the configured interval according to the uniform
105 public void send(Node src, Node dest, Object msg, int pid)
107 // avoid calling nextLong if possible
108 long delay = (range==1?min:min + CommonState.r.nextLong(range));
109 EDSimulator.add(delay, msg, dest, pid);
114 * delay, that is drawn from the configured interval according to the uniform
117 public long getLatency(Node src, Node dest)
119 return (range==1?min:min + CommonState.r.nextLong(range));