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 sen开发者_开发问答d 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?
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.
精彩评论