|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Open Source Initiative OSI - The MIT License (MIT):Licensing |
| 3 | +# |
| 4 | +# The MIT License (MIT) |
| 5 | +# Copyright (c) 2012 DotCloud Inc ([email protected]) |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 8 | +# this software and associated documentation files (the "Software"), to deal in |
| 9 | +# the Software without restriction, including without limitation the rights to |
| 10 | +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 11 | +# of the Software, and to permit persons to whom the Software is furnished to do |
| 12 | +# so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | + |
| 25 | +import sys |
| 26 | + |
| 27 | +import gevent.pool |
| 28 | +import gevent.queue |
| 29 | +import gevent.event |
| 30 | +import gevent.local |
| 31 | +import gevent.coros |
| 32 | + |
| 33 | +from .exceptions import TimeoutExpired |
| 34 | + |
| 35 | + |
| 36 | +class ChannelMultiplexer(object): |
| 37 | + def __init__(self, events, ignore_broadcast=False): |
| 38 | + self._events = events |
| 39 | + self._active_channels = {} |
| 40 | + self._channel_dispatcher_task = None |
| 41 | + self._broadcast_queue = None |
| 42 | + if events.recv_is_available and not ignore_broadcast: |
| 43 | + self._broadcast_queue = gevent.queue.Queue(maxsize=1) |
| 44 | + self._channel_dispatcher_task = gevent.spawn( |
| 45 | + self._channel_dispatcher) |
| 46 | + |
| 47 | + @property |
| 48 | + def recv_is_available(self): |
| 49 | + return self._events.recv_is_available |
| 50 | + |
| 51 | + def __del__(self): |
| 52 | + self.close() |
| 53 | + |
| 54 | + def close(self): |
| 55 | + if self._channel_dispatcher_task: |
| 56 | + self._channel_dispatcher_task.kill() |
| 57 | + |
| 58 | + def create_event(self, name, args, xheader={}): |
| 59 | + return self._events.create_event(name, args, xheader) |
| 60 | + |
| 61 | + def emit_event(self, event, identity=None): |
| 62 | + return self._events.emit_event(event, identity) |
| 63 | + |
| 64 | + def emit(self, name, args, xheader={}): |
| 65 | + return self._events.emit(name, args, xheader) |
| 66 | + |
| 67 | + def recv(self): |
| 68 | + if self._broadcast_queue is not None: |
| 69 | + event = self._broadcast_queue.get() |
| 70 | + else: |
| 71 | + event = self._events.recv() |
| 72 | + return event |
| 73 | + |
| 74 | + def _channel_dispatcher(self): |
| 75 | + while True: |
| 76 | + event = self._events.recv() |
| 77 | + channel_id = event.header.get('response_to', None) |
| 78 | + |
| 79 | + queue = None |
| 80 | + if channel_id is not None: |
| 81 | + channel = self._active_channels.get(channel_id, None) |
| 82 | + if channel is not None: |
| 83 | + queue = channel._queue |
| 84 | + elif self._broadcast_queue is not None: |
| 85 | + queue = self._broadcast_queue |
| 86 | + |
| 87 | + if queue is None: |
| 88 | + print >> sys.stderr, \ |
| 89 | + 'zerorpc.ChannelMultiplexer, ', \ |
| 90 | + 'unable to route event:', \ |
| 91 | + event.__str__(ignore_args=True) |
| 92 | + else: |
| 93 | + queue.put(event) |
| 94 | + |
| 95 | + def channel(self, from_event=None): |
| 96 | + if self._channel_dispatcher_task is None: |
| 97 | + self._channel_dispatcher_task = gevent.spawn( |
| 98 | + self._channel_dispatcher) |
| 99 | + return Channel(self, from_event) |
| 100 | + |
| 101 | + @property |
| 102 | + def active_channels(self): |
| 103 | + return self._active_channels |
| 104 | + |
| 105 | + |
| 106 | +class Channel(object): |
| 107 | + |
| 108 | + def __init__(self, multiplexer, from_event=None): |
| 109 | + self._multiplexer = multiplexer |
| 110 | + self._channel_id = None |
| 111 | + self._zmqid = None |
| 112 | + self._queue = gevent.queue.Queue(maxsize=1) |
| 113 | + if from_event is not None: |
| 114 | + self._channel_id = from_event.header['message_id'] |
| 115 | + self._zmqid = from_event.header.get('zmqid', None) |
| 116 | + self._multiplexer._active_channels[self._channel_id] = self |
| 117 | + self._queue.put(from_event) |
| 118 | + |
| 119 | + @property |
| 120 | + def recv_is_available(self): |
| 121 | + return self._multiplexer.recv_is_available |
| 122 | + |
| 123 | + def __del__(self): |
| 124 | + self.close() |
| 125 | + |
| 126 | + def close(self): |
| 127 | + if self._channel_id is not None: |
| 128 | + del self._multiplexer._active_channels[self._channel_id] |
| 129 | + self._channel_id = None |
| 130 | + |
| 131 | + def emit(self, name, args, xheader={}): |
| 132 | + event = self._multiplexer.create_event(name, args, xheader) |
| 133 | + |
| 134 | + if self._channel_id is None: |
| 135 | + self._channel_id = event.header['message_id'] |
| 136 | + self._multiplexer._active_channels[self._channel_id] = self |
| 137 | + else: |
| 138 | + event.header['response_to'] = self._channel_id |
| 139 | + |
| 140 | +# TODO debug middleware |
| 141 | +# print time.time(), 'channel emit', event |
| 142 | + self._multiplexer.emit_event(event, self._zmqid) |
| 143 | + |
| 144 | + def recv(self, timeout=None): |
| 145 | + try: |
| 146 | + event = self._queue.get(timeout=timeout) |
| 147 | + except gevent.queue.Empty: |
| 148 | + raise TimeoutExpired(timeout) |
| 149 | +# TODO debug middleware |
| 150 | +# print time.time(), 'channel recv', event |
| 151 | + return event |
0 commit comments