r97 - in /debtorrent/branches/http-listen/DebTorrent: BT1/AptListener.py launchmanycore.py

camrdale-guest at users.alioth.debian.org camrdale-guest at users.alioth.debian.org
Mon Jun 11 22:38:06 UTC 2007


Author: camrdale-guest
Date: Mon Jun 11 22:38:06 2007
New Revision: 97

URL: http://svn.debian.org/wsvn/debtorrent/?sc=1&rev=97
Log:
First attempt at downloading packages through debtorrent (currently broken).

Modified:
    debtorrent/branches/http-listen/DebTorrent/BT1/AptListener.py
    debtorrent/branches/http-listen/DebTorrent/launchmanycore.py

Modified: debtorrent/branches/http-listen/DebTorrent/BT1/AptListener.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/branches/http-listen/DebTorrent/BT1/AptListener.py?rev=97&op=diff
==============================================================================
--- debtorrent/branches/http-listen/DebTorrent/BT1/AptListener.py (original)
+++ debtorrent/branches/http-listen/DebTorrent/BT1/AptListener.py Mon Jun 11 22:38:06 2007
@@ -33,6 +33,7 @@
 from types import StringType, IntType, LongType, ListType, DictType
 from binascii import b2a_hex, a2b_hex, a2b_base64
 from string import lower
+from time import sleep
 import sys, os
 import signal
 import re
@@ -418,8 +419,37 @@
                 msg = 'Unknown error occurred'
             return (status, 'Not Found', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, msg)
             
-
-
+    def get_package(self, path):
+        """Download a package file from a torrent.
+        
+        @type path: C{list} of C{string}
+        @param path: the path of the file to download, starting with the mirror name
+        @rtype: (C{int}, C{string}, C{dictionary}, C{string})
+        @return: the HTTP status code, status message, headers, and bencoded 
+            metainfo file
+        
+        """
+        
+        d, f = self.handler.find_file(path[1:])
+        
+        if d is None:
+            return (404, 'Not Found', {'Content-Type': 'text/plain', 'Pragma': 'no-cache'}, alas)
+        
+        # TODO: check if the torrent is running/not paused
+        d.fileselector.storage.enable_file(f)
+        
+        # TODO: make this threaded using rawserver
+        data = ''
+        start_piece, end_piece = d.fileselector.storage.file_pieces[f]
+        for piece in xrange(start_piece, end_piece+1):
+            while not d.storagewrapper.do_I_have(piece):
+                sleep(2)
+            data.append(d.storagewrapper.get_piece(piece, 0, -1).getarray())
+        
+        # TODO: check for waiting too long
+        # TODO: add headers here
+        return (200, 'OK', {}, data)
+    
     def get(self, connection, path, headers):
         """Respond to a GET request.
         
@@ -506,8 +536,8 @@
 #            if path[-1] in ('Packages', 'Packages.gz', 'Packages.bz2'):
 #                return self.get_Packages(path)
 
-#            if path[-1][-4:] == '.deb':
-#                return self.get_package(path)
+            if path[-1][-4:] == '.deb':
+                return self.get_package(path)
             
             return self.get_file(path)
             

Modified: debtorrent/branches/http-listen/DebTorrent/launchmanycore.py
URL: http://svn.debian.org/wsvn/debtorrent/debtorrent/branches/http-listen/DebTorrent/launchmanycore.py?rev=97&op=diff
==============================================================================
--- debtorrent/branches/http-listen/DebTorrent/launchmanycore.py (original)
+++ debtorrent/branches/http-listen/DebTorrent/launchmanycore.py Mon Jun 11 22:38:06 2007
@@ -27,7 +27,7 @@
 from socket import error as socketerror
 from threading import Event
 from sys import argv, exit
-import sys, os
+import sys, os, binascii
 from clock import clock
 from __init__ import createPeerID, mapbase64, version
 from cStringIO import StringIO
@@ -41,6 +41,7 @@
     True = 1
     False = 0
 
+DEBUG = True
 
 def fmttime(n):
     """Formats seconds into a human-readable time.
@@ -569,6 +570,36 @@
         return saveas
 
 
+    def find_file(self, path):
+        """Find which running torrent has the file.
+        
+        Checks the metainfo of each torrent in the cache to find one that
+        has a file whose 'path' matches the given file's path.
+        
+        @type path: C{list} of C{string}
+        @param path: the path of the file to find
+        @rtype: L{download_bt1.BT1Download}, C{int}
+        @return: the running torrent that contains the file and the file's number
+            (or None if no running torrents contain the file)
+        
+        """
+
+        file = '/'.join(path)
+        if DEBUG:
+            print 'Trying to find file:', file
+        for hash, data in self.torrent_cache.items():
+            file_num = -1
+            for f in data['metainfo']['info']['files']:
+                file_num += 1
+                if '/'.join(f['path']) == file:
+                    if DEBUG:
+                        print 'Found file in:', binascii.b2a_hex(hash)
+                    return self.downloads[hash], file_num
+        if DEBUG:
+            print 'Failed to find file.'
+        return None, None
+
+
     def hashchecksched(self, hash = None):
         """Schedule a new torrent for hash checking.
         




More information about the Debtorrent-commits mailing list