-
Notifications
You must be signed in to change notification settings - Fork 6
/
async_io.py
73 lines (62 loc) · 2.46 KB
/
async_io.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
## @package PyHoot.async_io
# AsyncIO (with select_object or poll_object)
## @file async_io.py Implementation of @ref PyHoot.async_io
from . import base, common_events, constants, poll_object, select_object
class AsyncIO(base.Base):
"""AsyncIO object (with select_object or poll_object).
Return fd like poll no matter what API we use.
Args:
self.protocol= the API we are using
self._protocol= the object we will use, depent on API.
if we use poll, poll we will have poll_object,
if we use select, select_object
"""
def __init__(self, protocol):
"""Create AsyncIO object
Parameters:
protocol, the protocol we will use
"""
super(AsyncIO, self).__init__()
## The API we will use
self.protocol = protocol
"""API Object"""
self._protocol_object = None
def register_all(self, database):
"""Register all the sockets in the database"""
for s in database.keys():
entry = database[s]
events = common_events.CommonEvents.POLLERR
if entry["state"] == constants.CLOSE:
events |= common_events.CommonEvents.POLLOUT
elif entry["state"] == constants.SERVER:
events |= common_events.CommonEvents.POLLIN
elif entry["state"] == constants.CLIENT:
cl = entry["client"]
if cl.can_recv():
events |= common_events.CommonEvents.POLLIN
if cl.can_send():
events |= common_events.CommonEvents.POLLOUT
self.logger.debug(
"reistered %s with events %s",
entry["fd"],
events)
self._protocol_object.register([entry["fd"], s], events)
def create_object(self):
"""Create or re - create the protocol object
Args:
database, the database with all the socket we will use
"""
self._protocol_object = (
poll_object.Poll() if self.protocol == "poll" else
select_object.Select()
)
def poll(self, database):
"""Register all the sockets in database and return file descriptor we
can use
Args:
database, the database with all the socket we will use
Return:
events in the same protocol of poll
"""
self.register_all(database)
return self._protocol_object.poll()