views:

22

answers:

1

I'm using twisted. I have my protocols set up so that, to send an RPC, I do protocol.send("update_status", data). To document which RPCs I've implemented, I make a separate function call for each one, so in this case I'd call REQUEST_UPDATE_STATUS(data) to send that RPC. When a protocol receives an RPC, a function gets called based on its name, in this case, CMD_UPDATE_STATUS.

The problem is that REQUEST and CMD are a bit awkward. I can mistake REQUEST as part of the command, for example, REQUEST_NEW_DATA, and that would end up triggering an RPC called 'new_data'. However, REQUEST_REQUEST_NEW_DATA is just silly.

CMD is also awkward, as a REQUEST_SEND_NEW_DATA will become CMD_SEND_NEW_DATA, which is a bit awkward.

Any tips?

A: 

First tip: Use PB... it's well designed and does exactly that

Second Tip: If the first tip isn't going to work for you, just do what PB does. On the client end a "callRemote("foo_func")" asks the server ot invoke the "foo_func" function on the server object. The server will then use "getattr(server_obj, "remote_" + "foo_func")" to find the remote method. If the method exists, it's called. Otherwise an error is returned. The nice thing about this design is that it completely does away with your REQUEST... CMD... constants.

Rakis
PB means Perspective Broker - http://twistedmatrix.com/documents/current/core/examples/#auto4
THC4k
hmm I could do this already.. instead of `protocol.send` I do `protocol.callRemote`, and instead of `CMD_SEND_NEW_DATA` i have `remote_sendNewData` or `REMOTE_sendNewData` as I prefer. i think i might have pretty much implemented PB.
Claudiu
I suggest you read through the Perspective Broker documentation. Anyone implementing RPC in Python would benefit from an understanding of PB's design as it contains several elegant solutions to common problems. The lighter-weight AMP protocol (also in twisted) is another good background source.
Rakis