-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.py
73 lines (57 loc) · 1.57 KB
/
factory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from pipeline.server import PipeServer
from pipeline.client import PipeClient
from reqrep.server import RequestReplayServer
from reqrep.client import RRClient
from pubsub.server import PublishSubscribeServer
from pubsub.client import PubSubClient
from bus.node import BusNode
class FactoryException(Exception):
"""
Used in mode selection
"""
pass
class NanoFactory(object):
"""
Common factory class
"""
entities = None
def __init__(self, mode, **kwargs):
if mode not in self.entities.keys():
raise FactoryException('Unknown mode')
self.entity = self.entities[mode](**kwargs)
def run(self):
raise NotImplementedError
class ServerFactory(NanoFactory):
"""
Simple factory for pipeline, request/replay,
publish/subscribe and bus nodes
"""
entities = {
'pipe': PipeServer,
'reqrep': RequestReplayServer,
'pubsup': PublishSubscribeServer,
'bus': BusNode
}
def __init__(self, mode, **kwargs):
"""
Init server in one of 4 modes or raise exception
"""
if 'name' not in kwargs:
kwargs['name'] = '%s_server' % mode
super(ServerFactory, self).__init__(mode, **kwargs)
def run(self):
"""
Run server loop
"""
self.entity.run()
class ClientFactory(NanoFactory):
entities = {
'pipe': PipeClient,
'reqrep': RRClient,
'pubsup': PubSubClient,
}
def run(self, **kwargs):
"""
Do client action
"""
self.entity.action(**kwargs)