принудительное завершение всего

master
Бородин Роман 2019-05-05 09:56:12 +03:00
parent 0e81ccf6bc
commit 228714810a
1 changed files with 7 additions and 53 deletions

View File

@ -1,4 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import chardet import chardet
@ -303,7 +302,6 @@ class TorrentFS(object):
return self.files[name] return self.files[name]
file_ = self.__file_at_(index) file_ = self.__file_at_(index)
self.files[file_.name] = file_ self.files[file_.name] = file_
#self.handle.set_piece_deadline(self.files[startIndex].startPiece, 50)
self.setPriority(index, 1) self.setPriority(index, 1)
return file_ return file_
@ -435,7 +433,7 @@ def HttpHandlerFactory():
buf = create_string_buffer(chunk) buf = create_string_buffer(chunk)
else: else:
buf = bytearray(chunk) buf = bytearray(chunk)
while chunk > 0: while chunk > 0 and not self.server.root_obj.forceShutdown:
if start_range + chunk > end_range: if start_range + chunk > end_range:
chunk = end_range - start_range chunk = end_range - start_range
if os.getenv('ANDROID_ROOT'): if os.getenv('ANDROID_ROOT'):
@ -444,7 +442,7 @@ def HttpHandlerFactory():
buf = bytearray(chunk) buf = bytearray(chunk)
try: try:
if f.Read(buf) < 1: break if f.Read(buf) < 1: break
while self.server.root_obj.pause: while self.server.root_obj.pause and not self.server.root_obj.forceShutdown:
time.sleep(0.1) time.sleep(0.1)
continue continue
if os.getenv('ANDROID_ROOT'): if os.getenv('ANDROID_ROOT'):
@ -523,7 +521,6 @@ class Pyrrent2http(object):
self.config.bindAddress = bindAddress self.config.bindAddress = bindAddress
self.config.downloadPath = downloadPath self.config.downloadPath = downloadPath
self.config.idleTimeout = idleTimeout self.config.idleTimeout = idleTimeout
#self.config.fileIndex = fileIndex
self.config.keepComplete = keepComplete self.config.keepComplete = keepComplete
self.config.keepIncomplete = keepIncomplete self.config.keepIncomplete = keepIncomplete
self.config.keepFiles = keepFiles self.config.keepFiles = keepFiles
@ -624,30 +621,21 @@ class Pyrrent2http(object):
self.TorrentFS = TorrentFS(self, self.torrentHandle) self.TorrentFS = TorrentFS(self, self.torrentHandle)
except Exception as e: except Exception as e:
logging.error(e.args) logging.error(e.args)
#_ = self.TorrentFS.file(self.config.fileIndex)
name = self.TorrentFS.info.name() name = self.TorrentFS.info.name()
self.torrent_name = name.decode(chardet.detect(name)['encoding']) self.torrent_name = name.decode(chardet.detect(name)['encoding'])
def startHTTP(self): def startHTTP(self):
#def http_server_loop(listener, alive):
# while alive.is_set():
# print('+++handle request+++')
# listener.handle_request()
# listener.server_close()
#self.main_alive = threading.Event()
#self.main_alive.set()
logging.info('Starting HTTP Server...') logging.info('Starting HTTP Server...')
handler = HttpHandlerFactory() handler = HttpHandlerFactory()
handler.protocol_version = 'HTTP/1.1'
logging.info('Listening HTTP on %s...\n' % (self.config.bindAddress,)) logging.info('Listening HTTP on %s...\n' % (self.config.bindAddress,))
host, strport = self.config.bindAddress.split(':') host, strport = self.config.bindAddress.split(':')
if len(strport) > 0: if len(strport) > 0:
srv_port = int(strport) srv_port = int(strport)
self.httpListener = ThreadingHTTPServer((host, srv_port), handler) self.httpListener = ThreadingHTTPServer((host, srv_port), handler)
self.httpListener.root_obj = self self.httpListener.root_obj = self
#self.httpListener.timeout = 0.5 self.listener_thread = threading.Thread(target = self.httpListener.serve_forever)
#thread = threading.Thread(target = http_server_loop, args = (self.httpListener, self.main_alive)) self.listener_thread.start()
thread = threading.Thread(target = self.httpListener.serve_forever)
thread.start()
def startServices(self): def startServices(self):
if self.config.enableDHT: if self.config.enableDHT:
@ -757,8 +745,6 @@ class Pyrrent2http(object):
def Status(self): def Status(self):
info = self.TorrentFS.info info = self.TorrentFS.info
#tstatus = self.torrentHandle.status()
#tstatus = self.TorrentFS.handle.status()
tstatus = self.torrentHandle.status() tstatus = self.torrentHandle.status()
status = { status = {
@ -814,35 +800,6 @@ class Pyrrent2http(object):
} }
peers['peers'].append(pi) peers['peers'].append(pi)
return peers return peers
'''def stats(self):
status = self.torrentHandle.status()
dhtStatusStr = ''
if not status.has_metadata:
return
if self.config.showAllStats or self.config.showOverallProgress:
sessionStatus = self.session.status()
if self.session.is_dht_running():
dhtStatusStr = ', DHT nodes: %d' % (sessionStatus.dht_nodes,)
errorStr = ''
if len(status.error) > 0:
errorStr = ' (%s)' % (status.error,)
logging.info('%s, overall progress: %.2f%%, dl/ul: %.3f/%.3f kbps, peers/seeds: %d/%d' % (
str(status.state),
status.progress * 100,
float(status.download_rate)/1024,
float(status.upload_rate)/1024,
status.num_peers,
status.num_seeds
) + dhtStatusStr + errorStr
)
if self.config.showFilesProgress or self.config.showAllStats:
str_ = 'Files: '
for i, f in enumerate(self.TorrentFS.files):
str_ += '[%d] %.2f%% ' % (i, f.Progress()*100)
logging.info(str_)
if (self.config.showPiecesProgress or self.config.showAllStats) and self.TorrentFS.lastOpenedFile != None:
self.TorrentFS.lastOpenedFile.ShowPieces()
'''
def consumeAlerts(self): def consumeAlerts(self):
alerts = self.session.pop_alerts() alerts = self.session.pop_alerts()
for alert in alerts: for alert in alerts:
@ -860,7 +817,6 @@ class Pyrrent2http(object):
if type(alert) == alert_type: if type(alert) == alert_type:
return alert return alert
def loop(self): def loop(self):
#self.statsTicker = Ticker(30)
self.saveResumeDataTicker = Ticker(5) self.saveResumeDataTicker = Ticker(5)
time_start = time.time() time_start = time.time()
while True: while True:
@ -875,8 +831,6 @@ class Pyrrent2http(object):
if os.getppid() == 1: if os.getppid() == 1:
self.forceShutdown = True self.forceShutdown = True
time_start = time.time() time_start = time.time()
#if self.statsTicker.true:
# self.stats()
if self.saveResumeDataTicker.true: if self.saveResumeDataTicker.true:
self.saveResumeData(True) self.saveResumeData(True)
time.sleep(0.3) time.sleep(0.3)
@ -956,9 +910,9 @@ class Pyrrent2http(object):
def shutdown(self): def shutdown(self):
logging.info('Stopping pyrrent2http...') logging.info('Stopping pyrrent2http...')
self.forceShutdown = True self.forceShutdown = True
#self.statsTicker.stop()
self.saveResumeDataTicker.stop() self.saveResumeDataTicker.stop()
self.httpListener.shutdown() self.httpListener.shutdown()
self.httpListener.socket.close()
self.TorrentFS.Shutdown() self.TorrentFS.Shutdown()
if self.session != None: if self.session != None:
self.session.pause() self.session.pause()
@ -968,5 +922,5 @@ class Pyrrent2http(object):
self.saveSessionState() self.saveSessionState()
self.removeTorrent() self.removeTorrent()
logging.info('Aborting the session') logging.info('Aborting the session')
del self.session self.session = None
logging.info('Bye bye') logging.info('Bye bye')