[Twisted-Python] Perspective Broker

christian simms christian.simms at gmail.com
Mon Nov 27 06:37:13 EST 2006


On 11/27/06, le dahut <le.dahut at laposte.net> wrote:
> Hello,
>
> Using PB, I've setup a system where clients can control other clients in
> this way :
>
> client1 executes "remote_do_something" on server1 with "ip_client2" as
> argument.
> Then server1 connects to client2 and executes "remote_smthng" on it.

I haven't used PB before, but I have used other protocols with
Deferreds.  Assuming your PB code is OK, you're only missing a couple
lines related to callbacks, which I'll insert below.

> code is like this :
> - client1:
> """
> factory = pb.PBClientFactory()
> reactor.connectTCP(ip_server1, port, factory)
> d = factory.getRootObject()
> d.addCallback(lambda object: object.callRemote("do_something", ip_client2))
> d.addCallback(client1_call_b)
> d.addErrback(client1_err_b)

Please note that above, your client1_call_b function will be called
with its argument being the result of the remote method call
"do_something".

> """
>
> - server1:
> """
> def remote_do_something(ip):
>      factory = pb.PBClientFactory()
>      reactor.connectTCP(ip_client2, port, factory)
>      d = factory.getRootObject()
>      d.addCallback(lambda object: object.callRemote("smthng"))
>      d.addCallback(server1_call_b)
>      d.addErrback(server1_err_b)

Above, you need to add a "return d" statement at the very end of your
function "remote_do_something", or else Twisted won't know that the
output of the function "remote_do_something" should be the end result
of your Deferred "d".  Also, your function "server1_call_b" MUST
return its argument, or else the result will not get passed along.  It
could look something like this:

def server1_call_b(out):
    return out

The argument to server1_call_b is the result from the
"object.callRemote('smthng')" statement, and we are simply returning
it.  In fact, if that's all the function did, you should not even
define it (or use it in the callback chain) since it's not necessary.
But if you forgot the return statement then you would get Python's
default return value from a function, None, instead of the value.

>
> """
>
> - client2:
> """
> def remote_smthng():
>      <code>
>      return "Ok, everything's done"
> """
>
> How can client1 get client2's answer ?
>
> Thanks

Hopefully that does it.

Cheers,
Christian


>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>




More information about the Twisted-Python mailing list