Example #1
0
    def unregist_name_service(self):
        """
        Method:    unregist_name_service
        Description: 向名字服务中心注销服务
        Parameter: 无
        Return: 
        Others: 
        """
        if self._pid == local_const_def.INVALID_PID:
            return
            
        #按pid注销        
        frame = AppFrame()
        frame.set_cmd_code(local_cmd_code.UNREGISTER_NAME_COMMAND)

        #向MASTER的Monitor注册
        frame.set_receiver_pid(self.get_pid("Monitor", local_const_def.MASTER_PID))
        
        req = name_msg_def.AppUnRegisterRequest()
        req.init_all_attr()
        req.pid = self._pid        
        req.need_reponse = False
        frame.add_data(req.serialize())
        
        #不用关心结果
        rpc_worker.rpc_request(frame, 2)        
Example #2
0
    def _query_from_nameservice(self):
        """
        Method:    _query_from_nameservice
        Description: 从名字服务查询所有注册信息
        Parameter: 无
        Return: 
        Others: 
        """
        frame = AppFrame()
        frame.set_cmd_code(local_cmd_code.QUERY_APP_COMMAND)
        frame.set_receiver_pid(self.get_pid("Monitor", local_const_def.MASTER_PID))
        
        req = name_msg_def.AppQueryRequest()
        req.init_all_attr()        
        frame.add_data(req.serialize())
        
        ack_frames = rpc_worker.rpc_request(frame, 10)
        all_app_infos = []
        if len(ack_frames)==0:            
            return all_app_infos
        
        #这里不需要捕获异常
        rep = name_msg_def.AppQueryResponse.deserialize(ack_frames[0].get_data())
        if rep.return_code==0:
            all_app_infos = rep.app_infos

        return all_app_infos
Example #3
0
    def _shakehand_with_monitor(self, statflag):
        """
        Method:    _shakehand_with_monitor
        Description: 与monitor握手一次
        Parameter: 
            statflag: 握手消息中带的状态
        Return: 
            -1: 握手失败
            0: monitor返回握手应答,应答中带有stop消息
            1: 握手成功
        Others: 
        """


        if not self._is_need_shake_with_monitor():
            return 1
        
        frame = AppFrame()
        frame.set_cmd_code(local_cmd_code.CMD_SHAKEHAND_WITH_MONITOR)
        #和本机Monitor握手
        frame.set_receiver_pid(self.get_pid("Monitor", local_const_def.ONLYLOCAL_PID))        
        frame.add_data(self._instance_name)
        frame.add_data(statflag)
        ack_frames = rpc_worker.rpc_request(frame, 2)

        if len(ack_frames) == 0:
            self.__shakehandl_failed_count += 1
            
            if self.__shakehandl_failed_count >= 3:
                tracelog.error("shake hand with monitor failed.")
                self.stop()                
            return -1

        self.__shakehandl_failed_count = 0
        ack_frame = ack_frames[0]
        rst = ack_frame.get_data(0)
        
        if rst == BasicApp.SHAKEHANDACK_STOP:
            tracelog.info("receive stop event from monitor.")
            self.stop()
            return 0

        return 1