Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / example / bittorrent / BTObserver.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.config.*;
27 import peersim.core.*;
28 import peersim.util.*;
29
30 /**
31  * This {@link Control} provides a way to keep track of some
32  * parameters of the BitTorrent network.
33  */
34  public class BTObserver implements Control {
35          
36         /**
37          *      The protocol to operate on.
38          *      @config
39          */
40         private static final String PAR_PROT="protocol";
41         
42         /**
43          *      Protocol identifier, obtained from config property
44          */
45         private final int pid;
46         
47         /**
48          *      The basic constructor that reads the configuration file.
49          *      @param prefix the configuration prefix for this class
50          */
51         public BTObserver(String prefix) {
52                 pid = Configuration.getPid(prefix + "." + PAR_PROT);
53         }
54         
55         /**
56          * Prints information about the BitTorrent network
57          * and the number of leechers and seeders.
58          * Please refer to the code comments for more details.
59          * @return always false
60          */
61         public boolean execute() {
62                 IncrementalFreq nodeStatusStats = new IncrementalFreq();
63                 IncrementalStats neighborStats = new IncrementalStats();
64                 
65                 int numberOfNodes = Network.size();
66                 int numberOfCompletedPieces = 0;
67                 
68                 // cycles from 1, since the node 0 is the tracker
69                 for (int i=1; i<numberOfNodes; ++i) {
70                         
71                         // stats on number of leechers and seeders in the network
72                         // and consequently also on number of completed files in the network
73                         nodeStatusStats.add(((BitTorrent)(Network.get(i).getProtocol(pid))).getPeerStatus());
74                         
75                         // stats on number of neighbors per peer
76                         neighborStats.add(((BitTorrent)(Network.get(i).getProtocol(pid))).getNNodes());
77                 }
78                 
79                 // number of the pieces of the file, equal for every node, here 1 is chosen,
80                 // since 1 is the first "normal" node (0 is the tracker)
81                 int numberOfPieces = ((BitTorrent)(Network.get(1).getProtocol(pid))).nPieces;
82         
83                 for (int i=1; i<numberOfNodes; ++i) {
84                         numberOfCompletedPieces = 0;
85                         
86                         // discovers the status of the current peer (leecher or seeder)
87                         int ps = ((BitTorrent)(Network.get(i).getProtocol(pid))).getPeerStatus();
88                         String peerStatus;
89                         if (ps==0) {
90                                 peerStatus = "L"; //leecher
91                         }
92                         else {
93                                 peerStatus = "S"; //seeder
94                         }
95                         
96                         
97                         if (Network.get(i)!=null) {
98                                 
99                                 // counts the number of completed pieces for the i-th node
100                                 for (int j=0; j<numberOfPieces; j++) {
101                                         if ( ((BitTorrent)(Network.get(i).getProtocol(pid))).getFileStatus()[j] == 16) {
102                                                 numberOfCompletedPieces++;
103                                         }
104                                 }
105                                 
106                                 /*
107                                  * Put here the output lines of the Observer. An example is provided with
108                                  * basic information and stats.
109                                  * CommonState.getTime() is used to print out time references
110                                  * (useful for graph plotting).
111                                  */
112                                 
113                                 System.out.println("OBS: node " + ((BitTorrent)(Network.get(i).getProtocol(pid))).getThisNodeID() + "(" + peerStatus + ")" + "\t pieces completed: " + numberOfCompletedPieces + "\t \t down: " + ((BitTorrent)(Network.get(i).getProtocol(pid))).nPiecesDown + "\t up: " + ((BitTorrent)(Network.get(i).getProtocol(pid))).nPiecesUp + " time: " + CommonState.getTime());
114                                 //System.out.println("[OBS] t " + CommonState.getTime() + "\t pc " + numberOfCompletedPieces + "\t n " + ((BitTorrent)(Network.get(i).getProtocol(pid))).getThisNodeID());
115                                 //System.out.println( CommonState.getTime() + "\t" + numberOfCompletedPieces + "\t" + ((BitTorrent)(Network.get(i).getProtocol(pid))).getThisNodeID());
116
117                         }
118                         else {
119                                 //System.out.println("[OBS] t " + CommonState.getTime() + "\t pc " + "0" + "\t n " + "0");
120                         }
121                 
122                 }
123                 
124                 // prints the frequency of 0 (leechers) and 1 (seeders)
125                 nodeStatusStats.printAll(System.out);
126                 
127                 // prints the average number of neighbors per peer
128                 System.out.println("Avg number of neighbors per peer: " + neighborStats.getAverage());
129                 
130                 return false;
131         }
132 }