view channels.py @ 40:11c4fb8bec3c

Added timestamp to messages, though they're not currently displayed in any way.
author Atul Varma <varmaa@toolness.com>
date Mon, 27 Apr 2009 22:26:57 -0700
parents 425776983cf2
children b0f802c4fafc
line wrap: on
line source

import cosocket

_channels = {}

class until_message_sent(object):
    def __init__(self, channel_name, message):
        self.channel_name = channel_name
        self.message = message

    def execute(self, dispatcher):
        if self.channel_name in _channels:
            receivers = _channels[self.channel_name].values()
            del _channels[self.channel_name]
            for receiver in receivers:
                receiver.continue_from_yield(self.message)
        dispatcher.continue_from_yield()

class _until_message_received(object):
    def __init__(self, channel_name):
        self.channel_name = channel_name
        self._fd = None

    def execute(self, dispatcher):
        if self.channel_name not in _channels:
            _channels[self.channel_name] = {}
        self._fd = dispatcher.socket.fileno()
        _channels[self.channel_name][self._fd] = dispatcher

    def abort(self):
        if (self.channel_name in _channels and
            self._fd in _channels[self.channel_name]):
            del _channels[self.channel_name][self._fd]

def until_message_received(channel_name):
    instruction = _until_message_received(channel_name)
    try:
        message = yield instruction
        yield cosocket.return_value(message)
    except GeneratorExit:
        instruction.abort()
        raise