Beispiel #1
0
 def __init__(self, mid, calculateCost):
     self.__mid = mid
     self.__calculateCost = calculateCost
     self.__calledCount = 0
     self.__acceptedCount = 0
     self.__completedCount = 0
     self.__handingCount = 0
     self.__rwLock = RWLock()
Beispiel #2
0
 def __init__(self, bufCap, maxBufNumber):
     self.__bufCap = bufCap
     self.__maxBufNumber = maxBufNumber
     self.__bufNumber = 1
     self.__bufQueue = Queue(maxBufNumber)
     self.__closed = 0
     self.__rwlock = RWLock()
     self.__total = 0
     buf = Buffer(bufCap)
     self.__bufQueue.put_nowait(buf)
Beispiel #3
0
 def __init__(self):
     self.__status = SCHED_STATUS_UNINITIALIZED
     self.__rwlock = RWLock()
     self.__registrar = None
     self.__bankerBufferPool = None
     self.__signBufferPool = None
     self.__broadcastBufferPool = None
     self.__stakingBufferPool = None
     self.__distributionBufferPool = None
     self.__errorBufferPool = None
     self.__auth = None
     self.__node = 'tcp://172.38.8.89:26657'
Beispiel #4
0
class Buffer:
    def __init__(self, size):
        self.__queue = Queue(size)
        self.__closed = 0
        self.__rwlock = RWLock()

    def Cap(self):
        return self.__queue.maxsize

    def Len(self):
        return self.__queue.qsize()

    def Put(self, data):
        self.__rwlock.acquire_read()
        try:
            if self.Closed():
                return False, BufferClosedError()
            self.__queue.put_nowait(data)
            return True, None
        except Full:
            return False, None
        finally:
            self.__rwlock.release()

    def Get(self):
        try:
            if self.Closed() is True:
                return None, BufferClosedError()
            item = self.__queue.get_nowait()
            return item, None
        except Empty:
            return None, None

    def Close(self):
        self.__rwlock.acquire_write()
        try:
            if self.__closed == 0:
                self.__closed = 1
                self.__queue = None
                return True
            return False
        finally:
            self.__rwlock.release()

    def Closed(self):
        return self.__closed != 0 or self.__queue is None
Beispiel #5
0
 def __init__(self):
     self.__moduleTypeDict = {}
     self.__rwLock = RWLock()
Beispiel #6
0
class Registrar(Registry):
    def __init__(self):
        self.__moduleTypeDict = {}
        self.__rwLock = RWLock()

    def Register(self, module):
        if module is None:
            return False, IllegalError('module is none')
        mid = module.ID()
        separateMID, err = SplitMid(mid)
        if err is not None:
            return False, IllegalError('invalid mid: {mid}'.format(mid=mid))
        mType = LegalLetterType[list(separateMID)[0]]
        if CheckType(mType, module) is False:
            return False, IllegalError(
                'invalid module type: {mType}'.format(mType=mType))
        # 加写锁
        self.__rwLock.acquire_write()
        try:
            modules = self.__moduleTypeDict.get(mType)
            if modules is None:
                modules = {}
            if modules.get(mid) is not None:
                return False, None
            modules[mid] = module
            self.__moduleTypeDict[mType] = modules
            return True, None
        finally:
            self.__rwLock.release()

    def UnRegister(self, mid):
        if mid is None or str(mid) == '':
            return False, IllegalError('empty mid')
        separateMID, err = SplitMid(mid)
        if err is not None:
            return False, IllegalError('invalid mid: {mid}'.format(mid=mid))
        mType = LegalLetterType[list(separateMID)[0]]
        # 加读锁
        self.__rwLock.acquire_read()
        try:
            modules = self.__moduleTypeDict[mType]
            if modules is not None:
                if modules[mid] is not None:
                    del modules[mid]
            return True, None
        finally:
            self.__rwLock.release()

    def Get(self, moduleType):
        modules, err = self.GetAllByType(moduleType)
        if err is not None:
            return None, err
        costs = {}
        for mid, module in modules.items():
            costs[mid] = module.CalculateCost()
        costArr = costs.items()
        index = Select(list(costArr))  # 筛选出更优的实例
        selectModuleID = list(costArr)[index][0]
        return modules[selectModuleID], None

    def GetAllByType(self, mType):
        if LegalType(mType) is False:
            return None, IllegalError(
                'invalid module type: {mType}'.format(mType=mType))
        # 加读锁
        self.__rwLock.acquire_read()
        try:
            modules = self.__moduleTypeDict[mType]
            if len(modules) == 0:
                return None, EmptyError()
            result = {}
            for mid, module in modules.items():
                result[mid] = module
            return result, None
        finally:
            self.__rwLock.release()

    def GetAll(self):
        result = {}
        # 加读锁
        self.__rwLock.acquire_read()
        try:
            for i, modules in self.__moduleTypeDict:
                for mid, module in modules:
                    result[mid] = module
            return result
        finally:
            self.__rwLock.release()

    def Clear(self):
        # 加读锁
        self.__rwLock.acquire_read()
        try:
            del self.__moduleTypeDict
            self.__moduleTypeDict = {}
        finally:
            self.__rwLock.release()
Beispiel #7
0
 def __init__(self, size):
     self.__queue = Queue(size)
     self.__closed = 0
     self.__rwlock = RWLock()
Beispiel #8
0
def CheckStatus(srcStatus, dstStatus):
    lock = RWLock()
    lock.acquire_write()
    try:
        # 情况一
        if srcStatus == SCHED_STATUS_INITIALIZING:
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Warn('scheduler is initialing...'))
            return CheckStatusError(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Warn('scheduler is initialing...'))
        elif srcStatus == SCHED_STATUS_STARTING:
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Warn('scheduler is starting...'))
            return CheckStatusError(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Warn('scheduler is starting...'))
        elif srcStatus == SCHED_STATUS_STOPPING:
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Warn('scheduler is stopping...'))
            return CheckStatusError(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Warn('scheduler is stopping...'))
        # 情况二
        elif srcStatus == SCHED_STATUS_UNINITIALIZED and (
                dstStatus == SCHED_STATUS_STARTING
                or dstStatus == SCHED_STATUS_STOPPING):
            print(
                Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Warn(
                    'scheduler haven\'t been initialized!'))
            return CheckStatusError(
                Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Warn(
                    'scheduler haven\'t been initialized!'))
        # 情况三
        elif dstStatus == SCHED_STATUS_INITIALIZING:
            if srcStatus == SCHED_STATUS_STARTED:
                print(
                    Logger(time.strftime(
                        LOG_TIME_FOEMAT,
                        time.localtime())).Warn('scheduler has been started!'))
                return CheckStatusError(
                    Logger(time.strftime(
                        LOG_TIME_FOEMAT,
                        time.localtime())).Warn('scheduler has been started!'))
        elif dstStatus == SCHED_STATUS_STARTING:
            if srcStatus == SCHED_STATUS_UNINITIALIZED:
                print(
                    Logger(time.strftime(
                        LOG_TIME_FOEMAT, time.localtime())).Warn(
                            'scheduler is being uninitialized!'))
                return CheckStatusError(
                    Logger(time.strftime(
                        LOG_TIME_FOEMAT, time.localtime())).Warn(
                            'scheduler is being uninitialized!'))
            elif srcStatus == SCHED_STATUS_STARTED:
                print(
                    Logger(time.strftime(
                        LOG_TIME_FOEMAT,
                        time.localtime())).Warn('scheduler has been started!'))
                return CheckStatusError(
                    Logger(time.strftime(
                        LOG_TIME_FOEMAT,
                        time.localtime())).Warn('scheduler has been started!'))
        elif dstStatus == SCHED_STATUS_STOPPING:
            if srcStatus != SCHED_STATUS_STARTED:
                print(
                    Logger(time.strftime(
                        LOG_TIME_FOEMAT,
                        time.localtime())).Warn('scheduler haven\'t start!'))
                return CheckStatusError(
                    Logger(time.strftime(
                        LOG_TIME_FOEMAT,
                        time.localtime())).Warn('scheduler haven\'t start!'))
        else:
            print(
                Logger(time.strftime(LOG_TIME_FOEMAT,
                                     time.localtime())).Warn('unknown status'))
            return CheckStatusError(
                Logger(time.strftime(LOG_TIME_FOEMAT,
                                     time.localtime())).Warn('unknown status'))
        return None
    finally:
        if lock:
            lock.release()
Beispiel #9
0
class Module(BasicModule):
    def __init__(self, mid, calculateCost):
        self.__mid = mid
        self.__calculateCost = calculateCost
        self.__calledCount = 0
        self.__acceptedCount = 0
        self.__completedCount = 0
        self.__handingCount = 0
        self.__rwLock = RWLock()

    def ID(self):
        return self.__mid

    def CalledCount(self):
        self.__rwLock.acquire_read()
        try:
            return self.__calledCount
        finally:
            self.__rwLock.release()

    def AcceptedCount(self):
        self.__rwLock.acquire_read()
        try:
            return self.__acceptedCount
        finally:
            self.__rwLock.release()

    def CompletedCount(self):
        self.__rwLock.acquire_read()
        try:
            return self.__completedCount
        finally:
            self.__rwLock.release()

    def HandingCount(self):
        self.__rwLock.acquire_read()
        try:
            return self.__handingCount
        finally:
            self.__rwLock.release()

    def CalculateCost(self):
        self.__rwLock.acquire_read()
        try:
            return self.__calculateCost
        finally:
            self.__rwLock.release()

    def IncrCalledCount(self):
        self.__rwLock.acquire_write()
        try:
            self.__calledCount += 1
        finally:
            self.__rwLock.release()

    def IncrAcceptedCount(self):
        self.__rwLock.acquire_write()
        try:
            self.__acceptedCount += 1
        finally:
            self.__rwLock.release()

    def IncrCompletedCount(self):
        self.__rwLock.acquire_write()
        try:
            self.__completedCount += 1
        finally:
            self.__rwLock.release()

    def IncrHandingCount(self):
        self.__rwLock.acquire_write()
        try:
            self.__handingCount += 1
        finally:
            self.__rwLock.release()

    def DecrHandingCount(self):
        self.__rwLock.acquire_write()
        try:
            self.__handingCount -= 1
        finally:
            self.__rwLock.release()

    def SetCalculateCost(self, calculateCost):
        self.__rwLock.acquire_write()
        try:
            self.__calculateCost = calculateCost
        finally:
            self.__rwLock.release()

    def GetCalculateCost(self):
        self.__rwLock.acquire_read()
        try:
            return self.__calculateCost
        finally:
            self.__rwLock.release()

    def Counts(self):
        pass

    def Clear(self):
        self.__rwLock.acquire_write()
        try:
            self.__calledCount = 0
            self.__acceptedCount = 0
            self.__completedCount = 0
            self.__handingCount = 0
            self.__calculateCost = 0
        finally:
            self.__rwLock.release()
Beispiel #10
0
class Pool:
    def __init__(self, bufCap, maxBufNumber):
        self.__bufCap = bufCap
        self.__maxBufNumber = maxBufNumber
        self.__bufNumber = 1
        self.__bufQueue = Queue(maxBufNumber)
        self.__closed = 0
        self.__rwlock = RWLock()
        self.__total = 0
        buf = Buffer(bufCap)
        self.__bufQueue.put_nowait(buf)

    def BufCap(self):
        return self.__bufCap

    def MaxBufNumber(self):
        return self.__maxBufNumber

    def BufNumber(self):
        return self.__bufNumber

    def Total(self):
        return self.__total

    def Put(self, data):
        if self.Closed():
            return BufferPoolClosedError('pool has been closed!')
        count = 0
        maxCount = self.BufNumber() * 5
        err = None
        ok = False
        while True:
            try:
                buf = self.__bufQueue.get_nowait()

                def putData(buf, data, maxCount):
                    ok = False
                    err = None
                    if self.Closed():
                        return ok, BufferPoolClosedError(
                            'pool has been closed!')
                    try:
                        ok, err = buf.Put(data)
                        if ok:
                            self.__rwlock.acquire_write()
                            try:
                                self.__total += 1
                                return ok, err
                            finally:
                                self.__rwlock.release()
                        if err:
                            return ok, err
                        # 缓冲器已满
                        nonlocal count
                        count += 1
                        if count >= maxCount and self.BufNumber(
                        ) < self.MaxBufNumber():
                            self.__rwlock.acquire_write()
                            if self.BufNumber() < self.MaxBufNumber():
                                if self.Closed():
                                    self.__rwlock.release()
                                    return ok, err
                                newBuffer = Buffer(self.BufCap())
                                newBuffer.Put(data)
                                self.__bufQueue.put_nowait(newBuffer)
                                self.__bufNumber += 1
                                self.__total += 1
                                ok = True
                            self.__rwlock.release()
                            count = 0
                    finally:
                        self.__rwlock.acquire_read()
                        if self.Closed():
                            self.__bufNumber -= 1
                            return ok, BufferPoolClosedError(
                                'pool has been closed!')
                        else:
                            self.__bufQueue.put_nowait(buf)
                        self.__rwlock.release()
                        return ok, err

                ok, err = putData(buf, data, maxCount)
                if ok or err is not None:
                    break
            except Empty:
                continue
        return err

    def Get(self):
        err = None
        data = None
        if self.Closed():
            return data, BufferPoolClosedError('pool has been closed!')
        count = 0
        maxCount = self.BufNumber() * 10
        while True:
            try:
                buf = self.__bufQueue.get_nowait()

                def getData(buf, maxCount):
                    data = None
                    err = None
                    nonlocal count
                    try:
                        if self.Closed():
                            return data, BufferPoolClosedError(
                                'pool has been closed!')
                        data, err = buf.Get()
                        if data is not None:
                            self.__rwlock.acquire_write()
                            self.__total -= 1
                            self.__rwlock.release()
                            return data, err
                        if err:
                            return data, err
                        count += 1
                        return data, err
                    finally:
                        if count >= maxCount and buf.Len(
                        ) == 0 and self.BufNumber() > 1:
                            buf.Close()
                            self.__rwlock.acquire_write()
                            self.__bufNumber -= 1
                            self.__rwlock.release()
                            count = 0
                            return data, err
                        self.__rwlock.acquire_read()
                        if self.Closed():
                            self.__bufNumber -= 1
                            err = BufferPoolClosedError(
                                'buffer pool is closed!')
                        else:
                            self.__bufQueue.put_nowait(buf)
                        self.__rwlock.release()

                data, err = getData(buf, maxCount)
                if data is not None or err is not None:
                    break
            except Empty:
                break
        return data, err

    def Close(self):
        self.__rwlock.acquire_write()
        try:
            if self.__closed == 0:
                self.__closed = 1
                while True:
                    try:
                        buffer = self.__bufQueue.get_nowait()
                        buffer.Close()
                    except Empty:
                        break
                self.__bufQueue = None
                return True
            return False
        finally:
            self.__rwlock.release()

    def Closed(self):
        return self.__closed == 1 or self.__bufQueue is None
Beispiel #11
0
 def __init__(self, start, max):
     self.__start = start
     self.__max = max if max != 0 else sys.maxsize
     self.__next = start
     self.__rwLock = RWLock()
Beispiel #12
0
class SNGenerator(SequenceNumberGenerator):
    def __init__(self, start, max):
        self.__start = start
        self.__max = max if max != 0 else sys.maxsize
        self.__next = start
        self.__rwLock = RWLock()

    def Start(self):
        return self.__start

    def Max(self):
        return self.__max

    def Next(self):
        self.__rwLock.acquire_read()
        try:
            return self.__next
        finally:
            self.__rwLock.release()

    def CycleCount(self):
        self.__rwLock.acquire_read()
        try:
            return self.__cycleCount
        finally:
            self.__rwLock.release()

    def Get(self):
        self.__rwLock.acquire_read()
        try:
            sn = self.__next
            if sn == self.__max:
                self.__next = self.__start
                self.__cycleCount += 1
            else:
                self.__next += 1
            return sn
        finally:
            self.__rwLock.release()
Beispiel #13
0
 def __init__(self, mid, calculateCost):
     super(Auth, self).__init__(mid, calculateCost)
     self.__rwLock = RWLock()
     self.__accountDict = {}
     self.__validatorDict = {}
Beispiel #14
0
class Auth(Module):
    def __init__(self, mid, calculateCost):
        super(Auth, self).__init__(mid, calculateCost)
        self.__rwLock = RWLock()
        self.__accountDict = {}
        self.__validatorDict = {}

    def __addAccount(self, account):
        self.IncrHandingCount()
        self.IncrCalledCount()
        self.__rwLock.acquire_write()
        try:
            ok, err = checkAccount(account)
            if ok is True:
                self.__accountDict[account.getAddress()] = account
                self.IncrAcceptedCount()
                return True, None
            return False, err
        finally:
            self.DecrHandingCount()
            self.__rwLock.release()

    def DelAccount(self, account):
        self.IncrHandingCount()
        self.IncrCalledCount()
        self.__rwLock.acquire_write()
        try:
            account = Account(account)
            del self.__accountDict[account.getAddress()]
            self.IncrAcceptedCount()
        finally:
            self.DecrHandingCount()
            self.__rwLock.release()

    def __addAccountList(self, accountList):
        for account in accountList:
            self.__addAccount(account)

    def Add(self, accounts):
        if isinstance(accounts, list):
            self.__addAccountList(accounts)
        elif isinstance(accounts, Account):
            self.__addAccount(accounts)

    def GetAccountDict(self):
        self.IncrHandingCount()
        self.IncrCalledCount()
        try:
            self.IncrAcceptedCount()
            return self.__accountDict
        finally:
            self.DecrHandingCount()

    def AddValidatorSet(self, validators):
        self.IncrHandingCount()
        self.IncrCalledCount()
        self.__rwLock.acquire_write()
        try:
            for validator in validators:
                self.__validatorDict[validator.operatorAddr] = validator
            self.IncrAcceptedCount()
        finally:
            self.DecrHandingCount()
            self.__rwLock.release()

    def GetValidatorDict(self):
        self.IncrHandingCount()
        self.IncrCalledCount()
        try:
            self.IncrAcceptedCount()
            return self.__validatorDict
        finally:
            self.DecrHandingCount()
Beispiel #15
0
class Scheduler(Schedule):
    def __init__(self):
        self.__status = SCHED_STATUS_UNINITIALIZED
        self.__rwlock = RWLock()
        self.__registrar = None
        self.__bankerBufferPool = None
        self.__signBufferPool = None
        self.__broadcastBufferPool = None
        self.__stakingBufferPool = None
        self.__distributionBufferPool = None
        self.__errorBufferPool = None
        self.__auth = None
        self.__node = 'tcp://172.38.8.89:26657'

    def Init(self, modulesArgs, poolArgs, node):
        err = None
        srcStatus = self.__status
        dstStatus = SCHED_STATUS_INITIALIZING
        try:
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Info('Initialized scheduler ...'))
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Info('check scheduler status...'))
            err = CheckStatus(srcStatus, dstStatus)
            if err is not None:
                return err
            self.SetStatus(dstStatus)
            err = modulesArgs.Check()
            if err is not None:
                return err
            print(
                Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Info(
                    'scheduler is being initialing...'))
            if self.__registrar is None:
                self.__registrar = Registrar()
            else:
                self.__registrar.Clear()
            self.__initBufferPool(poolArgs)
            err = self.RegisterModules(modulesArgs)
            if err is not None:
                return err
            if node is not None:
                self.__node = node
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Info('scheduler has been initialized'))
            return err
        finally:
            if err is not None:
                self.SetStatus(srcStatus)
            else:
                self.SetStatus(SCHED_STATUS_INITIALIZED)

    def Start(self, auth):
        err = None
        srcStatus = self.__status
        dstStatus = SCHED_STATUS_STARTING
        try:
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Info('Start scheduler ...'))
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Info('Check scheduler status...'))
            err = CheckStatus(srcStatus, dstStatus)
            if err is not None:
                return err
            self.SetStatus(dstStatus)
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Info('Check auth module...'))
            if isinstance(auth, Auth) is False:
                return err
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Info('Auth module is valid...'))
            self.__auth = auth
            err = self.__checkBufferPool()
            if err is not None:
                return err
            self.randomTx(threading.currentThread())
            self.transfer(threading.currentThread())
            self.sign(threading.currentThread())
            self.broadcast(threading.currentThread())
            self.staking(threading.currentThread())
            self.distribution(threading.currentThread())
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Info('Scheduler has been started.'))
            return err
        finally:
            if err is not None:
                self.SetStatus(srcStatus)
            else:
                self.SetStatus(SCHED_STATUS_STARTED)

    def Stop(self):
        err = None
        srcStatus = self.__status
        dstStatus = SCHED_STATUS_STOPPING
        try:
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Info('Stop scheduler ...'))
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Info('Check scheduler status...'))
            err = CheckStatus(srcStatus, dstStatus)
            if err is not None:
                return err
            self.SetStatus(dstStatus)
            self.__bankerBufferPool.Close()
            self.__signBufferPool.Close()
            self.__broadcastBufferPool.Close()
            self.__stakingBufferPool.Close()
            self.__errorBufferPool.Close()
            self.__distributionBufferPool.Close()
            print(
                Logger(time.strftime(
                    LOG_TIME_FOEMAT,
                    time.localtime())).Info('Scheduler has been stopped.'))
            return err
        finally:
            if err is not None:
                self.SetStatus(srcStatus)
            else:
                self.SetStatus(SCHED_STATUS_STOPPED)

    def Status(self):
        self.__rwlock.acquire_read()
        try:
            return self.__status
        finally:
            self.__rwlock.release()

    def ErrorQueue(self, schedulerThread):
        errorBufPool = self.__errorBufferPool
        errorQueue = Queue(errorBufPool.BufCap)

        def func(errorBufPool, errorQueue):
            while True:
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'current threading is %s' %
                                             (threading.currentThread())))
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'scheduler thread %s is alive' %
                                             (schedulerThread.getName())))
                data, err = self.__errorBufferPool.Get()
                if err is not None:
                    print(
                        Logger(time.strftime(
                            LOG_TIME_FOEMAT, time.localtime())).Warn(
                                'the error pool is closed, break'))
                    break
                errorQueue.put_nowait(data)

        thread = threading.Thread(target=func(errorBufPool, errorQueue))
        thread.start()
        return errorQueue

    def SetStatus(self, dstStatus):
        self.__rwlock.acquire_write()
        try:
            self.__status = dstStatus
        finally:
            self.__rwlock.release()

    # register module
    def RegisterModules(self, modules):
        for banker in modules.Bankers:
            if banker is None:
                continue
            ok, err = self.__registrar.Register(banker)
            if err is not None:
                return err
            if ok is False:
                continue
        print(
            Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Info(
                'banker modules register successfully!'))
        for signer in modules.Signers:
            if signer is None:
                continue
            ok, err = self.__registrar.Register(signer)
            if err is not None:
                return err
            if ok is False:
                continue
        print(
            Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Info(
                'signer modules register successfully!'))
        for broadcaster in modules.Broadcasters:
            if broadcaster is None:
                continue
            ok, err = self.__registrar.Register(broadcaster)
            if err is not None:
                return err
            if ok is False:
                continue
        print(
            Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Info(
                'broadcaster modules register successfully!'))
        for staking in modules.Stakings:
            if staking is None:
                continue
            ok, err = self.__registrar.Register(staking)
            if err is not None:
                return err
            if ok is False:
                continue
        print(
            Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Info(
                'staking modules register successfully!'))
        for distributor in modules.Distributors:
            if distributor is None:
                continue
            ok, err = self.__registrar.Register(distributor)
            if err is not None:
                return err
            if ok is False:
                continue
        print(
            Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Info(
                'distribution modules register successfully!'))

    def __initBufferPool(self, poolArgs):
        # init banker buffer pool
        if self.__bankerBufferPool is not None and self.__bankerBufferPool.Closed(
        ) is False:
            self.__bankerBufferPool.Close()
        self.__bankerBufferPool = Pool(poolArgs.BankerBufCap,
                                       poolArgs.BankerMaxBufNumber)
        print(
            Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Info(
                'banker buffer pool built by bufCap = %s and maxBufNumber = %s'
                % (poolArgs.BankerBufCap, poolArgs.BankerMaxBufNumber)))
        # init signer buffer pool
        if self.__signBufferPool is not None and self.__signBufferPool.Closed(
        ) is False:
            self.__signBufferPool.Close()
        self.__signBufferPool = Pool(poolArgs.SignerBufCap,
                                     poolArgs.SignerBufMaxNumber)
        print(
            Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Info(
                'signer buffer pool built by bufCap = %s and maxBufNumber = %s'
                % (poolArgs.SignerBufCap, poolArgs.SignerBufMaxNumber)))
        # init broadcaster buffer pool
        if self.__broadcastBufferPool is not None and self.__broadcastBufferPool.Closed(
        ) is False:
            self.__broadcastBufferPool.Close()
        self.__broadcastBufferPool = Pool(poolArgs.BroadcasterBufCap,
                                          poolArgs.BroadcasterMaxNumber)
        print(
            Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Info(
                'broadcaster buffer pool built by bufCap = %s and maxBufNumber = %s'
                % (poolArgs.BroadcasterBufCap, poolArgs.BroadcasterMaxNumber)))
        # init staking buffer pool
        if self.__stakingBufferPool is not None and self.__stakingBufferPool.Closed(
        ) is False:
            self.__stakingBufferPool.Close()
        self.__stakingBufferPool = Pool(poolArgs.StakingBufCap,
                                        poolArgs.StakingMaxNumber)
        print(
            Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Info(
                'staking buffer pool built by bufCap = %s and maxBufNumber = %s'
                % (poolArgs.StakingBufCap, poolArgs.StakingMaxNumber)))
        # init distribution buffer pool
        if self.__distributionBufferPool is not None and self.__distributionBufferPool.Closed(
        ) is False:
            self.__distributionBufferPool.Close()
        self.__distributionBufferPool = Pool(poolArgs.DistributionBufCap,
                                             poolArgs.DistributionMaxNumber)
        print(
            Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Info(
                'distribution buffer pool built by bufCap = %s and maxBufNumber = %s'
                %
                (poolArgs.DistributionBufCap, poolArgs.DistributionMaxNumber)))
        # init error buffer pool
        if self.__errorBufferPool is not None and self.__errorBufferPool.Closed(
        ) is False:
            self.__errorBufferPool.Close()
        self.__errorBufferPool = Pool(poolArgs.BroadcasterBufCap,
                                      poolArgs.BroadcasterMaxNumber)
        print(
            Logger(time.strftime(LOG_TIME_FOEMAT, time.localtime())).Info(
                'error buffer pool built by bufCap = %s and maxBufNumber = %s'
                % (poolArgs.ErrorBufCap, poolArgs.ErrorMaxNumber)))

    def __checkBufferPool(self):
        if self.__bankerBufferPool is None:
            return CheckBufferPoolError('empty banker buffer pool')
        if self.__bankerBufferPool is not None and self.__bankerBufferPool.Closed(
        ):
            self.__bankerBufferPool = Pool(
                self.__bankerBufferPool.BufCap,
                self.__bankerBufferPool.MaxBufNumber)
        if self.__signBufferPool is None:
            return CheckBufferPoolError('empty sign buffer pool')
        if self.__signBufferPool is not None and self.__signBufferPool.Closed(
        ):
            self.__signBufferPool = Pool(self.__signBufferPool.BufCap,
                                         self.__signBufferPool.MaxBufNumber)
        if self.__broadcastBufferPool is None:
            return CheckBufferPoolError('empty broadcast buffer pool')
        if self.__broadcastBufferPool is not None and self.__broadcastBufferPool.Closed(
        ):
            self.__broadcastBufferPool = Pool(
                self.__broadcastBufferPool.BufCap,
                self.__broadcastBufferPool.MaxBufNumber)
        if self.__errorBufferPool is None:
            return CheckBufferPoolError('empty error buffer pool')
        if self.__errorBufferPool is not None and self.__errorBufferPool.Closed(
        ):
            self.__errorBufferPool = Pool(self.__errorBufferPool.BufCap,
                                          self.__errorBufferPool.MaxBufNumber)
        if self.__stakingBufferPool is None:
            return CheckBufferPoolError('empty staking buffer pool')
        if self.__stakingBufferPool is not None and self.__stakingBufferPool.Closed(
        ):
            self.__stakingBufferPool = Pool(
                self.__stakingBufferPool.BufCap,
                self.__stakingBufferPool.MaxBufNumber)
        if self.__distributionBufferPool is None:
            return CheckBufferPoolError('empty distribution buffer pool')
        if self.__distributionBufferPool is not None and self.__distributionBufferPool.Closed(
        ):
            self.__distributionBufferPool = Pool(
                self.__distributionBufferPool.BufCap,
                self.__distributionBufferPool.MaxBufNumber)

    def randomTx(self, schedulerThread):
        def func():
            while True:
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'current threading is %s' %
                                             (threading.currentThread())))
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'scheduler thread %s is alive' %
                                             (schedulerThread.getName())))
                accountList = list(self.__auth.GetAccountDict().values())
                validatorList = list(self.__auth.GetValidatorDict().values())
                seed = random.randint(0, 10)
                if seed % 2 == 0:
                    self.randomSendTx(accountList)
                elif seed % 3 == 0:
                    self.randomWithdrawDelegatorOneRewardTx(
                        accountList, validatorList)
                else:
                    self.randomDelegateTx(accountList, validatorList)
                time.sleep(random.randint(1, 10))

        thread = threading.Thread(target=func)
        thread.name = 'randomTx'
        thread.start()

    def transfer(self, schedulerThread):
        def func():
            while True:
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'current threading is %s' %
                                             (threading.currentThread())))
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'scheduler thread %s is alive' %
                                             (schedulerThread.getName())))
                data, err = self.__bankerBufferPool.Get()
                if err is not None:
                    print(
                        Logger(time.strftime(
                            LOG_TIME_FOEMAT, time.localtime())).Warn(
                                'the banker pool is closed, break'))
                    break
                self.transferOne(data)

        thread = threading.Thread(target=func)
        thread.name = 'transfer'
        thread.start()

    def transferOne(self, data):
        if data is None or isinstance(
                data, SendCoinArgs) is False or data.Check() is not None:
            return
        banker, err = self.__registrar.Get(TYPE_BANK)
        if banker is None or err is not None:
            errMsg = 'could not get a banker: %s' % (err)
            sendError(TxerError(ERROR_BANKER, errMsg), '',
                      self.__errorBufferPool)
            sendBank(data)
            return
        ok = isinstance(banker, Banker)
        if ok is False:
            errMsg = 'incorrect downloader type: ID: %s' % (banker.ID())
            sendError(TxerError(ERROR_BANKER, errMsg), banker.ID(),
                      self.__errorBufferPool)
            sendBank(data)
            return
        sendedTxJsonFilePath, err = banker.SendCoins(data.srcAccount,
                                                     data.dstAccount,
                                                     data.coins, data.fees,
                                                     data.gas, data.gasAdjust)
        if sendedTxJsonFilePath is not None:
            sendSignData = SendSignArgs(data.srcAccount, sendedTxJsonFilePath,
                                        self.__node)
            sendSign(sendSignData, self.__signBufferPool)
        if err is not None:
            sendError(err, banker.ID(), self.__errorBufferPool)

    def sign(self, schedulerThread):
        def func():
            while True:
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'current threading is %s' %
                                             (threading.currentThread())))
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'scheduler thread %s is alive' %
                                             (schedulerThread.getName())))
                data, err = self.__signBufferPool.Get()
                if err is not None:
                    print(
                        Logger(time.strftime(
                            LOG_TIME_FOEMAT, time.localtime())).Warn(
                                'the sign pool is closed, break'))
                    break
                self.signOne(data)

        thread = threading.Thread(target=func)
        thread.name = 'sign'
        thread.start()

    def signOne(self, data):
        if data is None or isinstance(
                data, SendSignArgs) is False or data.Check() is not None:
            return
        signer, err = self.__registrar.Get(TYPE_SIGN)
        if signer is None or err is not None:
            errMsg = 'could not get a signer: %s' % (err)
            sendError(TxerError(ERROR_BANKER, errMsg), '',
                      self.__errorBufferPool)
            sendSign(data, self.__signBufferPool)
            return
        ok = isinstance(signer, Signer)
        if ok is False:
            errMsg = 'incorrect downloader type: ID: %s' % (signer.ID())
            sendError(TxerError(ERROR_SIGNER, errMsg), signer.ID(),
                      self.__errorBufferPool)
            sendSign(data, self.__signBufferPool)
            return
        signedData, err = signer.Sign(data.srcAccount, data.sendedJsonFilePath,
                                      data.node)
        if signedData is not None:
            sendBroadcastData = SendBroadcastArgs(data.srcAccount,
                                                  signedData)  # mode 为默认
            sendBroadcast(sendBroadcastData, self.__broadcastBufferPool)
        if err is not None:
            sendError(err, signer.ID(), self.__errorBufferPool)

    def broadcast(self, schedulerThread):
        def func():
            while True:
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'current threading is %s' %
                                             (threading.currentThread())))
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'scheduler thread %s is alive' %
                                             (schedulerThread.getName())))
                broadcastData, err = self.__broadcastBufferPool.Get()
                if err is not None:
                    print(
                        Logger(time.strftime(
                            LOG_TIME_FOEMAT, time.localtime())).Warn(
                                'the broadcast pool is closed, break'))
                    break
                self.broadcastOne(broadcastData)

        thread = threading.Thread(target=func)
        thread.name = 'broadcast'
        thread.start()

    def broadcastOne(self, broadcastData):
        if broadcastData is None or isinstance(
                broadcastData, SendBroadcastArgs
        ) is False or broadcastData.Check() is not None:
            return
        broadcaster, err = self.__registrar.Get(TYPE_BROADCAST)
        if broadcaster is None or err is not None:
            errMsg = 'could not get a broadcaster: %s' % (err)
            sendError(TxerError(ERROR_BROADCASTER, errMsg), '',
                      self.__errorBufferPool)
            sendBroadcast(broadcastData, self.__signBufferPool)
            return
        ok = isinstance(broadcaster, BroadCaster)
        if ok is False:
            errMsg = 'incorrect downloader type: ID: %s' % (broadcaster.ID())
            sendError(TxerError(ERROR_SIGNER, errMsg), broadcaster.ID(),
                      self.__errorBufferPool)
            sendBroadcast(broadcastData, self.__signBufferPool)
            return
        broadcastedData, err = broadcaster.BroadCastTx(broadcastData.body,
                                                       broadcastData.mode)
        if broadcastData is not None:
            saveTxRecord(broadcastData.srcAccount, broadcastedData)  # 保存交易记录
        if err is not None:
            sendError(err, broadcaster.ID(), self.__errorBufferPool)

    def staking(self, schedulerThread):
        def func():
            while True:
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'current threading is %s' %
                                             (threading.currentThread())))
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'scheduler thread %s is alive' %
                                             (schedulerThread.getName())))
                stakingData, err = self.__stakingBufferPool.Get()
                if err is not None:
                    print(
                        Logger(time.strftime(
                            LOG_TIME_FOEMAT, time.localtime())).Warn(
                                'the stakinger pool is closed, break'))
                    break
                # include delegate, redelegate and unbondingDelegate
                if stakingData and stakingData.getType() == 'delegate':
                    self.delegateOne(stakingData.getData())

        thread = threading.Thread(target=func)
        thread.name = 'staking'
        thread.start()

    def delegateOne(self, delegateData):
        if delegateData is None or isinstance(
                delegateData,
                DelegateArgs) is False or delegateData.Check() is not None:
            return
        stakinger, err = self.__registrar.Get(TYPE_STAKING)
        if stakinger is None or err is not None:
            errMsg = 'could not get a stakinger: %s' % (err)
            sendError(TxerError(ERROR_STAKINGER, errMsg), '',
                      self.__errorBufferPool)
            stakingArgs = StakingArgs('delegate', delegateData)
            sendStaking(stakingArgs)
            return
        ok = isinstance(stakinger, Stakinger)
        if ok is False:
            errMsg = 'incorrect stakinger type: ID: %s' % (stakinger.ID())
            sendError(TxerError(ERROR_STAKINGER, errMsg), stakinger.ID(),
                      self.__errorBufferPool)
            stakingArgs = StakingArgs('delegate', delegateData)
            sendStaking(stakingArgs)
            return
        delegateTxJsonFilePath, err = stakinger.Delegate(
            delegateData.delegator, delegateData.validator, delegateData.coin,
            delegateData.fees, delegateData.gas, delegateData.gasAdjust)
        if delegateTxJsonFilePath is not None:
            sendSignData = SendSignArgs(delegateData.delegator,
                                        delegateTxJsonFilePath, self.__node)
            sendSign(sendSignData, self.__signBufferPool)
        if err is not None:
            sendError(err, stakinger.ID(), self.__errorBufferPool)

    def distribution(self, schedulerThread):
        def func():
            while True:
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'current threading is %s' %
                                             (threading.currentThread())))
                print(
                    Logger(time.strftime(LOG_TIME_FOEMAT,
                                         time.localtime())).Info(
                                             'scheduler thread %s is alive' %
                                             (schedulerThread.getName())))
                distributionData, err = self.__distributionBufferPool.Get()
                if err is not None:
                    print(
                        Logger(time.strftime(
                            LOG_TIME_FOEMAT, time.localtime())).Warn(
                                'the distribution pool is closed, break'))
                    break
                # include withdrawDelegatorOneReward, withdrawDelegatorAllReward
                if distributionData and distributionData.getType(
                ) == 'withdrawDelegatorOneReward':
                    self.withdrawDelegatorOneReward(distributionData.getData())

        thread = threading.Thread(target=func)
        thread.name = 'distribution'
        thread.start()

    def withdrawDelegatorOneReward(self, withdrawRewardData):
        if withdrawRewardData is None or isinstance(
                withdrawRewardData, WithdrawDelegatorOneRewardArgs
        ) is False or withdrawRewardData.Check() is not None:
            return
        distributor, err = self.__registrar.Get(TYPE_DISTRIBUTION)
        if distributor is None or err is not None:
            errMsg = 'could not get a distributor: %s' % (err)
            sendError(TxerError(ERROR_DISTRIBUTION, errMsg), '',
                      self.__errorBufferPool)
            distributionArgs = DistributionArgs('withdrawDelegatorOneReward',
                                                withdrawRewardData)
            sendDistribution(distributionArgs)
            return
        ok = isinstance(distributor, Distributor)
        if ok is False:
            errMsg = 'incorrect distributor type: ID: %s' % (distributor.ID())
            sendError(TxerError(ERROR_DISTRIBUTION, errMsg), distributor.ID(),
                      self.__errorBufferPool)
            distributionArgs = DistributionArgs('withdrawDelegatorOneReward',
                                                withdrawRewardData)
            sendDistribution(distributionArgs)
            return
        distributionTxJsonFilePath, err = distributor.WithdrawDelegatorOneReward(
            withdrawRewardData.delegator, withdrawRewardData.validator,
            withdrawRewardData.fees, withdrawRewardData.gas,
            withdrawRewardData.gasAdjust)
        if distributionTxJsonFilePath is not None:
            sendSignData = SendSignArgs(withdrawRewardData.delegator,
                                        distributionTxJsonFilePath,
                                        self.__node)
            sendSign(sendSignData, self.__signBufferPool)
        if err is not None:
            print(
                Logger(time.strftime(LOG_TIME_FOEMAT,
                                     time.localtime())).Warn(err.msg))
            sendError(err, distributor.ID(), self.__errorBufferPool)

    def randomSendTx(self, accountList):
        length = len(accountList)
        if length == 0:
            return
        random0 = random.randint(0, length - 1)
        random1 = random.randint(0, length - 1)
        if random0 == random1:
            return
        srcAccount = accountList[random0]
        dstAccount = accountList[random1]
        if srcAccount is None:
            return
        if dstAccount is None:
            return
        sendCoins = [{'denom': 'hsn', 'amount': str(random.randint(0, 100))}]
        fees = [{'denom': 'hsn', 'amount': str(random.randint(1, 10))}]
        gasList = ['100000', '200000', '150000']
        randomGas = random.randint(0, 2)
        gas = gasList[randomGas]
        gasAdjustList = ['1.0', '1.1', '1.2', '1.3', '1.4', '1.5']
        randomGasAdjust = random.randint(0, 5)
        gasAdjust = gasAdjustList[randomGasAdjust]
        sendCoinTx = SendCoinArgs(srcAccount, dstAccount, sendCoins, fees, gas,
                                  gasAdjust)
        sendBank(sendCoinTx, self.__bankerBufferPool)

    def randomDelegateTx(self, delegatorList, validatorList):
        length0 = len(delegatorList)
        length1 = len(validatorList)
        if length0 == 0 or length1 == 0:
            return
        random0 = random.randint(0, length0 - 1)
        random1 = random.randint(0, length1 - 1)
        delegator = delegatorList[random0]
        validator = validatorList[random1]
        if delegator is None:
            return
        if validator is None:
            return
        delegateCoin = {'denom': 'hsn', 'amount': str(random.randint(0, 10))}
        fees = [{'denom': 'hsn', 'amount': str(random.randint(1, 10))}]
        gasList = ['100000', '200000', '150000']
        randomGas = random.randint(0, 2)
        gas = gasList[randomGas]
        gasAdjustList = ['1.0', '1.1', '1.2', '1.3', '1.4', '1.5']
        randomGasAdjust = random.randint(0, 5)
        gasAdjust = gasAdjustList[randomGasAdjust]
        delegateTx = DelegateArgs(delegator, validator, delegateCoin, fees,
                                  gas, gasAdjust)
        stakingArgs = StakingArgs('delegate', delegateTx)
        sendStaking(stakingArgs, self.__stakingBufferPool)

    def randomWithdrawDelegatorOneRewardTx(self, delegatorList, validatorList):
        length0 = len(delegatorList)
        length1 = len(validatorList)
        if length0 == 0 or length1 == 0:
            return
        random0 = random.randint(0, length0 - 1)
        random1 = random.randint(0, length1 - 1)
        delegator = delegatorList[random0]
        validator = validatorList[random1]
        if delegator is None:
            return
        if validator is None:
            return
        fees = [{'denom': 'hsn', 'amount': str(random.randint(1, 10))}]
        gasList = ['100000', '200000', '150000']
        randomGas = random.randint(0, 2)
        gas = gasList[randomGas]
        gasAdjustList = ['1.0', '1.1', '1.2', '1.3', '1.4', '1.5']
        randomGasAdjust = random.randint(0, 5)
        gasAdjust = gasAdjustList[randomGasAdjust]
        withdrawDelegatorOneRewardArgs = WithdrawDelegatorOneRewardArgs(
            delegator, validator, fees, gas, gasAdjust)
        distributionArgs = DistributionArgs('withdrawDelegatorOneReward',
                                            withdrawDelegatorOneRewardArgs)
        sendDistribution(distributionArgs, self.__distributionBufferPool)