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 -- Retrieve the end time
29 tracker_data.deadline = tonumber(args[1])
31 simgrid.info("Tracker launched")
33 local now = simgrid.get_clock()
35 tracker_data.comm_received = simgrid.task.irecv("tracker")
36 while now < tracker_data.deadline do
37 task, err = tracker_data.comm_received:test()
39 simgrid.debug("Received a request from " .. task.mailbox)
40 tracker_data.comm_received = simgrid.task.irecv("tracker")
41 -- Sending peers to the peer
44 if #tracker_data.peers_list > 0 then
45 i = math.random(1,#tracker_data.peers_list)
47 while #peers < #tracker_data.peers_list and #peers < common_tracker.MAXIMUM_PEERS do
48 table.insert(peers,tracker_data.peers_list[i])
49 i = (i % #tracker_data.peers_list) +1
53 -- Add the peer to our peer list
54 table.insert(tracker_data.peers_list,task.peer_id)
55 -- Setting the interval
56 task.interval = TRACKER_QUERY_INTERVAL
57 -- Sending the task back to the peer
58 task:dsend(task.mailbox)
60 simgrid.process.sleep(1)
61 now = simgrid.get_clock()
65 simgrid.info("Tracker is leaving")