Esempio n. 1
0
    def _rerequest_single(self, t, s, l, callback):
        try:
            closer = [None]

            def timedout(self=self, l=l, closer=closer):
                if self.lock.trip(l):
                    self.errorcodes[
                        'troublecode'] = 'Problem connecting to tracker - timeout exceeded'
                    self.lock.unwait(l)
                try:
                    closer[0]()
                except:
                    pass

            self.externalsched(timedout, self.timeout)

            err = None
            try:
                url, q = t.split('?', 1)
                q += '&' + s
            except:
                url = t
                q = s
            try:
                h = urlopen(url + '?' + q)
                closer[0] = h.close
                data = h.read()
            except (IOError, error), e:
                err = 'Problem connecting to tracker - ' + str(e)
            except:
                err = 'Problem connecting to tracker'
            try:
                h.close()
            except:
                pass
            if err:
                if self.lock.trip(l):
                    self.errorcodes['troublecode'] = err
                    self.lock.unwait(l)
                return

            if data == '':
                if self.lock.trip(l):
                    self.errorcodes['troublecode'] = 'no data from tracker'
                    self.lock.unwait(l)
                return

            try:
                r = bdecode(data, sloppy=1)
                check_peers(r)
            except ValueError, e:
                if self.lock.trip(l):
                    self.errorcodes[
                        'bad_data'] = 'bad data from tracker - ' + str(e)
                    self.lock.unwait(l)
                return
Esempio n. 2
0
    def _rerequest_single(self, t, s, l, callback):
        try:
            closer = [None]

            def timedout(self=self, l=l, closer=closer):
                if self.lock.trip(l):
                    self.errorcodes["troublecode"] = "Problem connecting to tracker - timeout exceeded"
                    self.lock.unwait(l)
                try:
                    closer[0]()
                except:
                    pass

            self.externalsched(timedout, self.timeout)

            err = None
            try:
                url, q = t.split("?", 1)
                q += "&" + s
            except:
                url = t
                q = s
            try:
                h = urlopen(url + "?" + q)
                closer[0] = h.close
                data = h.read()
            except (IOError, error), e:
                err = "Problem connecting to tracker - " + str(e)
            except:
                err = "Problem connecting to tracker"
            try:
                h.close()
            except:
                pass
            if err:
                if self.lock.trip(l):
                    self.errorcodes["troublecode"] = err
                    self.lock.unwait(l)
                return

            if data == "":
                if self.lock.trip(l):
                    self.errorcodes["troublecode"] = "no data from tracker"
                    self.lock.unwait(l)
                return

            try:
                r = bdecode(data, sloppy=1)
                check_peers(r)
            except ValueError, e:
                if self.lock.trip(l):
                    self.errorcodes["bad_data"] = "bad data from tracker - " + str(e)
                    self.lock.unwait(l)
                return
Esempio n. 3
0
    def _rerequest_single(self, t, s, l, callback):
        try:        
            closer = [None]
            def timedout(self = self, l = l, closer = closer):
                if self.lock.trip(l):
                    self.errorcodes['troublecode'] = 'Problem connecting to tracker - timeout exceeded'
                    self.lock.unwait(l)
                try:
                    closer[0]()
                except:
                    pass
                    
            self.externalsched(timedout, self.timeout)

            err = None
            try:
                h = urlopen(t+s)
                closer[0] = h.close
                data = h.read()
            except (IOError, error), e:
                err = 'Problem connecting to tracker - ' + str(e)
            except:
                err = 'Problem connecting to tracker'
            try:
                h.close()
            except:
                pass
            if err:        
                if self.lock.trip(l):
                    self.errorcodes['troublecode'] = err
                    self.lock.unwait(l)
                return

            if data == '':
                if self.lock.trip(l):
                    self.errorcodes['troublecode'] = 'no data from tracker'
                    self.lock.unwait(l)
                return
            
            try:
                r = bdecode(data, sloppy=1)
                check_peers(r)
            except ValueError, e:
                if self.lock.trip(l):
                    self.errorcodes['bad_data'] = 'bad data from tracker - ' + str(e)
                    self.lock.unwait(l)
                return
Esempio n. 4
0
    def postrequest(self, data):
        try:
            r = bdecode(data)
            check_peers(r)
            if r.has_key('failure reason'):
                self.errorfunc('rejected by tracker - ' + r['failure reason'])
            else:
                if r.has_key('warning message'):
                    self.errorfunc('warning from tracker - ' +
                                   r['warning message'])
                self.announce_interval = r.get('interval',
                                               self.announce_interval)
                self.interval = r.get('min interval', self.interval)
                self.trackerid = r.get('tracker id', self.trackerid)
                self.last = r.get('last')
                p = r['peers']
                peers = []
                if type(p) == type(''):
                    # Deserialize the compact binary form.
                    for x in xrange(0, len(p), 6):
                        ip = '.'.join([str(ord(i)) for i in p[x:x + 4]])
                        port = (ord(p[x + 4]) << 8) | ord(p[x + 5])
                        peers.append((ip, port, None))
                else:
                    for x in p:
                        peers.append((x['ip'], x['port'], x.get('peer id')))

                ps = len(peers) + self.howmany()
                if ps < self.maxpeers:
                    if self.doneflag.isSet():
                        if r.get('num peers', 1000) - r.get('done peers',
                                                            0) > ps * 1.2:
                            self.last = None
                    else:
                        if r.get('num peers', 1000) > ps * 1.2:
                            self.last = None
                for x in peers:
                    self.connect((x[0], x[1]), x[2])
        except ValueError, e:
            if data != '':
                self.errorfunc('bad data from tracker - ' + str(e))
Esempio n. 5
0
 def postrequest(self, data):
     try:
         r = bdecode(data)
         check_peers(r)
         if r.has_key('failure reason'):
             if self.upratefunc() < 100 and self.downratefunc() < 100:
                 self.errorfunc('rejected by tracker - ' + r['failure reason'], 'tracker_refuse')
             else:
                 self.errorfunc('rejected by tracker - ' + r['failure reason'], 'tracker_bcool')
         else:
             self.announce_interval = r.get('interval', self.announce_interval)
             self.interval = r.get('min interval', self.interval)
             self.trackerid = r.get('tracker id', self.trackerid)
             self.last = r.get('last')
             p = r['peers']
             peers = []
             if type(p) == type(''):
                 for x in xrange(0, len(p), 6):
                     ip = '.'.join([str(ord(i)) for i in p[x:x+4]])
                     port = (ord(p[x+4]) << 8) | ord(p[x+5])
                     peers.append((ip, port, None))
             else:
                 for x in p:
                     peers.append((x['ip'], x['port'], x.get('peer id')))
             ps = len(peers) + self.howmany()
             if ps < self.maxpeers:
                 if self.doneflag.isSet():
                     if r.get('num peers', 1000) - r.get('done peers', 0) > ps * 1.2:
                         self.last = None
                 else:
                     if r.get('num peers', 1000) > ps * 1.2:
                         self.last = None
             for x in peers:
                 self.connect((x[0], x[1]), x[2])
     except ValueError, e:
         if data != '':
             if self.upratefunc() < 100 and self.downratefunc() < 100:
                 self.errorfunc('bad data from tracker - ' + str(e), 'tracker_refuse')
             else:
                 self.errorfunc('bad data from tracker - ' + str(e), 'tracker_bcool')
Esempio n. 6
0
 def postrequest(self, data):
     try:
         r = bdecode(data)
         check_peers(r)
         if r.has_key("failure reason"):
             self.errorfunc("rejected by tracker - " + r["failure reason"])
         else:
             if r.has_key("warning message"):
                 self.errorfunc("warning from tracker - " + r["warning message"])
             self.announce_interval = r.get("interval", self.announce_interval)
             self.interval = r.get("min interval", self.interval)
             self.trackerid = r.get("tracker id", self.trackerid)
             self.last = r.get("last")
             p = r["peers"]
             peers = []
             if type(p) == type(""):
                 for x in xrange(0, len(p), 6):
                     ip = ".".join([str(ord(i)) for i in p[x : x + 4]])
                     port = (ord(p[x + 4]) << 8) | ord(p[x + 5])
                     peers.append((ip, port, None))
             else:
                 for x in p:
                     peers.append((x["ip"], x["port"], x.get("peer id")))
             ps = len(peers) + self.howmany()
             if ps < self.maxpeers:
                 if self.doneflag.isSet():
                     if r.get("num peers", 1000) - r.get("done peers", 0) > ps * 1.2:
                         self.last = None
                 else:
                     if r.get("num peers", 1000) > ps * 1.2:
                         self.last = None
             for x in peers:
                 self.connect((x[0], x[1]), x[2])
     except ValueError, e:
         if data != "":
             self.errorfunc("bad data from tracker - " + str(e))
Esempio n. 7
0
    def _rerequest_single(self, t, s, l):
        try:
            closer = [None]

            def timedout(self=self, l=l, closer=closer):
                if self.lock.trip(l):
                    if DEBUG_LOCK:
                        log(
                            self.log_prefix +
                            '_rerequest_single:timedout: unwait: l', l, 't', t,
                            'thread',
                            currentThread().name)
                    self.errorcodes[
                        'troublecode'] = 'Problem connecting to tracker - timeout exceeded'
                    self.lock.unwait(l)
                elif DEBUG_LOCK:
                    log(
                        self.log_prefix +
                        '_rerequest_single:timedout: no trip: l', l, 't', t,
                        'thread',
                        currentThread().name)
                try:
                    closer[0]()
                except:
                    pass

            self.externalsched(timedout, self.timeout)
            err = None
            try:
                if DEBUG or DEBUG_ANNOUNCE:
                    log(self.log_prefix + '_rerequest_single: request tracker',
                        merge_announce(t, s), 'thread',
                        currentThread().name)
                h = urlopen(merge_announce(t, s), silent=True)
                closer[0] = h.close
                data = h.read()
            except (IOError, socket.error) as e:
                err = 'Problem connecting to tracker - ' + str(e)
                if DEBUG:
                    log(self.log_prefix +
                        '_rerequest_single: failed to connect to tracker')
            except:
                err = 'Problem connecting to tracker'
                if DEBUG:
                    log(self.log_prefix +
                        '_rerequest_single: failed to connect to tracker')

            try:
                h.close()
            except:
                pass

            if err:
                if self.lock.trip(l):
                    if DEBUG_LOCK:
                        log(
                            self.log_prefix +
                            '_rerequest_single: got error, unwait: l', l, 't',
                            t, 'thread',
                            currentThread().name, 'err', err)
                    self.errorcodes['troublecode'] = err
                    self.lock.unwait(l)
                elif DEBUG_LOCK:
                    log(
                        self.log_prefix +
                        '_rerequest_single: got error, no trip: l', l, 't', t,
                        'thread',
                        currentThread().name, 'err', err)
                return
            if not data:
                if self.lock.trip(l):
                    if DEBUG_LOCK:
                        log(
                            self.log_prefix +
                            '_rerequest_single: no date, unwait: l', l, 't', t,
                            'thread',
                            currentThread().name)
                    self.errorcodes['troublecode'] = 'no data from tracker'
                    self.lock.unwait(l)
                elif DEBUG_LOCK:
                    log(
                        self.log_prefix +
                        '_rerequest_single: no data, no trip: l', l, 't', t,
                        'thread',
                        currentThread().name)
                return
            try:
                r = bdecode(data, sloppy=1)
                if DEBUG or DEBUG_ANNOUNCE:
                    log(
                        self.log_prefix +
                        '_rerequest_single: respose from tracker: t', t, 'r',
                        r, 'thread',
                        currentThread().name)
                check_peers(r)
            except ValueError as e:
                if DEBUG:
                    log_exc()
                if self.lock.trip(l):
                    if DEBUG_LOCK:
                        log(
                            self.log_prefix +
                            '_rerequest_single: exception while decoding data, unwait: l',
                            l, 't', t, 'thread',
                            currentThread().name)
                    self.errorcodes[
                        'bad_data'] = 'bad data from tracker - ' + str(e)
                    self.lock.unwait(l)
                elif DEBUG_LOCK:
                    log(
                        self.log_prefix +
                        '_rerequest_single: exception while decoding data, no trip: l',
                        l, 't', t, 'thread',
                        currentThread().name)
                return

            if r.has_key('failure reason'):
                if self.lock.trip(l):
                    if DEBUG_LOCK:
                        log(
                            self.log_prefix +
                            '_rerequest_single: got failure reason, unwait: l',
                            l, 't', t, 'thread',
                            currentThread().name)
                    self.errorcodes['rejected'] = self.rejectedmessage + r[
                        'failure reason']
                    self.lock.unwait(l)
                elif DEBUG_LOCK:
                    log(
                        self.log_prefix +
                        '_rerequest_single: got failure reason, no trip: l', l,
                        't', t, 'thread',
                        currentThread().name)
                return
            if self.lock.trip(l, True):
                if DEBUG_LOCK:
                    log(
                        self.log_prefix +
                        '_rerequest_single: trip success, unwait: l', l, 't',
                        t, 'thread',
                        currentThread().name)
                self.lock.unwait(l)
            elif DEBUG_LOCK:
                log(
                    self.log_prefix +
                    '_rerequest_single: trip success, no trip: l', l, 't', t,
                    'thread',
                    currentThread().name)

            def add(self=self, r=r):
                self.postrequest(r, 'tracker=' + t, self.notifiers)

            self.externalsched(add)
        except:
            print_exc()
            if self.lock.trip(l):
                if DEBUG_LOCK:
                    log(
                        self.log_prefix +
                        '_rerequest_single: got exception, unwait: l', l, 't',
                        t, 'thread',
                        currentThread().name)
                self.lock.unwait(l)
Esempio n. 8
0
    def _rerequest_single(self, t, s, l, callback):
        try:        
            closer = [None]
            def timedout(self = self, l = l, closer = closer):
                if self.lock.trip(l):
                    self.errorcodes['troublecode'] = 'Problem connecting to tracker - timeout exceeded'
                    self.lock.unwait(l)
                try:
                    closer[0]()
                except:
                    pass
                    
            self.externalsched(timedout, self.timeout)

            err = None
            try:
                if DEBUG:
                    print >>sys.stderr,time.asctime(),'-', "Rerequest tracker:"
                    print >>sys.stderr,time.asctime(),'-', t+s
                h = urlopen(t+s)
                closer[0] = h.close
                data = h.read()
            except (IOError, error), e:
                err = 'Problem connecting to tracker - ' + str(e)
                if DEBUG:
                    print_exc()
            except:
                err = 'Problem connecting to tracker'
                if DEBUG:
                    print_exc()
                    
                    
            #if DEBUG:
            #    print >>sys.stderr,time.asctime(),'-', "rerequest: Got data",data
                    
            try:
                h.close()
            except:
                pass
            if err:        
                if self.lock.trip(l):
                    self.errorcodes['troublecode'] = err
                    self.lock.unwait(l)
                return

            if not data:
                if self.lock.trip(l):
                    self.errorcodes['troublecode'] = 'no data from tracker'
                    self.lock.unwait(l)
                return
            
            try:
                r = bdecode(data, sloppy=1)
                if DEBUG:
                    print >>sys.stderr,time.asctime(),'-', "Rerequester: Tracker returns:", r
                check_peers(r)
                
                #print >>sys.stderr,time.asctime(),'-', "Rerequester: Tracker returns, post check done"
                
            except ValueError, e:
                if DEBUG:
                    print_exc()
                if self.lock.trip(l):
                    self.errorcodes['bad_data'] = 'bad data from tracker - ' + str(e)
                    self.lock.unwait(l)
                return
Esempio n. 9
0
    def _rerequest_single(self, t, s, l):
        try:
            closer = [None]

            def timedout(self = self, l = l, closer = closer):
                if self.lock.trip(l):
                    if DEBUG_LOCK:
                        log(self.log_prefix + '_rerequest_single:timedout: unwait: l', l, 't', t, 'thread', currentThread().name)
                    self.errorcodes['troublecode'] = 'Problem connecting to tracker - timeout exceeded'
                    self.lock.unwait(l)
                elif DEBUG_LOCK:
                    log(self.log_prefix + '_rerequest_single:timedout: no trip: l', l, 't', t, 'thread', currentThread().name)
                try:
                    closer[0]()
                except:
                    pass

            self.externalsched(timedout, self.timeout)
            err = None
            try:
                if DEBUG or DEBUG_ANNOUNCE:
                    log(self.log_prefix + '_rerequest_single: request tracker', merge_announce(t, s), 'thread', currentThread().name)
                h = urlopen(merge_announce(t, s), silent=True)
                closer[0] = h.close
                data = h.read()
            except (IOError, socket.error) as e:
                err = 'Problem connecting to tracker - ' + str(e)
                if DEBUG:
                    log(self.log_prefix + '_rerequest_single: failed to connect to tracker')
            except:
                err = 'Problem connecting to tracker'
                if DEBUG:
                    log(self.log_prefix + '_rerequest_single: failed to connect to tracker')

            try:
                h.close()
            except:
                pass

            if err:
                if self.lock.trip(l):
                    if DEBUG_LOCK:
                        log(self.log_prefix + '_rerequest_single: got error, unwait: l', l, 't', t, 'thread', currentThread().name, 'err', err)
                    self.errorcodes['troublecode'] = err
                    self.lock.unwait(l)
                elif DEBUG_LOCK:
                    log(self.log_prefix + '_rerequest_single: got error, no trip: l', l, 't', t, 'thread', currentThread().name, 'err', err)
                return
            if not data:
                if self.lock.trip(l):
                    if DEBUG_LOCK:
                        log(self.log_prefix + '_rerequest_single: no date, unwait: l', l, 't', t, 'thread', currentThread().name)
                    self.errorcodes['troublecode'] = 'no data from tracker'
                    self.lock.unwait(l)
                elif DEBUG_LOCK:
                    log(self.log_prefix + '_rerequest_single: no data, no trip: l', l, 't', t, 'thread', currentThread().name)
                return
            try:
                r = bdecode(data, sloppy=1)
                if DEBUG or DEBUG_ANNOUNCE:
                    log(self.log_prefix + '_rerequest_single: respose from tracker: t', t, 'r', r, 'thread', currentThread().name)
                check_peers(r)
            except ValueError as e:
                if DEBUG:
                    log_exc()
                if self.lock.trip(l):
                    if DEBUG_LOCK:
                        log(self.log_prefix + '_rerequest_single: exception while decoding data, unwait: l', l, 't', t, 'thread', currentThread().name)
                    self.errorcodes['bad_data'] = 'bad data from tracker - ' + str(e)
                    self.lock.unwait(l)
                elif DEBUG_LOCK:
                    log(self.log_prefix + '_rerequest_single: exception while decoding data, no trip: l', l, 't', t, 'thread', currentThread().name)
                return

            if r.has_key('failure reason'):
                if self.lock.trip(l):
                    if DEBUG_LOCK:
                        log(self.log_prefix + '_rerequest_single: got failure reason, unwait: l', l, 't', t, 'thread', currentThread().name)
                    self.errorcodes['rejected'] = self.rejectedmessage + r['failure reason']
                    self.lock.unwait(l)
                elif DEBUG_LOCK:
                    log(self.log_prefix + '_rerequest_single: got failure reason, no trip: l', l, 't', t, 'thread', currentThread().name)
                return
            if self.lock.trip(l, True):
                if DEBUG_LOCK:
                    log(self.log_prefix + '_rerequest_single: trip success, unwait: l', l, 't', t, 'thread', currentThread().name)
                self.lock.unwait(l)
            elif DEBUG_LOCK:
                log(self.log_prefix + '_rerequest_single: trip success, no trip: l', l, 't', t, 'thread', currentThread().name)

            def add(self = self, r = r):
                self.postrequest(r, 'tracker=' + t, self.notifiers)

            self.externalsched(add)
        except:
            print_exc()
            if self.lock.trip(l):
                if DEBUG_LOCK:
                    log(self.log_prefix + '_rerequest_single: got exception, unwait: l', l, 't', t, 'thread', currentThread().name)
                self.lock.unwait(l)
Esempio n. 10
0
    def _rerequest_single(self, t, s, l, callback):
        try:
            closer = [None]

            def timedout(self=self, l=l, closer=closer):
                if self.lock.trip(l):
                    self.errorcodes[
                        'troublecode'] = 'Problem connecting to tracker - timeout exceeded'
                    self.lock.unwait(l)
                try:
                    closer[0]()
                except:
                    pass

            self.externalsched(timedout, self.timeout)

            err = None
            try:
                if DEBUG:
                    print >> sys.stderr, "Rerequest tracker:"
                    print >> sys.stderr, t + s
                h = urlopen(t + s)
                closer[0] = h.close
                data = h.read()
            except (IOError, error), e:
                err = 'Problem connecting to tracker - ' + str(e)
                if DEBUG:
                    print_exc()
            except:
                err = 'Problem connecting to tracker'
                if DEBUG:
                    print_exc()

            #if DEBUG:
            #    print >>sys.stderr,"rerequest: Got data",data

            try:
                h.close()
            except:
                pass
            if err:
                if self.lock.trip(l):
                    self.errorcodes['troublecode'] = err
                    self.lock.unwait(l)
                return

            if not data:
                if self.lock.trip(l):
                    self.errorcodes['troublecode'] = 'no data from tracker'
                    self.lock.unwait(l)
                return

            try:
                r = bdecode(data, sloppy=1)
                if DEBUG:
                    print >> sys.stderr, "Rerequester: Tracker returns:", r
                check_peers(r)

                #print >>sys.stderr,"Rerequester: Tracker returns, post check done"

            except ValueError, e:
                if DEBUG:
                    print_exc()
                if self.lock.trip(l):
                    self.errorcodes[
                        'bad_data'] = 'bad data from tracker - ' + str(e)
                    self.lock.unwait(l)
                return