version 0.1.5, install as user to readme, more debug on filerequests, if upnpc fail try without -e, announce interval from 10 to 20 min, detect computer wakeup and acts as startup, delete sites files websocket command support, pause stop all current downloads, wrapper confirmation dialog support
This commit is contained in:
parent
3bec738595
commit
a977feec33
13 changed files with 197 additions and 16 deletions
|
@ -2,6 +2,7 @@ import os, msgpack, shutil
|
|||
from Site import SiteManager
|
||||
from cStringIO import StringIO
|
||||
from Debug import Debug
|
||||
from Config import config
|
||||
|
||||
FILE_BUFF = 1024*512
|
||||
|
||||
|
@ -80,13 +81,17 @@ class FileRequest:
|
|||
self.send({"error": "Unknown site"})
|
||||
return False
|
||||
try:
|
||||
file = open(site.getPath(params["inner_path"]), "rb")
|
||||
file_path = site.getPath(params["inner_path"])
|
||||
if config.debug_socket: self.log.debug("Opening file: %s" % file_path)
|
||||
file = open(file_path, "rb")
|
||||
file.seek(params["location"])
|
||||
back = {}
|
||||
back["body"] = file.read(FILE_BUFF)
|
||||
back["location"] = file.tell()
|
||||
back["size"] = os.fstat(file.fileno()).st_size
|
||||
if config.debug_socket: self.log.debug("Sending file %s from position %s to %s" % (file_path, params["location"], back["location"]))
|
||||
self.send(back)
|
||||
if config.debug_socket: self.log.debug("File %s sent" % file_path)
|
||||
except Exception, err:
|
||||
self.send({"error": "File read error: %s" % Debug.formatException(err)})
|
||||
return False
|
||||
|
|
|
@ -49,10 +49,14 @@ class FileServer:
|
|||
self.log.info("Try to open port using upnpc...")
|
||||
try:
|
||||
exit = os.system("%s -e ZeroNet -r %s tcp" % (config.upnpc, self.port))
|
||||
if exit == 0:
|
||||
if exit == 0: # Success
|
||||
upnpc_success = True
|
||||
else:
|
||||
upnpc_success = False
|
||||
else: # Failed
|
||||
exit = os.system("%s -r %s tcp" % (config.upnpc, self.port)) # Try without -e option
|
||||
if exit == 0:
|
||||
upnpc_success = True
|
||||
else:
|
||||
upnpc_success = False
|
||||
except Exception, err:
|
||||
self.log.error("Upnpc run error: %s" % Debug.formatException(err))
|
||||
upnpc_success = False
|
||||
|
@ -122,13 +126,25 @@ class FileServer:
|
|||
# Announce sites every 10 min
|
||||
def announceSites(self):
|
||||
while 1:
|
||||
time.sleep(10*60) # Announce sites every 10 min
|
||||
time.sleep(20*60) # Announce sites every 20 min
|
||||
for address, site in self.sites.items():
|
||||
if site.settings["serving"]:
|
||||
site.announce() # Announce site to tracker
|
||||
time.sleep(2) # Prevent too quick request
|
||||
|
||||
|
||||
# Detects if computer back from wakeup
|
||||
def wakeupWatcher(self):
|
||||
last_time = time.time()
|
||||
while 1:
|
||||
time.sleep(30)
|
||||
if time.time()-last_time > 60: # If taken more than 60 second then the computer was in sleep mode
|
||||
self.log.info("Wakeup detected: time wrap from %s to %s (%s sleep seconds), acting like startup..." % (last_time, time.time(), time.time()-last_time))
|
||||
self.port_opened = None # Check if we still has the open port on router
|
||||
self.checkSites()
|
||||
last_time = time.time()
|
||||
|
||||
|
||||
# Bind and start serving sites
|
||||
def start(self, check_sites = True):
|
||||
self.log = logging.getLogger(__name__)
|
||||
|
@ -152,6 +168,7 @@ class FileServer:
|
|||
gevent.spawn(self.checkSites)
|
||||
|
||||
gevent.spawn(self.announceSites)
|
||||
gevent.spawn(self.wakeupWatcher)
|
||||
|
||||
while True:
|
||||
try:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue