| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
# or more contributor license agreements. See the NOTICE file |
| 5 |
# distributed with this work for additional information |
| 6 |
# regarding copyright ownership. The ASF licenses this file |
| 7 |
# to you under the Apache License, Version 2.0 (the |
| 8 |
# "License"); you may not use this file except in compliance |
| 9 |
# with the License. You may obtain a copy of the License at |
| 10 |
# |
| 11 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
# |
| 13 |
# Unless required by applicable law or agreed to in writing, |
| 14 |
# software distributed under the License is distributed on an |
| 15 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
# KIND, either express or implied. See the License for the |
| 17 |
# specific language governing permissions and limitations |
| 18 |
# under the License. |
| 19 |
# |
| 20 |
|
| 21 |
import tornado.ioloop |
| 22 |
from proton.reactor import Container as BaseContainer |
| 23 |
from proton.handlers import IOHandler |
| 24 |
|
| 25 |
|
| 26 |
class TornadoLoopHandler: |
| 27 |
|
| 28 |
def __init__(self, loop=None, handler_base=None): |
| 29 |
self.loop = loop or tornado.ioloop.IOLoop.instance() |
| 30 |
self.io = handler_base or IOHandler() |
| 31 |
self.count = 0 |
| 32 |
|
| 33 |
def on_reactor_init(self, event): |
| 34 |
self.reactor = event.reactor |
| 35 |
|
| 36 |
def on_reactor_quiesced(self, event): |
| 37 |
event.reactor.yield_() |
| 38 |
|
| 39 |
def on_unhandled(self, name, event): |
| 40 |
event.dispatch(self.io) |
| 41 |
|
| 42 |
def _events(self, sel): |
| 43 |
events = self.loop.ERROR |
| 44 |
if sel.reading: |
| 45 |
events |= self.loop.READ |
| 46 |
if sel.writing: |
| 47 |
events |= self.loop.WRITE |
| 48 |
return events |
| 49 |
|
| 50 |
def _schedule(self, sel): |
| 51 |
if sel.deadline: |
| 52 |
self.loop.add_timeout(sel.deadline, lambda: self._expired(sel)) |
| 53 |
|
| 54 |
def _expired(self, sel): |
| 55 |
self.reactor.mark() |
| 56 |
sel.expired() |
| 57 |
self._process() |
| 58 |
|
| 59 |
def _process(self): |
| 60 |
self.reactor.process() |
| 61 |
if not self.reactor.quiesced: |
| 62 |
self.loop.add_callback(self._process) |
| 63 |
|
| 64 |
def _callback(self, sel, events): |
| 65 |
if self.loop.READ & events: |
| 66 |
sel.readable() |
| 67 |
if self.loop.WRITE & events: |
| 68 |
sel.writable() |
| 69 |
self._process() |
| 70 |
|
| 71 |
def on_selectable_init(self, event): |
| 72 |
sel = event.context |
| 73 |
if sel.fileno() >= 0: |
| 74 |
self.loop.add_handler(sel.fileno(), lambda fd, events: self._callback(sel, events), self._events(sel)) |
| 75 |
self._schedule(sel) |
| 76 |
self.count += 1 |
| 77 |
|
| 78 |
def on_selectable_updated(self, event): |
| 79 |
sel = event.context |
| 80 |
if sel.fileno() >= 0: |
| 81 |
self.loop.update_handler(sel.fileno(), self._events(sel)) |
| 82 |
self._schedule(sel) |
| 83 |
|
| 84 |
def on_selectable_final(self, event): |
| 85 |
sel = event.context |
| 86 |
if sel.fileno() >= 0: |
| 87 |
self.loop.remove_handler(sel.fileno()) |
| 88 |
sel.close() |
| 89 |
self.count -= 1 |
| 90 |
if self.count == 0: |
| 91 |
self.loop.add_callback(self._stop) |
| 92 |
|
| 93 |
def _stop(self): |
| 94 |
# We could have received a new selectable since we sent the stop |
| 95 |
if self.count == 0: |
| 96 |
self.reactor.stop() |
| 97 |
self.loop.stop() |
| 98 |
|
| 99 |
|
| 100 |
class Container(object): |
| 101 |
def __init__(self, *handlers, **kwargs): |
| 102 |
self.tornado_loop = kwargs.get('loop', tornado.ioloop.IOLoop.instance()) |
| 103 |
kwargs['global_handler'] = TornadoLoopHandler(self.tornado_loop, kwargs.get('handler_base', None)) |
| 104 |
self.container = BaseContainer(*handlers, **kwargs) |
| 105 |
|
| 106 |
def initialise(self): |
| 107 |
self.container.start() |
| 108 |
self.container.process() |
| 109 |
|
| 110 |
def run(self): |
| 111 |
self.initialise() |
| 112 |
self.tornado_loop.start() |
| 113 |
|
| 114 |
def stop(self): |
| 115 |
self.container.stop() |
| 116 |
|
| 117 |
def touch(self): |
| 118 |
self._process() |
| 119 |
|
| 120 |
def _process(self): |
| 121 |
self.container.process() |
| 122 |
if not self.container.quiesced: |
| 123 |
self.tornado_loop.add_callback(self._process) |