Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / example / bittorrent / NodeInitializer.java
1 /*
2  * Copyright (c) 2007-2008 Fabrizio Frioli, Michele Pedrolli
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  * Please send your questions/suggestions to:
20  * {fabrizio.frioli, michele.pedrolli} at studenti dot unitn dot it
21  *
22  */
23
24 package example.bittorrent;
25
26 import peersim.core.*;
27 import peersim.config.Configuration;
28
29 /**
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.
33  */
34 public class NodeInitializer{
35         
36         /**
37          *      The protocol to operate on.
38          *      @config
39          */
40         private static final String PAR_PROT="protocol";
41         
42         /**
43          *      The percentage of nodes with no downloaded pieces.
44          *      @config
45          *      @see "The documentation for an example on how to properly set this parameter."
46          */
47         private static final String PAR_NEWER_DISTR="newer_distr";
48         
49         /**
50          *      The percentage of seeders in the network.
51          *      @config
52          */
53         private static final String PAR_SEEDER_DISTR="seeder_distr";
54
55         /**
56          *      The percentage of nodes with no downloaded pieces,
57          *      as defined in {@see #PAR_NEWER_DISTR}.
58          */
59         private int newerDistr;
60         
61         /**
62          *      The percentage of seeder nodes,
63          *      as defined in {@see #PAR_SEEDER_DISTR}.
64          */
65         private int seederDistr;
66         
67         /**
68          *      The BitTorrent protocol ID.
69          */     
70         private final int pid;
71         
72         /**
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
76          */
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);
81         }
82         
83         /**
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
88          */
89         public void initialize(Node n){
90                 Node tracker = Network.get(0);
91                 BitTorrent p;
92                 p = (BitTorrent)n.getProtocol(pid);
93                 p.setTracker(tracker);
94                 p.setThisNodeID(n.getID());
95                 setFileStatus(p);
96                 setBandwidth(p);
97         }
98
99         /**
100          *      Sets the status of the shared file according to the
101          *      probability value given by {@link #getProbability()}.
102          *      @param p The BitTorrent protocol
103          */
104         private void setFileStatus(BitTorrent p){
105                 int percentage = getProbability();
106                 choosePieces(percentage, p);
107         }
108         
109         /**
110          *      Set the maximum bandwidth for the node, choosing
111          *      uniformly at random among 4 values.
112          *      <p>
113          *      The allowed bandwidth speed are 640 Kbps, 1 Mbps, 2 Mbps and 4 Mbps.
114          *      </p>
115          *      @param p The BitTorrent protocol
116          */
117         private void setBandwidth(BitTorrent p){
118                 int value = CommonState.r.nextInt(4);
119                 switch(value){
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
124                 }
125         }
126         
127         /**
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
131          */
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)
139                         p.setPeerStatus(1);
140                 int tmp;
141                 while(completed!=0){
142                         tmp = CommonState.r.nextInt(p.nPieces);
143                         if(p.getStatus(tmp)!=16){
144                                 p.setStatus(tmp, 16);
145                                 completed--;
146                         }
147                 }
148         }
149         
150         /**
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
156          */
157         private int getProbability(){
158                 int value = CommonState.r.nextInt(100);
159                 if((value+1)<=seederDistr)
160                         return 100;
161                 value = CommonState.r.nextInt(100);
162                 if((value+1)<=newerDistr){
163                         return 0; // A newer peer, with probability newer_distr
164                 }
165                 else{
166                         value = CommonState.r.nextInt(9);
167                         return (value+1)*10;
168                 }
169         }
170 }