Exemple #1
0
    def test_aps_request_from_frames(self):
        from aps.message import APSRequest, EMPTY, VERSION
        from aps.util import get_timestamp, ensure_bytes
        envelope = ['test request envelope']
        sequence = 1
        timestamp = get_timestamp()
        expiry = None
        method = 'sp.up'
        params = ['p1', 'p2']
        extras = ['ex1', 'ex2', ['ex3', ['ex4', 'ex5']]]

        _frames = map(ensure_bytes, envelope)
        _frames.append(EMPTY)
        _frames.append(VERSION)
        _frames.append(packb((sequence, timestamp, expiry)))
        _frames.append(ensure_bytes(method))
        _frames.append(packb(params))
        _frames.extend(map(packb, extras))

        request = APSRequest(frames=_frames)

        self.assertEqual(request.envelope, envelope)
        self.assertEqual(request.sequence, sequence)
        self.assertEqual(request.timestamp, timestamp)
        self.assertEqual(request.expiry, expiry)
        self.assertEqual(request.method, method)
        self.assertEqual(request.params, params)
        self.assertEqual(request.extras, extras)
Exemple #2
0
    def test_aps_request_frames(self):
        from aps.message import APSRequest, EMPTY, VERSION
        from aps.util import get_timestamp
        envelope = ['test request envelope']
        sequence = 1
        timestamp = get_timestamp()
        expiry = None
        method = 'sp.up'
        params = ['p1', 'p2']
        extras = ['ex1', 'ex2', ('ex3', ('ex4', 'ex5'))]

        request = APSRequest(envelope=envelope,
                             sequence=sequence,
                             timestamp=timestamp,
                             expiry=expiry,
                             method=method,
                             params=params,
                             extras=extras)

        from aps.util import ensure_bytes
        _frames = map(ensure_bytes, envelope)
        _frames.append(EMPTY)
        _frames.append(VERSION)
        _frames.append(packb((sequence, timestamp, expiry)))
        _frames.append(ensure_bytes(method))
        _frames.append(packb(params))
        _frames.extend(map(packb, extras))

        self.assertEqual(request.frames, _frames)
Exemple #3
0
    def test_aps_reply_from_frames(self):
        from aps.message import APSReply, EMPTY, VERSION
        from aps.util import get_timestamp, ensure_bytes
        envelope = ['test reply envelope']
        sequence = 1
        timestamp = get_timestamp()
        status = 200
        result = 'ok!!!'
        extras = ['ex1', ['ex2', ['ex3', 'ex4']], 'ex5']

        _frames = map(ensure_bytes, envelope)
        _frames.append(EMPTY)
        _frames.append(VERSION)
        _frames.append(packb((sequence, timestamp, status)))
        _frames.append(packb(result))
        _frames.extend(map(packb, extras))

        reply = APSReply(frames=_frames)

        self.assertEqual(reply.envelope, envelope)
        self.assertEqual(reply.sequence, sequence)
        self.assertEqual(reply.timestamp, timestamp)
        self.assertEqual(reply.status, status)
        self.assertEqual(reply.result, result)
        self.assertEqual(reply.extras, extras)
Exemple #4
0
    def test_aps_request_frames(self):
        from aps.message import APSRequest, EMPTY, VERSION
        from aps.util import get_timestamp
        envelope = ['test request envelope']
        sequence = 1
        timestamp = get_timestamp()
        expiry = None
        method = 'sp.up'
        params = ['p1', 'p2']
        extras = ['ex1', 'ex2', ('ex3', ('ex4', 'ex5'))]

        request = APSRequest(envelope=envelope,
                             sequence=sequence,
                             timestamp=timestamp,
                             expiry=expiry,
                             method=method,
                             params=params,
                             extras=extras)

        from aps.util import ensure_bytes
        _frames = map(ensure_bytes, envelope)
        _frames.append(EMPTY)
        _frames.append(VERSION)
        _frames.append(packb((sequence, timestamp, expiry)))
        _frames.append(ensure_bytes(method))
        _frames.append(packb(params))
        _frames.extend(map(packb, extras))

        self.assertEqual(request.frames, _frames)
Exemple #5
0
    def test_aps_reply_from_frames(self):
        from aps.message import APSReply, EMPTY, VERSION
        from aps.util import get_timestamp, ensure_bytes
        envelope = ['test reply envelope']
        sequence = 1
        timestamp = get_timestamp()
        status = 200
        result = 'ok!!!'
        extras = ['ex1', ['ex2', ['ex3', 'ex4']], 'ex5']

        _frames = map(ensure_bytes, envelope)
        _frames.append(EMPTY)
        _frames.append(VERSION)
        _frames.append(packb((sequence, timestamp, status)))
        _frames.append(packb(result))
        _frames.extend(map(packb, extras))

        reply = APSReply(frames=_frames)

        self.assertEqual(reply.envelope, envelope)
        self.assertEqual(reply.sequence, sequence)
        self.assertEqual(reply.timestamp, timestamp)
        self.assertEqual(reply.status, status)
        self.assertEqual(reply.result, result)
        self.assertEqual(reply.extras, extras)
Exemple #6
0
    def test_aps_request_from_frames(self):
        from aps.message import APSRequest, EMPTY, VERSION
        from aps.util import get_timestamp, ensure_bytes
        envelope = ['test request envelope']
        sequence = 1
        timestamp = get_timestamp()
        expiry = None
        method = 'sp.up'
        params = ['p1', 'p2']
        extras = ['ex1', 'ex2', ['ex3', ['ex4', 'ex5']]]

        _frames = map(ensure_bytes, envelope)
        _frames.append(EMPTY)
        _frames.append(VERSION)
        _frames.append(packb((sequence, timestamp, expiry)))
        _frames.append(ensure_bytes(method))
        _frames.append(packb(params))
        _frames.extend(map(packb, extras))

        request = APSRequest(frames=_frames)

        self.assertEqual(request.envelope, envelope)
        self.assertEqual(request.sequence, sequence)
        self.assertEqual(request.timestamp, timestamp)
        self.assertEqual(request.expiry, expiry)
        self.assertEqual(request.method, method)
        self.assertEqual(request.params, params)
        self.assertEqual(request.extras, extras)
Exemple #7
0
    def __init__(self, frames=None, **kwargs):
        if frames:
            self._frames = frames
            try:
                self._sep = frames.index(EMPTY)
            except:
                self._sep = -1
            return

        self.envelope  = kwargs.pop('envelope', [])
        # avoid wierd attribute issue in __getattr__
        assert type(self.envelope) == list
        self.version   = kwargs.pop('version', VERSION)
        self.sequence  = kwargs.pop('sequence', None)
        self.timestamp = kwargs.pop('timestamp', get_timestamp())
        self.status    = kwargs.pop('status', None)
        self.result    = kwargs.pop('result', None)
        self.extras    = kwargs.pop('extras', None)
Exemple #8
0
    def __init__(self, frames=None, **kwargs):
        if frames:
            self._frames = frames
            try:
                self._sep = frames.index(EMPTY)
            except:
                self._sep = -1
            return

        self.envelope = kwargs.pop('envelope', [])
        # avoid wierd attribute issue in __getattr__
        assert type(self.envelope) == list
        self.version = kwargs.pop('version', VERSION)
        self.sequence = kwargs.pop('sequence', None)
        self.timestamp = kwargs.pop('timestamp', get_timestamp())
        self.status = kwargs.pop('status', None)
        self.result = kwargs.pop('result', None)
        self.extras = kwargs.pop('extras', None)
Exemple #9
0
    def test_aps_reply(self):
        from aps.message import APSReply
        from aps.util import get_timestamp
        envelope = ['test reply envelope']
        sequence = 1
        timestamp = get_timestamp()
        status = 200
        result = 'ok!!!'
        extras = ['ex1', ('ex2', ('ex3', 'ex4')), 'ex5']

        reply = APSReply(envelope=envelope,
                         sequence=sequence,
                         timestamp=timestamp,
                         status=status,
                         result=result,
                         extras=extras)

        self.assertEqual(reply.envelope, envelope)
        self.assertEqual(reply.sequence, sequence)
        self.assertEqual(reply.timestamp, timestamp)
        self.assertEqual(reply.status, status)
        self.assertEqual(reply.result, result)
        self.assertEqual(reply.extras, extras)
Exemple #10
0
    def test_aps_request(self):
        from aps.message import APSRequest
        from aps.util import get_timestamp
        envelope = ['test envelope']
        sequence = 1
        timestamp = get_timestamp()
        expiry = None
        method = 'sp.up'
        params = ['p1', 'p2']

        request = APSRequest(envelope=envelope,
                             sequence=sequence,
                             timestamp=timestamp,
                             expiry=expiry,
                             method=method,
                             params=params)

        self.assertEqual(request.envelope, envelope)
        self.assertEqual(request.sequence, sequence)
        self.assertEqual(request.timestamp, timestamp)
        self.assertEqual(request.expiry, expiry)
        self.assertEqual(request.method, method)
        self.assertEqual(request.params, params)
Exemple #11
0
    def test_aps_reply(self):
        from aps.message import APSReply
        from aps.util import get_timestamp
        envelope = ['test reply envelope']
        sequence = 1
        timestamp = get_timestamp()
        status = 200
        result = 'ok!!!'
        extras = ['ex1', ('ex2', ('ex3', 'ex4')), 'ex5']

        reply = APSReply(envelope=envelope,
                         sequence=sequence,
                         timestamp=timestamp,
                         status=status,
                         result=result,
                         extras=extras)

        self.assertEqual(reply.envelope, envelope)
        self.assertEqual(reply.sequence, sequence)
        self.assertEqual(reply.timestamp, timestamp)
        self.assertEqual(reply.status, status)
        self.assertEqual(reply.result, result)
        self.assertEqual(reply.extras, extras)
Exemple #12
0
    def test_aps_request(self):
        from aps.message import APSRequest
        from aps.util import get_timestamp
        envelope = ['test envelope']
        sequence = 1
        timestamp = get_timestamp()
        expiry = None
        method = 'sp.up'
        params = ['p1', 'p2']

        request = APSRequest(envelope=envelope,
                             sequence=sequence,
                             timestamp=timestamp,
                             expiry=expiry,
                             method=method,
                             params=params)

        self.assertEqual(request.envelope, envelope)
        self.assertEqual(request.sequence, sequence)
        self.assertEqual(request.timestamp, timestamp)
        self.assertEqual(request.expiry, expiry)
        self.assertEqual(request.method, method)
        self.assertEqual(request.params, params)
Exemple #13
0
def wait_for_replies(*handlers, **kwargs):
    """receive and fetch replies from all sockets

    Args:
        +handlers+ is list of handlers
        +timeout+ to receive the replies
    """
    timeout = kwargs.get('timeout', None) or default_timeout

    rv = {}
    callbacks = set()

    if len(handlers) == 0:
        pending = set(pending_requests.keys())
    else:
        pending = set(handlers)

    timelimit = True
    if timeout == -1:
        timelimit = False

    def _process(socket):
        frames = aps_recv_frames(socket)
        reply = APSReply(frames)
        _, callback = pending_requests.get(reply.sequence)
        if reply.sequence in pending:
            if callable(callback):
                callbacks.add((reply, callback))
            else:
                rv[reply.sequence] = reply
            pending.remove(reply.sequence)
            pending_requests.pop(reply.sequence)

    while len(pending) > 0:
        _start = get_timestamp()

        if timeout == -1:
            events = {}
            for socket in sockets:
                events[socket] = zmq.POLLIN
        else:
            events = dict(poller.poll(timeout))

        for socket in sockets:
            if socket in events and events.get(socket) == zmq.POLLIN:
                try:
                    while True:
                        _process(socket)
                except zmq.error.Again:
                    pass

        _end = get_timestamp()
        timeout -= _end - _start
        if timelimit and timeout <= 0:
            break

    # callbacks
    # TODO: spawn callback with greenlet
    # for _ in callbacks:
    #     reply, callback = _
    #     callback(reply)
    return rv
Exemple #14
0
def wait_for_replies(*handlers, **kwargs):
    """receive and fetch replies from all sockets

    Args:
        +handlers+ is list of handlers
        +timeout+ to receive the replies
    """
    timeout = kwargs.get('timeout', None) or default_timeout

    rv = {}
    callbacks = set()

    if len(handlers) == 0:
        pending = set(pending_requests.keys())
    else:
        pending = set(handlers)

    timelimit = True
    if timeout == -1:
        timelimit = False

    def _process(socket):
        frames = aps_recv_frames(socket)
        reply = APSReply(frames)
        _, callback = pending_requests.get(reply.sequence)
        if reply.sequence in pending:
            if callable(callback):
                callbacks.add((reply, callback))
            else:
                rv[reply.sequence] = reply
            pending.remove(reply.sequence)
            pending_requests.pop(reply.sequence)

    while len(pending) > 0:
        _start = get_timestamp()

        if timeout == -1:
            events = {}
            for socket in sockets:
                events[socket] = zmq.POLLIN
        else:
            events = dict(poller.poll(timeout))

        for socket in sockets:
            if socket in events and events.get(socket) == zmq.POLLIN:
                try:
                    while True:
                        _process(socket)
                except zmq.error.Again:
                    pass

        _end = get_timestamp()
        timeout -= _end - _start
        if timelimit and timeout <= 0:
            break

    # callbacks
    # TODO: spawn callback with greenlet
    # for _ in callbacks:
    #     reply, callback = _
    #     callback(reply)
    return rv