Esempio n. 1
0
    def txwind(self, items, size, timeout=None):
        '''
        Execute a windowed transmission loop from a generator.
        '''
        wind = 0

        try:

            for item in items:

                self.tx((True, item))
                wind += 1

                while wind >= size:
                    acks = self.slice(wind, timeout=timeout)
                    wind -= len(acks)

        except Exception as e:
            enfo = s_common.getexcfo(e)
            self.txerr(enfo)
            logger.exception('tx wind genr')
            return

        self.tx((False, None))

        while wind > 0:
            try:
                acks = self.slice(wind, timeout=timeout)
                wind -= len(acks)
            except Exception as e:
                print('TXWIND REMAIN WIND: %r' % (wind, ))
                raise

        return True
Esempio n. 2
0
    def _onMesgHelo(self, link, mesg):
        '''
        Handle receiving the session establishment message from the peer.

        send back our ephemerical public, our cert, and, if the listener, an encrypted message
        '''
        try:
            peer_cert = self._handSessMesg(mesg)
        except Exception as e:
            logger.exception('Exception encountered handling session message.')
            self._send_fail(s_common.getexcfo(e))
            self.txfini()
            return

        if self.is_lisn:
            # This would be a good place to stick version or info stuff
            testmesg = {}
            with self._tx_lock:
                self.link.tx(('helo', {
                    'version': CELL_PROTO_VERSION,
                    'ephem_pub': self._my_ephem_prv.public().dump(),
                    'cert': self._sess_boss.certbyts,
                    'testmesg': self._crypter.encrypt(testmesg)
                }))

        user = peer_cert.tokn.get('user')
        self.setLinkProp('cell:peer', user)

        self.fire('sess:txok')

        self._my_ephem_prv = None
Esempio n. 3
0
    def _onMesgXmit(self, link, mesg):

        if self._crypter is None:
            logger.warning(
                'xmit message before session establishment complete')
            raise s_common.NotReady()

        ciphertext = mesg[1].get('data')
        try:
            newm = self._crypter.decrypt(ciphertext)
        except Exception as e:
            self._send_fail(s_common.getexcfo(e))
            logger.exception('decryption')
            self.txfini()
            return

        try:
            self.taskplex.rx(self, newm)
        except Exception as e:
            self._send_fail(s_common.getexcfo(e))
            logger.exception('xmit taskplex error')
            self.txfini()
Esempio n. 4
0
    def test_common_getexcfo(self):
        try:
            1 / 0
        except ZeroDivisionError as e:
            excfo = s_common.getexcfo(e)

        self.istufo(excfo)
        self.eq(excfo[0], 'ZeroDivisionError')
        self.isin('msg', excfo[1])
        self.isin('file', excfo[1])
        self.isin('line', excfo[1])
        self.isin('name', excfo[1])
        self.isin('src', excfo[1])
        self.notin('syn:err', excfo[1])

        excfo = s_common.getexcfo(s_exc.SynErr(mesg='hehe', key=1))
        self.eq(excfo[0], 'SynErr')
        self.isin('msg', excfo[1])
        self.isin('file', excfo[1])
        self.isin('line', excfo[1])
        self.isin('name', excfo[1])
        self.isin('src', excfo[1])
        self.isin('syn:err', excfo[1])
        self.eq(excfo[1].get('syn:err'), {'mesg': 'hehe', 'key': 1})
Esempio n. 5
0
    def test_common_getexcfo(self):
        try:
            1 / 0
        except ZeroDivisionError as e:
            excfo = s_common.getexcfo(e)

        self.istufo(excfo)
        self.eq(excfo[0], 'ZeroDivisionError')
        self.isin('msg', excfo[1])
        self.isin('file', excfo[1])
        self.isin('line', excfo[1])
        self.isin('name', excfo[1])
        self.isin('src', excfo[1])
        self.notin('syn:err', excfo[1])

        excfo = s_common.getexcfo(s_exc.SynErr(mesg='hehe', key=1))
        self.eq(excfo[0], 'SynErr')
        self.isin('msg', excfo[1])
        self.isin('file', excfo[1])
        self.isin('line', excfo[1])
        self.isin('name', excfo[1])
        self.isin('src', excfo[1])
        self.isin('syn:err', excfo[1])
        self.eq(excfo[1].get('syn:err'), {'mesg': 'hehe', 'key': 1})
Esempio n. 6
0
    def _onCryoInit(self, chan, mesg):
        name = mesg[1].get('name')
        conf = mesg[1].get('conf')

        with chan:

            tank = self.tanks.get(name)
            if tank:
                return chan.tx((True, False))

            try:
                self.genCryoTank(name, conf)
                return chan.tx((True, True))

            except Exception as e:
                retn = s_common.getexcfo(e)
                return chan.tx((False, retn))
Esempio n. 7
0
    def authReact(self, mesg):
        '''
        General interface for interfacing with Auth via messages.

        Args:
            mesg ((str, dict)): A message we react to.

        Returns:
            (bool, ((str, dict))): isok, retn tuple.
        '''
        try:
            isok, retn = self._mxrtor.react(mesg)
        except Exception as e:
            logger.exception('Failed to process mesg [%s]', mesg)
            retn = s_common.getexcfo(e)
            isok = False
        finally:
            return isok, retn
Esempio n. 8
0
    def errx(self, exc):
        '''
        Set the exception information for the current RetnWait object.

        Args:
            exc (Exception): An Exception, or an Exception subclass.

        Notes:
            This is used by a caller to signal that an exception has occured.
            This sets the retn_evnt under the hood, so a caller which is
            blocked on a ``wait()`` call will return the excfo tufo.

        Returns:
            None
        '''

        with retnlock:
            self._retn_exc = s_common.getexcfo(exc)
            if self._retn_evnt is not None:
                self._retn_evnt.set()
Esempio n. 9
0
    def wait(self, timeout=None):
        '''
        Wait for an async callback to complete.

        Args:
            timeout (int/float): Timeout in seconds.

        Returns:
            ((bool, object)): A Boolean flag indicating if the operation
            finished or had a timeout or error condition set. The object
            is either the return value from the callback or an excfo tufo.
        '''
        evnt = self._retn_evnt
        if evnt is None:
            return True, self._retn_valu

        if not evnt.wait(timeout=timeout):
            return False, s_common.getexcfo(
                s_common.RetnTimeout(timeout=timeout))

        if self._retn_exc is not None:
            return False, self._retn_exc

        return True, self._retn_valu