2 * Copyright (c) 2007-2008 Fabrizio Frioli, Michele Pedrolli
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 * Please send your questions/suggestions to:
20 * {fabrizio.frioli, michele.pedrolli} at studenti dot unitn dot it
24 package example.bittorrent;
26 import peersim.core.*;
27 import peersim.config.Configuration;
30 * This class provides a way to initialize a single node of the network.
31 * The initialization is performed by choosing the bandwidth of the node
32 * and choosing how much the shared file has been downloaded.
34 public class NodeInitializer{
37 * The protocol to operate on.
40 private static final String PAR_PROT="protocol";
43 * The percentage of nodes with no downloaded pieces.
45 * @see "The documentation for an example on how to properly set this parameter."
47 private static final String PAR_NEWER_DISTR="newer_distr";
50 * The percentage of seeders in the network.
53 private static final String PAR_SEEDER_DISTR="seeder_distr";
56 * The percentage of nodes with no downloaded pieces,
57 * as defined in {@see #PAR_NEWER_DISTR}.
59 private int newerDistr;
62 * The percentage of seeder nodes,
63 * as defined in {@see #PAR_SEEDER_DISTR}.
65 private int seederDistr;
68 * The BitTorrent protocol ID.
70 private final int pid;
73 * The basic constructor of the class, which reads the parameters
74 * from the configuration file.
75 * @param prefix the configuration prefix for this class
77 public NodeInitializer(String prefix){
78 pid = Configuration.getPid(prefix+"."+PAR_PROT);
79 newerDistr = Configuration.getInt(prefix+"."+PAR_NEWER_DISTR);
80 seederDistr = Configuration.getInt(prefix+"."+PAR_SEEDER_DISTR);
84 * Initializes the node <tt>n</tt> associating it
85 * with the BitTorrent protocol and setting the reference to the tracker,
86 * the status of the file and the bandwidth.
87 * @param n The node to initialize
89 public void initialize(Node n){
90 Node tracker = Network.get(0);
92 p = (BitTorrent)n.getProtocol(pid);
93 p.setTracker(tracker);
94 p.setThisNodeID(n.getID());
100 * Sets the status of the shared file according to the
101 * probability value given by {@link #getProbability()}.
102 * @param p The BitTorrent protocol
104 private void setFileStatus(BitTorrent p){
105 int percentage = getProbability();
106 choosePieces(percentage, p);
110 * Set the maximum bandwidth for the node, choosing
111 * uniformly at random among 4 values.
113 * The allowed bandwidth speed are 640 Kbps, 1 Mbps, 2 Mbps and 4 Mbps.
115 * @param p The BitTorrent protocol
117 private void setBandwidth(BitTorrent p){
118 int value = CommonState.r.nextInt(4);
120 case 0: p.setBandwidth(640);break; //640Kbps
121 case 1: p.setBandwidth(1024);break;// 1Mbps
122 case 2: p.setBandwidth(2048);break;// 2Mbps
123 case 3: p.setBandwidth(4096);break; //4Mbps
128 * Sets the completed pieces for the given protocol <tt>p</tt>.
129 * @parm percentage The percentage of the downloaded pieces, according to {@link #getProbability()}
130 * @param p the BitTorrent protocol
132 private void choosePieces(int percentage, BitTorrent p){
133 double temp = ((double)p.nPieces/100.0)*percentage; // We use a double to avoid the loss of precision
134 // during the division operation
135 int completed = (int)temp; //integer number of piece to set as completed
136 //0 if the peer is a newer
137 p.setCompleted(completed);
138 if(percentage == 100)
142 tmp = CommonState.r.nextInt(p.nPieces);
143 if(p.getStatus(tmp)!=16){
144 p.setStatus(tmp, 16);
151 * Gets a probability according with the parameter <tt>newer_distr</tt>
152 * defined in the configuration file.
153 * @return the probabilty value, where 0 means that the peer is new and no pieces has been downloaded,
154 * 100 means that the peer is a seeder; other values defines a random probability.
155 * @see #PAR_NEWER_DISTR
157 private int getProbability(){
158 int value = CommonState.r.nextInt(100);
159 if((value+1)<=seederDistr)
161 value = CommonState.r.nextInt(100);
162 if((value+1)<=newerDistr){
163 return 0; // A newer peer, with probability newer_distr
166 value = CommonState.r.nextInt(9);