kqueue.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2012 Facebook
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License"); you may
00006 # not use this file except in compliance with the License. You may obtain
00007 # a copy of the License at
00008 #
00009 #     http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00014 # License for the specific language governing permissions and limitations
00015 # under the License.
00016 """KQueue-based IOLoop implementation for BSD/Mac systems."""
00017 from __future__ import absolute_import, division, print_function, with_statement
00018 
00019 import select
00020 
00021 from tornado.ioloop import IOLoop, PollIOLoop
00022 
00023 assert hasattr(select, 'kqueue'), 'kqueue not supported'
00024 
00025 
00026 class _KQueue(object):
00027     """A kqueue-based event loop for BSD/Mac systems."""
00028     def __init__(self):
00029         self._kqueue = select.kqueue()
00030         self._active = {}
00031 
00032     def fileno(self):
00033         return self._kqueue.fileno()
00034 
00035     def close(self):
00036         self._kqueue.close()
00037 
00038     def register(self, fd, events):
00039         if fd in self._active:
00040             raise IOError("fd %s already registered" % fd)
00041         self._control(fd, events, select.KQ_EV_ADD)
00042         self._active[fd] = events
00043 
00044     def modify(self, fd, events):
00045         self.unregister(fd)
00046         self.register(fd, events)
00047 
00048     def unregister(self, fd):
00049         events = self._active.pop(fd)
00050         self._control(fd, events, select.KQ_EV_DELETE)
00051 
00052     def _control(self, fd, events, flags):
00053         kevents = []
00054         if events & IOLoop.WRITE:
00055             kevents.append(select.kevent(
00056                 fd, filter=select.KQ_FILTER_WRITE, flags=flags))
00057         if events & IOLoop.READ or not kevents:
00058             # Always read when there is not a write
00059             kevents.append(select.kevent(
00060                 fd, filter=select.KQ_FILTER_READ, flags=flags))
00061         # Even though control() takes a list, it seems to return EINVAL
00062         # on Mac OS X (10.6) when there is more than one event in the list.
00063         for kevent in kevents:
00064             self._kqueue.control([kevent], 0)
00065 
00066     def poll(self, timeout):
00067         kevents = self._kqueue.control(None, 1000, timeout)
00068         events = {}
00069         for kevent in kevents:
00070             fd = kevent.ident
00071             if kevent.filter == select.KQ_FILTER_READ:
00072                 events[fd] = events.get(fd, 0) | IOLoop.READ
00073             if kevent.filter == select.KQ_FILTER_WRITE:
00074                 if kevent.flags & select.KQ_EV_EOF:
00075                     # If an asynchronous connection is refused, kqueue
00076                     # returns a write event with the EOF flag set.
00077                     # Turn this into an error for consistency with the
00078                     # other IOLoop implementations.
00079                     # Note that for read events, EOF may be returned before
00080                     # all data has been consumed from the socket buffer,
00081                     # so we only check for EOF on write events.
00082                     events[fd] = IOLoop.ERROR
00083                 else:
00084                     events[fd] = events.get(fd, 0) | IOLoop.WRITE
00085             if kevent.flags & select.KQ_EV_ERROR:
00086                 events[fd] = events.get(fd, 0) | IOLoop.ERROR
00087         return events.items()
00088 
00089 
00090 class KQueueIOLoop(PollIOLoop):
00091     def initialize(self, **kwargs):
00092         super(KQueueIOLoop, self).initialize(impl=_KQueue(), **kwargs)


rosbridge_server
Author(s): Jonathan Mace
autogenerated on Wed Sep 13 2017 03:18:20