Changes between Version 1 and Version 2 of Development/UiClient1.2


Ignore:
Timestamp:
02/10/2009 11:57:39 PM (15 years ago)
Author:
andar
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Development/UiClient1.2

    v1 v2  
    22 
    33Deluge is separated into two distinct portions, the daemon and the client, and as such, there needs to be a way for these components to communicate.  The deluge.ui.client module is designed to facilitate this communication for the various UserInterfaces.  The client module hides the DelugeRPC protocol from us and presents with an easy to use API to access Deluge daemons. 
     4 
     5If you are not familiar with Twisted Deferred objects, then I would strongly suggest [http://twistedmatrix.com/projects/core/documentation/howto/defer.html reading about them] before getting started. 
    46 
    57= Writing a simple client = 
     
    5153All this client script will do is try to connect to a daemon running on localhost and then disconnect from it right away.  The script is pretty useless, but it shows how things are done in an asynchronous matter and this is an important concept for developing more complex interfaces. 
    5254 
    53 You'll notice that the client.connect() function returns a Twisted Deferred object.  If you are not familiar with Twisted or Deferred objects, then I strongly suggest [http://twistedmatrix.com/projects/core/documentation/howto/defer.html reading about them] before continuing as we will be using Deferreds throughout these examples. 
     55So now that we've got a basic script to get us connected to a daemon, let's extend it a bit to do something useful.  We'll start by adding a remote procedure call in the '''on_connect_success()''' function. 
     56 
     57{{{ 
     58#!python 
     59 
     60def on_connect_success(result): 
     61    print "Connection was successful!" 
     62    def on_get_config_value(key, value): 
     63        print "Got config value from the daemon!" 
     64        print "%s: %s" % (key, value) 
     65        # Disconnect from the daemon once we've got what we needed 
     66        client.disconnect() 
     67        # Stop the twisted main loop and exit 
     68        reactor.stop() 
     69     
     70    # Request the config value for the key 'download_location'     
     71    client.core.get_config_value("download_location").addCallback(on_get_config_value, "download_location") 
     72 
     73}}} 
     74 
     75Ok! We now should be getting a print out of the ''download_location'' config value.  You'll notice that any RPC method returns a Deferred object, just like the '''client.connect()''' method.  Since the '''core.get_config_value()''' method only returns the value we are passing the ''key'' to the callback function too.  So in the '''on_get_config_value()''' callback, the first argument is from our '''addCallback()''' call and the second is the return value from the daemon. 
     76