Exemple #1
0
def UpdateAuthTimeHuman(client_id,balance,sock_):
    from SBPS import InternalMessage
    InternalMessage.RegistFilter(InternalMessage.TTYPE_HUMAN,client_id)
    with session_scope() as session :
        session.query(SBDB_ORM.Client).filter(SBDB_ORM.Client.id==client_id).update({SBDB_ORM.Client.dt_auth:datetime.datetime.now()})
        session.commit()
    InternalMessage.NotifyTerminalStatus(InternalMessage.TTYPE_HUMAN, client_id, sock_, InternalMessage.OPER_ONLINE,balance)
    def connectionLost(self, reason=twistedError.ConnectionDone):

        if self.role == Command.BaseCommand.PV_ROLE_HUMAN:
            InternalMessage.UnregistFilter(InternalMessage.TTYPE_HUMAN,
                                           self.client_id)
            InternalMessage.NotifyTerminalStatus(InternalMessage.TTYPE_HUMAN,
                                                 self.client_id,
                                                 id(self.transport),
                                                 InternalMessage.OPER_OFFLINE,
                                                 'n')
        elif self.role == Command.BaseCommand.PV_ROLE_RELAYER:
            InternalMessage.UnregistFilter(InternalMessage.TTYPE_GATEWAY,
                                           self.relayer_id)
            InternalMessage.NotifyTerminalStatus(InternalMessage.TTYPE_GATEWAY,
                                                 self.relayer_id, 0,
                                                 InternalMessage.OPER_OFFLINE)

        try:
            self.timer.cancel()
        except Exception:
            pass
        #print ("connection lost:",id(self.transport),reason)
        self.releaseFromDict()

        with self.factory.lockPendingCmd:
            SBProtocol.connection_count = SBProtocol.connection_count - 1
        Protocol.connectionLost(self, reason=reason)
Exemple #3
0
def UpdateAuthTimeRelayer(relayer_id):
    from SBPS import InternalMessage
    InternalMessage.RegistFilter(InternalMessage.TTYPE_GATEWAY,relayer_id)
    with session_scope() as session :
        session.query(SBDB_ORM.Relayer).filter(SBDB_ORM.Relayer.id==relayer_id).update({SBDB_ORM.Relayer.dt_auth:datetime.datetime.now(),SBDB_ORM.Relayer.server_id:InternalMessage.MyServerID})
        session.commit()
    InternalMessage.NotifyTerminalStatus(InternalMessage.TTYPE_GATEWAY, relayer_id, 0, InternalMessage.OPER_ONLINE)
Exemple #4
0
def UpdateActiveTime(role_session, terminal_id,sock_=0):
    from Command import BaseCommand
    from SBPS import InternalMessage
    with session_scope() as session :
        if role_session==BaseCommand.PV_ROLE_HUMAN:
            session.query(SBDB_ORM.Client).filter(SBDB_ORM.Client.id==terminal_id).update({SBDB_ORM.Client.dt_active:datetime.datetime.now()})
            session.commit()
            InternalMessage.NotifyTerminalStatus(InternalMessage.TTYPE_HUMAN, terminal_id, sock_, InternalMessage.OPER_ONLINE)
        elif role_session==BaseCommand.PV_ROLE_RELAYER:
            session.query(SBDB_ORM.Relayer).filter(SBDB_ORM.Relayer.id==terminal_id).update({SBDB_ORM.Relayer.dt_active:datetime.datetime.now()})
            session.commit()
            InternalMessage.NotifyTerminalStatus(InternalMessage.TTYPE_GATEWAY, terminal_id, 0, InternalMessage.OPER_ONLINE)
def Run(withListen=True):

    global instance_SBProtocolFactory, bReactorStopped
    instance_SBProtocolFactory = SBProtocolFactory()

    InternalMessage.protocolInternal = SBProtocol()
    InternalMessage.protocolInternal.role = "internal"
    InternalMessage.protocolInternal.factory = instance_SBProtocolFactory
    threading.Thread(target=InternalMessage.traced_Run, daemon=True).start()

    if withListen:
        reactor.listenTCP(9630,
                          instance_SBProtocolFactory)  # @UndefinedVariable

        cert = None
        with open(
                os.path.join(os.path.dirname(os.path.abspath(__file__)),
                             os.path.pardir, 'server.pem')) as keyAndCert:
            cert = ssl.PrivateCertificate.loadPEM(keyAndCert.read())
        reactor.listenSSL(9631, instance_SBProtocolFactory, cert.options())
        reactor.listenTCP(9632, EchoProtocolFactory())  # @UndefinedVariable

    try:
        reactor.run()  # @UndefinedVariable
    except:
        pass
    InternalMessage.Stop()
    bReactorStopped = True
    try:
        # reactor.stop()
        import sys
        sys.exit(0)
    except:
        pass
Exemple #6
0
    def Finish(self, status, resp_body=None):
        resp = self.GetResp()
        resp.command_status = status
        if resp_body: resp.body = resp_body

        if self.protocol.role == BaseCommand.PV_ROLE_HUMAN:
            with self.protocol.lock_dictWaitResp:
                me = self.protocol.dictWaitResp.pop(self.command_seq, None)
                if me is None:
                    return  #this request has been feedbacked or timeout

            resp.Send()

        elif self.protocol.role == BaseCommand.PV_ROLE_INTERNAL:
            sb_protocol = None
            with self.protocol.factory.lockDict:
                sb_protocol = self.protocol.factory.dictRelayer.get(
                    self.internalMessage.destId)

            if sb_protocol:
                with sb_protocol.lock_dictWaitResp:
                    me = sb_protocol.dictWaitResp.pop(self.command_seq, None)
                    if me is None:
                        return  #this request has been feedbacked or timeout

            interMessage = InternalMessage.CInternalMessage()
            interMessage.SetParam(
                self.internalMessage.fromType, self.internalMessage.fromId,
                self.internalMessage.fromSock, InternalMessage.OPER_RESPONSE,
                "", self.internalMessage.destType, self.internalMessage.destId,
                self.internalMessage.destSock)

            resp.Send(interMessage)
Exemple #7
0
    def Run(self):
        if self.protocol.role == BaseCommand.PV_ROLE_INTERNAL:
            account_protocol = self.protocol.factory.GetAccountProtocol(
                relayer_id=self.internalMessage.fromId,
                client_id=self.internalMessage.destId)
            if account_protocol is None: return

            with account_protocol.lock_dictWaitResp:
                request = account_protocol.dictWaitResp.pop(
                    self.command_seq, None)
                if request is None:
                    return  #this request has been feedbacked or timeout
            self.Send()
        else:
            with self.protocol.lock_dictWaitResp:
                request = self.protocol.dictWaitResp.pop(
                    self.command_seq, None)
                if request is None:
                    return  #this request has been feedbacked or timeout

            interMessage = InternalMessage.CInternalMessage()
            interMessage.SetParam(request.internalMessage.fromType,
                                  request.internalMessage.fromId,
                                  request.internalMessage.fromSock,
                                  InternalMessage.OPER_RESPONSE, "",
                                  request.internalMessage.destType,
                                  request.internalMessage.destId,
                                  request.internalMessage.destSock)
            self.Send(interMessage)
Exemple #8
0
    def Feedback(self):
        if self.bFinished:
            return
        respond = self.GetResp()
        #         if self.protocol is not None and self.protocol.role ==BaseCommand.PV_ROLE_HUMAN:
        if self.protocol is not None:
            result = -1
            # if one control command fail, the total command fail
            for relayer_id in self.dictFinishedRelayerControls.keys():
                for dc in self.dictFinishedRelayerControls[relayer_id]:
                    if result <= 0:
                        result = dc.result
                        respond.body = dc.body

            if result == -1:
                respond.SetErrorCode(BaseCommand.CS_RELAYEROFFLINE)
            else:
                respond.SetErrorCode(result)

            interMessage = None
            if self.protocol.role == BaseCommand.PV_ROLE_INTERNAL:
                interMessage = InternalMessage.CInternalMessage()
                interMessage.SetParam(self.internalMessage.fromType,
                                      self.internalMessage.fromId,
                                      self.internalMessage.fromSock,
                                      InternalMessage.OPER_RESPONSE, "",
                                      self.internalMessage.destType,
                                      self.internalMessage.destId,
                                      self.internalMessage.destSock)

            respond.Send(interMessage)
        self.bFinished = True
        return respond
Exemple #9
0
    def Run(self):
        if self.protocol.role == BaseCommand.PV_ROLE_INTERNAL:
            sb_protocol = None
            with self.protocol.factory.lockDict:
                sb_protocol = self.protocol.factory.dictRelayer.get(
                    self.internalMessage.destId)

            if sb_protocol is None:
                self.Finish(False)
                return

            with sb_protocol.lock_dictWaitResp:
                sb_protocol.dictWaitResp[self.command_seq] = self
            self.Execute()
            return

        with self.protocol.lockCmd:
            if self.protocol.role == BaseCommand.PV_ROLE_HUMAN:
                if not self.Authorized():
                    self.SendUnauthorizedResp()
                    return
                CBaseCommand.Run(self)

                #check whether contain parameter relayer_id
                relayer_id = self.body.get(BaseCommand.PN_RELAYERID, None)
                if relayer_id is None:
                    respond = self.GetResp()
                    respond.SetErrorCode(BaseCommand.CS_PARAMLACK)
                    respond.Send()
                    return

                with self.protocol.lock_dictWaitResp:
                    self.protocol.dictWaitResp[self.command_seq] = self

                interMessage = InternalMessage.CInternalMessage()
                interMessage.SetParam(InternalMessage.TTYPE_GATEWAY,
                                      relayer_id, 0,
                                      InternalMessage.OPER_REQUEST, "",
                                      InternalMessage.TTYPE_HUMAN,
                                      self.protocol.client_id,
                                      id(self.protocol.transport))

                self.Send(interMessage)
                #threading.Timer(Config.timeout_relayer_control,self.timeout).start()
                reactor.callLater(Config.timeout_relayer_control, self.timeout)
Exemple #10
0
    def Send(self):
        maxcmd_flow_control = 1000
        if self.protocol.role == BaseCommand.PV_ROLE_RELAYER:
            maxcmd_flow_control = Config.maxcmd_relayer_control
        while True:
            with self.protocol.cond_dictControlling:
                if len(self.protocol.dictControlling.keys()
                       ) > maxcmd_flow_control:
                    logging.debug(
                        "call self.protocol.cond_dictControlling.wait() due to reach maxcmd in protocol %d",
                        id(self.protocol.transport))
                    self.protocol.cond_dictControlling.wait()
                elif self.command_seq in self.protocol.dictControlling:
                    logging.debug(
                        "call self.protocol.cond_dictControlling.wait() due to same command_seq in protocol %d",
                        id(self.protocol.transport))
                    self.protocol.cond_dictControlling.wait()
                else:
                    interMessage = None
                    self.protocol.dictControlling[self.command_seq] = self
                    self.timer = reactor.callLater(
                        Config.timeout_relayer_control, self.timeout)

                    # if isinstance(self.requireCommand,BaseCommand.CBaseCommand):
                    if self.requireCommand is not None and self.requireCommand.internalMessage is None:
                        interMessage = InternalMessage.CInternalMessage()
                        interMessage.SetParam(
                            InternalMessage.TTYPE_GATEWAY, self.relayer_id, 0,
                            InternalMessage.OPER_REQUEST, "",
                            InternalMessage.TTYPE_HUMAN,
                            self.requireCommand.protocol.client_id,
                            id(self.requireCommand.protocol.transport))

                    CBaseCommand.Send(self, interMessage)

                    break