Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[Lua] Fixed garbage collection segfault
[simgrid.git] / examples / lua / bittorrent / peer.lua
index 496af85..7166c00 100644 (file)
@@ -98,32 +98,32 @@ function leech_loop()
        simgrid.info("Starting main leech loop")
        local task, err
        while now < data.deadline and data.pieces < common.FILE_PIECES do
-               task, err = data.comm_received:test()
-               if task then
-                       handle_message(task)
-                       data.comm_received = simgrid.task.irecv(data.mailbox)
-                       now = simgrid.get_clock()
-               elseif err then
-                       data.comm_received = simgrid.task.irecv(data.mailbox)           
-               else
-                       -- If the user has a pending interesting
-                       if data.current_piece ~= -1 then
-                               send_interested_to_peers()
-                       else
-                               if table.getn(data.current_pieces) < common.MAX_PIECES then
-                                       update_current_piece()
-                               end
-                       end
-                       -- We don't execute the choke algorithm if we don't already have a piece
-                       if now >= next_choked_update and data.pieces > 0 then
-                               update_choked_peers()
-                               next_choked_update = next_choked_update + common.UPDATE_CHOKED_INTERVAL
-                               now = simgrid.get_clock()
-                       else
-                               simgrid.process.sleep(1)
-                               now = simgrid.get_clock()
-                       end
-               end             
+        task, err = data.comm_received:test()
+        if task then
+            handle_message(task)
+            data.comm_received = simgrid.task.irecv(data.mailbox)
+            now = simgrid.get_clock()
+        elseif err then
+            data.comm_received = simgrid.task.irecv(data.mailbox)
+        else
+            -- If the user has a pending interesting
+            if data.current_piece ~= -1 then
+                send_interested_to_peers()
+            else
+                if #data.current_pieces < common.MAX_PIECES then
+                    update_current_piece()
+                end
+            end
+            -- We don't execute the choke algorithm if we don't already have a piece
+            if now >= next_choked_update and data.pieces > 0 then
+                update_choked_peers()
+                next_choked_update = next_choked_update + common.UPDATE_CHOKED_INTERVAL
+                now = simgrid.get_clock()
+            else
+                simgrid.process.sleep(1)
+                now = simgrid.get_clock()
+            end
+        end
        end
 end
 -- Peer main loop when it is seeding
@@ -183,16 +183,26 @@ function get_peers_data()
                                if v ~= data.id then
                                        --Add the peer to our list and build its data
                                        local peer_data = {}
-                                       peer_data.id = v;
+                                       peer_data.id = math.tointeger(v);
                                        peer_data.bitfield = nil
-                                       peer_data.mailbox = tostring(v);
+                                       peer_data.mailbox = math.tointeger(v);
                                        peer_data.am_interested = false
                                        peer_data.interested = false
                                        peer_data.choked_upload = true
                                        peer_data.choked_download = true
-                                       data.peers[v] = peer_data
+                    data.peers[v] = peer_data
+                    simgrid.info("Added " .. v)
                                end
                        end
+            mt = {}
+            mt.__len = function(obj)
+                local len = 0;
+                for j,k in pairs(obj) do
+                    len = len+1
+                end
+                return len
+            end
+            setmetatable(data.peers, mt)
                else
                        success = false
                end
@@ -276,6 +286,7 @@ function handle_message(task)
                        end
                end
        elseif task.type == "PIECE" then
+        task.piece = math.tointeger(task.piece)
                if task.stalled == true then
                        simgrid.debug("The received piece is stalled")
                else
@@ -416,6 +427,7 @@ function send_interested(mailbox)
 end
 -- Send a "not interested" message to a peer.
 function send_not_interested(mailbox)
+    simgrid.info("Sending a send_not_interested")
        local task = new_task("NOTINTERESTED")
        task:dsend(mailbox)
 end
@@ -446,6 +458,7 @@ function send_unchoked(mailbox)
 end
 -- Send a "HAVE" message to all peers we are connected to
 function send_have(piece)
+      simgrid.debug("Sending a HAVE message")
        for i,v in pairs(data.peers) do
                local task = new_task("HAVE")
                task.piece = piece
@@ -454,6 +467,7 @@ function send_have(piece)
 end
 -- Send request messages to a peer that have unchoked us       
 function send_requests_to_peer(remote_peer)
+    simgrid.debug("Sending a request to peer " .. remote_peer.mailbox)
        for i,v in pairs(data.current_pieces) do
                send_request(remote_peer.mailbox,i)
        end
@@ -481,7 +495,7 @@ function send_piece(mailbox, piece, stalled)
        task:dsend(mailbox)     
 end
 function new_task(type)
-       local task = simgrid.task.new("", 0, common.MESSAGE_SIZE)
+       local task = simgrid.task.new(type, 0, common.MESSAGE_SIZE)
        task.type = type
        task.mailbox = data.mailbox
        task.peer_id = data.id