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.*;
26 * This transport protocol can be combined with other transports
27 * to simulate message losses. Its behavior is the following: each message
28 * can be dropped based on the configured probability, or it will be sent
29 * using the underlying transport protocol.
31 * The memory requirements are minimal, as a single instance is created and
32 * inserted in the protocol array of all nodes (because instances have no state
33 * that depends on the hosting node).
35 * @author Alberto Montresor
36 * @version $Revision: 1.13 $
38 public final class UnreliableTransport implements Transport
41 //---------------------------------------------------------------------
43 //---------------------------------------------------------------------
46 * The name of the underlying transport protocol. This transport is
47 * extended with dropping messages.
50 private static final String PAR_TRANSPORT = "transport";
53 * String name of the parameter used to configure the probability that a
54 * message sent through this transport is lost.
57 private static final String PAR_DROP = "drop";
60 //---------------------------------------------------------------------
62 //---------------------------------------------------------------------
64 /** Protocol identifier for the support transport protocol */
65 private final int transport;
67 /** Probability of dropping messages */
68 private final float loss;
70 //---------------------------------------------------------------------
72 //---------------------------------------------------------------------
75 * Reads configuration parameter.
77 public UnreliableTransport(String prefix)
79 transport = Configuration.getPid(prefix+"."+PAR_TRANSPORT);
80 loss = (float) Configuration.getDouble(prefix+"."+PAR_DROP);
83 //---------------------------------------------------------------------
86 * Returns <code>this</code>. This way only one instance exists in the system
87 * that is linked from all the nodes. This is because this protocol has no
88 * state that depends on the hosting node.
95 //---------------------------------------------------------------------
97 //---------------------------------------------------------------------
99 /** Sends the message according to the underlying transport protocol.
100 * With the configured probability, the message is not sent (i.e. the method does
103 public void send(Node src, Node dest, Object msg, int pid)
107 if (CommonState.r.nextFloat() >= loss)
109 // Message is not lost
110 Transport t = (Transport) src.getProtocol(transport);
111 t.send(src, dest, msg, pid);
114 catch(ClassCastException e)
116 throw new IllegalArgumentException("Protocol " +
117 Configuration.lookupPid(transport) +
118 " does not implement Transport");
122 /** Returns the latency of the underlying protocol.*/
123 public long getLatency(Node src, Node dest)
125 Transport t = (Transport) src.getProtocol(transport);
126 return t.getLatency(src, dest);