|
533.
|
|
|
<a href="../examples/pbsimple.py" class="py-listing" skipLines="5" >pbsimple.py</a> <a href="../examples/pbsimpleclient.py" class="py-listing" skipLines="5" >pbsimpleclient.py</a>
|
|
|
type: Content of: <html><body>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/pb-usage.xhtml:16
|
|
534.
|
|
|
First we look at the server. This defines an Echoer class (derived from <code class="API" base="twisted.spread">pb.Root</code>), with a method called <code>remote_echo()</code>. <code class="API" base="twisted.spread">pb.Root</code> objects (because of their inheritance of <code class="API" base="twisted.spread">pb.Referenceable</code>, described later) can define methods with names of the form <code>remote_*</code>; a client which obtains a remote reference to that <code class="API" base="twisted.spread">pb.Root</code> object will be able to invoke those methods.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/pb-usage.xhtml:21
|
|
535.
|
|
|
The <code class="API" base="twisted.spread">pb.Root</code>-ish object is given to a <code class="API" base="twisted.spread">pb.PBServerFactory</code><code>()</code>. This is a <code class="API" base="twisted.internet.protocol">Factory</code> object like any other: the <code class="API" base="twisted.internet.protocol">Protocol</code> objects it creates for new connections know how to speak the PB protocol. The object you give to <code>pb.PBServerFactory()</code> becomes the <q>root object</q>, which simply makes it available for the client to retrieve. The client may only request references to the objects you want to provide it: this helps you implement your security model. Because it is so common to export just a single object (and because a <code>remote_*</code> method on that one can return a reference to any other object you might want to give out), the simplest example is one where the <code class="API" base="twisted.spread.pb">PBServerFactory</code> is given the root object, and the client retrieves it.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/pb-usage.xhtml:32
|
|
536.
|
|
|
The client side uses <code class="API" base="twisted.spread">pb.PBClientFactory</code> to make a connection to a given port. This is a two-step process involving opening a TCP connection to a given host and port and requesting the root object using <code>.getRootObject()</code>.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/pb-usage.xhtml:49
|
|
537.
|
|
|
Because <code>.getRootObject()</code> has to wait until a network connection has been made and exchange some data, it may take a while, so it returns a Deferred, to which the gotObject() callback is attached. (See the documentation on <a href="defer.xhtml">Deferring Execution</a> for a complete explanation of <code class="API" base="twisted.internet.defer">Deferred</code>s). If and when the connection succeeds and a reference to the remote root object is obtained, this callback is run. The first argument passed to the callback is a remote reference to the distant root object. (you can give other arguments to the callback too, see the other parameters for <code>.addCallback()</code> and <code>.addCallbacks()</code>).
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/pb-usage.xhtml:55
|
|
538.
|
|
|
The callback does:
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/pb-usage.xhtml:67
|
|
539.
|
|
|
object.callRemote("echo", "hello network")
|
|
|
type: Content of: <html><body><pre>
|
|
|
represents a line break.
Start a new line in the equivalent position in the translation.
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/pb-usage.xhtml:70
|
|
540.
|
|
|
which causes the server's <code>.remote_echo()</code> method to be invoked. (running <code>.callRemote("boom")</code> would cause <code>.remote_boom()</code> to be run, etc). Again because of the delay involved, <code>callRemote()</code> returns a <code class="API" base="twisted.internet.defer">Deferred</code>. Assuming the remote method was run without causing an exception (including an attempt to invoke an unknown method), the callback attached to that <code class="API" base="twisted.internet.defer">Deferred</code> will be invoked with any objects that were returned by the remote method call.
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/pb-usage.xhtml:73
|
|
541.
|
|
|
In this example, the server's <code>Echoer</code> object has a method invoked, <em>exactly</em> as if some code on the server side had done:
|
|
|
type: Content of: <html><body><p>
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/pb-usage.xhtml:83
|
|
542.
|
|
|
echoer_object.remote_echo("hello network")
|
|
|
type: Content of: <html><body><pre>
|
|
|
represents a line break.
Start a new line in the equivalent position in the translation.
|
|
|
|
(no translation yet)
|
|
|
|
Located in
howto/pb-usage.xhtml:87
|