Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1a812b6771c47c4ff3ae5fa226ac1d4a42a19f1e
[simgrid.git] / examples / java / bittorrent / Tracker.java
1 /*
2  * Copyright (c) 2006-2013. The SimGrid Team.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. 
7  */
8 package bittorrent;
9 import java.util.ArrayList;
10
11 import org.simgrid.msg.Comm;
12 import org.simgrid.msg.Host;
13 import org.simgrid.msg.Msg;
14 import org.simgrid.msg.MsgException;
15 import org.simgrid.msg.Process;
16 import org.simgrid.msg.RngStream;
17 import org.simgrid.msg.Task;
18 /**
19  * Tracker, handle requests from peers.
20  */
21 public class Tracker extends Process {
22         protected RngStream stream;
23         /**
24          * Peers list
25          */
26         protected ArrayList<Integer> peersList;
27         /**
28          * End time for the simulation
29          */
30         protected double deadline;
31         /**
32          * Current comm received
33          */
34         protected Comm commReceived = null;
35         
36         public Tracker(Host host, String name, String[]args) {
37                 super(host,name,args);
38         }
39         
40         @Override
41         public void main(String[] args) throws MsgException {
42                 if (args.length != 1) {
43                         Msg.info("Wrong number of arguments for the tracker.");
44                         return;
45                 }
46                 //Build the RngStream object for randomness
47                 stream = new RngStream("tracker");
48                 //Retrieve the end time
49                 deadline = Double.valueOf(args[0]);
50                 //Building peers array
51                 peersList = new ArrayList<Integer>();
52                 
53                 Msg.info("Tracker launched.");          
54                 while (Msg.getClock() < deadline) {
55                         if (commReceived == null) {
56                                 commReceived = Task.irecv(Common.TRACKER_MAILBOX);
57                         }
58                         try {
59                                 if (commReceived.test()) {
60                                         Task task = commReceived.getTask();
61                                         if (task instanceof TrackerTask) {
62                                                 TrackerTask tTask = (TrackerTask)task;
63                                                 //Sending peers to the peer
64                                                 int nbPeers = 0;
65                                                 while (nbPeers < Common.MAXIMUM_PEERS && nbPeers < peersList.size()) {
66                                                         int nextPeer;
67                                                         do {
68                                                                 nextPeer = stream.randInt(0, peersList.size() - 1);
69                                                         } while (tTask.peers.contains(nextPeer));
70                                                         tTask.peers.add(peersList.get(nextPeer));
71                                                         nbPeers++;
72                                                 }
73                                                 //Adding the peer to our list
74                                                 peersList.add(tTask.peerId);
75                                                 tTask.type = TrackerTask.Type.ANSWER;
76                                                 //Setting the interval
77                                                 tTask.interval = Common.TRACKER_QUERY_INTERVAL;
78                                                 //Sending the task back to the peer
79                                                 tTask.dsend(tTask.mailbox);
80                                         }
81                                         commReceived = null;
82                                 }
83                                 else {
84                                         waitFor(1);
85                                 }
86                         }
87                         catch (MsgException e) {
88                                 commReceived = null;                            
89                         }
90                 }
91                 Msg.info("Tracker is leaving");
92         }
93
94 }