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.config.*;
27 import peersim.core.*;
28 import peersim.util.*;
31 * This {@link Control} provides a way to keep track of some
32 * parameters of the BitTorrent network.
34 public class BTObserver implements Control {
37 * The protocol to operate on.
40 private static final String PAR_PROT="protocol";
43 * Protocol identifier, obtained from config property
45 private final int pid;
48 * The basic constructor that reads the configuration file.
49 * @param prefix the configuration prefix for this class
51 public BTObserver(String prefix) {
52 pid = Configuration.getPid(prefix + "." + PAR_PROT);
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
61 public boolean execute() {
62 IncrementalFreq nodeStatusStats = new IncrementalFreq();
63 IncrementalStats neighborStats = new IncrementalStats();
65 int numberOfNodes = Network.size();
66 int numberOfCompletedPieces = 0;
68 // cycles from 1, since the node 0 is the tracker
69 for (int i=1; i<numberOfNodes; ++i) {
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());
75 // stats on number of neighbors per peer
76 neighborStats.add(((BitTorrent)(Network.get(i).getProtocol(pid))).getNNodes());
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;
83 for (int i=1; i<numberOfNodes; ++i) {
84 numberOfCompletedPieces = 0;
86 // discovers the status of the current peer (leecher or seeder)
87 int ps = ((BitTorrent)(Network.get(i).getProtocol(pid))).getPeerStatus();
90 peerStatus = "L"; //leecher
93 peerStatus = "S"; //seeder
97 if (Network.get(i)!=null) {
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++;
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).
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());
119 //System.out.println("[OBS] t " + CommonState.getTime() + "\t pc " + "0" + "\t n " + "0");
124 // prints the frequency of 0 (leechers) and 1 (seeders)
125 nodeStatusStats.printAll(System.out);
127 // prints the average number of neighbors per peer
128 System.out.println("Avg number of neighbors per peer: " + neighborStats.getAverage());