0

Reading through the Cap'n Proto RPC docs, if you need object references to persist across multiple connections, you need to write restorers.

On the client side, it's pretty clear how to get the write object out of it (although the lack of type info makes it obnoxiously verbose).

On the server side, it's unclear how to select what kind of object you'll get out of it?

AstraLuma
  • 609
  • 5
  • 16

1 Answers1

0

The docs are super unclear on this, and I'm not even sure I got this right.

First, you need to select what shape of reference you'll use: "ez" or struct (although you could maybe use a struct type as ez? The docs don't discuss this at all.)

If you go with ez, you'll use it like so:

# Client
calculator = client.ez_restore('calculator').cast_as(calculator_capnp.Calculator)

# Server
def restore(ref_id):
    if ref_id == 'calculator':
        return Calculator()
    else:
        raise Exception('Unrecognized ref passed to restore')

server = capnp.TwoPartyServer(sock, restore)

Otherwise, your reference can be a struct, and you would use code like this:

# Client
calculator = client.restore(calculator_capnp.ObjRef.new_message(id='calculator')).cast_as(calculator_capnp.Calculator)

# Server
class ObjRestorer(calculator_capnp.ObjRef.Restorer):

    def restore(self, objref):
        if objref.id == 'calculator':
            return Calculator()
        else:
            raise Exception('Unrecognized ref passed to restore')

server = capnp.TwoPartyServer(sock, ObjRestorer())

A given RPC must have a single restorer style and type, I think. You certainly cannot use multiple struct restorers.

AstraLuma
  • 609
  • 5
  • 16