Implement initial Python bindings for Open vSwitch database.
[sliver-openvswitch.git] / python / ovs / poller.py
1 # Copyright (c) 2010 Nicira Networks
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import errno
16 import logging
17 import select
18
19 class Poller(object):
20     """High-level wrapper around the "poll" system call.
21
22     Intended usage is for the program's main loop to go about its business
23     servicing whatever events it needs to.  Then, when it runs out of immediate
24     tasks, it calls each subordinate module or object's "wait" function, which
25     in turn calls one (or more) of the functions Poller.fd_wait(),
26     Poller.immediate_wake(), and Poller.timer_wait() to register to be awakened
27     when the appropriate event occurs.  Then the main loop calls
28     Poller.block(), which blocks until one of the registered events happens."""
29
30     def __init__(self):
31         self.__reset()
32
33     def fd_wait(self, fd, events):
34         """Registers 'fd' as waiting for the specified 'events' (which should
35         be select.POLLIN or select.POLLOUT or their bitwise-OR).  The following
36         call to self.block() will wake up when 'fd' becomes ready for one or
37         more of the requested events.
38         
39         The event registration is one-shot: only the following call to
40         self.block() is affected.  The event will need to be re-registered
41         after self.block() is called if it is to persist.
42
43         'fd' may be an integer file descriptor or an object with a fileno()
44         method that returns an integer file descriptor."""
45         self.poll.register(fd, events)
46
47     def __timer_wait(self, msec):
48         if self.timeout < 0 or msec < self.timeout:
49             self.timeout = msec
50
51     def timer_wait(self, msec):
52         """Causes the following call to self.block() to block for no more than
53         'msec' milliseconds.  If 'msec' is nonpositive, the following call to
54         self.block() will not block at all.
55
56         The timer registration is one-shot: only the following call to
57         self.block() is affected.  The timer will need to be re-registered
58         after self.block() is called if it is to persist."""
59         if msec <= 0:
60             self.immediate_wake()
61         else:
62             self.__timer_wait(msec)
63
64     def timer_wait_until(self, msec):
65         """Causes the following call to self.block() to wake up when the
66         current time, as returned by Time.msec(), reaches 'msec' or later.  If
67         'msec' is earlier than the current time, the following call to
68         self.block() will not block at all.
69
70         The timer registration is one-shot: only the following call to
71         self.block() is affected.  The timer will need to be re-registered
72         after self.block() is called if it is to persist."""
73         now = Time.msec()
74         if msec <= now:
75             self.immediate_wake()
76         else:
77             self.__timer_wait(msec - now)
78
79     def immediate_wake(self):
80         """Causes the following call to self.block() to wake up immediately,
81         without blocking."""
82         self.timeout = 0
83
84     def block(self):
85         """Blocks until one or more of the events registered with
86         self.fd_wait() occurs, or until the minimum duration registered with
87         self.timer_wait() elapses, or not at all if self.immediate_wake() has
88         been called."""
89         try:
90             try:
91                 events = self.poll.poll(self.timeout)
92                 self.__log_wakeup(events)
93             except select.error, e:
94                 # XXX rate-limit
95                 error, msg = e
96                 if error != errno.EINTR:
97                     logging.error("poll: %s" % e[1])
98         finally:
99             self.__reset()
100
101     def __log_wakeup(self, events):
102         if not events:
103             logging.debug("%d-ms timeout" % self.timeout)
104         else:
105             for fd, revents in events:
106                 if revents != 0:
107                     s = ""
108                     if revents & select.POLLIN:
109                         s += "[POLLIN]"
110                     if revents & select.POLLOUT:
111                         s += "[POLLOUT]"
112                     if revents & select.POLLERR:
113                         s += "[POLLERR]"
114                     if revents & select.POLLHUP:
115                         s += "[POLLHUP]"
116                     if revents & select.POLLNVAL:
117                         s += "[POLLNVAL]"
118                     logging.debug("%s on fd %d" % (s, fd))
119
120     def __reset(self):
121         self.poll = select.poll()
122         self.timeout = -1            
123