1 /* Copyright (c) 2006-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
8 import java.util.ArrayList;
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;
18 * Tracker, handle requests from peers.
20 public class Tracker extends Process {
21 protected RngStream stream;
25 protected ArrayList<Integer> peersList;
27 * End time for the simulation
29 protected double deadline;
31 * Current comm received
33 protected Comm commReceived = null;
35 public Tracker(Host host, String name, String[]args) {
36 super(host,name,args);
40 public void main(String[] args) throws MsgException {
41 if (args.length != 1) {
42 Msg.info("Wrong number of arguments for the tracker.");
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>();
52 Msg.info("Tracker launched.");
53 while (Msg.getClock() < deadline) {
54 if (commReceived == null) {
55 commReceived = Task.irecv(Common.TRACKER_MAILBOX);
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
64 while (nbPeers < Common.MAXIMUM_PEERS && nbPeers < peersList.size()) {
67 nextPeer = stream.randInt(0, peersList.size() - 1);
68 } while (tTask.peers.contains(nextPeer));
69 tTask.peers.add(peersList.get(nextPeer));
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);
86 catch (MsgException e) {
90 Msg.info("Tracker is leaving");