Skip to content

Commit

Permalink
count peer connection error, remove offline peers, content sign modif…
Browse files Browse the repository at this point in the history
…ication time fix, tracker order fix, reset peer hash_failed on download start, avoid util package name conflict
  • Loading branch information
shortcutme committed Jan 13, 2015
1 parent 6424c82 commit 3f974e0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
37 changes: 29 additions & 8 deletions src/Peer/Peer.py
Expand Up @@ -7,13 +7,15 @@

# 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
Expand All @@ -28,11 +30,6 @@ def connect(self):
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()
Expand All @@ -45,9 +42,12 @@ def sendCmd(self, cmd, params = {}):
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
Expand All @@ -65,7 +65,7 @@ def getFile(self, site, inner_path):
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"])
Expand All @@ -82,3 +82,24 @@ def getFile(self, site, inner_path):
# 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
5 changes: 2 additions & 3 deletions src/Site/Site.py
Expand Up @@ -211,7 +211,7 @@ def addPeer(self, ip, port, return_peer = False):
else:
return False
else: # New peer
peer = Peer(ip, port)
peer = Peer(ip, port, self)
self.peers[key] = peer
return peer

Expand Down Expand Up @@ -403,12 +403,11 @@ def signContent(self, privatekey=None):

# Generate new content.json
self.log.info("Adding timestamp and sha1sums to new content.json...")
import datetime, time

content = self.content.copy() # Create a copy of current content.json
content["address"] = self.address # Add files sha1 hash
content["files"] = hashed_files # Add files sha1 hash
content["modified"] = time.mktime(datetime.datetime.utcnow().utctimetuple()) # Add timestamp
content["modified"] = time.time() # Add timestamp
del(content["sign"]) # Delete old site

# Signing content
Expand Down
4 changes: 2 additions & 2 deletions src/Site/SiteManager.py
Expand Up @@ -2,9 +2,9 @@
import gevent

TRACKERS = [
("udp", "sugoi.pomf.se", 2710), # Retry 3 times
("udp", "sugoi.pomf.se", 2710),
("udp", "sugoi.pomf.se", 2710),
("udp", "open.demonii.com", 1337), # Retry 3 times
("udp", "open.demonii.com", 1337),
("udp", "open.demonii.com", 1337),
("udp", "bigfoot1942.sektori.org", 6969),
("udp", "tracker.coppersurfer.tk", 80),
Expand Down
3 changes: 2 additions & 1 deletion src/Worker/Worker.py
Expand Up @@ -13,6 +13,7 @@ def __init__(self, manager, peer):

# Downloader thread
def downloader(self):
self.peer.hash_failed = 0 # Reset hash error counter
while self.running:
# Try to pickup free file download task
task = self.manager.getTask(self.peer)
Expand Down Expand Up @@ -53,8 +54,8 @@ def downloader(self):
task["workers_num"] -= 1
self.manager.log.error("%s: Hash failed: %s" % (self.key, task["inner_path"]))
time.sleep(1)
self.peer.onWorkerDone()
self.running = False
self.peer.disconnect()
self.manager.removeWorker(self)


Expand Down
2 changes: 1 addition & 1 deletion src/main.py
@@ -1,5 +1,5 @@
import os, sys
sys.path.append(os.path.dirname(__file__)) # Imports relative to main.py
sys.path.insert(0, os.path.dirname(__file__)) # Imports relative to main.py

# Create necessary files and dirs
if not os.path.isdir("log"): os.mkdir("log")
Expand Down

0 comments on commit 3f974e0

Please sign in to comment.