count peer connection error, remove offline peers, content sign modification time fix, tracker order fix, reset peer hash_failed on download start, avoid util package name conflict
This commit is contained in:
parent
6424c82887
commit
3f974e0bc7
5 changed files with 36 additions and 15 deletions
|
@ -7,13 +7,15 @@ context = zmq.Context()
|
|||
|
||||
# Communicate remote peers
|
||||
class Peer:
|
||||
def __init__(self, ip, port):
|
||||
def __init__(self, ip, port, site):
|
||||
self.ip = ip
|
||||
self.port = port
|
||||
self.site = site
|
||||
self.socket = None
|
||||
self.last_found = None
|
||||
self.added = time.time()
|
||||
|
||||
self.connection_error = 0
|
||||
self.hash_failed = 0
|
||||
self.download_bytes = 0
|
||||
self.download_time = 0
|
||||
|
@ -28,11 +30,6 @@ class Peer:
|
|||
self.socket.connect('tcp://%s:%s' % (self.ip, self.port))
|
||||
|
||||
|
||||
# Done working with peer
|
||||
def disconnect(self):
|
||||
pass
|
||||
|
||||
|
||||
# Found a peer on tracker
|
||||
def found(self):
|
||||
self.last_found = time.time()
|
||||
|
@ -45,9 +42,12 @@ class Peer:
|
|||
self.socket.send(msgpack.packb({"cmd": cmd, "params": params}, use_bin_type=True))
|
||||
response = msgpack.unpackb(self.socket.recv())
|
||||
if "error" in response:
|
||||
self.log.error("%s %s error: %s" % (cmd, params, response["error"]))
|
||||
self.log.debug("%s %s error: %s" % (cmd, params, response["error"]))
|
||||
else: # Successful request, reset connection error num
|
||||
self.connection_error = 0
|
||||
return response
|
||||
except Exception, err:
|
||||
self.onConnectionError()
|
||||
self.log.error("%s" % err)
|
||||
if config.debug:
|
||||
import traceback
|
||||
|
@ -65,7 +65,7 @@ class Peer:
|
|||
s = time.time()
|
||||
while 1: # Read in 512k parts
|
||||
back = self.sendCmd("getFile", {"site": site, "inner_path": inner_path, "location": location}) # Get file content from last location
|
||||
if "body" not in back: # Error
|
||||
if not back or "body" not in back: # Error
|
||||
return False
|
||||
|
||||
buff.write(back["body"])
|
||||
|
@ -82,3 +82,24 @@ class Peer:
|
|||
# Send a ping request
|
||||
def ping(self):
|
||||
return self.sendCmd("ping")
|
||||
|
||||
|
||||
# Stop and remove from site
|
||||
def remove(self):
|
||||
self.log.debug("Removing peer...Connection error: %s, Hash failed: %s" % (self.connection_error, self.hash_failed))
|
||||
del(self.site.peers[self.key])
|
||||
self.socket.close()
|
||||
|
||||
|
||||
# - EVENTS -
|
||||
|
||||
# On connection error
|
||||
def onConnectionError(self):
|
||||
self.connection_error += 1
|
||||
if self.connection_error > 5: # Dead peer
|
||||
self.remove()
|
||||
|
||||
|
||||
# Done working with peer
|
||||
def onWorkerDone(self):
|
||||
pass
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue