Logo AND Algorithmique Numérique Distribuée

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