コード例 #1
0
    def simpleSetUp(self):
        #self.kfiles()
        d = dict([(x[0], x[1]) for x in common_options + rare_options])
        self.r = RawServer(Event(), d)
        self.l = []
        for i in range(self.num):
            self.l.append(
                UTKhashmir('', self.startport + i,
                           'kh%s.db' % (self.startport + i), self.r))

        for i in self.l:
            i.addContact(self.localip, self.l[randrange(0, self.num)].port)
            i.addContact(self.localip, self.l[randrange(0, self.num)].port)
            i.addContact(self.localip, self.l[randrange(0, self.num)].port)
            self.r.listen_once(1)
            self.r.listen_once(1)
            self.r.listen_once(1)

        for i in self.l:
            self.done = 0
            i.findCloseNodes(self._done)
            while not self.done:
                self.r.listen_once(1)
        for i in self.l:
            self.done = 0
            i.findCloseNodes(self._done)
            while not self.done:
                self.r.listen_once(1)
コード例 #2
0
class Network:
    def __init__(self, size=0, startport=5555, localip='127.0.0.1'):
        self.num = size
        self.startport = startport
        self.localip = localip

    def _done(self, val):
        self.done = 1

    def simpleSetUp(self):
        #self.kfiles()
        d = dict([(x[0], x[1]) for x in common_options + rare_options])
        self.r = RawServer(Event(), d)
        self.l = []
        for i in range(self.num):
            self.l.append(
                UTKhashmir('', self.startport + i,
                           'kh%s.db' % (self.startport + i), self.r))

        for i in self.l:
            i.addContact(self.localip, self.l[randrange(0, self.num)].port)
            i.addContact(self.localip, self.l[randrange(0, self.num)].port)
            i.addContact(self.localip, self.l[randrange(0, self.num)].port)
            self.r.listen_once(1)
            self.r.listen_once(1)
            self.r.listen_once(1)

        for i in self.l:
            self.done = 0
            i.findCloseNodes(self._done)
            while not self.done:
                self.r.listen_once(1)
        for i in self.l:
            self.done = 0
            i.findCloseNodes(self._done)
            while not self.done:
                self.r.listen_once(1)

    def tearDown(self):
        for i in self.l:
            i.rawserver.stop_listening_udp(i.socket)
            i.socket.close()
        #self.kfiles()

    def kfiles(self):
        for i in range(self.startport, self.startport + self.num):
            try:
                os.unlink('kh%s.db' % i)
            except:
                pass

        self.r.listen_once(1)
コード例 #3
0
ファイル: unet.py プロジェクト: git-plus/BitTorrent-Py-4.1.7
class Network:
    def __init__(self, size=0, startport=5555, localip='127.0.0.1'):
        self.num = size
        self.startport = startport
        self.localip = localip

    def _done(self, val):
        self.done = 1
        
    def simpleSetUp(self):
        #self.kfiles()
        d = dict([(x[0],x[1]) for x in common_options + rare_options])
        self.r = RawServer(Event(), d)
        self.l = []
        for i in range(self.num):
            self.l.append(UTKhashmir('', self.startport + i, 'kh%s.db' % (self.startport + i), self.r))
        
        for i in self.l:
            i.addContact(self.localip, self.l[randrange(0,self.num)].port)
            i.addContact(self.localip, self.l[randrange(0,self.num)].port)
            i.addContact(self.localip, self.l[randrange(0,self.num)].port)
            self.r.listen_once(1)
            self.r.listen_once(1)
            self.r.listen_once(1) 
            
        for i in self.l:
            self.done = 0
            i.findCloseNodes(self._done)
            while not self.done:
                self.r.listen_once(1)
        for i in self.l:
            self.done = 0
            i.findCloseNodes(self._done)
            while not self.done:
                self.r.listen_once(1)

    def tearDown(self):
        for i in self.l:
            i.rawserver.stop_listening_udp(i.socket)
            i.socket.close()
        #self.kfiles()
        
    def kfiles(self):
        for i in range(self.startport, self.startport+self.num):
            try:
                os.unlink('kh%s.db' % i)
            except:
                pass
            
        self.r.listen_once(1)
コード例 #4
0
ファイル: IPC.py プロジェクト: galaxy001/libtorrent
class IPCUnixSocket(IPCSocketBase):
    def __init__(self, *args):
        IPCSocketBase.__init__(self, *args)
        self.socket_filename = os.path.join(self.config['data_dir'],
                                            'ui_socket')

    def create(self):
        filename = self.socket_filename
        if os.path.exists(filename):
            try:
                self.send_command('no-op')
            except BTFailure:
                pass
            else:
                raise BTFailure(
                    _("Could not create control socket: already in use"))

            try:
                os.unlink(filename)
            except OSError, e:
                raise BTFailure(
                    _("Could not remove old control socket filename:") +
                    str(e))
        try:
            controlsocket = RawServer.create_unixserversocket(filename)
        except socket.error, e:
            raise BTFailure(_("Could not create control socket: ") + str(e))
コード例 #5
0
ファイル: unet.py プロジェクト: git-plus/BitTorrent-Py-4.1.7
 def simpleSetUp(self):
     #self.kfiles()
     d = dict([(x[0],x[1]) for x in common_options + rare_options])
     self.r = RawServer(Event(), d)
     self.l = []
     for i in range(self.num):
         self.l.append(UTKhashmir('', self.startport + i, 'kh%s.db' % (self.startport + i), self.r))
     
     for i in self.l:
         i.addContact(self.localip, self.l[randrange(0,self.num)].port)
         i.addContact(self.localip, self.l[randrange(0,self.num)].port)
         i.addContact(self.localip, self.l[randrange(0,self.num)].port)
         self.r.listen_once(1)
         self.r.listen_once(1)
         self.r.listen_once(1) 
         
     for i in self.l:
         self.done = 0
         i.findCloseNodes(self._done)
         while not self.done:
             self.r.listen_once(1)
     for i in self.l:
         self.done = 0
         i.findCloseNodes(self._done)
         while not self.done:
             self.r.listen_once(1)
コード例 #6
0
ファイル: controlsocket.py プロジェクト: rays/ipodderx-core
 def create_socket_inet(self, port = CONTROL_SOCKET_PORT):
     
     try:
         controlsocket = RawServer.create_serversocket(port,
                                                       '127.0.0.1', reuse=True)
     except socket.error, e:
         raise BTFailure(_("Could not create control socket: ")+str(e))
コード例 #7
0
ファイル: IPC.py プロジェクト: galaxy001/libtorrent
    def create(self):
        obtain_mutex = 1
        mutex = win32event.CreateMutex(None, obtain_mutex, app_name)

        # prevent the PyHANDLE from going out of scope, ints are fine
        self.mutex = int(mutex)
        mutex.Detach()

        lasterror = win32api.GetLastError()

        if lasterror == winerror.ERROR_ALREADY_EXISTS:
            takeover = 0

            try:
                # if the mutex already exists, discover which port to connect to.
                # if something goes wrong with that, tell us to take over the
                # role of master
                takeover = self.discover_sic_socket()
            except:
                pass

            if not takeover:
                raise BTFailure(_("Global mutex already created."))

        self.master = 1

        # lazy free port code
        port_limit = 50000
        while self.port < port_limit:
            try:
                controlsocket = RawServer.create_serversocket(self.port, "127.0.0.1", reuse=True)
                self.controlsocket = controlsocket
                break
            except socket.error, e:
                self.port += 1
コード例 #8
0
ファイル: khashmir.py プロジェクト: galaxy001/libtorrent
 def __init__(self, host, port, data_dir, rawserver=None, max_ul_rate=1024, checkpoint=True, errfunc=None, rlcount=foo, config={'pause':False, 'max_rate_period':20}):
     if rawserver:
         self.rawserver = rawserver
     else:
         self.flag = Event()
         d = dict([(x[0],x[1]) for x in common_options + rare_options])            
         self.rawserver = RawServer(self.flag, d)
     self.max_ul_rate = max_ul_rate
     self.socket = None
     self.config = config
     self.setup(host, port, data_dir, rlcount, checkpoint)
コード例 #9
0
 def __init__(self,
              host,
              port,
              data_dir,
              rawserver=None,
              max_ul_rate=1024,
              checkpoint=True,
              errfunc=None,
              rlcount=foo,
              config={
                  'pause': False,
                  'max_rate_period': 20
              }):
     if rawserver:
         self.rawserver = rawserver
     else:
         self.flag = Event()
         d = dict([(x[0], x[1]) for x in common_options + rare_options])
         self.rawserver = RawServer(self.flag, d)
     self.max_ul_rate = max_ul_rate
     self.socket = None
     self.config = config
     self.setup(host, port, data_dir, rlcount, checkpoint)
コード例 #10
0
ファイル: download.py プロジェクト: hiplayer/BitFountain
 def __init__(self, config, doneflag, errorfunc, listen_fail_ok=False):
     self.dht = None
     self.config = config
     self.errorfunc = errorfunc
     self.rawserver = RawServer(doneflag, config, errorfunc=errorfunc,
                                tos=config['peer_socket_tos'])
     set_zurllib_rawserver(self.rawserver)
     add_unsafe_thread()
     self.nattraverser = NatTraverser(self.rawserver, logfunc=errorfunc)
     self.singleport_listener = SingleportListener(self.rawserver,
                                                   self.nattraverser)
     self.ratelimiter = RateLimiter(self.rawserver.add_task)
     self.ratelimiter.set_parameters(config['max_upload_rate'],
                                     config['upload_unit_size'])
     self._find_port(listen_fail_ok)
     self.filepool = FilePool(config['max_files_open'])
     set_filesystem_encoding(config['filesystem_encoding'],
                                              errorfunc)
コード例 #11
0
ファイル: IPC.py プロジェクト: galaxy001/libtorrent
    def create(self):
        obtain_mutex = 1
        mutex = win32event.CreateMutex(None, obtain_mutex, app_name)

        # prevent the PyHANDLE from going out of scope, ints are fine
        self.mutex = int(mutex)
        mutex.Detach()

        lasterror = win32api.GetLastError()

        if lasterror == winerror.ERROR_ALREADY_EXISTS:
            takeover = 0

            try:
                # if the mutex already exists, discover which port to connect to.
                # if something goes wrong with that, tell us to take over the
                # role of master
                takeover = self.discover_sic_socket()
            except:
                pass

            if not takeover:
                raise BTFailure(_("Global mutex already created."))

        self.master = 1

        # lazy free port code
        port_limit = 50000
        while self.port < port_limit:
            try:
                controlsocket = RawServer.create_serversocket(self.port,
                                                              '127.0.0.1',
                                                              reuse=True)
                self.controlsocket = controlsocket
                break
            except socket.error, e:
                self.port += 1
コード例 #12
0
class KhashmirBase:
    _Node = KNodeBase

    def __init__(self,
                 host,
                 port,
                 data_dir,
                 rawserver=None,
                 max_ul_rate=1024,
                 checkpoint=True,
                 errfunc=None,
                 rlcount=foo,
                 config={
                     'pause': False,
                     'max_rate_period': 20
                 }):
        if rawserver:
            self.rawserver = rawserver
        else:
            self.flag = Event()
            d = dict([(x[0], x[1]) for x in common_options + rare_options])
            self.rawserver = RawServer(self.flag, d)
        self.max_ul_rate = max_ul_rate
        self.socket = None
        self.config = config
        self.setup(host, port, data_dir, rlcount, checkpoint)

    def setup(self, host, port, data_dir, rlcount, checkpoint=True):
        self.host = host
        self.port = port
        self.ddir = data_dir
        self.store = KStore()
        self.pingcache = {}
        self.socket = self.rawserver.create_udpsocket(self.port, self.host,
                                                      False)
        self.udp = krpc.hostbroker(self, (self.host, self.port), self.socket,
                                   self.rawserver.add_task, self.max_ul_rate,
                                   self.config, rlcount)
        self._load()
        self.rawserver.start_listening_udp(self.socket, self.udp)
        self.last = time()
        KeyExpirer(self.store, self.rawserver.add_task)
        self.refreshTable(force=1)
        if checkpoint:
            self.rawserver.add_task(self.findCloseNodes, 30,
                                    (lambda a: a, True))
            self.rawserver.add_task(self.checkpoint, 60, (1, ))

    def Node(self):
        n = self._Node(self.udp.connectionForAddr)
        n.table = self
        return n

    def __del__(self):
        if self.socket is not None:
            self.rawserver.stop_listening_udp(self.socket)
            self.socket.close()

    def _load(self):
        do_load = False
        try:
            s = open(os.path.join(self.ddir, "routing_table"), 'r').read()
            dict = bdecode(s)
        except:
            id = newID()
        else:
            id = dict['id']
            do_load = True

        self.node = self._Node(self.udp.connectionForAddr).init(
            id, self.host, self.port)
        self.table = KTable(self.node)
        if do_load:
            self._loadRoutingTable(dict['rt'])

    def checkpoint(self, auto=0):
        d = {}
        d['id'] = self.node.id
        d['rt'] = self._dumpRoutingTable()
        try:
            f = open(os.path.join(self.ddir, "routing_table"), 'wb')
            f.write(bencode(d))
            f.close()
        except Exception, e:
            #XXX real error here
            print ">>> unable to dump routing table!", str(e)
            pass

        if auto:
            self.rawserver.add_task(
                self.checkpoint,
                randrange(int(const.CHECKPOINT_INTERVAL * .9),
                          int(const.CHECKPOINT_INTERVAL * 1.1)), (1, ))
コード例 #13
0
ファイル: khashmir.py プロジェクト: galaxy001/libtorrent
class KhashmirBase:
    _Node = KNodeBase
    def __init__(self, host, port, data_dir, rawserver=None, max_ul_rate=1024, checkpoint=True, errfunc=None, rlcount=foo, config={'pause':False, 'max_rate_period':20}):
        if rawserver:
            self.rawserver = rawserver
        else:
            self.flag = Event()
            d = dict([(x[0],x[1]) for x in common_options + rare_options])            
            self.rawserver = RawServer(self.flag, d)
        self.max_ul_rate = max_ul_rate
        self.socket = None
        self.config = config
        self.setup(host, port, data_dir, rlcount, checkpoint)

    def setup(self, host, port, data_dir, rlcount, checkpoint=True):
        self.host = host
        self.port = port
        self.ddir = data_dir
        self.store = KStore()
        self.pingcache = {}
        self.socket = self.rawserver.create_udpsocket(self.port, self.host, False)
        self.udp = krpc.hostbroker(self, (self.host, self.port), self.socket, self.rawserver.add_task, self.max_ul_rate, self.config, rlcount)
        self._load()
        self.rawserver.start_listening_udp(self.socket, self.udp)
        self.last = time()
        KeyExpirer(self.store, self.rawserver.add_task)
        self.refreshTable(force=1)
        if checkpoint:
            self.rawserver.add_task(self.findCloseNodes, 30, (lambda a: a, True))
            self.rawserver.add_task(self.checkpoint, 60, (1,))

    def Node(self):
        n = self._Node(self.udp.connectionForAddr)
        n.table = self
        return n
    
    def __del__(self):
        if self.socket is not None:
            self.rawserver.stop_listening_udp(self.socket)
            self.socket.close()
        
    def _load(self):
        do_load = False
        try:
            s = open(os.path.join(self.ddir, "routing_table"), 'r').read()
            dict = bdecode(s)
        except:
            id = newID()
        else:
            id = dict['id']
            do_load = True
            
        self.node = self._Node(self.udp.connectionForAddr).init(id, self.host, self.port)
        self.table = KTable(self.node)
        if do_load:
            self._loadRoutingTable(dict['rt'])

        
    def checkpoint(self, auto=0):
        d = {}
        d['id'] = self.node.id
        d['rt'] = self._dumpRoutingTable()
        try:
            f = open(os.path.join(self.ddir, "routing_table"), 'wb')
            f.write(bencode(d))
            f.close()
        except Exception, e:
            #XXX real error here
            print ">>> unable to dump routing table!", str(e)
            pass
        
        
        if auto:
            self.rawserver.add_task(self.checkpoint,
                                    randrange(int(const.CHECKPOINT_INTERVAL * .9),
                                              int(const.CHECKPOINT_INTERVAL * 1.1)),
                                    (1,))
コード例 #14
0
ファイル: track.py プロジェクト: gepolquevedo/EE300-Quevedo
                    del self.seedcount[key]
        self.rawserver.add_task(self.expire_downloaders, self.timeout_downloaders_interval)


def track(args):
    if len(args) == 0:
        print formatDefinitions(defaults, 80)
        return
    try:
        config, files = parseargs(args, defaults, 0, 0)
    except ValueError, e:
        print _("error: ") + str(e)
        print _("run with no arguments for parameter explanations")
        return
    file(config["pid"], "w").write(str(os.getpid()))
    r = RawServer(Event(), config)
    t = Tracker(config, r)
    s = r.create_serversocket(config["port"], config["bind"], True)
    r.start_listening(s, HTTPHandler(t.get, config["min_time_between_log_flushes"]))
    r.listen_forever()
    t.save_dfile()
    print _("# Shutting down: ") + isotime()


def size_format(s):
    if s < 1024:
        r = str(s) + "B"
    elif s < 1048576:
        r = str(int(s / 1024)) + "KiB"
    elif s < 1073741824:
        r = str(int(s / 1048576)) + "MiB"
コード例 #15
0
ファイル: track.py プロジェクト: hiplayer/BitFountain
        self.rawserver.add_task(self.expire_downloaders,
                                self.timeout_downloaders_interval)


def track(args):
    if len(args) == 0:
        print formatDefinitions(defaults, 80)
        return
    try:
        config, files = parseargs(args, defaults, 0, 0)
    except ValueError, e:
        print _("error: ") + str(e)
        print _("run with no arguments for parameter explanations")
        return
    #file(config['pid'], 'w').write(str(os.getpid()))
    r = RawServer(Event(), config)
    t = Tracker(config, r)
    s = r.create_serversocket(config['port'], config['bind'], True)
    r.start_listening(
        s, HTTPHandler(t.get, config['min_time_between_log_flushes']))
    r.listen_forever()
    t.save_dfile()
    print _("# Shutting down: ") + isotime()


def size_format(s):
    if (s < 1024):
        r = str(s) + 'B'
    elif (s < 1048576):
        r = str(int(s / 1024)) + 'KiB'
    elif (s < 1073741824):
コード例 #16
0
ファイル: khashmir.py プロジェクト: rays/ipodderx-core
class KhashmirBase:
    _Node = KNodeBase
    def __init__(self, host, port, data_dir, rawserver=None, max_ul_rate=1024, checkpoint=True, errfunc=None, rlcount=foo, config={'pause':False, 'max_rate_period':20}):
        if rawserver:
            self.rawserver = rawserver
        else:
            self.flag = Event()
            d = dict([(x[0],x[1]) for x in common_options + rare_options])            
            self.rawserver = RawServer(self.flag, d)
        self.max_ul_rate = max_ul_rate
        self.socket = None
        self.config = config
        self.setup(host, port, data_dir, rlcount, checkpoint)

    def setup(self, host, port, data_dir, rlcount, checkpoint=True):
        self.host = host
        self.port = port
        self.ddir = data_dir
        self.store = KStore()
        self.pingcache = {}
        self.socket = self.rawserver.create_udpsocket(self.port, self.host, False)
        self.udp = krpc.hostbroker(self, (self.host, self.port), self.socket, self.rawserver.add_task, self.max_ul_rate, self.config, rlcount)
        self._load()
        self.rawserver.start_listening_udp(self.socket, self.udp)
        self.last = time()
        KeyExpirer(self.store, self.rawserver.add_task)
        self.refreshTable(force=1)
        if checkpoint:
            self.rawserver.add_task(self.findCloseNodes, 30, (lambda a: a, True))
            self.rawserver.add_task(self.checkpoint, 60, (1,))

    def Node(self):
        n = self._Node(self.udp.connectionForAddr)
        n.table = self
        return n
    
    def __del__(self):
        if self.socket is not None:
            self.rawserver.stop_listening_udp(self.socket)
            self.socket.close()
        
    def _load(self):
        do_load = False
        try:
            s = open(os.path.join(self.ddir, "routing_table"), 'r').read()
            dict = bdecode(s)
        except:
            id = newID()
        else:
            id = dict['id']
            do_load = True
            
        self.node = self._Node(self.udp.connectionForAddr).init(id, self.host, self.port)
        self.table = KTable(self.node)
        if do_load:
            self._loadRoutingTable(dict['rt'])

        
    def checkpoint(self, auto=0):
        d = {}
        d['id'] = self.node.id
        d['rt'] = self._dumpRoutingTable()
        try:
            f = open(os.path.join(self.ddir, "routing_table"), 'wb')
            f.write(bencode(d))
            f.close()
        except:
            #XXX real error here
            print ">>> unable to dump routing table!", str(e)
            pass
        
        
        if auto:
            self.rawserver.add_task(self.checkpoint,
                                    randrange(int(const.CHECKPOINT_INTERVAL * .9),
                                              int(const.CHECKPOINT_INTERVAL * 1.1)),
                                    (1,))
        
    def _loadRoutingTable(self, nodes):
        """
            load routing table nodes from database
            it's usually a good idea to call refreshTable(force=1) after loading the table
        """
        for rec in nodes:
            n = self.Node().initWithDict(rec)
            self.table.insertNode(n, contacted=0, nocheck=True)

    def _dumpRoutingTable(self):
        """
            save routing table nodes to the database
        """
        l = []
        for bucket in self.table.buckets:
            for node in bucket.l:
                l.append({'id':node.id, 'host':node.host, 'port':node.port, 'age':int(node.age)})
        return l
        
            
    def _addContact(self, host, port, callback=None):
        """
            ping this node and add the contact info to the table on pong!
        """
        n =self.Node().init(const.NULL_ID, host, port)
        try:
            self.sendPing(n, callback=callback)
        except krpc.KRPCSelfNodeError:
            # our own node
            pass


    #######
    #######  LOCAL INTERFACE    - use these methods!
    def addContact(self, ip, port, callback=None):
        """
            ping this node and add the contact info to the table on pong!
        """
        if ip_pat.match(ip):
            self._addContact(ip, port)
        else:
            def go(ip=ip, port=port):
                ip = gethostbyname(ip)
                self.rawserver.external_add_task(self._addContact, 0, (ip, port))
            t = Thread(target=go)
            t.start()


    ## this call is async!
    def findNode(self, id, callback, errback=None):
        """ returns the contact info for node, or the k closest nodes, from the global table """
        # get K nodes out of local table/cache, or the node we want
        nodes = self.table.findNodes(id, invalid=True)
        l = [x for x in nodes if x.invalid]
        if len(l) > 4:
            nodes = sample(l , 4) + self.table.findNodes(id, invalid=False)[:4]

        d = Deferred()
        if errback:
            d.addCallbacks(callback, errback)
        else:
            d.addCallback(callback)
        if len(nodes) == 1 and nodes[0].id == id :
            d.callback(nodes)
        else:
            # create our search state
            state = FindNode(self, id, d.callback, self.rawserver.add_task)
            self.rawserver.external_add_task(state.goWithNodes, 0, (nodes,))
    
    def insertNode(self, n, contacted=1):
        """
        insert a node in our local table, pinging oldest contact in bucket, if necessary
        
        If all you have is a host/port, then use addContact, which calls this method after
        receiving the PONG from the remote node.  The reason for the seperation is we can't insert
        a node into the table without it's peer-ID.  That means of course the node passed into this
        method needs to be a properly formed Node object with a valid ID.
        """
        old = self.table.insertNode(n, contacted=contacted)
        if old and old != n:
            if not old.inPing():
                self.checkOldNode(old, n, contacted)
            else:
                l = self.pingcache.get(old.id, [])
                if len(l) < 10 or contacted:
                    l.append((n, contacted))
                    self.pingcache[old.id] = l

                

    def checkOldNode(self, old, new, contacted=False):
        ## these are the callbacks used when we ping the oldest node in a bucket

        def cmp(a, b):
            if a[1] == 1 and b[1] == 0:
                return -1
            elif b[1] == 1 and a[1] == 0:
                return 1
            else:
                return 0
            
        def _staleNodeHandler(dict, old=old, new=new, contacted=contacted):
            """ called if the pinged node never responds """
            if old.fails >= 2:
                l = self.pingcache.get(old.id, [])
                l.sort(cmp)
                if l:
                    n, nc = l[0]
                    if (not contacted) and nc:
                        l = l[1:] + [(new, contacted)]
                        new = n
                        contacted = nc
                o = self.table.replaceStaleNode(old, new)
                if o and o != new:
                    self.checkOldNode(o, new)
                    try:
                        self.pingcache[o.id] = self.pingcache[old.id]
                        del(self.pingcache[old.id])
                    except KeyError:
                        pass
                else:
                    if l:
                        del(self.pingcache[old.id])
                        l.sort(cmp)
                        for node in l:
                            self.insertNode(node[0], node[1])
            else:
                l = self.pingcache.get(old.id, [])
                if l:
                    del(self.pingcache[old.id])
                self.insertNode(new, contacted)
                for node in l:
                    self.insertNode(node[0], node[1])
                    
        def _notStaleNodeHandler(dict, old=old, new=new, contacted=contacted):
            """ called when we get a pong from the old node """
            self.table.insertNode(old, True)
            self.insertNode(new, contacted)
            l = self.pingcache.get(old.id, [])
            l.sort(cmp)
            for node in l:
                self.insertNode(node[0], node[1])
            try:
                del(self.pingcache[old.id])
            except KeyError:
                pass
        try:
            df = old.ping(self.node.id)
        except krpc.KRPCSelfNodeError:
            pass
        df.addCallbacks(_notStaleNodeHandler, _staleNodeHandler)

    def sendPing(self, node, callback=None):
        """
            ping a node
        """
        try:
            df = node.ping(self.node.id)
        except krpc.KRPCSelfNodeError:
            pass
        else:
            ## these are the callbacks we use when we issue a PING
            def _pongHandler(dict, node=node, table=self.table, callback=callback):
                _krpc_sender = dict['_krpc_sender']
                dict = dict['rsp']
                sender = {'id' : dict['id']}
                sender['host'] = _krpc_sender[0]
                sender['port'] = _krpc_sender[1]
                n = self.Node().initWithDict(sender)
                table.insertNode(n)
                if callback:
                    callback()
            def _defaultPong(err, node=node, table=self.table, callback=callback):
                if callback:
                    callback()

            df.addCallbacks(_pongHandler,_defaultPong)

    def findCloseNodes(self, callback=lambda a: a, auto=False):
        """
            This does a findNode on the ID one away from our own.  
            This will allow us to populate our table with nodes on our network closest to our own.
            This is called as soon as we start up with an empty table
        """
        if not self.config['pause']:
            id = self.node.id[:-1] + chr((ord(self.node.id[-1]) + 1) % 256)
            self.findNode(id, callback)
        if auto:
            if not self.config['pause']:
                self.refreshTable()
            self.rawserver.external_add_task(self.findCloseNodes, randrange(int(const.FIND_CLOSE_INTERVAL *0.9),
                                                                   int(const.FIND_CLOSE_INTERVAL *1.1)), (lambda a: True, True))

    def refreshTable(self, force=0):
        """
            force=1 will refresh table regardless of last bucket access time
        """
        def callback(nodes):
            pass

        refresh = [bucket for bucket in self.table.buckets if force or (len(bucket.l) < K) or len(filter(lambda a: a.invalid, bucket.l)) or (time() - bucket.lastAccessed > const.BUCKET_STALENESS)]
        for bucket in refresh:
            id = newIDInRange(bucket.min, bucket.max)
            self.findNode(id, callback)

    def stats(self):
        """
        Returns (num_contacts, num_nodes)
        num_contacts: number contacts in our routing table
        num_nodes: number of nodes estimated in the entire dht
        """
        num_contacts = reduce(lambda a, b: a + len(b.l), self.table.buckets, 0)
        num_nodes = const.K * (2**(len(self.table.buckets) - 1))
        return {'num_contacts':num_contacts, 'num_nodes':num_nodes}

    def krpc_ping(self, id, _krpc_sender):
        sender = {'id' : id}
        sender['host'] = _krpc_sender[0]
        sender['port'] = _krpc_sender[1]        
        n = self.Node().initWithDict(sender)
        self.insertNode(n, contacted=0)
        return {"id" : self.node.id}
        
    def krpc_find_node(self, target, id, _krpc_sender):
        nodes = self.table.findNodes(target, invalid=False)
        nodes = map(lambda node: node.senderDict(), nodes)
        sender = {'id' : id}
        sender['host'] = _krpc_sender[0]
        sender['port'] = _krpc_sender[1]        
        n = self.Node().initWithDict(sender)
        self.insertNode(n, contacted=0)
        return {"nodes" : packNodes(nodes), "id" : self.node.id}
コード例 #17
0
ファイル: controlsocket.py プロジェクト: rays/ipodderx-core
        filename = self.socket_filename
        if os.path.exists(filename):
            try:
                self.send_command_unix('no-op')
            except BTFailure:
                pass
            else:
                raise BTFailure(_("Could not create control socket: already in use"))

            try:
                os.unlink(filename)
            except OSError, e:
                raise BTFailure(_("Could not remove old control socket filename:")
                                + str(e))
        try:
            controlsocket = RawServer.create_unixserversocket(filename)
        except socket.error, e:
            raise BTFailure(_("Could not create control socket: ")+str(e))

        self.controlsocket = controlsocket

##    def send_command_unix(self, rawserver, action, data = ''):
##        s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
##        filename = self.socket_filename
##        try:
##            s.connect(filename)
##        except socket.error, e:
##            raise BTFailure(_("Could not send command: ") + str(e))
##        r = MessageReceiver(lambda action, data: None)
##        conn = rawserver.wrap_socket(s, r, ip = s.getpeername())
##        conn.write(tobinary(len(action)))
コード例 #18
0
ファイル: track.py プロジェクト: galaxy001/libtorrent
                    del self.downloads[key]
                    del self.seedcount[key]
        self.rawserver.add_task(self.expire_downloaders, self.timeout_downloaders_interval)

def track(args):
    if len(args) == 0:
        print formatDefinitions(defaults, 80)
        return
    try:
        config, files = parseargs(args, defaults, 0, 0)
    except ValueError, e:
        print _("error: ") + str(e)
        print _("run with no arguments for parameter explanations")
        return
    file(config['pid'], 'w').write(str(os.getpid()))
    r = RawServer(Event(), config)
    t = Tracker(config, r)
    s = r.create_serversocket(config['port'], config['bind'], True)
    r.start_listening(s, HTTPHandler(t.get, config['min_time_between_log_flushes']))
    r.listen_forever()
    t.save_dfile()
    print _("# Shutting down: ") + isotime()

def size_format(s):
    if (s < 1024):
        r = str(s) + 'B'
    elif (s < 1048576):
        r = str(int(s/1024)) + 'KiB'
    elif (s < 1073741824):
        r = str(int(s/1048576)) + 'MiB'
    elif (s < 1099511627776):