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;
28 import peersim.edsim.EDSimulator;
29 import peersim.transport.Transport;
30 import java.util.Random;
33 * This {@link Control} ...
35 public class NetworkInitializer implements Control {
37 * The protocol to operate on.
41 private static final String PAR_PROT="protocol";
43 private static final String PAR_TRANSPORT="transport";
45 private static final int TRACKER = 11;
47 private static final int CHOKE_TIME = 13;
49 private static final int OPTUNCHK_TIME = 14;
51 private static final int ANTISNUB_TIME = 15;
53 private static final int CHECKALIVE_TIME = 16;
55 private static final int TRACKERALIVE_TIME = 17;
57 /** Protocol identifier, obtained from config property */
58 private final int pid;
59 private final int tid;
60 private NodeInitializer init;
64 public NetworkInitializer(String prefix) {
65 pid = Configuration.getPid(prefix+"."+PAR_PROT);
66 tid = Configuration.getPid(prefix+"."+PAR_TRANSPORT);
67 init = new NodeInitializer(prefix);
70 public boolean execute() {
72 Node tracker = Network.get(0);
74 // manca l'inizializzazione del tracker;
76 ((BitTorrent)Network.get(0).getProtocol(pid)).initializeTracker();
78 for(int i=1; i<Network.size(); i++){
79 System.err.println("chiamate ad addNeighbor " + i);
80 ((BitTorrent)Network.get(0).getProtocol(pid)).addNeighbor(Network.get(i));
81 init.initialize(Network.get(i));
83 for(int i=1; i< Network.size(); i++){
84 Node n = Network.get(i);
86 Object ev = new SimpleMsg(TRACKER, n);
87 long latency = ((Transport)n.getProtocol(tid)).getLatency(n,tracker);
88 EDSimulator.add(latency,ev,tracker,pid);
89 // ((Transport) n.getProtocol(tid)).send(n,tracker, ev, pid);
91 ev = new SimpleEvent(CHOKE_TIME);
92 EDSimulator.add(10000,ev,n,pid);
93 ev = new SimpleEvent(OPTUNCHK_TIME);
94 EDSimulator.add(30000,ev,n,pid);
95 ev = new SimpleEvent(ANTISNUB_TIME);
96 EDSimulator.add(60000,ev,n,pid);
97 ev = new SimpleEvent(CHECKALIVE_TIME);
98 EDSimulator.add(120000,ev,n,pid);
99 ev = new SimpleEvent(TRACKERALIVE_TIME);
100 EDSimulator.add(1800000,ev,n,pid);