/usr/share/pyshared/eventnet/net.py is in python-fibranet 10-3.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | import struct
import cStringIO
import gherkin as encoder
from twisted.internet import protocol, reactor
import driver
import warnings
warnings.warn("This module has been deprecated in favour of nanotubes.")
class StringQueue(object):
def __init__(self):
self.l_buffer = []
self.s_buffer = ""
def write(self, data):
self.l_buffer.append(data)
def read(self, count=None):
if count > len(self.s_buffer) or count==None:
self.build_s_buffer()
result = self.s_buffer[:count]
self.s_buffer = self.s_buffer[count:]
return result
def build_s_buffer(self):
new_string = "".join(self.l_buffer)
self.s_buffer = "".join((self.s_buffer, new_string))
self.l_buffer = []
def __len__(self):
self.build_s_buffer()
return len(self.s_buffer)
def message_unpacker(data):
while True:
if len(data) >= 4:
size = struct.unpack("!l", data.read(4))[0]
while len(data) < size:
yield None
message = data.read(size)
yield message
else:
yield None
def pack_message(data):
size = struct.pack("!l", len(data))
return "".join((size, data))
class MessageProtocol(protocol.Protocol):
def __init__(self, *args, **kw):
self.buffer = StringQueue()
self.message_unpacker = message_unpacker(self.buffer)
def connectionMade(self):
if self.factory.handleNewConnection:
self.factory.handleNewConnection(self)
def connectionLost(self, reason):
if self.factory.handleCloseConnection:
self.factory.handleCloseConnection(self, reason)
def post(self, name, **kw):
data = pack_message(encoder.dumps((name, kw)))
self.transport.write(data)
def flush(self):
reactor.iterate()
def dataReceived(self, data):
self.buffer.write(data)
for message in self.message_unpacker:
if message is not None:
name, kw = encoder.loads(message)
kw['_conn'] = self
kw['conn'] = self
kw['reply'] = self.post
if hasattr(self, 'event_handler'):
self.event_handler.post(name, **kw)
else:
driver.post(name, **kw)
else: break
class ClientFactory(protocol.ReconnectingClientFactory):
def __init__(self, onConnect,onClose, protocol):
self.handleNewConnection = onConnect
self.handleCloseConnection = onClose
self.maxDelay = 10
self.__protocol = protocol
def buildProtocol(self, addr):
self.resetDelay()
p = self.__protocol()
p.factory = self
return p
class ServerFactory(protocol.ServerFactory):
def __init__(self, onConnect,onClose, protocol):
self.handleNewConnection = onConnect
self.handleCloseConnection = onClose
self.__protocol = protocol
def buildProtocol(self, addr):
p = self.__protocol()
p.factory = self
return p
def listen(port, onConnect, onClose, protocol=MessageProtocol):
"""
Listen on a port.
Calls onConnect (with connection as argument) when a new connection occurs.
Calls onClose (with connection and reason as arguments) when a connection is closed.
"""
reactor.listenTCP(port, ServerFactory(onConnect, onClose, protocol))
def connect(address, port, onConnect, onClose, protocol=MessageProtocol):
"""
Connects to an address and port.
Calls onConnect (with connection as argument) when a new connection occurs.
Calls onClose (with connection and reason as arguments) when the connection is closed.
"""
reactor.connectTCP(address, port, ClientFactory(onConnect, onClose, protocol))
poll = reactor.iterate
def poll_iterator():
"""
An iterator which polls the network stuff as it is iterated.
"""
while True:
yield poll()
|