Ejemplo n.º 1
0
    def __init__(self, ustrCfgFileName, ustrDbFileName):
        ustrCfgFileName = CTYLB_MainSys_MiscFunc.SafeGetUnicode(ustrCfgFileName)
        ustrDbFileName = CTYLB_MainSys_MiscFunc.SafeGetUnicode(ustrDbFileName)

        # 设置运行任务提交
        self.s_RunnDTaskArray = []   # 正在运行的任务队列

        # 创建机器人框架
        self.s_LessTYLB_Bot_FrameThread = CLessTYBotFrameThread(ustrCfgFileName, ustrDbFileName)
        # 设置默认环境处理
        # self.s_LessTYLB_Bot_FrameThread.SetDefaultConsoleCompatible( CtrlCHandle)
        # 准备运行
        self.s_LessTYLB_Bot_FrameThread.Prepare_Start()
        self.s_strSelfID = self.s_LessTYLB_Bot_FrameThread.GetMyName()
        # 注册 单元到达的通知,HLSession的新数据单元创建
        self.s_LessTYLB_Bot_FrameThread.RegisterNewCTUnit( CTYLB_Bot_BaseDef.s_g_iDataType_TaskRegCenter_sk_v1,
                                                           CTYLB_Bot_BaseDef.s_g_iMainCmd_TaskRegCenter_ReportResult_V2,
                                                           CTYLB_Bot_BaseDef.s_g_iSubCmd_TaskRegCenter_ReportResult_V2_PingTraceWebCrawl,
                                                           CTYBot_CTUnit_TaskRegCenter_CreateNewUnit_V2_ReportResult)
        self.s_LessTYLB_Bot_FrameThread.RegisterNewCTUnit( CTYLB_Bot_BaseDef.s_g_iDataType_TaskRegCenter_sk_v1,
                                                           CTYLB_Bot_BaseDef.s_g_iMainCmd_TaskRegCenter_ReportResult,
                                                           CTYLB_Bot_BaseDef.s_g_iSubCmd_TaskRegCenter_ReportResult_PingTraceWebCrawl,
                                                           CTYBot_CTUnit_TaskRegCenter_CreateNewUnit_ReportResult)

        # 获得管套管理对象
        self.s_hlSockMang = self.s_LessTYLB_Bot_FrameThread.s_HLSockMang
        # 读取配置参数
        ustrTaskRegCenterID = self.s_LessTYLB_Bot_FrameThread.s_execTYLB_Bot_Sample.ReadIniSectionValue( ustrCfgFileName,
                                                            u'client', u'taskcenter_id',
                                                            CTYLB_Bot_BaseDef.s_g_strBotName_TaskRegCenter_sk_v1)
        self.s_bstrTaskRegCenterID = CTYLB_MainSys_MiscFunc.SafeGetUTF8(ustrTaskRegCenterID)
        # 创建连接管套
        self.s_x_TaskRegCenter_Sock = CTYFBot_OpSession_ConnectSock(self.s_hlSockMang, self.s_bstrTaskRegCenterID,
                                                                    self.s_g_iTaskMang_Listen_Port_PromptTask)
        pass
Ejemplo n.º 2
0
    config_file = os.path.join(config_dir, "config.yaml")
    if not os.path.exists(config_dir):
        os.makedirs(config_dir)
    if not os.path.isfile(config_file):
        CTYLB_Log.ShowLog(0, 'Main',
                          '[%s] config file missing...Quit!' % g_strProgName)
        #配置文件不存在,直接退出
        os._exit(-1)

    db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "db")
    db_file = os.path.join(db_dir, "data.db")
    if not os.path.exists(db_dir):
        os.makedirs(db_dir)

    # 创建机器人框架
    g_LessTYLB_Bot_FrameThread = CLessTYBotFrameThread(config_file, db_file)
    # 设置默认环境处理
    g_LessTYLB_Bot_FrameThread.SetDefaultConsoleCompatible(CtrlCHandle)
    # 准备运行
    g_LessTYLB_Bot_FrameThread.Prepare_Start()
    g_strSelfID = g_LessTYLB_Bot_FrameThread.GetMyName()

    hlSockMang = g_LessTYLB_Bot_FrameThread.s_HLSockMang

    # 获得管套管理对象
    connectEchoSockArray = []
    iMaxCount = 1  # random.randint(2,6)
    for i in range(iMaxCount):
        connectEchoSock = CTYFBot_OpSession_ConnectSock(
            hlSockMang, g_strEchoSockSrvName, g_iEchoSockSrvPort, 3, 1200)
        connectEchoSockArray.append(connectEchoSock)
Ejemplo n.º 3
0
    config_file = os.path.join(config_dir, u"config.ini")
    if not os.path.exists(config_dir):
        os.makedirs(config_dir)
    if not os.path.isfile(config_file):
        CTYLB_Log.ShowLog(0, u'Main',
                          u'[%s] config file missing...Quit!' % g_strProgName)
        #配置文件不存在,直接退出
        os._exit(-1)

    db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), u"db")
    db_file = os.path.join(db_dir, u"data.db")
    if not os.path.exists(db_dir):
        os.makedirs(db_dir)

    # 创建机器人框架
    g_LessTYLB_Bot_TRC_Srv_FrameThread = CLessTYBotFrameThread(
        config_file, db_file)
    # 设置默认环境处理
    g_LessTYLB_Bot_TRC_Srv_FrameThread.SetDefaultConsoleCompatible(CtrlCHandle)
    # 注册用到的数据单元,当HLSession单元收到后,里面用到的单元数据
    g_LessTYLB_Bot_TRC_Srv_FrameThread.RegisterNewCTUnit(
        CTYLB_Bot_BaseDef.s_g_iDataType_TaskRegCenter_sk_v1,
        CTYLB_Bot_BaseDef.s_g_iMainCmd_TaskRegCenter_AssignTask,
        CTYLB_Bot_BaseDef.s_g_iSubCmd_TaskRegCenter_AssignTask_Param,
        CTYBot_CTUnit_TaskRegCenter_CreateNewUnit_AssignTaskParam)
    g_LessTYLB_Bot_TRC_Srv_FrameThread.RegisterNewCTUnit(
        CTYLB_Bot_BaseDef.s_g_iDataType_TaskRegCenter_sk_v1,
        CTYLB_Bot_BaseDef.s_g_iMainCmd_TaskRegCenter_AssignTask,
        CTYLB_Bot_BaseDef.s_g_iSubCmd_TaskRegCenter_AssignTask_BroadCast_Param,
        CTYBot_CTUnit_TaskRegCenter_CreateNewUnit_AssignTaskParam)
    g_LessTYLB_Bot_TRC_Srv_FrameThread.RegisterNewCTUnit(
        CTYLB_Bot_BaseDef.s_g_iDataType_TaskRegCenter_sk_v1,
Ejemplo n.º 4
0
    def __init__(self, config_file="config/config.yaml", funcCallBack=None):
        TYFiberBot_Mang_NATS_Instance_Base.__init__(self, config_file,
                                                    funcCallBack)

        # 如果有更新,2秒发一次。否则,半分钟发一次 我的单元列表
        self.s_TimerCheck = CSkBot_Common_Share.CSkBot_TimeIdentify(2)
        self.s_AlwaysSendFbUnitList_TimerCheck = CSkBot_Common_Share.CSkBot_TimeIdentify(
            30)
        self.s_FBotSendFbUnitList_TimerCheck = CSkBot_Common_Share.CSkBot_TimeIdentify(
            30)

        CLessTYBotFrameThread.SetDefaultConsoleCompatible(
            FBotV4.environment_event_handle)

        # 初始化,加载FBot的配置
        strFBotUTRC_Server = CTylb_Bot_Exec_Frame.ReadIniSectionValue(
            config_file, Share_UTRC_CfgDef.g_str_section_FiberUTRC,
            Share_UTRC_CfgDef.g_str_key_FBot_UTRC_Server, "")
        self.s_utrcFBot = None

        self.s_toBeSend_FBot_PackageArray = []  # 等到发送给伙伴FBot的数据包队列
        self.s_timeDiff_SendInit = CSkBot_Common_Share.CSkBot_TimeIdentify(1)

        if (strFBotUTRC_Server):
            loopCallBack = []
            if (self.s_funcTimerCallBack):
                loopCallBack.append((self.s_funcTimerCallBack))
            loopCallBack.append(self.LoopEventCallBack)

            connectUTRCFBotSrvAddr = {
                strFBotUTRC_Server:
                [Share_UTRC_CfgDef.g_iFbUTRC_AcceptExecClient_ListenPort]
            }
            self.s_utrcFBot = FBotV4(connect_to_socks=connectUTRCFBotSrvAddr,
                                     loop_event_callbacks=loopCallBack,
                                     config_file=config_file)

            self.s_utrcFBot.add_connect_to_callbacks(
                sock_id=strFBotUTRC_Server,
                sock_port=Share_UTRC_CfgDef.
                g_iFbUTRC_AcceptExecClient_ListenPort,
                cmd_id=Share_UTRC_CfgDef.g_iFBot_Cmd_Nop_SendEcho_To_Srv,
                callback=self.OnFBot_InitCall_LoopSendToSrv,
                is_auto_callback=True)

            self.s_utrcFBot.add_connect_to_callbacks(
                sock_id=strFBotUTRC_Server,
                sock_port=Share_UTRC_CfgDef.
                g_iFbUTRC_AcceptExecClient_ListenPort,
                cmd_id=Share_UTRC_CfgDef.g_iFBot_Cmd_NeedExecCmd_To_Client,
                callback=self.OnFBot_Client_Cmd_NeedExecCmd,
                is_auto_callback=False)

            self.s_utrcFBot.add_connect_to_callbacks(
                sock_id=strFBotUTRC_Server,
                sock_port=Share_UTRC_CfgDef.
                g_iFbUTRC_AcceptExecClient_ListenPort,
                cmd_id=Share_UTRC_CfgDef.g_iFbUTRC_Cmd_Reply_Message_Result,
                callback=self.OnFBot_Client_Cmd_ReplyMessageResult,
                is_auto_callback=False)

        pass
Ejemplo n.º 5
0
    def run(self):
        global IS_SYS_RUNNING
        IS_SYS_RUNNING = True
        # 获取配置文件路径及文件名,文件夹不存在则创建
        config_file = os.path.abspath(self.__config_file)
        if not os.path.isfile(config_file):
            CTYLB_Log.ShowLog(
                0, 'Main',
                '[%s] config file missing...Quit!' % self.__bot_name)
            # 配置文件不存在,直接退出
            os._exit(-1)

        db_dir = os.path.join(os.path.abspath("."), u"db")
        db_file = os.path.join(db_dir, "{0}.db".format(self.__bot_name))
        if not os.path.exists(db_dir):
            os.makedirs(db_dir)
        # 创建机器人框架
        botFrameThread = CLessTYBotFrameThread(config_file, db_file)
        # 设置默认对环境全局事件处理
        botFrameThread.SetDefaultConsoleCompatible(
            FBotV4.environment_event_handle)
        # 准备运行
        botFrameThread.Prepare_Start()

        # 获得通讯管套管理对象
        sockManager = botFrameThread.s_HLSockMang

        connectToSocks = {}
        # 获得通讯管套管理对象、操作结果以及操作参数块
        for key in self.__connect_to_socks.keys():
            connectToSocks[key] = {}
            for port in self.__connect_to_socks[key]:
                # 若为状态汇报,则间隔时间由用户指定
                if key == TYFBOT_STATUS_MANAGER_IDENTIFY_NAME:
                    sock = CTYFBot_OpSession_ConnectSock(
                        hlSockMang=sockManager,
                        strDestSrvName=key,
                        iDestPort=port,
                        iDefaultIdleTick=self.__report_interval)
                else:
                    sock = CTYFBot_OpSession_ConnectSock(
                        hlSockMang=sockManager,
                        strDestSrvName=key,
                        iDestPort=port,
                        iDefaultIdleTick=1)
                connectToSocks[key][port] = sock

                CTYLB_Log.ShowLog(
                    0, 'exec',
                    'Start HLSock connect remote [{0}:{1}]  --{2}'.format(
                        key, port, self.__bot_name))

        listenSocks = []
        acceptSocks = []
        for sockID in self.__listen_socks:
            listenSock = sockManager.CreateListenSock(sockID)
            listenSocks.append(listenSock)

        iFreeCount = 0
        while IS_SYS_RUNNING:
            isTaskBusy = False
            for key in connectToSocks.keys():
                for port in connectToSocks[key].keys():
                    sock = connectToSocks[key][port]
                    if (sock.ExecNextCheck()):
                        isTaskBusy = True

                    if (sock.CanExecSendData()):
                        for port in self.__connect_to_callbacks[key].keys():
                            execSendCTArray = self.__connect_send_package_handle(
                                key, port)
                            sock.ExecSendData(execSendCTArray)
                        isTaskBusy = True
                        if self.__report_status:
                            if key in self.__node_status.SocksInfo[
                                    'Socks'].keys():
                                self.__node_status.SocksInfo['Socks'][key][
                                    'Status'] = "Up"
                                self.__node_status.SocksInfo['Socks'][key][
                                    'TotalSend'] += len(execSendCTArray)
                                self.__node_status.SocksInfo['Socks'][key][
                                    'LastSendTime'] = NodeStatus.get_now_timestamp(
                                    )
                                self.__node_status.SocksInfo[
                                    'TotalSend'] += len(execSendCTArray)
                    recvCTArray = sock.PopRetriveRecvCTArray()

                    if (not recvCTArray):
                        bSockExist, recvCTArray = sockManager.PassiveRecv_From_ConnectSock(
                            sock.s_iExecConnectSockIndex)
                        if (not bSockExist):
                            CTYLB_Log.ShowLog(
                                0, 'exec', 'sock close by remote. restarting.')
                            sock.Close()

                    if (recvCTArray):
                        isTaskBusy = True
                        strPeerName, iPeerPort = sockManager.GetSockPeerAddr(
                            sock.s_iExecConnectSockIndex)
                        if isinstance(strPeerName, bytes):
                            strPeerName = strPeerName.decode()
                        isNonePackageReply = True
                        arrayLen = len(recvCTArray)
                        if self.__report_status:
                            if key in self.__node_status.SocksInfo[
                                    'Socks'].keys():
                                self.__node_status.SocksInfo['Socks'][key][
                                    'Status'] = "Up"
                                self.__node_status.SocksInfo['Socks'][key][
                                    'TotalRecv'] += arrayLen
                                self.__node_status.SocksInfo['Socks'][key][
                                    'LastRecvTime'] = NodeStatus.get_now_timestamp(
                                    )
                                self.__node_status.SocksInfo[
                                    'TotalRecv'] += arrayLen
                        packageCount = 0
                        for eachUnit in recvCTArray:
                            packageCount += 1
                            eachRetCommCTUnitArray = self.__connect_to_basic_handle(
                                key, port, eachUnit, packageCount == arrayLen)
                            if eachRetCommCTUnitArray:
                                isNonePackageReply = False
                                self.__connect_wait_to_send_arrays[key][
                                    port].extend(eachRetCommCTUnitArray)
                        if isNonePackageReply:
                            defaultPackage = CTYBot_CTUnit_CommonData()
                            self.__connect_wait_to_send_arrays[key][
                                port].append(defaultPackage)
            # 若存在监听服务
            for listenSock in listenSocks:
                # 监听服务通信检查,检查新接受的连接
                newClientSockIndex = sockManager.ExAcceptNewListenArriveSock(
                    listenSock)
                if newClientSockIndex:
                    strPeerName, iPeerPort = sockManager.GetSockPeerAddr(
                        newClientSockIndex)
                    if isinstance(strPeerName, bytes):
                        strPeerName = strPeerName.decode()
                    if self.__listen_accept_callback:
                        self.__listen_accept_callback(
                            listen_sock_id=listenSock,
                            peer_name=strPeerName,
                            peer_port=iPeerPort,
                            client_index=newClientSockIndex)
                    CTYLB_Log.ShowLog(
                        0, u'New Client Accept',
                        u'From [%s:%d] new HLConnect %d accept.' %
                        (strPeerName, iPeerPort, newClientSockIndex))
                    bTaskBusy = True
                    acceptSocks.append(newClientSockIndex)
                    if self.__report_status:
                        # 添加Listen端SockInfo
                        sockInfo = SockInfo(name=strPeerName,
                                            operateId=key,
                                            sockType='Listen')
                        self.__node_status.SocksInfo['Socks'][
                            strPeerName] = sockInfo.dump_obj()
                        self.__node_status.SocksInfo['Socks'][strPeerName][
                            'Status'] = "Up"
                    pass
                for iEachAcceptSock in acceptSocks:
                    # 检查有否新数据包到达
                    strPeerName, iPeerPort = sockManager.GetSockPeerAddr(
                        iEachAcceptSock)
                    if isinstance(strPeerName, bytes):
                        strPeerName = strPeerName.decode()
                    bSockExist, ctUnitArray = sockManager.ActiveRecv_From_AcceptSock(
                        iEachAcceptSock)
                    if (not bSockExist):
                        # 检查是否管套已经关闭
                        if self.__report_status:
                            if strPeerName in self.__node_status.SocksInfo[
                                    'Socks'].keys():
                                self.__node_status.SocksInfo['Socks'][
                                    strPeerName]['Status'] = "Down"
                        CTYLB_Log.ShowLog(
                            0, u'Accept Client Closed',
                            u'Accept sock %d closed' % (iEachAcceptSock))
                        acceptSocks.remove(iEachAcceptSock)

                        break
                    else:
                        if (ctUnitArray):
                            bTaskBusy = True
                            self.__listen_basic_handle(sockManager,
                                                       iEachAcceptSock,
                                                       ctUnitArray)
                            if self.__report_status:
                                if strPeerName in self.__node_status.SocksInfo[
                                        'Socks'].keys():
                                    self.__node_status.SocksInfo['Socks'][
                                        strPeerName]['Status'] = "Up"
                                    self.__node_status.SocksInfo['Socks'][
                                        strPeerName]['TotalRecv'] += len(
                                            ctUnitArray)
                                    self.__node_status.SocksInfo['Socks'][
                                        strPeerName][
                                            'LastRecvTime'] = NodeStatus.get_now_timestamp(
                                            )
                                    self.__node_status.SocksInfo[
                                        'TotalRecv'] += len(ctUnitArray)
            # 循环需要调用的回调处理,比如定时从数据库读取任务发送等
            for loop_event_callback in self.__loop_event_callbacks:
                loop_event_callback()

            if (not isTaskBusy):
                iFreeCount += 1
                if (iFreeCount > 50):
                    time.sleep(0.01)
            else:
                iFreeCount = 0

        for key in connectToSocks.keys():
            for port in connectToSocks[key]:
                connectToSocks[key][port].Close()
        connectToSocks = {}
        listenSocks = []

        botFrameThread.SafeStop()
        CTYLB_Log.ShowLog(0, "System Exited", "Bye Bye!")
Ejemplo n.º 6
0
class CTYBot_Sock_D_PromptTask:
    # 远程管套定义
    s_g_iTaskMang_Listen_Port_PromptTask = 3   # 任务管理中心,对任务提交者的监听端口

    def __init__(self, ustrCfgFileName, ustrDbFileName):
        ustrCfgFileName = CTYLB_MainSys_MiscFunc.SafeGetUnicode(ustrCfgFileName)
        ustrDbFileName = CTYLB_MainSys_MiscFunc.SafeGetUnicode(ustrDbFileName)

        # 设置运行任务提交
        self.s_RunnDTaskArray = []   # 正在运行的任务队列

        # 创建机器人框架
        self.s_LessTYLB_Bot_FrameThread = CLessTYBotFrameThread(ustrCfgFileName, ustrDbFileName)
        # 设置默认环境处理
        # self.s_LessTYLB_Bot_FrameThread.SetDefaultConsoleCompatible( CtrlCHandle)
        # 准备运行
        self.s_LessTYLB_Bot_FrameThread.Prepare_Start()
        self.s_strSelfID = self.s_LessTYLB_Bot_FrameThread.GetMyName()
        # 注册 单元到达的通知,HLSession的新数据单元创建
        self.s_LessTYLB_Bot_FrameThread.RegisterNewCTUnit( CTYLB_Bot_BaseDef.s_g_iDataType_TaskRegCenter_sk_v1,
                                                           CTYLB_Bot_BaseDef.s_g_iMainCmd_TaskRegCenter_ReportResult_V2,
                                                           CTYLB_Bot_BaseDef.s_g_iSubCmd_TaskRegCenter_ReportResult_V2_PingTraceWebCrawl,
                                                           CTYBot_CTUnit_TaskRegCenter_CreateNewUnit_V2_ReportResult)
        self.s_LessTYLB_Bot_FrameThread.RegisterNewCTUnit( CTYLB_Bot_BaseDef.s_g_iDataType_TaskRegCenter_sk_v1,
                                                           CTYLB_Bot_BaseDef.s_g_iMainCmd_TaskRegCenter_ReportResult,
                                                           CTYLB_Bot_BaseDef.s_g_iSubCmd_TaskRegCenter_ReportResult_PingTraceWebCrawl,
                                                           CTYBot_CTUnit_TaskRegCenter_CreateNewUnit_ReportResult)

        # 获得管套管理对象
        self.s_hlSockMang = self.s_LessTYLB_Bot_FrameThread.s_HLSockMang
        # 读取配置参数
        ustrTaskRegCenterID = self.s_LessTYLB_Bot_FrameThread.s_execTYLB_Bot_Sample.ReadIniSectionValue( ustrCfgFileName,
                                                            u'client', u'taskcenter_id',
                                                            CTYLB_Bot_BaseDef.s_g_strBotName_TaskRegCenter_sk_v1)
        self.s_bstrTaskRegCenterID = CTYLB_MainSys_MiscFunc.SafeGetUTF8(ustrTaskRegCenterID)
        # 创建连接管套
        self.s_x_TaskRegCenter_Sock = CTYFBot_OpSession_ConnectSock(self.s_hlSockMang, self.s_bstrTaskRegCenterID,
                                                                    self.s_g_iTaskMang_Listen_Port_PromptTask)
        pass

    # 提交分布任务进行运行, 参数1-dRunTask = CTYBot_DTask_Base 单元
    # start by sk. 170226
    def PromptDTask(self, dRunTask):
        self.s_RunnDTaskArray.append( dRunTask)
        pass

    # 运行前的准备 - 子类可继承
    # start by sk. 170226
    def Vilu_Prepare_Run(self):
        # 对每个任务的准备函数进行调用
        for eachDTask in self.s_RunnDTaskArray:
            eachDTask.Viul_Internal_ParamBlock_PrePare(self.s_bstrTaskRegCenterID)
        pass

    # 单位时间调度 - 子类可继承
    # start by sk. 170226
    def Vilu_TimerCheck(self):
        bRetValue = False

        # 对每个扫描任务进行检查调度
        for eachPromptTask in self.s_RunnDTaskArray:
            if( eachPromptTask.Vilu_ExecTimerCheck()):
                bRetValue = True

        if( self.s_x_TaskRegCenter_Sock.ExecNextCheck()):
            bRetValue = True
        if( self.s_x_TaskRegCenter_Sock.CanExecSendData()):
            bRetValue = True
            # 对每个任务的准备函数进行调用
            needPromptParamArray = []
            for eachDTask in self.s_RunnDTaskArray:
                curParamArray = eachDTask.Sys_Schedule_RequestParamBlock()
                needPromptParamArray.extend(curParamArray)
                # 请求插件ID的空余数量
                newCommDataCTUnit = CTYBot_CTUnit_CommonData()
                newCommDataCTUnit.SetIntData(CTYBot_CTUnit_CommonData.s_g_iIntValue_Query_PluginID_Run_ParamBlock_Count)
                newCommDataCTUnit.SetParam(str(eachDTask.s_iRunPluginID).encode(), b'')
                needPromptParamArray.append(newCommDataCTUnit)
            # 提交发送
            if( not needPromptParamArray):
                newCommDataCTUnit = CTYBot_CTUnit_CommonData()
                needPromptParamArray.append( newCommDataCTUnit)
            self.s_x_TaskRegCenter_Sock.ExecSendData( needPromptParamArray)
            pass

        recvCTArray = self.s_x_TaskRegCenter_Sock.PopRetriveRecvCTArray()
        if( recvCTArray):
            bRetValue = True
            bstrPeerName, iPeerPort = self.s_hlSockMang.GetSockPeerAddr(self.s_x_TaskRegCenter_Sock.s_iExecConnectSockIndex)
            for eachUnit in recvCTArray:
                # CTYLB_Log.ShowLog(0, 'data-receive', 'from [%s:%d] recv [%d]' % (strPeerName, iPeerPort, eachUnit.s_iMyDataType))
                msgObject = {
                    "monitor_type": "status",
                    "level": "info",
                    "target": "",
                    "plugin_id": -1,
                    "block_id": "",
                    "block_size": 0,
                    "free_size": 0,
                    "wait_size": 0,
                    "success_size": 0,
                    "result_code": 0,
                    "msg": "PLC received <%d> bytes from <%s:%d>" % (eachUnit.s_iMyDataType, bstrPeerName, iPeerPort)
                }
                CTYLB_Log.ShowMonitor(msgType="PLC", msgObject=msgObject)

                if (eachUnit.s_iMyDataType == CTYLB_Bot_BaseDef.s_g_iDataType_CommonTask):
                    self.HandleRecv_CommonData(bstrPeerName, eachUnit)
                elif (eachUnit.s_iMyDataType == CTYLB_Bot_BaseDef.s_g_iDataType_TaskRegCenter_sk_v1):
                    if (eachUnit.s_iBotCmd_Main == CTYLB_Bot_BaseDef.s_g_iMainCmd_TaskRegCenter_ReportResult_V2):
                        self.HandleRecv_Result_V2(bstrPeerName, eachUnit)
                        self.s_x_TaskRegCenter_Sock.SetNextTempSendWaitTick(0)   # 有结果到达,可能还有结果数据,这时候不等待,立即再发送获取
                    elif (eachUnit.s_iBotCmd_Main == CTYLB_Bot_BaseDef.s_g_iMainCmd_TaskRegCenter_ReportResult):
                        self.HandleRecv_Result(bstrPeerName, eachUnit)
                        self.s_x_TaskRegCenter_Sock.SetNextTempSendWaitTick(0)   # 有结果到达,可能还有结果数据,这时候不等待,立即再发送获取
                else:
                    CTYLB_Log.ShowLog(1, 'data-receive', 'Unknown packet:%d-%d-%d' % (eachUnit.s_iMyDataType,
                                                                                      eachUnit.s_iBotCmd_Main,
                                                                                      eachUnit.s_iBotCmd_Sub))
                    msgObject = {
                        "monitor_type": "status",
                        "level": "warning",
                        "target": "",
                        "plugin_id": -1,
                        "block_id": "",
                        "block_size": 0,
                        "free_size": 0,
                        "wait_size": 0,
                        "success_size": 0,
                        "result_code": 0,
                        "msg": "PLC unknown package received <Type:%d,MainCMD:%d,SubCMD:%d>"
                               % (eachUnit.s_iMyDataType, eachUnit.s_iBotCmd_Main, eachUnit.s_iBotCmd_Sub)
                    }
                    CTYLB_Log.ShowMonitor(msgType="PLC", msgObject=msgObject)

                pass

        return bRetValue

    # 处理接收数据的回调 - 通用数据 - CTYBot_CTUnit_CommonData
    # start by sk. 170226
    def HandleRecv_CommonData(self, bstrFromUser, commuCTUnit):
        if (commuCTUnit.s_iType == CTYBot_CTUnit_CommonData.s_g_iType_int):
            bHandleParamResult = False  # 处理参数结果
            bReplyBlockCount = False   # 处理块个数
            bCenterReplyResult = False  # 处理中心回应结果到达

            if (commuCTUnit.s_iValue == CTYBot_CTUnit_CommonData.s_g_iIntValue_Task_Finish):
                bHandleParamResult = True
            elif(commuCTUnit.s_iValue == CTYBot_CTUnit_CommonData.s_g_iIntValue_Task_BCast_Fail_No_Host):
                bHandleParamResult = True
            elif(commuCTUnit.s_iValue == CTYBot_CTUnit_CommonData.s_g_iIntValue_Task_Finish_Fail):
                bHandleParamResult = True
            elif ( commuCTUnit.s_iValue == CTYBot_CTUnit_CommonData.s_g_iIntValue_Reply_PluginID_Run_ParamBlock_Count):
                bReplyBlockCount = True
            elif ( commuCTUnit.s_iValue == CTYBot_CTUnit_CommonData.s_g_iIntValue_Reply_TaskCenter_Send_Promptor_Result_Finish):
                # [任务中心->提交者] 返回当前提交者的结果个数。strParam1=结果个数, strParam2=请求包标识符
                bCenterReplyResult = True

            # 处理块个数
            if( bReplyBlockCount):
                try:
                    iQueryPluginID = int(commuCTUnit.s_bstrParam1)
                    iCanRunCount = int(commuCTUnit.s_bstrParam2)
                    for eachDTask in self.s_RunnDTaskArray:
                        eachDTask.Sys_Reply_Plugin_RunParam_Count( iQueryPluginID, iCanRunCount)
                except Exception as e:
                    pass

            # 处理参数结果
            if( bHandleParamResult):
                for eachDTask in self.s_RunnDTaskArray:
                    if (eachDTask.Sys_IsParamSign_MyRunningParamArray(commuCTUnit.s_bstrParam1)):
                        eachDTask.Sys_Reply_ParamBlock_Result(commuCTUnit.s_bstrParam1, commuCTUnit.s_iValue)
                        break

            # 处理结果到达完成
            if( bCenterReplyResult):
                iResultCount = int(commuCTUnit.s_bstrParam1)
                bstrOrigSign = commuCTUnit.s_bstrParam2
                for eachDTask in self.s_RunnDTaskArray:
                    eachDTask.Sys_Reply_SendResultCount_Finish(iResultCount, bstrOrigSign)
            pass

    # 处理接收数据的回调 - 结果数据 - CTYBot_CTUnit_TaskRegCenter_ReportResult
    # start by sk. 170226
    def HandleRecv_Result(self, bstrFromName, resultCTUnit):
        for eachDTask in self.s_RunnDTaskArray:
            if( eachDTask.IsParamBlockSignInMyArray(resultCTUnit.s_bstrOrigTaskUniqueSign)):
                bstrFixOrigResult = CTYLB_P2P_ContentUnit_Base_bytes.SafeConvertBase64Str_ToUTF8( resultCTUnit.s_bstrRunResult)
                eachDTask.Viul_Notify_Recv_RunResult( resultCTUnit.s_bstrOrigTaskUniqueSign,
                                                      resultCTUnit.s_iExecPluginID,
                                                      resultCTUnit.s_bstrExecDomainName,
                                                      bstrFixOrigResult)
                break
        pass

    # 处理接收数据的回调 - 结果数据V2 - CTYBot_CTUnit_TaskRegCenter_ReportResult_V2
    # start by sk. 170226
    def HandleRecv_Result_V2(self, bstrFromName, resultV2CTUnit):
        for eachDTask in self.s_RunnDTaskArray:
            if( eachDTask.IsParamBlockSignInMyArray(resultV2CTUnit.s_bstrOrigTaskUniqueSign)):
                for eachSubUnit in resultV2CTUnit.s_subResultUnitArray:
                    bstrFixOrigResult = CTYLB_P2P_ContentUnit_Base_bytes.SafeConvertBase64Str_ToUTF8(
                        eachSubUnit.s_bstrRunResult)
                    eachDTask.Viul_Notify_Recv_RunResult( resultV2CTUnit.s_bstrOrigTaskUniqueSign,
                                                          eachSubUnit.s_iExecPluginID,
                                                          eachSubUnit.s_bstrExecDomainName,
                                                          bstrFixOrigResult)
                break
        pass

    def StopQuit(self):
        self.s_x_TaskRegCenter_Sock.Close()
        self.s_LessTYLB_Bot_FrameThread.SafeStop()
Ejemplo n.º 7
0
    if not os.path.exists(config_dir):
        os.makedirs(config_dir)
    if not os.path.isfile(config_file):
        CTYLB_Log.ShowLog(0, u'Main', u'[%s] config file missing...Quit!' % g_ustrProgName)
        #配置文件不存在,直接退出
        os._exit(-1)

    db_dir=os.path.join(os.path.dirname(os.path.realpath(__file__)),u"db")
    db_file= os.path.join(db_dir,u"data.db")
    if not os.path.exists(db_dir):
        os.makedirs(db_dir)

    g_iSockEchoListenPort = 2  # Echo端口

    # 创建机器人框架
    g_LessTYLB_Bot_Echo_Srv_FrameThread = CLessTYBotFrameThread(config_file, db_file)
    # 设置默认环境处理
    g_LessTYLB_Bot_Echo_Srv_FrameThread.SetDefaultConsoleCompatible( CtrlCHandle)
    # 准备运行
    g_LessTYLB_Bot_Echo_Srv_FrameThread.Prepare_Start()

    # 获得管套管理对象
    hlSockMang = g_LessTYLB_Bot_Echo_Srv_FrameThread.s_HLSockMang
    iListenSockIndex = hlSockMang.CreateListenSock( g_iSockEchoListenPort)
    iNewAcceptSockIndexArray = []
    while(g_bSysRunning):
        bTaskBusy = False
        iNewAcceptSockIndex = hlSockMang.ExAcceptNewListenArriveSock( iListenSockIndex)
        if( iNewAcceptSockIndex):
            bstrPeerName, iPeerPort = hlSockMang.GetSockPeerAddr(iNewAcceptSockIndex)
            CTYLB_Log.ShowLog(0, u'sys-echo', u'from [%s:%d] new HLConnect %d accept.' % (bstrPeerName, iPeerPort, iNewAcceptSockIndex))
    if not os.path.exists(config_dir):
        os.makedirs(config_dir)
    if not os.path.isfile(config_file):
        CTYLB_Log.ShowLog(0, u'Main',
                          u'[%s] config file missing...Quit!' % g_strProgName)
        #配置文件不存在,直接退出
        os._exit(-1)

    db_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), u"db")
    db_file = os.path.join(db_dir, u"data.db")
    if not os.path.exists(db_dir):
        os.makedirs(db_dir)

    s_g_iTaskMang_Listen_Port_PluginStatusResult = 4  # 任务管理中心,插件报告状态,和传回结果。收到后,发送参数块过去
    # 创建机器人框架
    g_LessTYLB_Bot_ExecPlugin_FrameThread = CLessTYBotFrameThread(
        config_file, db_file)
    # 设置默认环境处理
    g_LessTYLB_Bot_ExecPlugin_FrameThread.SetDefaultConsoleCompatible(
        CtrlCHandle)
    # 注册用到的数据单元,当HLSession单元收到后,里面用到的单元数据
    g_LessTYLB_Bot_ExecPlugin_FrameThread.RegisterNewCTUnit(
        CTYLB_Bot_BaseDef.s_g_iDataType_TaskRegCenter_sk_v1,
        CTYLB_Bot_BaseDef.s_g_iMainCmd_TaskRegCenter_Assign_WebNameBlock,
        CTYLB_Bot_BaseDef.s_g_iSubCmd_TaskRegCenter_Assign_WebNameBlock,
        CTYBot_CTUnit_TaskRegCenter_CreateNewUnit_AssignWebNameBlock)
    # 准备运行
    g_LessTYLB_Bot_ExecPlugin_FrameThread.Prepare_Start()
    # 读取服务器配置
    ustrTaskRegCenterName = g_LessTYLB_Bot_ExecPlugin_FrameThread.s_execTYLB_Bot_Sample.ReadIniSectionValue(
        config_file, u'client', u'taskcenter_id',
        CTYLB_Bot_BaseDef.s_g_strBotName_TaskRegCenter_sk_v1)