converted twisted_ex3_server.py to use twisteds.basic and twisteds.join_reactor
This commit is contained in:
@@ -1,73 +1,29 @@
|
|||||||
from twisted.internet import pollreactor; pollreactor.install()
|
from eventlet.twisteds import basic
|
||||||
from twisted.internet.protocol import Factory
|
from eventlet.twisteds import join_reactor
|
||||||
from twisted.internet import reactor
|
|
||||||
from twisted.protocols import basic
|
|
||||||
from twisted.internet.error import ConnectionDone
|
|
||||||
|
|
||||||
from eventlet.api import spawn, get_hub
|
class Chat:
|
||||||
from eventlet.channel import channel
|
|
||||||
|
|
||||||
class LineOnlyReceiver(basic.LineOnlyReceiver):
|
def __init__(self):
|
||||||
|
self.participants = []
|
||||||
|
|
||||||
def __init__(self, channel):
|
def handler(self, conn):
|
||||||
self.channel = channel
|
peer = conn.getPeer()
|
||||||
|
print 'new connection from %s' % (peer, )
|
||||||
def lineReceived(self, line):
|
self.participants.append(conn)
|
||||||
spawn(self.channel.send, line)
|
|
||||||
|
|
||||||
def connectionLost(self, reason):
|
|
||||||
self.channel.send_exception(reason.value)
|
|
||||||
|
|
||||||
|
|
||||||
class line_only_receiver:
|
|
||||||
|
|
||||||
def __init__(self, protocol, channel):
|
|
||||||
self.protocol = protocol
|
|
||||||
self.channel = channel
|
|
||||||
|
|
||||||
def readline(self):
|
|
||||||
return self.channel.receive()
|
|
||||||
|
|
||||||
def send(self, data):
|
|
||||||
self.protocol.transport.write(data)
|
|
||||||
|
|
||||||
def sendline(self, line):
|
|
||||||
self.protocol.sendLine(line)
|
|
||||||
|
|
||||||
# iterator protocol:
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def next(self):
|
|
||||||
try:
|
try:
|
||||||
return self.readline()
|
for line in conn:
|
||||||
except ConnectionDone:
|
print 'received from %s: %s' % (peer, line)
|
||||||
raise StopIteration
|
for buddy in self.participants:
|
||||||
|
if buddy is not conn:
|
||||||
|
buddy.sendline('from %s: %s' % (peer, line))
|
||||||
|
except Exception, ex:
|
||||||
|
print peer, ex
|
||||||
|
else:
|
||||||
|
print peer, 'connection done'
|
||||||
|
finally:
|
||||||
|
self.participants.remove(conn)
|
||||||
|
|
||||||
|
chat = Chat()
|
||||||
class MyFactory(Factory):
|
basic.listenTCP(basic.line_only_receiver, chat.handler, 8007)
|
||||||
protocol = LineOnlyReceiver
|
from twisted.internet import reactor
|
||||||
|
reactor.run()
|
||||||
def __init__(self, handler):
|
|
||||||
self.handler = handler
|
|
||||||
|
|
||||||
def buildProtocol(self, addr):
|
|
||||||
ch = channel()
|
|
||||||
p = self.protocol(ch)
|
|
||||||
p.factory = self
|
|
||||||
spawn(self.handler, line_only_receiver(p, ch))
|
|
||||||
return p
|
|
||||||
|
|
||||||
def handler(linerecv):
|
|
||||||
peer = linerecv.protocol.transport.getPeer()
|
|
||||||
print 'new connection from %s' % (peer, )
|
|
||||||
try:
|
|
||||||
for line in linerecv:
|
|
||||||
print 'received from %s: %s' % (peer, line)
|
|
||||||
print peer, 'connection done'
|
|
||||||
except Exception, ex:
|
|
||||||
print peer, ex
|
|
||||||
|
|
||||||
reactor.listenTCP(8007, MyFactory(handler))
|
|
||||||
get_hub().switch()
|
|
||||||
|
Reference in New Issue
Block a user