コード例 #1
0
    def handler(self, msg):
        try:
            pkg_params, svc_params, mt_subject, tail = nrpc.parse_subject(
                PKG_SUBJECT, PKG_SUBJECT_PARAMS_COUNT, Greeter_SUBJECT,
                Greeter_SUBJECT_PARAMS_COUNT, msg.subject)

            mname, params_count, input_type, has_reply, void_reply, streamed_reply = self.methods[
                mt_subject]
            mt_params, count = nrpc.parse_subject_tail(params_count, tail)

            method = getattr(self.server, mname)
            if input_type is not None:
                req = input_type.FromString(msg.data)
                mt_params.append(req)
            err = None
            if streamed_reply:
                yield from nrpc.streamed_reply_handler(self.nc, msg.reply,
                                                       method(*mt_params))
                return
            try:
                rep = yield from method(*mt_params)
            except nrpc.ClientError as e:
                err = e.as_nrpc_error()
            except Exception as e:
                err = nrpc.exc.server_error(e)
            else:
                if isinstance(rep, nrpc.ClientError):
                    err = rep
                elif void_reply and rep is not None:
                    raise ValueError(
                        "Method %s implementation should return None" % mname)
            if has_reply:
                if err is not None:
                    rawRep = b'\x00' + err.SerializeToString()
                elif rep is not None:
                    rawRep = rep.SerializeToString()
                else:
                    rawRep = b''
                yield from self.nc.publish(msg.reply, rawRep)
        except Exception as e:
            import traceback
            traceback.print_exc()
            print("Error in Greeter.%s handler:" % mname, e)
コード例 #2
0
ファイル: lib_test.py プロジェクト: zhwei820/python-nrpc
def test_parse_subject(
    pkg_subject, pkg_params_count, svc_subject, svc_params_count,
    mt_params_count, subject,
    e_pkg_params, e_svc_params, e_mt_params, e_mt_name, e_encoding, e_error,
):
    if e_error:
        ctx = pytest.raises(e_error[0], match=e_error[1])
    else:
        ctx = ExitStack()

    with ctx:
        pkg_params, svc_params, mt_name, tail = nrpc.parse_subject(
            pkg_subject, pkg_params_count, svc_subject, svc_params_count,
            subject)
        assert pkg_params == e_pkg_params
        assert svc_params == e_svc_params
        assert mt_name == e_mt_name

        mt_params, encoding = nrpc.parse_subject_tail(mt_params_count, tail)
        assert mt_params == e_mt_params
        assert encoding == e_encoding