Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / transport / UnreliableTransport.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
24
25 /**
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. 
30  * <p>
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). 
34  *
35  * @author Alberto Montresor
36  * @version $Revision: 1.13 $
37  */
38 public final class UnreliableTransport implements Transport
39 {
40
41 //---------------------------------------------------------------------
42 //Parameters
43 //---------------------------------------------------------------------
44
45 /**
46  * The name of the underlying transport protocol. This transport is
47  * extended with dropping messages.
48  * @config
49  */
50 private static final String PAR_TRANSPORT = "transport";
51
52 /** 
53  * String name of the parameter used to configure the probability that a 
54  * message sent through this transport is lost.
55  * @config
56  */
57 private static final String PAR_DROP = "drop";
58
59
60 //---------------------------------------------------------------------
61 //Fields
62 //---------------------------------------------------------------------
63
64 /** Protocol identifier for the support transport protocol */
65 private final int transport;
66
67 /** Probability of dropping messages */
68 private final float loss;
69
70 //---------------------------------------------------------------------
71 //Initialization
72 //---------------------------------------------------------------------
73
74 /**
75  * Reads configuration parameter.
76  */
77 public UnreliableTransport(String prefix)
78 {
79         transport = Configuration.getPid(prefix+"."+PAR_TRANSPORT);
80         loss = (float) Configuration.getDouble(prefix+"."+PAR_DROP);
81 }
82
83 //---------------------------------------------------------------------
84
85 /**
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.
89  */
90 public Object clone()
91 {
92         return this;
93 }
94
95 //---------------------------------------------------------------------
96 //Methods
97 //---------------------------------------------------------------------
98
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
101 * nothing).
102 */
103 public void send(Node src, Node dest, Object msg, int pid)
104 {
105         try
106         {
107                 if (CommonState.r.nextFloat() >= loss)
108                 {
109                         // Message is not lost
110                         Transport t = (Transport) src.getProtocol(transport);
111                         t.send(src, dest, msg, pid);
112                 }
113         }
114         catch(ClassCastException e)
115         {
116                 throw new IllegalArgumentException("Protocol " +
117                                 Configuration.lookupPid(transport) + 
118                                 " does not implement Transport");
119         }
120 }
121
122 /** Returns the latency of the underlying protocol.*/
123 public long getLatency(Node src, Node dest)
124 {
125         Transport t = (Transport) src.getProtocol(transport);
126         return t.getLatency(src, dest);
127 }
128
129 }