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()
def test_pub(self, session_mock): session_mock.post('http://127.0.0.1:13330/services/publish', text=json.dumps(dict(success=True))) pubreq = PublishServiceRequest( serviceName="com.alipay.pybolt.test:1.0", providerMetaInfo=ProviderMetaInfo(protocol="1", version="4.0", serializeType="protobuf", appName="pybolt_test_app")) print(attr.asdict(pubreq)) mesh = MeshClient(ApplicationInfo("pybolt_test_app")) mesh.publish(pubreq)