1 -- Copyright (c) 2012, 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.
7 -- A SimGrid Lua implementation of the Bittorrent protocol.
23 -- Check the arguments
26 simgrid.info("Wrong number of arguments for the tracker")
28 -- Initialization of the random generator
30 -- Retrieve the end time
31 tracker_data.deadline = tonumber(args[1])
33 simgrid.info("Tracker launched")
35 local now = simgrid.get_clock()
37 tracker_data.comm_received = simgrid.task.irecv("tracker")
38 while now < tracker_data.deadline do
39 task, err = tracker_data.comm_received:test()
41 simgrid.debug("Received a request from " .. task.mailbox)
42 tracker_data.comm_received = simgrid.task.irecv("tracker")
43 -- Sending peers to the peer
46 if #tracker_data.peers_list > 0 then
47 i = math.random(1,#tracker_data.peers_list)
49 while #peers < #tracker_data.peers_list and #peers < common_tracker.MAXIMUM_PEERS do
50 table.insert(peers,tracker_data.peers_list[i])
51 i = (i + 1) % #tracker_data.peers_list
55 -- Add the peer to our peer list
56 table.insert(tracker_data.peers_list,task.peer_id)
57 -- Setting the interval
58 task.interval = TRACKER_QUERY_INTERVAL
59 -- Sending the task back to the peer
60 task:dsend(task.mailbox)
62 simgrid.process.sleep(1)
63 now = simgrid.get_clock()
67 simgrid.info("Tracker is leaving")