Change core.test_listen_port to use twisted instead of urllib

Add test for test_listen_port
Remove some unncessary code
This commit is contained in:
Andrew Resch 2009-07-25 00:30:45 +00:00
commit e24ba6a025
3 changed files with 38 additions and 41 deletions

View file

@ -71,16 +71,6 @@ from deluge.core.authmanager import AuthManager
from deluge.core.eventmanager import EventManager from deluge.core.eventmanager import EventManager
from deluge.core.rpcserver import export from deluge.core.rpcserver import export
STATUS_KEYS = ['active_time', 'compact', 'distributed_copies', 'download_payload_rate', 'eta',
'file_priorities', 'file_progress', 'files', 'hash', 'is_auto_managed', 'is_seed', 'max_connections',
'max_download_speed', 'max_upload_slots', 'max_upload_speed', 'message', 'move_on_completed',
'move_on_completed_path', 'name', 'next_announce', 'num_files', 'num_peers', 'num_pieces',
'num_seeds', 'paused', 'peers', 'piece_length', 'prioritize_first_last', 'private', 'progress',
'queue', 'ratio', 'remove_at_ratio', 'save_path', 'seed_rank', 'seeding_time', 'state', 'stop_at_ratio',
'stop_ratio', 'time_added', 'total_done', 'total_payload_download', 'total_payload_upload', 'total_peers',
'total_seeds', 'total_size', 'total_uploaded', 'total_wanted', 'tracker', 'tracker_host',
'tracker_status', 'trackers', 'upload_payload_rate']
class Core(component.Component): class Core(component.Component):
def __init__(self, listen_interface=None): def __init__(self, listen_interface=None):
log.debug("Core init..") log.debug("Core init..")
@ -107,13 +97,6 @@ class Core(component.Component):
self.settings.send_redundant_have = True self.settings.send_redundant_have = True
self.session.set_settings(self.settings) self.session.set_settings(self.settings)
# Create an ip filter
self.ip_filter = lt.ip_filter()
# This keeps track of the timer to set the ip filter.. We do this a few
# seconds aftering adding a rule so that 'batch' adding of rules isn't slow.
self.__set_ip_filter_timer = None
# Load metadata extension # Load metadata extension
self.session.add_extension(lt.create_metadata_plugin) self.session.add_extension(lt.create_metadata_plugin)
self.session.add_extension(lt.create_ut_metadata_plugin) self.session.add_extension(lt.create_ut_metadata_plugin)
@ -124,7 +107,7 @@ class Core(component.Component):
self.preferencesmanager = PreferencesManager() self.preferencesmanager = PreferencesManager()
self.alertmanager = AlertManager() self.alertmanager = AlertManager()
self.pluginmanager = PluginManager(self) self.pluginmanager = PluginManager(self)
self.torrentmanager = TorrentManager(self.session, self.alertmanager) self.torrentmanager = TorrentManager()
self.filtermanager = FilterManager(self) self.filtermanager = FilterManager(self)
self.autoadd = AutoAdd() self.autoadd = AutoAdd()
self.authmanager = AuthManager() self.authmanager = AuthManager()
@ -453,13 +436,6 @@ class Core(component.Component):
for torrent_id in torrent_ids: for torrent_id in torrent_ids:
self.torrentmanager[torrent_id].resume() self.torrentmanager[torrent_id].resume()
@export
def get_status_keys(self):
"""
returns all possible keys for the keys argument in get_torrent(s)_status.
"""
return STATUS_KEYS + self.pluginmanager.status_fields.keys()
@export @export
def get_torrent_status(self, torrent_id, keys): def get_torrent_status(self, torrent_id, keys):
# Build the status dictionary # Build the status dictionary
@ -790,16 +766,26 @@ class Core(component.Component):
@export @export
def test_listen_port(self): def test_listen_port(self):
""" Checks if active port is open """ """
import urllib Checks if the active port is open
port = self.get_listen_port()
try: :returns: True if the port is open, False if not
status = urllib.urlopen("http://deluge-torrent.org/test_port.php?port=%s" % port).read() :rtype: bool
except IOError:
log.debug("Network error while trying to check status of port %s", port) """
return 0 from twisted.web.client import getPage
else: d = defer.Deferred()
return int(status)
gp = getPage("http://deluge-torrent.org/test_port.php?port=%s" % self.get_listen_port())
def on_get_page(result):
d.callback(bool(int(result)))
def on_get_page_failure(result):
d.errback(result)
gp.addCallbacks(on_get_page, on_get_page_failure)
return d
@export @export
def get_free_space(self, path): def get_free_space(self, path):

View file

@ -121,17 +121,19 @@ class TorrentManagerState:
self.torrents = [] self.torrents = []
class TorrentManager(component.Component): class TorrentManager(component.Component):
"""TorrentManager contains a list of torrents in the current libtorrent """
TorrentManager contains a list of torrents in the current libtorrent
session. This object is also responsible for saving the state of the session. This object is also responsible for saving the state of the
session for use on restart.""" session for use on restart.
"""
def __init__(self, session, alerts): def __init__(self):
component.Component.__init__(self, "TorrentManager", interval=5, depend=["CorePluginManager"]) component.Component.__init__(self, "TorrentManager", interval=5, depend=["CorePluginManager"])
log.debug("TorrentManager init..") log.debug("TorrentManager init..")
# Set the libtorrent session # Set the libtorrent session
self.session = session self.session = component.get("Core").session
# Set the alertmanager # Set the alertmanager
self.alerts = alerts self.alerts = component.get("AlertManager")
# Get the core config # Get the core config
self.config = ConfigManager("core.conf") self.config = ConfigManager("core.conf")

View file

@ -100,4 +100,13 @@ class CoreTestCase(unittest.TestCase):
self.assertTrue(space >= 0) self.assertTrue(space >= 0)
self.assertRaises(deluge.error.InvalidPathError, self.core.get_free_space, "/someinvalidpath") self.assertRaises(deluge.error.InvalidPathError, self.core.get_free_space, "/someinvalidpath")
def test_test_listen_port(self):
d = self.core.test_listen_port()
def result(r):
self.assertTrue(r in (True, False))
d.addCallback(result)
return d