Пример #1
0
class BaseListener(object):
    """
    Base class for listener(server) implementation. provides publish/unpublish method.
    """
    handlerCls = BaseHandler

    def __init__(self, address, app_name, data_center="", zone="", registry_end_point=None,
                 is_antsharecloud=False, access_key=None, secret_key=None,
                 **server_kwargs):
        """
        :param address: the socket address will be listened on.
        :type address: tuple (host:str, port:int)
        Check ApplicationInfo's comment for other params' explanations.
        """
        self.address = address
        self.app_name = app_name
        self.server_kwargs = server_kwargs
        self.handler = self.handlerCls()
        try:
            self._mesh_client = MeshClient(ApplicationInfo(app_name, data_center, zone, registry_end_point,
                                                           access_key, secret_key, is_antsharecloud))
            self._mesh_client.startup()
        except:
            logger.error("Fail to startup mesh client")
            self._mesh_client = None

    def initialize(self):
        raise NotImplementedError()

    def publish(self):
        """
        Publish all the interfaces in handler.interface_mapping to mosnd
        """
        if self._mesh_client:
            for service_name in self.handler.interface_mapping:
                self._mesh_client.publish(PublishServiceRequest(
                    port=str(self.address[1]),
                    serviceName=service_name,
                    providerMetaInfo=ProviderMetaInfo(protocol="1",
                                                      version="4.0",
                                                      serializeType="protobuf",
                                                      appName=self.app_name)))

    def unpublish(self):
        """
        Revoke all the interfaces in handler.interface_mapping from mosnd.
        """
        if self._mesh_client:
            for service_name in self.handler.interface_mapping:
                self._mesh_client.unpublish(service_name)

    def run_forever(self):
        raise NotImplementedError()

    def shutdown(self):
        raise NotImplementedError()

    def run_threading(self):
        t = threading.Thread(target=self.run_forever, daemon=True)
        t.start()
Пример #2
0
 def test_start(self, session_mock):
     session_mock.post('http://127.0.0.1:13330/configs/application',
                       text=json.dumps(dict(success=True)))
     mesh = MeshClient(ApplicationInfo("pybolt_test_app", "", "", ""))
     mesh.startup()
Пример #3
0
class _BaseClient(object):
    """
    Basic class for client implementation. Provides subscribe/unsubscribe method.
    """
    mesh_service_address = ("127.0.0.1", 12220)
    sofa_default_port = 12200

    def __init__(self,
                 app_name,
                 data_center=None,
                 zone=None,
                 registry_end_point=None,
                 is_antsharecloud=False,
                 access_key=None,
                 secret_key=None):
        """
        Check ApplicationInfo's comment for params' explanations.
        """
        try:
            self._mesh_client = MeshClient(
                ApplicationInfo(app_name, data_center, zone,
                                registry_end_point, access_key, secret_key,
                                is_antsharecloud))
            self._mesh_client.startup()
        except:
            logger.error("Fail to startup mesh client")
            self._mesh_client = None

    def _get_address(self, interface):
        addr = SERVICE_MAP.get(interface, self.mesh_service_address)
        if isinstance(addr, str):
            addr = (addr, self.sofa_default_port)
        return addr

    def subscribe(self, *interface):
        """
        Subscribe interfaces from mosnd, must called one before call to relevant interface.
        :param interface: the interface name.
        :type interface: str
        """
        if not self._mesh_client:
            return
        for inf in interface:
            try:
                self._mesh_client.subscribe(inf)
                SERVICE_MAP[inf] = self.mesh_service_address
            except Exception as e:
                logger.error(e)

    def unsubscribe(self, *interface):
        """
        Unsubscribe interfaces from mosnd.

        :param interface: the interface name
        :type interface: str
        """
        if not self._mesh_client:
            return
        for inf in interface:
            try:
                self._mesh_client.unsubscribe(inf)
                SERVICE_MAP.pop(inf)
            except Exception as e:
                logger.error(e)

    def invoke_sync(self, interface, method, content, **kwargs):
        raise NotImplementedError()

    def invoke_async(self, interface, method, content, **kwargs):
        raise NotImplementedError()

    def invoke_oneway(self, interface, method, content, **kwargs):
        raise NotImplementedError()