renamed twistedutil/basic.py to twistedutil/protocol.py;

removed listenXXX shortcuts: use reactor.listenXXX+SpawnFactory;
added buffer_class class attribute to SpawnFactory;
twistedutil: converted class names to CamelCase;
moved LineOnlyReceiverBuffer to twistedutil.protocols.basic;
fixed the examples.
This commit is contained in:
Denis Bilenko
2008-11-04 23:31:54 +06:00
parent 88184317c0
commit 597a2c0914
7 changed files with 64 additions and 105 deletions

View File

@@ -1,6 +1,5 @@
"""Basic twisted protocols converted to synchronous mode"""
from twisted.internet.protocol import ClientCreator, Protocol as _Protocol
from twisted.protocols import basic
from twisted.internet.protocol import ClientCreator, Protocol as twistedProtocol
from twisted.internet.error import ConnectionDone
from twisted.internet.protocol import Factory, _InstanceFactory
from twisted.internet import defer
@@ -39,37 +38,8 @@ def connectTLS(host, port, *args, **kwargs):
chan = protocol.channel = channel()
return buffer_class(protocol, chan)
def listenTCP(port, handler, *args, **kwargs):
from twisted.internet import reactor
buffer_class = kwargs.pop('buffer_class', DEFAULT_BUFFER)
return reactor.listenTCP(port, SpawnFactory(buffer_class, handler), *args, **kwargs)
def listenSSL(port, handler, *args, **kwargs):
from twisted.internet import reactor
buffer_class = kwargs.pop('buffer_class', DEFAULT_BUFFER)
return reactor.listenSSL(port, SpawnFactory(buffer_class, handler), *args, **kwargs)
def listenTLS(port, handler, *args, **kwargs):
from twisted.internet import reactor
buffer_class = kwargs.pop('buffer_class', DEFAULT_BUFFER)
return reactor.listenTLS(port, SpawnFactory(buffer_class, handler), *args, **kwargs)
class SpawnFactory(Factory):
def __init__(self, buffer_class, handler):
self.handler = handler
self.buffer_class = buffer_class
self.protocol = buffer_class.protocol_class
def buildProtocol(self, addr):
protocol = self.protocol()
chan = protocol.channel = channel()
protocol.factory = self
spawn(self.handler, self.buffer_class(protocol, chan))
return protocol
class buffer_base(object):
class BaseBuffer(object):
def __init__(self, protocol, channel):
self.protocol = protocol
@@ -83,7 +53,7 @@ class buffer_base(object):
return getattr(self.protocol.transport, item)
class Protocol(_Protocol):
class Protocol(twistedProtocol):
def dataReceived(self, data):
spawn(self.channel.send, data)
@@ -93,7 +63,7 @@ class Protocol(_Protocol):
self.channel = None # QQQ channel creates a greenlet. does it actually finish and memory is reclaimed?
class unbuffered(buffer_base):
class Unbuffered(BaseBuffer):
protocol_class = Protocol
@@ -159,12 +129,12 @@ class unbuffered(buffer_base):
return result
class buffer(buffer_base):
class Buffer(BaseBuffer):
protocol_class = Protocol
def __init__(self, *args):
buffer_base.__init__(self, *args)
BaseBuffer.__init__(self, *args)
self.buf = ''
def read(self, size=-1):
@@ -303,38 +273,24 @@ class buffer(buffer_base):
raise StopIteration
return res
DEFAULT_BUFFER = buffer
DEFAULT_BUFFER = Buffer
class SpawnFactory(Factory):
buffer_class = DEFAULT_BUFFER
class LineOnlyReceiver(basic.LineOnlyReceiver):
def __init__(self, handler, buffer_class=None):
self.handler = handler
if buffer_class is not None:
self.buffer_class = buffer_class
self.protocol = self.buffer_class.protocol_class
def lineReceived(self, line):
spawn(self.channel.send, line)
def connectionLost(self, reason):
self.channel.send_exception(reason.value)
class line_only_receiver(buffer_base):
protocol_class = LineOnlyReceiver
def readline(self):
return self.channel.receive()
def sendline(self, line):
self.protocol.sendLine(line)
# iterator protocol:
def __iter__(self):
return self
def next(self):
try:
return self.readline()
except ConnectionDone:
raise StopIteration
def buildProtocol(self, addr):
protocol = self.protocol()
chan = protocol.channel = channel()
protocol.factory = self
spawn(self.handler, self.buffer_class(protocol, chan))
return protocol
def __setup_server_tcp(exit='clean', delay=0.1, port=0):

View File

@@ -0,0 +1,34 @@
from twisted.protocols import basic
from twisted.internet.error import ConnectionDone
from eventlet.api import spawn
from eventlet.twistedutil.protocol import BaseBuffer
class LineOnlyReceiver(basic.LineOnlyReceiver):
def lineReceived(self, line):
spawn(self.channel.send, line)
def connectionLost(self, reason):
self.channel.send_exception(reason.value)
class LineOnlyReceiverBuffer(BaseBuffer):
protocol_class = LineOnlyReceiver
def readline(self):
return self.channel.receive()
def sendline(self, line):
self.protocol.sendLine(line)
# iterator protocol:
def __iter__(self):
return self
def next(self):
try:
return self.readline()
except ConnectionDone:
raise StopIteration

View File

@@ -1,33 +0,0 @@
from twisted.internet import defer
from twisted.python import failure
from eventlet.support.greenlet import greenlet
from eventlet import greenlib
from eventlet.api import get_hub, spawn
def block_on(deferred):
cur = greenlet.getcurrent()
def cb(value):
greenlib.switch(cur, value)
def eb(err):
greenlib.switch(cur, exc=(err.type, err.value, err.tb))
deferred.addCallback(cb)
deferred.addErrback(eb)
return get_hub().switch()
def _putResultInDeferred(deferred, f, args, kwargs):
try:
result = f(*args, **kwargs)
except:
f = failure.Failure()
deferred.errback(f)
else:
deferred.callback(result)
def deferToGreenThread(func, *args, **kwargs):
d = defer.Deferred()
spawn(_putResultInDeferred, d, func, args, kwargs)
return d
def callInGreenThread(func, *args, **kwargs):
return spawn(func, *args, **kwargs)

View File

@@ -1,6 +1,7 @@
from eventlet.twisteds import basic
from eventlet.twistedutil.protocol import connectTCP
from eventlet.twistedutil.protocols.basic import LineOnlyReceiverBuffer
conn = basic.connectTCP('www.google.com', 80, buffer_class=basic.line_only_receiver)
conn = connectTCP('www.google.com', 80, buffer_class=LineOnlyReceiverBuffer)
conn.write('GET / HTTP/1.0\r\n\r\n')
for line in conn:
print line

View File

@@ -1,5 +1,6 @@
from eventlet.twisteds import basic
from eventlet.twisteds import join_reactor
from eventlet.twistedutil import join_reactor
from eventlet.twistedutil.protocol import SpawnFactory
from eventlet.twistedutil.protocols.basic import LineOnlyReceiverBuffer
class Chat:
@@ -24,6 +25,6 @@ class Chat:
self.participants.remove(conn)
chat = Chat()
basic.listenTCP(8007, chat.handler, 8007, buffer_class=basic.line_only_receiver)
from twisted.internet import reactor
reactor.listenTCP(8007, SpawnFactory(chat.handler, LineOnlyReceiverBuffer))
reactor.run()

View File

@@ -4,8 +4,8 @@ from twisted.protocols import basic
from xcaplib.green import XCAPClient
from eventlet.twisteds.util import deferToGreenThread
from eventlet.twisteds import join_reactor
from eventlet.twistedutil import deferToGreenThread
from eventlet.twistedutil import join_reactor
class LineOnlyReceiver(basic.LineOnlyReceiver):