diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2013-11-28 14:29:59 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2013-11-29 09:23:37 +0900 |
commit | bd897e79152a625f064511b3321e9bc22df881c0 (patch) | |
tree | 89f8cb019576b77c09cd3a23004e57496a556bea | |
parent | 5ce96a4b32966be6315625a342790eae8cce315f (diff) |
msgpack rpc helper
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/lib/rpc.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/ryu/lib/rpc.py b/ryu/lib/rpc.py new file mode 100644 index 00000000..8455791a --- /dev/null +++ b/ryu/lib/rpc.py @@ -0,0 +1,37 @@ +import msgpack + + +class RpcMessage(object): + REQUEST = 0 + RESPONSE = 1 + NOTIFY = 2 + + +class RpcSession(object): + def __init__(self): + super(RpcSession, self).__init__() + self._packer = msgpack.Packer() + self._unpacker = msgpack.Unpacker() + self._next_msgid = 0 + + def _create_msgid(self): + this_id = self._next_msgid + self._next_msgid += 1 + return this_id + + def create_request(self, method, params): + msgid = self._create_msgid() + return self._packer.pack([RpcMessage.REQUEST, msgid, method, params]) + + def create_response(self, msgid, error, result): + return self._packer.pack([RpcMessage.RESPONSE, msgid, error, result]) + + def create_notification(self, method, params): + return self._packer.pack([RpcMessage.NOTIFY, method, params]) + + def get_messages(self, data): + self._unpacker.feed(data) + messages = [] + for msg in self._unpacker: + messages.append(msg) + return messages |