0
votes

I'm trying to play with twisted. I've already try to create a server and a client using the appropriate Factory. It's a fantastic framework but only strings can be sent :( I'd like to send some list or array objects as a start, and maybe later use objects from the server inside clients. But currently I'm stuck with some simple questions:

1) In the Factory model, I used methods like "clientConnectionFailed" or "clientConnectionLost", how can I get the same method using PB ? They were very useful for prevent client/server problems.

2) I also used methods like "dataReceived" and "connectionLost" or "connectionMade", how can I get same functions of this methods using PB ?

As you can see I'd like to start by rewriting my code using the PB model instead of Factories. I've looked at the exemples but unlike the Factory's one, I'm unable to understand them.

Thanks in advance for any help =)

1

1 Answers

2
votes

PBClientFactory is a ClientFactory, so you still get clientConnectionFailed and clientConnectionLost calls on it. Subclass and override, if you want. You can also use the notifyOnDisconnect method which is implemented by both the Broker (the protocol class used with PB) and by RemoteReference which you'll often have instances of when using PB.

You can still override connectionMade and connectionLost on the protocol, if you want. As I mentioned, the protocol is Broker. Subclass it, override some methods, and set your subclass (the class, not an instance) as the protocol attribute on your PBClientFactory.

You should not, however, override Broker.dataReceived. PB is not an ordered, reliable, stream-oriented byte transport. You don't wait for a chunk of bytes to be delivered and then respond with your own chunk of bytes. PB is a remote object and method call protocol. You use it to call methods over the network and pass objects to those method calls. If you a client to be able to call methods on your server, then you define remote_* methods.

The PB documentation explains this in greater detail. See the "Perspective Broker" section of http://twistedmatrix.com/documents/current/core/howto/.