Beispiel #1
0
def master_conncet(name: str, master: dict = None):
    '''
    :param
        master : dict {
            SRC_NAME : str,
	        PORT : int,
	        HOST : str,
        }
    '''
    # master_config = GlobalObject().config.get("DISTRIBUTED", {}).get("MASTER", {})
    logger.debug(f"master node is runing on {master}")
    # # GlobalObject().masterremote = RemoteMasterObject(service_config.get("NAME"))
    GlobalObject().masterremote = RemoteObject(name)
    logger.debug("connecting into master node...")
    # node连接 master 节点,若连接不成功,则会退出服务
    GlobalObject().masterremote.connect(
        (master.get('HOST'), int(master.get('PORT'))))
    logger.debug("connect master node success...")
    logger.debug("providing methods to the master node")
    logger.debug("provide methods to the master node success")
    # ########################################
    # 切勿删除本段代码
    # 导入master服务,该服务主要提供
    #     master 控制 node 的 remote连接
    #     master 控制 node 的 reload
    #     master 控制 node 的 stop
    from txfirefly.core import masterservice
Beispiel #2
0
	def __init__(self, name: str,service_path = None,port=None):
		'''
		:return
		'''
		# root对象监听制定端口
		super(RPCServer,self).__init__()
		
		self.name = name
		
		if not service_path:
			self.service_path = GlobalObject().config.get("DISTRIBUTED").get(name).get("APP")
		
		if not port:
			port = int(GlobalObject().config.get("DISTRIBUTED").get(name).get("PORT"))
		
		self.pbRoot = PBRoot()
		
		from twisted.internet import reactor
		
		reactor.listenTCP(port, BilateralFactory(self.pbRoot))
		
		service = Service(name=name)
		
		# 将服务添加到root
		self.pbRoot.addServiceChannel(service)
		
		self.registerService(self.service_path)
Beispiel #3
0
def serverReload():
    """
    
    """
    logger.debug('service reload !!!')
    if GlobalObject().node:
        GlobalObject().node._doWhenReload()
    return True
Beispiel #4
0
def serverStop():
    """
    """
    logger.debug('service stop !!!')
    if GlobalObject().node:
        GlobalObject().node._doWhenStop()
    from twisted.internet import reactor
    reactor.callLater(1, reactor.stop)
    return True
Beispiel #5
0
	def dataReceived(self, data):
		self.setTimeout(GlobalObject().config.get("TIME_OUT_COUNT", MQTT_TIME_OUT))
		logger.debug('received from target:{target} :\n  {data}'.format(
			target=(self.transport.client[0], self.transport.client[1]), data=data))
		logger.debug('received from target:{target} :\n  {data}'.format(
			target=(self.transport.client[0], self.transport.client[1]), data=[hex(item) for item in data]))
		self._accumulatePacket(data)
Beispiel #6
0
    async def startup_event():
        """
        获取链接
        :return:
        """
        # await register_zmq()
        import asyncio
        loop = asyncio.get_event_loop()

        from twisted.internet import asyncioreactor
        asyncioreactor.install(eventloop=loop)

        from txrpc.globalobject import GlobalObject

        with open(
                os.sep.join([
                    os.path.dirname(os.path.dirname(
                        os.path.abspath(__file__))), "config.json"
                ])) as f:
            GlobalObject().config = json.load(f)

        from txrpc.client import RPCClient

        global client

        client = RPCClient("CLIENT").clientConnect()
Beispiel #7
0
    def _connectRemote(self,
                       name: str,
                       target_name: str,
                       host: str,
                       port: int,
                       weight: int = 10):
        '''
			连接 远端 节点
		:param name: 本节点名称
		:param target_name: 远端节点名称
		:param host: 远端节点host
		:param port:  远端节点port
		:param weight:  本节点权重
		:return:
		'''

        assert name != None, "local 名称不能为空"
        logger.debug("名称检查通过 ...")
        assert port != None, "port 不能为空"
        logger.debug("port ...")
        assert host != None, "host 不能为空"
        logger.debug("host ...")
        assert target_name != None, "target_name 不能为空"
        logger.debug("target_name ...")

        remote = RemoteObject(name)
        remote.setWeight(weight)
        GlobalObject().remote_map[target_name] = remote
        d = remote.connect((host, port))
        d.addCallback(lambda ign: self._doWhenConnect())
        d.addCallback(lambda ign: self.registerService(self.service_path))
Beispiel #8
0
    def clientConnect(self,
                      name=None,
                      host=None,
                      port=None,
                      weight=None,
                      service_path=None):
        '''
			连接 远端 节点
		:param name:  本节点名称
		:param host:  server节点host
		:param port:  server节点port
		:param weight:  本节点权重
		:param service_path:  本节点服务地址
		:return:
		'''
        logger.debug("clientConnect ...")
        if not service_path:
            self.service_path = GlobalObject().config.get("DISTRIBUTED").get(
                self.name).get("APP")
        else:
            self.service_path = service_path
        if not name:
            name = GlobalObject().config.get("DISTRIBUTED").get(
                self.name).get("REMOTE").get("NAME")

        if not host:
            host = GlobalObject().config.get("DISTRIBUTED").get(
                self.name).get("REMOTE").get("HOST")

        if not port:
            port = int(GlobalObject().config.get("DISTRIBUTED").get(
                self.name).get("REMOTE").get("PORT"))

        if not weight:
            weight = int(GlobalObject().config.get("DISTRIBUTED").get(
                self.name).get("REMOTE").get("WEIGHT", 10))

        logger.debug("local<{name}> -> remote:<{target_name}>".format(
            name=self.name, target_name=name))

        self._connectRemote(name=self.name,
                            target_name=name,
                            host=host,
                            port=port,
                            weight=weight)

        return self
Beispiel #9
0
 def dataReceived(self, data):
     '''
     数据到达处理
     @param data: str 客户端传送过来的数据
     '''
     self.setTimeout(GlobalObject().config.get("TIME_OUT_COUNT",
                                               30))  # 添加超时定时器
     self.datahandler.send(data)  # 触发datahandler生成器
Beispiel #10
0
 def onMessage(self, payload, isBinary):
     '''
     接受websocket数据触发函数
     :param payload:
     :param isBinary:
     :return:
     '''
     self.setTimeout(GlobalObject().config.get("TIME_OUT_COUNT", 30))
     self.datahandler.send(payload)
Beispiel #11
0
	def callRemote(remoteName: str, functionName: str, *args, **kwargs):
		'''
			调用子节点挂载的函数
		:param remoteName:  节点名称
		:param functionName: 方法名称
		:param args:  参数
		:param kwargs:  参数
		:return:
		'''
		return GlobalObject().node.pbRoot.callChildByName(remoteName, functionName, *args, **kwargs)
Beispiel #12
0
    async def startup_event():
        """
        获取链接
        :return:
        """
        # await register_zmq()
        import asyncio
        loop = asyncio.get_event_loop()

        from twisted.internet import asyncioreactor
        asyncioreactor.install(eventloop=loop)

        from twisted.internet import reactor

        from txrpc.globalobject import GlobalObject
        from loguru import logger

        with open(
                os.sep.join([
                    os.path.dirname(os.path.dirname(
                        os.path.abspath(__file__))), "config.json"
                ])) as f:
            GlobalObject().config = json.load(f)

        global server

        from txrpc.server import RPCServer

        def fun():
            d = RPCServer.callRemote("CLIENT", "client_test")
            if not d:
                return None
            d.addCallback(logger.debug)
            d.addErrback(logger.error)
            return d

        server = RPCServer("SERVER")

        @server.childConnectHandle
        def doChildConnect(name, transport):
            '''
            :return
            '''
            logger.debug("{} connected".format(name))

            for i in range(1000):
                reactor.callLater(i * 2 + 1, fun)

        @server.childLostConnectHandle
        def doChildLostConnect(childId):
            '''
            :return
            '''
            logger.debug("{} lost connect".format(childId))
Beispiel #13
0
    def callRemote(remoteName: str, functionName: str, *args, **kwargs):
        '''
			调用远端节点
		:param remoteName: 节点名称
		:param functionName:  函数名称
		:param args:  参数1
		:param kwargs:  参数2
		:return:
		'''
        return GlobalObject().getRemote(remoteName).callRemote(
            functionName, *args, **kwargs)
Beispiel #14
0
def fun_():
    d = GlobalObject().node.pbRoot.callChildByName("CLIENT", "client_test")
    if not d:
        return None
    d.addCallback(logger.debug)
    d.addErrback(logger.error)
    return d
Beispiel #15
0
def remoteConnect(name, remote : dict,app : List[str]):
    '''
        控制 节点 连接 另一个节点
        :param name:  当前节点名称
        :param remotes: dict {
            NAME : str,
            PORT : int,
            HOST : str,
            WEIGHT : int,
        }
        :app 需要导入的服务路径
        :return:
        '''
    remote_name = remote.get("NAME")
    weight = remote.get("WEIGHT", 10)
    logger.debug(f"master 指令:当前节点 : {name} 连接节点 : {remote_name}")
    port = int(remote.get("PORT"))
    host = remote.get("HOST")
    GlobalObject().remote_map[remote_name] = RemoteObject(name)
    GlobalObject().remote_map[remote_name].setWeight(weight)
    GlobalObject().remote_map[remote_name].connect((host, port))
    logger.debug(f"当前节点 : {name} 连接节点 : {remote_name} 成功 准备导入服务 : {app}")
    GlobalObject().node.service_path = app
    delay_import(app)
Beispiel #16
0
 def connectionMade(self):
     '''
     连接成功后自动触发的函数
     推荐重写,可以加入连接对象添加,连接数量统计
     :return:
     '''
     logger.info('Client %d login in.[%s,%d]' %
                 (self.transport.sessionno, self.transport.client[0],
                  self.transport.client[1]))
     self.conn_id = self.transport.sessionno  # 连接ID
     self.setTimeout(GlobalObject().config.get("TIME_OUT_COUNT", 30))
     logger.info('Client : {} {} connected...'.format(
         self.transport.client[0], self.transport.client[1]))
     self.datahandler = self.dataHandleCoroutine()  # 创建数据生成器
     self.datahandler.__next__()  # 创建一个生成器,当数据接收时,触发生成器
     self.factory.doConnectionMade(self, self.conn_id)
Beispiel #17
0
	async def startup_event():
		"""
		获取链接
		:return:
		"""
		import asyncio
		loop = asyncio.get_event_loop()
		
		from twisted.internet import asyncioreactor
		asyncioreactor.install(eventloop=loop)
		
		from txfirefly.client import ClientNode
		from txrpc.globalobject import GlobalObject
		from twisted.internet import defer
		
		with open("config.json", "r") as f:
			GlobalObject().config = json.load(f)

		app.state.client = ClientNode("CLIENT")
		
		@app.state.client.startServiceHandle
		def start():
			logger.debug("i am start")
		
		@app.state.client.startServiceHandle
		@defer.inlineCallbacks
		def start2():
			logger.debug(32 * "*")
			ret = yield treq.get("http://httpbin.org")
			logger.debug(ret)
			defer.returnValue(ret)
			
		@app.state.client.startServiceHandle
		async def start3():
			pass
			# async with aiohttp.ClientSession() as session:
			# 	url = 'http://httpbin.org'
			# 	async with session.get(url) as response:
			# 		logger.debug(response.status)
			# logger.debug(await response.text())
		
		app.state.client.install()
Beispiel #18
0
asyncioreactor.install(eventloop=loop)

import json

import aiohttp
from twisted.internet import defer

from txfirefly.client import ClientNode
from txfirefly.exts.ffrequest import FFrequest
from txrpc.globalobject import GlobalObject
from txrpc.utils import asDeferred

from loguru import logger

with open("config.json","r") as f:
	GlobalObject().config = json.load(f)

app = ClientNode("CLIENT")

@app.startServiceHandle
def start():
	logger.debug("i am start")

@app.startServiceHandle
@defer.inlineCallbacks
def start2():
	# async with aiohttp.ClientSession() as session:
	# 	url = 'http://httpbin.org'
	# 	async with session.get(url) as response:
	# 		logger.debug(response.status)
	# 		logger.debug(response.text())
Beispiel #19
0
 def onConnect(self, request):
     logger.info("Client connecting: {}".format(request.peer))
     self.setTimeout(GlobalObject().config.get("TIME_OUT_COUNT", 30))
     self.factory.doConnectionMade(self, self.transport.sessionno)
     self.datahandler = self.dataHandleCoroutine()
     self.datahandler.__next__()
Beispiel #20
0
def masterserviceHandle(target):
    """
    将服务添加进入master节点
    """
    assert GlobalObject().masterremote != None,"请检查master节点是否正常运行"
    GlobalObject().masterremote._reference._service.mapTarget(target)
Beispiel #21
0
	def connectionMade(self):
		self.setTimeout(GlobalObject().config.get("TIME_OUT_COUNT", MQTT_TIME_OUT))
		logger.info('客户端:{} {} 连入...'.format(self.transport.client[0], self.transport.client[1]))