23
23
# SOFTWARE.
24
24
25
25
26
- import inspect
27
26
import sys
28
27
import traceback
29
28
import gevent .pool
38
37
from .socket import SocketBase
39
38
from .heartbeat import HeartBeatOnChannel
40
39
from .context import Context
41
-
40
+ from .decorators import DecoratorBase , rep
41
+ import patterns
42
42
43
43
class ServerBase (object ):
44
44
@@ -171,12 +171,11 @@ def stop(self):
171
171
172
172
class ClientBase (object ):
173
173
174
- def __init__ (self , channel , patterns , context = None , timeout = 30 , heartbeat = 5 ,
174
+ def __init__ (self , channel , context = None , timeout = 30 , heartbeat = 5 ,
175
175
passive_heartbeat = False ):
176
176
self ._multiplexer = ChannelMultiplexer (channel ,
177
177
ignore_broadcast = True )
178
178
self ._context = context or Context .get_instance ()
179
- self ._patterns = patterns
180
179
self ._timeout = timeout
181
180
self ._heartbeat_freq = heartbeat
182
181
self ._passive_heartbeat = passive_heartbeat
@@ -195,7 +194,7 @@ def _raise_remote_error(self, event):
195
194
raise RemoteError ('RemoteError' , msg , None )
196
195
197
196
def _select_pattern (self , event ):
198
- for pattern in self . _patterns :
197
+ for pattern in patterns . patterns_list :
199
198
if pattern .accept_answer (event ):
200
199
return pattern
201
200
msg = 'Unable to find a pattern for: {0}' .format (event )
@@ -211,7 +210,7 @@ def _process_response(self, method, bufchan, timeout):
211
210
212
211
pattern = self ._select_pattern (event )
213
212
return pattern .process_answer (self ._context , bufchan , event , method ,
214
- timeout , self ._raise_remote_error )
213
+ self ._raise_remote_error )
215
214
except :
216
215
bufchan .close ()
217
216
bufchan .channel .close ()
@@ -246,89 +245,6 @@ def __getattr__(self, method):
246
245
return lambda * args , ** kargs : self (method , * args , ** kargs )
247
246
248
247
249
- class DecoratorBase (object ):
250
- pattern = None
251
-
252
- def __init__ (self , functor ):
253
- self ._functor = functor
254
- self .__doc__ = functor .__doc__
255
- self .__name__ = functor .__name__
256
-
257
- def __get__ (self , instance , type_instance = None ):
258
- if instance is None :
259
- return self
260
- return self .__class__ (self ._functor .__get__ (instance , type_instance ))
261
-
262
- def __call__ (self , * args , ** kargs ):
263
- return self ._functor (* args , ** kargs )
264
-
265
- def _zerorpc_args (self ):
266
- try :
267
- args_spec = self ._functor ._zerorpc_args ()
268
- except AttributeError :
269
- try :
270
- args_spec = inspect .getargspec (self ._functor )
271
- except TypeError :
272
- try :
273
- args_spec = inspect .getargspec (self ._functor .__call__ )
274
- except (AttributeError , TypeError ):
275
- args_spec = None
276
- return args_spec
277
-
278
-
279
- class PatternReqRep ():
280
-
281
- def process_call (self , context , bufchan , event , functor ):
282
- result = context .middleware_call_procedure (functor , * event .args )
283
- bufchan .emit ('OK' , (result ,), context .middleware_get_task_context ())
284
-
285
- def accept_answer (self , event ):
286
- return True
287
-
288
- def process_answer (self , context , bufchan , event , method , timeout ,
289
- raise_remote_error ):
290
- result = event .args [0 ]
291
- if event .name == 'ERR' :
292
- raise_remote_error (event )
293
- bufchan .close ()
294
- bufchan .channel .close ()
295
- bufchan .channel .channel .close ()
296
- return result
297
-
298
-
299
- class rep (DecoratorBase ):
300
- pattern = PatternReqRep ()
301
-
302
-
303
- class PatternReqStream ():
304
-
305
- def process_call (self , context , bufchan , event , functor ):
306
- xheader = context .middleware_get_task_context ()
307
- for result in iter (context .middleware_call_procedure (functor ,
308
- * event .args )):
309
- bufchan .emit ('STREAM' , result , xheader )
310
- bufchan .emit ('STREAM_DONE' , None , xheader )
311
-
312
- def accept_answer (self , event ):
313
- return event .name in ('STREAM' , 'STREAM_DONE' )
314
-
315
- def process_answer (self , context , bufchan , event , method , timeout ,
316
- raise_remote_error ):
317
- def iterator (event ):
318
- while event .name == 'STREAM' :
319
- yield event .args
320
- event = bufchan .recv ()
321
- if event .name == 'ERR' :
322
- raise_remote_error (event )
323
- bufchan .close ()
324
- bufchan .channel .channel .close ()
325
- return iterator (event )
326
-
327
-
328
- class stream (DecoratorBase ):
329
- pattern = PatternReqStream ()
330
-
331
-
332
248
class Server (SocketBase , ServerBase ):
333
249
334
250
def __init__ (self , methods = None , name = None , context = None , pool_size = None ,
@@ -346,13 +262,12 @@ def close(self):
346
262
347
263
348
264
class Client (SocketBase , ClientBase ):
349
- patterns = [PatternReqStream (), PatternReqRep ()]
350
265
351
266
def __init__ (self , connect_to = None , context = None , timeout = 30 , heartbeat = 5 ,
352
267
passive_heartbeat = False ):
353
268
SocketBase .__init__ (self , zmq .XREQ , context = context )
354
- ClientBase .__init__ (self , self ._events , Client . patterns , context ,
355
- timeout , heartbeat , passive_heartbeat )
269
+ ClientBase .__init__ (self , self ._events , context , timeout , heartbeat ,
270
+ passive_heartbeat )
356
271
if connect_to :
357
272
self .connect (connect_to )
358
273
0 commit comments