[Twisted-Python] Newest Resolver Patch
Moshe Zadka
m at moshez.org
Sun Sep 23 23:37:56 MDT 2001
It seems to work, but I'd like people to play with this
a bit more. Note that mktap's default is the native resolver,
but the transparent upgrade is to DummyResolver, which cannot
resolve at all.
Index: bin/mktap
===================================================================
RCS file: /cvs/Twisted/bin/mktap,v
retrieving revision 1.9
diff -u -r1.9 mktap
--- bin/mktap 2001/08/25 07:16:13 1.9
+++ bin/mktap 2001/09/24 05:36:05
@@ -40,6 +40,7 @@
from twisted.protocols import telnet
from twisted.internet import main,tcp
+from twisted.names import native
from twisted.python import usage
import sys, traceback, os, cPickle
@@ -90,6 +91,7 @@
if not options.append:
a = main.Application(options.args[0], int(options.uid), int(options.gid))
+ a.resolver = native.Resolver()
else:
a = cPickle.load(open(options.append))
for num, proto in mod.getPorts(a, config):
Index: twisted/internet/abstract.py
===================================================================
RCS file: /cvs/Twisted/twisted/internet/abstract.py,v
retrieving revision 1.5
diff -u -r1.5 abstract.py
--- twisted/internet/abstract.py 2001/08/20 04:15:35 1.5
+++ twisted/internet/abstract.py 2001/09/24 05:36:05
@@ -17,7 +17,7 @@
# System Imports
-import types
+import types, string
# Twisted Imports
from twisted.python import threadable, log
@@ -237,6 +237,19 @@
raise NotImplementedError(str(self.__class__)+' has no fileno method')
+
+def isIPAddress(addr):
+ parts = string.split(addr, '.')
+ if len(parts) == 4:
+ try:
+ for part in map(int, parts):
+ if not (0<=part<256):
+ break
+ else:
+ return 1
+ except ValueError:
+ pass
+ return 0
# Sibling Imports
import main
Index: twisted/internet/main.py
===================================================================
RCS file: /cvs/Twisted/twisted/internet/main.py,v
retrieving revision 1.27
diff -u -r1.27 main.py
--- twisted/internet/main.py 2001/09/19 12:18:09 1.27
+++ twisted/internet/main.py 2001/09/24 05:46:21
@@ -50,9 +50,13 @@
if os.name == "posix":
self.uid = uid or os.getuid()
self.gid = gid or os.getgid()
+ self.resolver = DummyResolver()
- persistentVersion = 1
+ persistentVersion = 2
+ def upgradeToVersion2(self):
+ self.resolver = DummyResolver()
+
def upgradeToVersion1(self):
"""Version 1 Persistence Upgrade
"""
@@ -161,6 +165,7 @@
def run(self, save=1):
"""Run this application, running the main loop if necessary.
"""
+ global resolver
if not self.running:
threadable.dispatcher.own(self)
delayeds.extend(self.delayeds)
@@ -174,6 +179,7 @@
return
for service in self.services.values():
service.startService()
+ resolver = self.resolver
self.running = 1
threadable.dispatcher.disown(self)
if not running:
@@ -192,6 +198,11 @@
"""
theTimeouts.later(method, seconds)
+class DummyResolver:
+
+ def resolve(self, address, success, fail):
+ fail()
+
reads = {}
writes = {}
running = None
@@ -199,6 +210,7 @@
if threadable.threaded:
delayeds.append(threadtask.theScheduler)
shutdowns = [theTimeouts.runEverything]
+resolver = DummyResolver()
def shutDown(a=None, b=None):
"""Run all shutdown callbacks (save all running Applications) and exit.
@@ -523,6 +535,7 @@
# currentPlugins = os.path.abspath("TwistedPlugins")
allPlugins = [systemPlugins, userPlugins, confPlugins] #, currentPlugins]
sys.path.extend(allPlugins)
+
# Sibling Import
import process
Index: twisted/internet/tcp.py
===================================================================
RCS file: /cvs/Twisted/twisted/internet/tcp.py,v
retrieving revision 1.22
diff -u -r1.22 tcp.py
--- twisted/internet/tcp.py 2001/09/11 04:08:28 1.22
+++ twisted/internet/tcp.py 2001/09/24 05:46:21
@@ -28,6 +28,7 @@
import socket
import sys
import traceback
+import string
if os.name == 'nt':
EWOULDBLOCK = 10035
@@ -49,7 +50,7 @@
# Sibling Imports
import abstract
-from main import CONNECTION_LOST, CONNECTION_DONE
+import main
class Connection(abstract.FileDescriptor,
protocol.Transport,
@@ -79,9 +80,9 @@
if se.args[0] == EWOULDBLOCK:
return
else:
- return CONNECTION_LOST
+ return main.CONNECTION_LOST
if not data:
- return CONNECTION_LOST
+ return main.CONNECTION_LOST
return self.protocol.dataReceived(data)
def writeSomeData(self, data):
@@ -95,7 +96,7 @@
except socket.error, se:
if se.args[0] == EWOULDBLOCK:
return 0
- return CONNECTION_LOST
+ return main.CONNECTION_LOST
def connectionLost(self):
"""See abstract.FileDescriptor.connectionLost().
@@ -145,7 +146,7 @@
self.port = port
Connection.__init__(self, skt, protocol)
self.doWrite = self.doConnect
- self.doConnect()
+ self.resolveAddress()
self.logstr = self.protocol.__class__.__name__+",client"
if timeout is not None:
main.addTimeout(self.failIfNotConnected, timeout)
@@ -161,13 +162,24 @@
# factored out so as to minimise the code necessary for SecureClient
return socket.socket(socket.AF_INET,socket.SOCK_STREAM)
+ def resolveAddress(self):
+ if abstract.isIPAddress(self.addr[0]):
+ self.setRealdAddress(self.addr[0])
+ else:
+ main.resolver.resolve(self.addr[0], self.setRealAddress,
+ self.failIfNotConnected)
+
+ def setRealAddress(self, address):
+ self.realAddress = address
+ self.doConnect()
+
def doConnect(self):
"""I connect the socket.
Then, call the protocol's makeConnection, and start waiting for data.
"""
try:
- self.socket.connect(self.addr)
+ self.socket.connect((self.realAddress, self.addr[1]))
except socket.error, se:
if se.args[0] == EMYSTERY:
self.startWriting()
Index: twisted/internet/udp.py
===================================================================
RCS file: /cvs/Twisted/twisted/internet/udp.py,v
retrieving revision 1.4
diff -u -r1.4 udp.py
--- twisted/internet/udp.py 2001/09/23 01:44:48 1.4
+++ twisted/internet/udp.py 2001/09/24 05:46:23
@@ -35,8 +35,7 @@
from twisted.python import log
# Sibling Imports
-import abstract
-from main import CONNECTION_LOST, CONNECTION_DONE
+import abstract, main
class Connection(abstract.FileDescriptor,
@@ -61,12 +60,26 @@
self.local = local
self.sessionno = sessionno
self.connected = 1
- self.logstr = "%s,%s,%s (UDP)" % (self.protocol.__class__.__name__, sessionno, self.remote[0])
+ self.logstr = "%s,%s,%s (UDP)" % (self.protocol.__class__.__name__,
+ sessionno, self.remote[0])
+ if abstract.isIPAddress(self.remote[0]):
+ self.realAddress = self.remote[0]
+ else:
+ self.realAddress = None
+ main.resolver.resolve(self.remote[0], self.setRealAddress,
+ self.connectionLost)
+
+
+ def setRealAddress(self, address):
+ self.realAddress = address
+ self.startWriting()
def write(self,data):
res = abstract.FileDescriptor.write(self,data)
if not self.keepConnection:
self.loseConnection()
+ if self.realAddress is None:
+ self.stopWriting()
return res
def writeSomeData(self, data):
@@ -81,7 +94,7 @@
except socket.error, se:
if se.args[0] == EWOULDBLOCK:
return 0
- return CONNECTION_LOST
+ return main.CONNECTION_LOST
else:
return 0
@@ -171,14 +184,11 @@
def createConnection(self, addr):
"""Creates a virtual connection over UDP"""
- try:
- protocol = self.factory.buildProtocol(addr)
- s = self.sessionno
- self.sessionno = s+1
- transport = Connection(self.socket.dup(), protocol, addr, self, s)
- protocol.makeConnection(transport, self)
- except:
- traceback.print_exc(file=log.logfile)
+ protocol = self.factory.buildProtocol(addr)
+ s = self.sessionno
+ self.sessionno = s+1
+ transport = Connection(self.socket.dup(), protocol, addr, self, s)
+ protocol.makeConnection(transport, self)
return transport
def doRead(self):
--
The Official Moshe Zadka FAQ: http://moshez.geek
The Official Moshe Zadka FAQ For Dummies: http://moshez.org
Read the FAQ
More information about the Twisted-Python
mailing list