From a25d4f4a712857a0d07a39a8b23884d6bd39890e Mon Sep 17 00:00:00 2001
From: str4d <str4d@mail.i2p>
Date: Wed, 16 Oct 2013 20:40:32 -0500
Subject: [PATCH] Migrated Listener to Twisted endpoints Service
---
foolscap/pb.py | 52 ++++++++++++++++++++++++++++++++++------------------
1 file changed, 34 insertions(+), 18 deletions(-)
diff --git a/foolscap/pb.py b/foolscap/pb.py
index 473d853..88a16fa 100644
|
a
|
b
|
|
| 3 | 3 | import os.path, weakref, binascii, re |
| 4 | 4 | from zope.interface import implements |
| 5 | 5 | from twisted.internet import defer, protocol, error |
| | 6 | from twisted.internet.endpoints import serverFromString |
| 6 | 7 | from twisted.application import service, internet |
| 7 | 8 | from twisted.python.failure import Failure |
| 8 | 9 | |
| … |
… |
except ImportError: |
| 25 | 26 | pass |
| 26 | 27 | |
| 27 | 28 | |
| | 29 | # TODO: Only used by foolscap.logging.web, remove when |
| | 30 | # that has been migrated to use Twisted endpoints. |
| 28 | 31 | def parse_strport(port): |
| 29 | 32 | if port.startswith("unix:"): |
| 30 | 33 | raise ValueError("UNIX sockets are not supported for Listeners") |
| … |
… |
class Listener(protocol.ServerFactory): |
| 50 | 53 | |
| 51 | 54 | # this also serves as the ServerFactory |
| 52 | 55 | |
| 53 | | def __init__(self, port, options={}, |
| | 56 | def __init__(self, desc, options={}, |
| 54 | 57 | negotiationClass=negotiate.Negotiation): |
| 55 | 58 | """ |
| 56 | | @type port: string |
| 57 | | @param port: a L{twisted.application.strports} -style description, |
| 58 | | specifying a TCP server |
| | 59 | @type desc: string |
| | 60 | @param desc: a L{twisted.internet.endpoints.serverFromString} -style |
| | 61 | description, specifying the endpoint to use |
| 59 | 62 | """ |
| 60 | | # parse the following 'port' strings: |
| 61 | | # 80 |
| 62 | | # tcp:80 |
| 63 | | # tcp:80:interface=127.0.0.1 |
| 64 | | # we reject UNIX sockets.. I don't know if they ever worked. |
| 65 | | |
| 66 | | portnum, interface = parse_strport(port) |
| 67 | | self.port = port |
| | 63 | self.desc = desc |
| 68 | 64 | self.options = options |
| 69 | 65 | self.negotiationClass = negotiationClass |
| 70 | 66 | self.parentTub = None |
| 71 | 67 | self.tubs = {} |
| 72 | 68 | self.redirects = {} |
| 73 | | self.s = internet.TCPServer(portnum, self, interface=interface) |
| | 69 | # Import the reactor here so we don't mess up reactor selection |
| | 70 | from twisted.internet import reactor |
| | 71 | endpoint = serverFromString(reactor, desc) |
| | 72 | self.s = internet.StreamServerEndpointService(endpoint, self) |
| | 73 | self.port = None |
| 74 | 74 | Listeners.append(self) |
| 75 | 75 | |
| | 76 | def setPort(self, port): |
| | 77 | self.port = port |
| | 78 | |
| 76 | 79 | def getPortnum(self): |
| 77 | 80 | """When this Listener was created with a port string of '0' or |
| 78 | 81 | 'tcp:0' (meaning 'please allocate me something'), and if the Listener |
| … |
… |
class Listener(protocol.ServerFactory): |
| 86 | 89 | """ |
| 87 | 90 | |
| 88 | 91 | assert self.s.running |
| 89 | | return self.s._port.getHost().port |
| | 92 | # TODO: Not all endpoints will necessarily have a port? |
| | 93 | return self.port.getHost().port |
| | 94 | |
| | 95 | def getHost(self): |
| | 96 | """ |
| | 97 | Returns the IAddress provider for the endpoint. |
| | 98 | """ |
| | 99 | # The IListeningPort is started when we get it. |
| | 100 | assert self.port |
| | 101 | return self.port.getHost() |
| 90 | 102 | |
| 91 | 103 | def __repr__(self): |
| 92 | 104 | if self.tubs: |
| 93 | 105 | return "<Listener at 0x%x on %s with tubs %s>" % ( |
| 94 | 106 | abs(id(self)), |
| 95 | | self.port, |
| | 107 | self.desc, |
| 96 | 108 | ",".join([str(k) for k in self.tubs.keys()])) |
| 97 | 109 | return "<Listener at 0x%x on %s with no tubs>" % (abs(id(self)), |
| 98 | | self.port) |
| | 110 | self.desc) |
| 99 | 111 | |
| 100 | 112 | def addTub(self, tub): |
| 101 | 113 | if tub.tubID in self.tubs: |
| 102 | 114 | if tub.tubID is None: |
| 103 | 115 | raise RuntimeError("This Listener (on %s) already has an " |
| 104 | 116 | "unauthenticated Tub, you cannot add a " |
| 105 | | "second one" % self.port) |
| | 117 | "second one" % self.desc) |
| 106 | 118 | raise RuntimeError("This Listener (on %s) is already connected " |
| 107 | | "to TubID '%s'" % (self.port, tub.tubID)) |
| | 119 | "to TubID '%s'" % (self.desc, tub.tubID)) |
| 108 | 120 | self.tubs[tub.tubID] = tub |
| 109 | 121 | if self.parentTub is None: |
| 110 | 122 | self.parentTub = tub |
| 111 | 123 | self.s.setServiceParent(self.parentTub) |
| | 124 | if self.s._waitingForPort: |
| | 125 | self.s._waitingForPort.addCallback(self.setPort) |
| 112 | 126 | |
| 113 | 127 | def removeTub(self, tub): |
| 114 | 128 | # this might return a Deferred, since the removal might cause the |
| … |
… |
class Listener(protocol.ServerFactory): |
| 124 | 138 | # do this? It looks like setServiceParent does |
| 125 | 139 | # disownServiceParent first, so it may glitch. |
| 126 | 140 | self.s.setServiceParent(self.parentTub) |
| | 141 | if self.s._waitingForPort: |
| | 142 | self.s._waitingForPort.addCallback(self.setPort) |
| 127 | 143 | else: |
| 128 | 144 | # no more tubs, this Listener will go away now |
| 129 | 145 | d = self.s.disownServiceParent() |