33import os
44import logging
55import random
6- from hashlib import sha1
76from tempfile import mkdtemp
87from typing import Optional , Tuple
98
@@ -59,6 +58,7 @@ def largest_file_index(self):
5958 return index
6059
6160 def stop_tasks (self ):
61+ self ._handle .save_resume_data ()
6262 while self .tasks :
6363 self .tasks .pop ().cancel ()
6464
@@ -152,13 +152,13 @@ def __init__(self, loop, executor):
152152
153153 async def add_fake_torrent (self ):
154154 tmpdir = mkdtemp ()
155- info , btih = _create_fake_torrent (tmpdir )
155+ info = _create_fake_torrent (tmpdir )
156156 flags = libtorrent .add_torrent_params_flags_t .flag_seed_mode
157157 handle = self ._session .add_torrent ({
158158 'ti' : info , 'save_path' : tmpdir , 'flags' : flags
159159 })
160- self ._handles [btih ] = TorrentHandle (self ._loop , self ._executor , handle )
161- return btih
160+ self ._handles [str ( info . info_hash ()) ] = TorrentHandle (self ._loop , self ._executor , handle )
161+ return str ( info . info_hash ())
162162
163163 async def bind (self , interface : str = '0.0.0.0' , port : int = 10889 ):
164164 settings = {
@@ -172,14 +172,12 @@ async def bind(self, interface: str = '0.0.0.0', port: int = 10889):
172172 self .tasks .append (self ._loop .create_task (self .process_alerts ()))
173173
174174 def stop (self ):
175+ while self ._handles :
176+ self ._handles .popitem ()[1 ].stop_tasks ()
175177 while self .tasks :
176178 self .tasks .pop ().cancel ()
177179 self ._session .save_state ()
178180 self ._session .pause ()
179- self ._session .stop_dht ()
180- self ._session .stop_lsd ()
181- self ._session .stop_natpmp ()
182- self ._session .stop_upnp ()
183181 self ._session = None
184182
185183 def _pop_alerts (self ):
@@ -271,9 +269,7 @@ def _create_fake_torrent(tmpdir):
271269 file_storage .add_file ('tmp' , size )
272270 t = libtorrent .create_torrent (file_storage , 0 , 4 * 1024 * 1024 )
273271 libtorrent .set_piece_hashes (t , tmpdir )
274- info = libtorrent .torrent_info (t .generate ())
275- btih = sha1 (info .metadata ()).hexdigest ()
276- return info , btih
272+ return libtorrent .torrent_info (t .generate ())
277273
278274
279275async def main ():
0 commit comments