File: multiplex.py

package info (click to toggle)
python-thriftpy 0.3.9%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 560 kB
  • sloc: python: 3,287; ansic: 30; makefile: 7
file content (34 lines) | stat: -rw-r--r-- 1,145 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-

from thriftpy.thrift import TMultiplexedProcessor, TMessageType


class TMultiplexedProtocol(object):
    """Multiplex the protocol by prepend service name to api for every api call.
    Can be used together with all original protocols.
    """

    def __init__(self, proto, service_name):
        self.service_name = service_name
        self._proto = proto

    def __getattr__(self, name):
        return getattr(self._proto, name)

    def write_message_begin(self, name, ttype, seqid):
        if ttype in (TMessageType.CALL, TMessageType.ONEWAY):
            self._proto.write_message_begin(
                self.service_name + TMultiplexedProcessor.SEPARATOR + name,
                ttype, seqid)
        else:
            self._proto.write_message_begin(name, ttype, seqid)


class TMultiplexedProtocolFactory(object):
    def __init__(self, proto_factory, service_name):
        self._proto_factory = proto_factory
        self.service_name = service_name

    def get_protocol(self, trans):
        proto = self._proto_factory.get_protocol(trans)
        return TMultiplexedProtocol(proto, self.service_name)