/usr/share/pyshared/circuits/node/server.py is in python-circuits 2.1.0-2.
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 | # Module: server
# Date: ...
# Author: ...
"""Server
...
"""
from circuits import handler, BaseComponent
from circuits.net.sockets import TCPServer, Write
from .utils import load_event, dump_value
DELIMITER = b"\r\n\r\n"
class Server(BaseComponent):
"""Server
...
"""
channel = "node"
def __init__(self, bind, channel=channel):
super(Server, self).__init__(channel=channel)
self._buffers = {}
TCPServer(bind, channel=self.channel).register(self)
def process(self, sock, packet):
e, id = load_event(packet)
name = "%s_value_changed" % e.name
channel = e.channels[0] if e.channels else self
@handler(name, channel=self)
def on_value_changed(self, event, value):
self.send(value)
self.addHandler(on_value_changed)
v = self.fire(e, *e.channels)
v.notify = True
v.node_trn = id
v.node_sock = sock
def send(self, v):
data = dump_value(v)
packet = data.encode("utf-8") + DELIMITER
self.fire(Write(v.node_sock, packet))
@handler("read")
def _on_read(self, sock, data):
buffer = self._buffers.get(sock, b"")
buffer += data
delimiter = buffer.find(DELIMITER)
if delimiter > 0:
packet = buffer[:delimiter].decode("utf-8")
self._buffers[sock] = buffer[(delimiter + len(DELIMITER)):]
self.process(sock, packet)
|