Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / internet / pollreactor.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 A poll() based implementation of the twisted main loop.
6
7 To install the event loop (and you should do this before any connections,
8 listeners or connectors are added)::
9
10     from twisted.internet import pollreactor
11     pollreactor.install()
12 """
13
14 # System imports
15 import errno
16 from select import error as SelectError, poll
17 from select import POLLIN, POLLOUT, POLLHUP, POLLERR, POLLNVAL
18
19 from zope.interface import implements
20
21 # Twisted imports
22 from twisted.python import log
23 from twisted.internet import posixbase
24 from twisted.internet.interfaces import IReactorFDSet
25
26
27
28 class PollReactor(posixbase.PosixReactorBase, posixbase._PollLikeMixin):
29     """
30     A reactor that uses poll(2).
31
32     @ivar _poller: A L{poll} which will be used to check for I/O
33         readiness.
34
35     @ivar _selectables: A dictionary mapping integer file descriptors to
36         instances of L{FileDescriptor} which have been registered with the
37         reactor.  All L{FileDescriptors} which are currently receiving read or
38         write readiness notifications will be present as values in this
39         dictionary.
40
41     @ivar _reads: A dictionary mapping integer file descriptors to arbitrary
42         values (this is essentially a set).  Keys in this dictionary will be
43         registered with C{_poller} for read readiness notifications which will
44         be dispatched to the corresponding L{FileDescriptor} instances in
45         C{_selectables}.
46
47     @ivar _writes: A dictionary mapping integer file descriptors to arbitrary
48         values (this is essentially a set).  Keys in this dictionary will be
49         registered with C{_poller} for write readiness notifications which will
50         be dispatched to the corresponding L{FileDescriptor} instances in
51         C{_selectables}.
52     """
53     implements(IReactorFDSet)
54
55     _POLL_DISCONNECTED = (POLLHUP | POLLERR | POLLNVAL)
56     _POLL_IN = POLLIN
57     _POLL_OUT = POLLOUT
58
59     def __init__(self):
60         """
61         Initialize polling object, file descriptor tracking dictionaries, and
62         the base class.
63         """
64         self._poller = poll()
65         self._selectables = {}
66         self._reads = {}
67         self._writes = {}
68         posixbase.PosixReactorBase.__init__(self)
69
70
71     def _updateRegistration(self, fd):
72         """Register/unregister an fd with the poller."""
73         try:
74             self._poller.unregister(fd)
75         except KeyError:
76             pass
77
78         mask = 0
79         if fd in self._reads:
80             mask = mask | POLLIN
81         if fd in self._writes:
82             mask = mask | POLLOUT
83         if mask != 0:
84             self._poller.register(fd, mask)
85         else:
86             if fd in self._selectables:
87                 del self._selectables[fd]
88
89     def _dictRemove(self, selectable, mdict):
90         try:
91             # the easy way
92             fd = selectable.fileno()
93             # make sure the fd is actually real.  In some situations we can get
94             # -1 here.
95             mdict[fd]
96         except:
97             # the hard way: necessary because fileno() may disappear at any
98             # moment, thanks to python's underlying sockets impl
99             for fd, fdes in self._selectables.items():
100                 if selectable is fdes:
101                     break
102             else:
103                 # Hmm, maybe not the right course of action?  This method can't
104                 # fail, because it happens inside error detection...
105                 return
106         if fd in mdict:
107             del mdict[fd]
108             self._updateRegistration(fd)
109
110     def addReader(self, reader):
111         """Add a FileDescriptor for notification of data available to read.
112         """
113         fd = reader.fileno()
114         if fd not in self._reads:
115             self._selectables[fd] = reader
116             self._reads[fd] =  1
117             self._updateRegistration(fd)
118
119     def addWriter(self, writer):
120         """Add a FileDescriptor for notification of data available to write.
121         """
122         fd = writer.fileno()
123         if fd not in self._writes:
124             self._selectables[fd] = writer
125             self._writes[fd] =  1
126             self._updateRegistration(fd)
127
128     def removeReader(self, reader):
129         """Remove a Selectable for notification of data available to read.
130         """
131         return self._dictRemove(reader, self._reads)
132
133     def removeWriter(self, writer):
134         """Remove a Selectable for notification of data available to write.
135         """
136         return self._dictRemove(writer, self._writes)
137
138     def removeAll(self):
139         """
140         Remove all selectables, and return a list of them.
141         """
142         return self._removeAll(
143             [self._selectables[fd] for fd in self._reads],
144             [self._selectables[fd] for fd in self._writes])
145
146
147     def doPoll(self, timeout):
148         """Poll the poller for new events."""
149         if timeout is not None:
150             timeout = int(timeout * 1000) # convert seconds to milliseconds
151
152         try:
153             l = self._poller.poll(timeout)
154         except SelectError, e:
155             if e.args[0] == errno.EINTR:
156                 return
157             else:
158                 raise
159         _drdw = self._doReadOrWrite
160         for fd, event in l:
161             try:
162                 selectable = self._selectables[fd]
163             except KeyError:
164                 # Handles the infrequent case where one selectable's
165                 # handler disconnects another.
166                 continue
167             log.callWithLogger(selectable, _drdw, selectable, fd, event)
168
169     doIteration = doPoll
170
171     def getReaders(self):
172         return [self._selectables[fd] for fd in self._reads]
173
174
175     def getWriters(self):
176         return [self._selectables[fd] for fd in self._writes]
177
178
179
180 def install():
181     """Install the poll() reactor."""
182     p = PollReactor()
183     from twisted.internet.main import installReactor
184     installReactor(p)
185
186
187 __all__ = ["PollReactor", "install"]