Newer
Older
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
#!/usr/bin/env python
#coding:utf8
import select
import threading
class ConnectionManager(object):
'''
Base class for all connection manager classes.
'''
# The timeout to wait before the poll call release the hand with no events:
POLL_TIMEOUT = 1
# Masks for fd registration on poll object:
MASK_NORMAL = (select.EPOLLIN | select.EPOLLPRI |
select.EPOLLERR | select.EPOLLHUP)
MASK_WRITABLE = MASK_NORMAL | select.EPOLLOUT
def __init__(self):
self._poll = select.epoll()
self._running = True
self._received_msg = {}
self._wait_groups = {}
def register(self, connection):
'''
Register a :class:`RpcConnection` object on this manager.
:param connection: the instance of :class:`RpcConnection` to register
:type param: instance of :class:`RpcConnection`
'''
self._poll.register(connection.get_fd(), ConnectionManager.MASK_NORMAL)
def is_running(self):
return self._running
def run(self):
'''
Run the main loop of the :class:`ConnectionManager`. It will catch
events on registered :class:`RpcConnection` and process them.
'''
while self._running:
try:
events = self._poll.poll(ConnectionManager.POLL_TIMEOUT)
except IOError:
pass
else:
for fd, event in events:
self.handle_event(fd, event)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
def start(self, daemonize=False):
'''
Run the main loop in a separated thread.
:param daemonize: set the thread daemon state
'''
t = threading.Thread(target=self.run)
t.daemon = daemonize
t.start()
def wait(self, msg_id_set, timeout=None, wait_all=True):
'''
Wait for the asynchronous messages in ``msg_id_set``.
When the timeout argument is present and not ``None``, it should be a
floating point number specifying a timeout for the operation in
seconds (or fractions thereof).
You can also set ``wait_all`` to False if you want to unlock the call
when the first response is received.
:param msg_id_set: set of message to wait
:type msg_id_set: :class:`frozenset`
:param timeout: timeout value or None to disable timeout (default: None)
:type timeout: :class:`int` or :class:`None`
:param wait_all: wait for all messages (default: True)
:type wait_all: :class:`bool`
.. warning:
This is important that ``msg_id_set`` is a :class:`frozenset`
and not a :class:`set`.
'''
waiter = {'event': threading.Event(), 'wait_all': wait_all}
self._wait_groups.setdefault(msg_id_set, waiter)
already_completed = self._check_waiter(msg_id_set)
if not already_completed:
waiter['event'].wait(timeout=timeout)
messages = waiter['responses']
del self._wait_groups[msg_id_set]
return messages
def signal_arrival(self, message):
'''
Signal the arrival of a new message to the :class:`ConnectionManager`.
This method is ordinary called by the :class:`RpcConnections` objects,
when a response to an asynchronous call is received.
:param message: the message received
'''
self._received_msg[message['id']] = message
for waitset in self._wait_groups.keys():
self._check_waiter(waitset)
def _check_waiter(self, waitset):
'''
Check if a waitset is completed and process it.
:param waitset: the waitset to check
:return: True if waitset is completed else None
'''
# Make a set of received messages ids:
recv_msg = set(self._received_msg)
try:
waiter = self._wait_groups[waitset]
except KeyError:
return False
is_ok = (waiter['wait_all'] and waitset <= recv_msg
or not waiter['wait_all'] and not recv_msg.isdisjoint(waitset))
if is_ok:
# Clean the call list on each attached RpcConnection
for connection in self.all_connections():
connection.clean_all_calls(waitset)
# Get the messages:
messages = []
for msg_id, msg in self._received_msg.items():
if msg_id in waitset:
messages.append(msg)
del self._received_msg[msg_id]
waiter['responses'] = tuple(messages)
# Unlock the event:
waiter['event'].set()
return True
else:
return False
def all_connections(self):
'''
Return all connection attached to this :class:`ConnectionManager`.
:return: a set of :class:`RpcConnection` attached
to this :class:`ConnectionManager`
'''
raise NotImplementedError
def shutdown(self):
'''
Shutdown the manager properly.
'''
self._running = False
def data_to_write(self, connection):
'''
Method called by a connection to inform the manager that it have data
to send.
:param connection: the :class:`RpcConnection` object which inform the
manager
'''
fd = connection.get_fd()
self._poll.modify(fd, ConnectionManager.MASK_WRITABLE)
def nothing_to_write(self, connection):
'''
Method called by a connection to inform the manager that it have no
more data to send.
:param connection: the :class:`RpcConnection` object which inform the
manager
'''
fd = connection.get_fd()
self._poll.modify(fd, ConnectionManager.MASK_NORMAL)
def handle_event(self, fd, event):
'''
Handle an event and make associated action. This is an abstract method to
overload on derived classes.
:param fd: the fd that have generated the event
:param event: the event as returned by the poller object
'''
pass