Ignore:
Timestamp:
02/16/2009 12:46:16 AM (16 years ago)
Author:
Damien Churchill <damoc@gmail.com>
Branches:
2.0.x, develop, extjs4-port, master
Children:
990790
Parents:
5bd5db
Message:

more progress on the connection manager

File:
1 edited

Legend:

Unmodified
Added
Removed
  • deluge/ui/web/server.py

    r5bd5db re99dd0  
    2525import os
    2626import sys
     27import time
    2728import locale
    2829import shutil
     
    4950from deluge.log import setupLogger, LOG as _log
    5051from deluge.ui import common as uicommon
    51 from deluge.ui.client import client
     52from deluge.ui.client import client, Client
    5253from deluge.ui.tracker_icons import TrackerIcons
    5354log = logging.getLogger(__name__)
     
    6566except Exception, e:
    6667    log.error("Unable to initialize gettext/locale: %s", e)
     68
     69_ = gettext.gettext
    6770
    6871current_dir = os.path.dirname(__file__)
     
    8992    "refresh_secs": 10
    9093}
     94
     95DEFAULT_HOST = "127.0.0.1"
     96DEFAULT_PORT = 58846
     97DEFAULT_HOSTS = {
     98    "hosts": [(hashlib.sha1(str(time.time())).hexdigest(), DEFAULT_HOST, DEFAULT_PORT, "", "")]
     99}
     100
     101HOSTLIST_COL_ID = 0
     102HOSTLIST_COL_HOST = 1
     103HOSTLIST_COL_PORT = 2
     104HOSTLIST_COL_STATUS = 3
     105HOSTLIST_COL_USER = 4
     106HOSTLIST_COL_PASS = 5
     107HOSTLIST_COL_VERSION = 6
     108
    91109config = ConfigManager("webui06.conf", CONFIG_DEFAULTS)
     110hostlist = ConfigManager("hostlist.conf.1.2", DEFAULT_HOSTS)
    92111
    93112def rpath(path):
     
    100119   
    101120    builtins = {
    102         "_": gettext.gettext,
     121        "_": _,
    103122        "version": common.get_version()
    104123    }
     
    127146            "web.get_torrent_info": self.get_torrent_info,
    128147            "web.add_torrents": self.add_torrents,
    129             "web.login": self.login
     148            "web.login": self.login,
     149            "web.get_hosts": self.get_hosts
    130150        }
    131151        for entry in open(common.get_default_config_dir("auth")):
     
    140160            self.local_username = username
    141161            self.local_password = password
    142         self.connect()
    143    
    144     def connect(self, host="localhost", username=None, password=None):
     162   
     163    def connect(self, host="localhost", port=58846, username=None, password=None):
    145164        """
    146165        Connects the client to a daemon
     
    215234                return self._exec_remote(method, params), request_id
    216235        except Exception, e:
     236            log.exception(e)
    217237            raise JSONException(e)
    218238   
     
    353373   
    354374    def login(self, password):
    355         """
     375        """Method to allow the webui to authenticate
    356376        """
    357377        m = hashlib.md5()
     
    362382        return d
    363383   
     384    def get_hosts(self):
     385        """Return the hosts in the hostlist"""
     386        hosts = dict((host[0], host[:]) for host in hostlist["hosts"])
     387       
     388        main_deferred = Deferred()
     389        def run_check():
     390            if all(map(lambda x: x[5] is not None, hosts.values())):
     391                main_deferred.callback(hosts.values())
     392       
     393        def on_connect(result, c, host_id):
     394            def on_info(info, c):
     395                hosts[host_id][5] = _("Online")
     396                hosts[host_id][6] = info
     397                c.disconnect()
     398                run_check()
     399           
     400            def on_info_fail(reason):
     401                hosts[host_id][5] = _("Offline")
     402                run_check()
     403           
     404            d = c.daemon.info()
     405            d.addCallback(on_info, c)
     406            d.addErrback(on_info_fail, c)
     407           
     408        def on_connect_failed(reason, host_id):
     409            print reason
     410            hosts[host_id][5] = _("Offline")
     411            run_check()
     412       
     413        for host in hosts.values():
     414            host_id, host, port, user, password = host[0:5]
     415            hosts[host_id].append(None)
     416            hosts[host_id].append(None)
     417           
     418            if client.connected() and (host, port, user) == client.connection_info():
     419                def on_info(info):
     420                    hosts[host_id][6] = info
     421                    run_check()
     422                host[5] = _("Connected")
     423                client.daemon.info().addCallback(on_info)
     424                hosts[host_id] = host
     425                continue
     426           
     427            c = Client()
     428            d = c.connect(host, port, user, password)
     429            d.addCallback(on_connect, c, host_id)
     430            d.addErrback(on_connect_failed, host_id)
     431        return main_deferred
    364432
    365433class GetText(resource.Resource):
Note: See TracChangeset for help on using the changeset viewer.