partial cleanup of Peer.py
This commit is contained in:
parent
b6cb1062ce
commit
3f14d3d200
1 changed files with 165 additions and 177 deletions
|
@ -25,23 +25,21 @@ class Peer(object):
|
|||
self.download_bytes = 0 # Bytes downloaded
|
||||
self.download_time = 0 # Time spent to download
|
||||
|
||||
|
||||
def log(self, text):
|
||||
if self.site:
|
||||
self.site.log.debug("%s:%s %s" % (self.ip, self.port, text))
|
||||
else:
|
||||
logging.debug("%s:%s %s" % (self.ip, self.port, text))
|
||||
|
||||
|
||||
# Connect to host
|
||||
def connect(self, connection = None):
|
||||
def connect(self, connection=None):
|
||||
if self.connection:
|
||||
self.log("Getting connection (Closing %s)..." % self.connection)
|
||||
self.connection.close()
|
||||
else:
|
||||
self.log("Getting connection...")
|
||||
|
||||
if connection: # Connection specificed
|
||||
if connection: # Connection specified
|
||||
self.connection = connection
|
||||
else: # Try to find from connection pool or create new connection
|
||||
self.connection = None
|
||||
|
@ -53,7 +51,6 @@ class Peer(object):
|
|||
self.log("Getting connection error: %s (connection_error: %s, hash_failed: %s)" % (Debug.formatException(err), self.connection_error, self.hash_failed))
|
||||
self.connection = None
|
||||
|
||||
|
||||
# Check if we have connection to peer
|
||||
def findConnection(self):
|
||||
if self.connection and self.connection.connected: # We have connection to peer
|
||||
|
@ -62,28 +59,23 @@ class Peer(object):
|
|||
self.connection = self.connection_server.getConnection(self.ip, self.port, create=False) # Do not create new connection if not found
|
||||
return self.connection
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return "Peer %-12s" % self.ip
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s>" % self.__str__()
|
||||
|
||||
|
||||
# Peer ip:port to packed 6byte format
|
||||
def packAddress(self):
|
||||
return socket.inet_aton(self.ip)+struct.pack("H", self.port)
|
||||
|
||||
|
||||
def unpackAddress(self, packed):
|
||||
return (socket.inet_ntoa(packed[0:4]), struct.unpack_from("H", packed, 4)[0])
|
||||
|
||||
return socket.inet_ntoa(packed[0:4]), struct.unpack_from("H", packed, 4)[0]
|
||||
|
||||
# Found a peer on tracker
|
||||
def found(self):
|
||||
self.last_found = time.time()
|
||||
|
||||
|
||||
# Send a command to peer
|
||||
def request(self, cmd, params = {}):
|
||||
if not self.connection or self.connection.closed:
|
||||
|
@ -99,7 +91,8 @@ class Peer(object):
|
|||
#if config.debug_socket: self.log.debug("sendCmd: %s %s" % (cmd, params.get("inner_path")))
|
||||
try:
|
||||
response = self.connection.request(cmd, params)
|
||||
if not response: raise Exception("Send error")
|
||||
if not response:
|
||||
raise Exception("Send error")
|
||||
#if config.debug_socket: self.log.debug("Got response to: %s" % cmd)
|
||||
if "error" in response:
|
||||
self.log("%s error: %s" % (cmd, response["error"]))
|
||||
|
@ -119,7 +112,6 @@ class Peer(object):
|
|||
self.connect()
|
||||
return None # Failed after 4 retry
|
||||
|
||||
|
||||
# Get a file content from peer
|
||||
def getFile(self, site, inner_path):
|
||||
location = 0
|
||||
|
@ -141,7 +133,6 @@ class Peer(object):
|
|||
buff.seek(0)
|
||||
return buff
|
||||
|
||||
|
||||
# Send a ping request
|
||||
def ping(self):
|
||||
response_time = None
|
||||
|
@ -165,29 +156,28 @@ class Peer(object):
|
|||
self.last_ping = response_time
|
||||
return response_time
|
||||
|
||||
|
||||
# Request peer exchange from peer
|
||||
def pex(self, site=None, need_num=5):
|
||||
if not site: site = self.site # If no site definied request peers for this site
|
||||
packed_peers = [peer.packAddress() for peer in self.site.getConnectablePeers(5)] # give him/her 5 connectable peers
|
||||
if not site:
|
||||
site = self.site # If no site defined request peers for this site
|
||||
# give him/her 5 connectable peers
|
||||
packed_peers = [peer.packAddress() for peer in self.site.getConnectablePeers(5)]
|
||||
response = self.request("pex", {"site": site.address, "peers": packed_peers, "need": need_num})
|
||||
if not response or "error" in response:
|
||||
return False
|
||||
added = 0
|
||||
for peer in response.get("peers", []):
|
||||
address = self.unpackAddress(peer)
|
||||
if (site.addPeer(*address)): added += 1
|
||||
if site.addPeer(*address):
|
||||
added += 1
|
||||
if added:
|
||||
self.log("Added peers using pex: %s" % added)
|
||||
return added
|
||||
|
||||
|
||||
# List modified files since the date
|
||||
# Return: {inner_path: modification date,...}
|
||||
def listModified(self, since):
|
||||
response = self.request("listModified", {"since": since, "site": self.site.address})
|
||||
return response
|
||||
|
||||
return self.request("listModified", {"since": since, "site": self.site.address})
|
||||
|
||||
# Stop and remove from site
|
||||
def remove(self):
|
||||
|
@ -196,7 +186,6 @@ class Peer(object):
|
|||
if self.connection:
|
||||
self.connection.close()
|
||||
|
||||
|
||||
# - EVENTS -
|
||||
|
||||
# On connection error
|
||||
|
@ -205,7 +194,6 @@ class Peer(object):
|
|||
if self.connection_error >= 3: # Dead peer
|
||||
self.remove()
|
||||
|
||||
|
||||
# Done working with peer
|
||||
def onWorkerDone(self):
|
||||
pass
|
||||
|
|
Loading…
Reference in a new issue