Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardizing the code and adding a feature to enhance performance #255

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 56 additions & 39 deletions torrent-client/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import libtorrent as lt
import sys
from fifo import Fifo
import asyncio
import json
import threading
import sys
import time

import libtorrent as lt
from fifo import Fifo

torrent_port = sys.argv[1]
read_sock_path = sys.argv[2]
write_sock_path = sys.argv[3]

session = lt.session({'listen_interfaces': '0.0.0.0:{port}'.format(port=torrent_port)})
class SessionSingleton:
_instance = None

def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance.session = lt.session({'listen_interfaces': '0.0.0.0:{port}'.format(port=torrent_port)})
return cls._instance

def get_session(self):
return self.session

read_fifo = Fifo(read_sock_path)
write_fifo = Fifo(write_sock_path)

Expand All @@ -24,40 +36,16 @@ def get_eta(status):
else:
return 1

def start_download(game_id: int, magnet: str, save_path: str):
async def get_download_updates():
global torrent_handle
global downloading_game_id

params = {'url': magnet, 'save_path': save_path}
torrent_handle = session.add_torrent(params)
downloading_game_id = game_id
torrent_handle.set_flags(lt.torrent_flags.auto_managed)
torrent_handle.resume()

def pause_download():
global downloading_game_id

if torrent_handle:
torrent_handle.pause()
torrent_handle.unset_flags(lt.torrent_flags.auto_managed)
downloading_game_id = 0

def cancel_download():
global downloading_game_id
global torrent_handle

if torrent_handle:
torrent_handle.pause()
session.remove_torrent(torrent_handle)
torrent_handle = None
downloading_game_id = 0

def get_download_updates():
while True:
if downloading_game_id == 0:
time.sleep(0.5)
await asyncio.sleep(0.5)
continue

session = SessionSingleton().get_session()
status = torrent_handle.status()
info = torrent_handle.get_torrent_info()

Expand All @@ -77,9 +65,12 @@ def get_download_updates():
if status.progress == 1:
cancel_download()

time.sleep(0.5)
await asyncio.sleep(0.5)

async def listen_to_socket():
global torrent_handle
global downloading_game_id

def listen_to_socket():
while True:
msg = read_fifo.recv(1024 * 2)
payload = json.loads(msg.decode("utf-8"))
Expand All @@ -95,9 +86,35 @@ def listen_to_socket():
if payload['action'] == "cancel":
cancel_download()

if __name__ == "__main__":
p1 = threading.Thread(target=get_download_updates)
p2 = threading.Thread(target=listen_to_socket)
async def start_download(game_id: int, magnet: str, save_path: str):
global torrent_handle
global downloading_game_id

session = SessionSingleton().get_session()
params = {'url': magnet, 'save_path': save_path}
torrent_handle = session.add_torrent(params)
downloading_game_id = game_id
torrent_handle.set_flags(lt.torrent_flags.auto_managed)
torrent_handle.resume()

async def pause_download():
global downloading_game_id

p1.start()
p2.start()
if torrent_handle:
torrent_handle.pause()
torrent_handle.unset_flags(lt.torrent_flags.auto_managed)
downloading_game_id = 0

async def cancel_download():
global downloading_game_id
global torrent_handle

if torrent_handle:
torrent_handle.pause()
session = SessionSingleton().get_session()
session.remove_torrent(torrent_handle)
torrent_handle = None
downloading_game_id = 0

if __name__ == "__main__":
asyncio.run(asyncio.gather(get_download_updates(), listen_to_socket()))
Loading