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:
@@ -1,6 +1,5 @@
|
|||||||
"""Basic twisted protocols converted to synchronous mode"""
|
"""Basic twisted protocols converted to synchronous mode"""
|
||||||
from twisted.internet.protocol import ClientCreator, Protocol as _Protocol
|
from twisted.internet.protocol import ClientCreator, Protocol as twistedProtocol
|
||||||
from twisted.protocols import basic
|
|
||||||
from twisted.internet.error import ConnectionDone
|
from twisted.internet.error import ConnectionDone
|
||||||
from twisted.internet.protocol import Factory, _InstanceFactory
|
from twisted.internet.protocol import Factory, _InstanceFactory
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
@@ -39,37 +38,8 @@ def connectTLS(host, port, *args, **kwargs):
|
|||||||
chan = protocol.channel = channel()
|
chan = protocol.channel = channel()
|
||||||
return buffer_class(protocol, chan)
|
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):
|
class BaseBuffer(object):
|
||||||
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):
|
|
||||||
|
|
||||||
def __init__(self, protocol, channel):
|
def __init__(self, protocol, channel):
|
||||||
self.protocol = protocol
|
self.protocol = protocol
|
||||||
@@ -83,7 +53,7 @@ class buffer_base(object):
|
|||||||
return getattr(self.protocol.transport, item)
|
return getattr(self.protocol.transport, item)
|
||||||
|
|
||||||
|
|
||||||
class Protocol(_Protocol):
|
class Protocol(twistedProtocol):
|
||||||
|
|
||||||
def dataReceived(self, data):
|
def dataReceived(self, data):
|
||||||
spawn(self.channel.send, 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?
|
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
|
protocol_class = Protocol
|
||||||
|
|
||||||
@@ -159,12 +129,12 @@ class unbuffered(buffer_base):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
class buffer(buffer_base):
|
class Buffer(BaseBuffer):
|
||||||
|
|
||||||
protocol_class = Protocol
|
protocol_class = Protocol
|
||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
buffer_base.__init__(self, *args)
|
BaseBuffer.__init__(self, *args)
|
||||||
self.buf = ''
|
self.buf = ''
|
||||||
|
|
||||||
def read(self, size=-1):
|
def read(self, size=-1):
|
||||||
@@ -303,38 +273,24 @@ class buffer(buffer_base):
|
|||||||
raise StopIteration
|
raise StopIteration
|
||||||
return res
|
return res
|
||||||
|
|
||||||
DEFAULT_BUFFER = buffer
|
DEFAULT_BUFFER = Buffer
|
||||||
|
|
||||||
|
class SpawnFactory(Factory):
|
||||||
|
|
||||||
class LineOnlyReceiver(basic.LineOnlyReceiver):
|
buffer_class = DEFAULT_BUFFER
|
||||||
|
|
||||||
def lineReceived(self, line):
|
def __init__(self, handler, buffer_class=None):
|
||||||
spawn(self.channel.send, line)
|
self.handler = handler
|
||||||
|
if buffer_class is not None:
|
||||||
|
self.buffer_class = buffer_class
|
||||||
|
self.protocol = self.buffer_class.protocol_class
|
||||||
|
|
||||||
def connectionLost(self, reason):
|
def buildProtocol(self, addr):
|
||||||
self.channel.send_exception(reason.value)
|
protocol = self.protocol()
|
||||||
|
chan = protocol.channel = channel()
|
||||||
|
protocol.factory = self
|
||||||
class line_only_receiver(buffer_base):
|
spawn(self.handler, self.buffer_class(protocol, chan))
|
||||||
|
return protocol
|
||||||
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 __setup_server_tcp(exit='clean', delay=0.1, port=0):
|
def __setup_server_tcp(exit='clean', delay=0.1, port=0):
|
0
eventlet/twistedutil/protocols/__init__.py
Normal file
0
eventlet/twistedutil/protocols/__init__.py
Normal file
34
eventlet/twistedutil/protocols/basic.py
Normal file
34
eventlet/twistedutil/protocols/basic.py
Normal 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
|
||||||
|
|
@@ -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)
|
|
||||||
|
|
@@ -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')
|
conn.write('GET / HTTP/1.0\r\n\r\n')
|
||||||
for line in conn:
|
for line in conn:
|
||||||
print line
|
print line
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
from eventlet.twisteds import basic
|
from eventlet.twistedutil import join_reactor
|
||||||
from eventlet.twisteds import join_reactor
|
from eventlet.twistedutil.protocol import SpawnFactory
|
||||||
|
from eventlet.twistedutil.protocols.basic import LineOnlyReceiverBuffer
|
||||||
|
|
||||||
class Chat:
|
class Chat:
|
||||||
|
|
||||||
@@ -24,6 +25,6 @@ class Chat:
|
|||||||
self.participants.remove(conn)
|
self.participants.remove(conn)
|
||||||
|
|
||||||
chat = Chat()
|
chat = Chat()
|
||||||
basic.listenTCP(8007, chat.handler, 8007, buffer_class=basic.line_only_receiver)
|
|
||||||
from twisted.internet import reactor
|
from twisted.internet import reactor
|
||||||
|
reactor.listenTCP(8007, SpawnFactory(chat.handler, LineOnlyReceiverBuffer))
|
||||||
reactor.run()
|
reactor.run()
|
||||||
|
@@ -4,8 +4,8 @@ from twisted.protocols import basic
|
|||||||
|
|
||||||
from xcaplib.green import XCAPClient
|
from xcaplib.green import XCAPClient
|
||||||
|
|
||||||
from eventlet.twisteds.util import deferToGreenThread
|
from eventlet.twistedutil import deferToGreenThread
|
||||||
from eventlet.twisteds import join_reactor
|
from eventlet.twistedutil import join_reactor
|
||||||
|
|
||||||
class LineOnlyReceiver(basic.LineOnlyReceiver):
|
class LineOnlyReceiver(basic.LineOnlyReceiver):
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user