Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c068aac

Browse files
aluzzardibombela
authored andcommitted
Refactoring
Conflicts: zerorpc/gevent_zerorpc.py -> deleted
1 parent 213d94e commit c068aac

File tree

8 files changed

+1119
-960
lines changed

8 files changed

+1119
-960
lines changed

zerorpc/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@
2828
__license__ = 'MIT'
2929
__copyright__ = 'Copyright 2012 dotCloud, Inc.'
3030

31-
from gevent_zerorpc import *
31+
from .exceptions import *
32+
from .context import *
33+
from .socket import *
34+
from .channel import *
35+
from .events import *
36+
from .core import *

zerorpc/channel.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

zerorpc/context.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
26+
import uuid
27+
28+
import gevent_zmq as zmq
29+
30+
31+
class Context(zmq.Context):
32+
_instance = None
33+
34+
def __init__(self):
35+
self._middlewares = []
36+
self._middlewares_hooks = {
37+
'resolve_endpoint': [],
38+
'raise_error': []
39+
}
40+
41+
@staticmethod
42+
def get_instance():
43+
if Context._instance is None:
44+
Context._instance = Context()
45+
return Context._instance
46+
47+
def new_msgid(self):
48+
return str(uuid.uuid4())
49+
50+
def register_middleware(self, middleware_instance):
51+
registered_count = 0
52+
self._middlewares.append(middleware_instance)
53+
for hook in self._middlewares_hooks.keys():
54+
functor = getattr(middleware_instance, hook, None)
55+
if functor is None:
56+
try:
57+
functor = middleware_instance.get(hook, None)
58+
except AttributeError:
59+
pass
60+
if functor is not None:
61+
self._middlewares_hooks[hook].append(functor)
62+
registered_count += 1
63+
return registered_count
64+
65+
def middleware_resolve_endpoint(self, endpoint):
66+
for functor in self._middlewares_hooks['resolve_endpoint']:
67+
endpoint = functor(endpoint)
68+
return endpoint
69+
70+
def middleware_raise_error(self, event):
71+
for functor in self._middlewares_hooks['raise_error']:
72+
functor(event)

0 commit comments

Comments
 (0)