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