Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another out of date script
[simgrid.git] / contrib / psg / src / example / bittorrent / NetworkInitializer.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 import peersim.edsim.EDSimulator;
29 import peersim.transport.Transport;
30 import java.util.Random;
31
32 /**
33  * This {@link Control} ...
34  */
35 public class NetworkInitializer implements Control {
36         /**
37         * The protocol to operate on.
38         *
39         * @config
40         */
41         private static final String PAR_PROT="protocol";
42                 
43         private static final String PAR_TRANSPORT="transport";
44         
45         private static final int TRACKER = 11;
46         
47         private static final int CHOKE_TIME = 13;
48         
49         private static final int OPTUNCHK_TIME = 14;
50         
51         private static final int ANTISNUB_TIME = 15;
52         
53         private static final int CHECKALIVE_TIME = 16;
54         
55         private static final int TRACKERALIVE_TIME = 17;
56         
57         /** Protocol identifier, obtained from config property */
58         private final int pid;
59         private final int tid;
60         private NodeInitializer init;
61         
62         private Random rnd;
63         
64         public NetworkInitializer(String prefix) {
65                 pid = Configuration.getPid(prefix+"."+PAR_PROT);
66                 tid = Configuration.getPid(prefix+"."+PAR_TRANSPORT);
67                 init = new NodeInitializer(prefix);
68         }
69         
70         public boolean execute() {
71                 int completed;
72                 Node tracker = Network.get(0);
73                 
74                 // manca l'inizializzazione del tracker;
75                 
76                 ((BitTorrent)Network.get(0).getProtocol(pid)).initializeTracker();
77                 
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));
82                 }
83                 for(int i=1; i< Network.size(); i++){
84                         Node n = Network.get(i);
85                         
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);
90
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);
101                 }
102                 return true;
103         }
104         
105         }