python - Implementing Referenceable objects client-side with Twisted Perspective Broker -
i trying implement simple server reply in perspective broker.
possible implementation (please suggest others if possible):
client requests server execute server method, server executes replies (by executing client method sole purpose print message):
[client-side]: class clientprint(pb.referenceable): def remote_clientprint(self, message): print "printing message server: ", message [server-side]: class rootserverobject(pb.root): def remote_onefunc(self, ...): ... print "now sending reply..." *get clientprint object?* clientprintobj.callremote("clientprint", "this reply!")
how can implement grabbing of client-side objects? there better way implement server replies grabbing client-side object , calling print-only client method?
here full code trying implement replies:
[client-side]:
from twisted.internet import reactor twisted.spread import pb class client(): def __init__(self, addr, port, spec): self.addr = none self.port = none self.somedata = none def connect(self, addr, port): factory = pb.pbclientfactory() reactor.connecttcp(addr, port, factory) def1 = factory.getrootobject() def1.addcallbacks(self.got_obj, self.err_obj) def got_obj(self, rootsrvobj): print "got root server obj:", rootsrvobj self.server = rootsrvobj def2 = self.server.callremote("somefunc", somedata) def err_obj(self, reason): print "error getting root server obj:", reason self.quit() def cmdsub(addr, port, somedata): c = client(addr, port, somedata) c.connect(addr, port)
[server-side]:
class rootserverobject(pb.root): def __init__(self): self.dataout = none def remote_somefunc(self, somedata): self.dataout = hash(somedata) print "now sending reply..." *implement reply?*
perhaps there more advanced twisted (or twisted pb) features make simpler.
documentation: https://twistedmatrix.com/documents/12.3.0/core/howto/pb-usage.html#auto3
thanks.
the simplest way take client-side object server needs use , pass server. solution can think of has @ core.
change client's got_obj
method more this:
def got_obj(self, rootsrvobj): print "got root server obj:", rootsrvobj self.server = rootsrvobj def2 = self.server.callremote("somefunc", self, somedata)
and change implementation of remote_somefunc
more this:
def remote_somefunc(self, client, somedata): self.dataout = hash(somedata) print "now sending reply..." client.callremote("client_print", "here reply")
you might want investigate twisted cred more structured way manage references client object - cred building on exact feature of perspective broker provide more abstract, more featureful interface.
however, notice said "almost" above...
keep in mind twisted's implementation of perspective broker has well-integrated support deferreds. if remote_
method returns deferred
no response sent method call until deferred
fires , result sent result of method call. might consider putting logic of client_print
callback on deferred
returned self.server.callremote("somefunc", somedata)
, making server's remote_somefunc
return reply, either synchronously or asynchronously (as deferred
).
Comments
Post a Comment