Skip to content

Instantly share code, notes, and snippets.

@maxtaco
Last active September 4, 2015 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxtaco/1cbdffd55f1d964853ab to your computer and use it in GitHub Desktop.
Save maxtaco/1cbdffd55f1d964853ab to your computer and use it in GitHub Desktop.
KB wrapper w/ session awareness on top of regular RPC
##
## Extremely rough & hacky pseudocode, but I guarantee we can get something
## like this to work. I believe this is the interface you wanted, but
## we can easily implement it on top of the interface we have.
##
class KbClient extends Client
# construct with a KbTransport below....
constructor :() ->
call : (name, args, megaMultiCallback) ->
# it's the client's job to come up with a new sessionId, so do that
# here.
sessionId = @transport.sessionId++
# Tell the transport that if any calls come in the opposite
# way for our sessionID, then to call our megaMultiCallback
@transport.table[sessionId] = megaMultiCallback
# Send the sessionId to the other side (note the caller of this
# function never needs to worry about it)
args.sessionId = sessionId
# Now call into the RPC library as usual.
super name, args, (err, reply) =>
# When this RPC is done, deregister the sessionId
delete @transport.table[sessionId]
# And finally, call out megaMultiCallback that we're
# done with this
megaMultiCallback "final", { err, reply }
class KbTransport extends RpcTransport
constructor : () ->
# init super here..
# setup this transport to also **SERVE** RPC calls,
# sending them to the method below:
@set_generic_handler (args) => @handle_incoming_rpc args
@table = {}
@sessionId = 0
handle_incoming_rpc : ({method, param, response, dispatch}) ->
if (hook = @table[param.sessionId])?
hook method, { args : param, responseObject : response }
else
# something's wrong, drop it....
response.error new Error "whoops, unknown session #{param.sessionId}"
# lots of details let out of course! But now you can do what you want:
myKbRpc.call "keybase.1.identify.identify", { userAssertion : "max" }, (whichCallback, args) ->
# From above, this callback can be called multiple times, but 'final'
# is guaranteed to happen only once, and is the last one.
# this is a little bit like the Node.js EventEmitter model,
# which personally I'm not a big fan of, but some people like it.
switch whichCallback
when 'final'
# do something with args.err and args.reply
when 'keybase.1.identifyUi.finishSocialProofCheck'
# do something with args.args.rp and args.args.lcr (see protocol) and
# when done, call args.reponseObject.reply()
when 'keybase1.identifyUi.finishWebProofCheck'
# do something with args.args.rp and args.args.lcr
when # etc, there are many others!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment