2012-03-03 12:55:38 -05:00
|
|
|
import socket
|
|
|
|
import os
|
|
|
|
|
|
|
|
#
|
|
|
|
# Message classes
|
|
|
|
#
|
|
|
|
|
2012-03-06 13:29:39 -05:00
|
|
|
class Int32(int):
|
|
|
|
pass
|
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
class Message:
|
|
|
|
def __init__(self, *pvalues):
|
|
|
|
for parameter, value in zip(self.parameters, pvalues):
|
|
|
|
assert(isinstance(value, parameter[0]))
|
|
|
|
setattr(self, parameter[1], value)
|
2012-03-04 13:17:03 -05:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
p = ""
|
|
|
|
for parameter in self.parameters:
|
|
|
|
p += parameter[1] + "=" + str(getattr(self, parameter[1]))
|
|
|
|
if p:
|
|
|
|
p = " " + p
|
|
|
|
return "<" + self.__class__.__name__ + p + ">"
|
2012-03-03 12:55:38 -05:00
|
|
|
|
|
|
|
class MessageTick(Message):
|
|
|
|
code = 0
|
|
|
|
parameters = []
|
|
|
|
|
|
|
|
class MessageGo(Message):
|
|
|
|
code = 1
|
|
|
|
parameters = []
|
|
|
|
|
|
|
|
class MessageWrite(Message):
|
|
|
|
code = 2
|
2012-03-06 13:29:39 -05:00
|
|
|
parameters = [(str, "name"), (Int32, "index"), (int, "value")]
|
2012-03-03 12:55:38 -05:00
|
|
|
|
|
|
|
class MessageRead(Message):
|
|
|
|
code = 3
|
2012-03-06 13:29:39 -05:00
|
|
|
parameters = [(str, "name"), (Int32, "index")]
|
2012-03-03 12:55:38 -05:00
|
|
|
|
|
|
|
class MessageReadReply(Message):
|
|
|
|
code = 4
|
|
|
|
parameters = [(int, "value")]
|
|
|
|
|
|
|
|
message_classes = [MessageTick, MessageGo, MessageWrite, MessageRead, MessageReadReply]
|
|
|
|
|
|
|
|
#
|
|
|
|
# Packing
|
|
|
|
#
|
|
|
|
|
|
|
|
def _pack_int(v):
|
2012-03-04 13:17:03 -05:00
|
|
|
p = []
|
|
|
|
while v != 0:
|
|
|
|
p.append(v & 0xff)
|
|
|
|
v >>= 8
|
|
|
|
p.insert(0, len(p))
|
|
|
|
return p
|
2012-03-03 12:55:38 -05:00
|
|
|
|
|
|
|
def _pack_str(v):
|
2012-03-04 13:17:03 -05:00
|
|
|
p = [ord(c) for c in v]
|
|
|
|
p.append(0)
|
|
|
|
return p
|
2012-03-03 12:55:38 -05:00
|
|
|
|
2012-03-06 13:29:39 -05:00
|
|
|
def _pack_int32(v):
|
|
|
|
return [
|
|
|
|
v & 0xff,
|
|
|
|
(v & 0xff00) >> 8,
|
|
|
|
(v & 0xff0000) >> 16,
|
|
|
|
(v & 0xff000000) >> 24
|
|
|
|
]
|
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
def _pack(message):
|
|
|
|
r = [message.code]
|
2012-03-04 13:17:03 -05:00
|
|
|
for t, p in message.parameters:
|
2012-03-03 12:55:38 -05:00
|
|
|
value = getattr(message, p)
|
|
|
|
assert(isinstance(value, t))
|
|
|
|
if t == int:
|
|
|
|
r += _pack_int(value)
|
|
|
|
elif t == str:
|
|
|
|
r += _pack_str(value)
|
2012-03-06 13:29:39 -05:00
|
|
|
elif t == Int32:
|
|
|
|
r += _pack_int32(value)
|
2012-03-03 12:55:38 -05:00
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
return bytes(r)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Unpacking
|
|
|
|
#
|
|
|
|
|
2012-03-06 13:29:39 -05:00
|
|
|
def _unpack_int(i, nchunks=None):
|
2012-03-04 13:17:03 -05:00
|
|
|
v = 0
|
|
|
|
power = 1
|
2012-03-06 13:29:39 -05:00
|
|
|
if nchunks is None:
|
|
|
|
nchunks = next(i)
|
2012-03-04 13:17:03 -05:00
|
|
|
for j in range(nchunks):
|
|
|
|
v += power*next(i)
|
|
|
|
power *= 256
|
|
|
|
return v
|
2012-03-03 12:55:38 -05:00
|
|
|
|
|
|
|
def _unpack_str(i):
|
2012-03-04 13:17:03 -05:00
|
|
|
v = ""
|
|
|
|
c = next(i)
|
|
|
|
while c:
|
|
|
|
v += chr(c)
|
|
|
|
c = next(i)
|
|
|
|
return v
|
2012-03-03 12:55:38 -05:00
|
|
|
|
|
|
|
def _unpack(message):
|
|
|
|
i = iter(message)
|
|
|
|
code = next(i)
|
|
|
|
msgclass = next(filter(lambda x: x.code == code, message_classes))
|
|
|
|
pvalues = []
|
2012-03-04 13:17:03 -05:00
|
|
|
for t, p in msgclass.parameters:
|
2012-03-03 12:55:38 -05:00
|
|
|
if t == int:
|
|
|
|
v = _unpack_int(i)
|
|
|
|
elif t == str:
|
|
|
|
v = _unpack_str(i)
|
2012-03-06 13:29:39 -05:00
|
|
|
elif t == Int32:
|
|
|
|
v = _unpack_int(i, 4)
|
2012-03-03 12:55:38 -05:00
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
pvalues.append(v)
|
|
|
|
return msgclass(*pvalues)
|
|
|
|
|
|
|
|
#
|
|
|
|
# I/O
|
|
|
|
#
|
|
|
|
|
|
|
|
class PacketTooLarge(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Initiator:
|
2012-03-06 07:58:49 -05:00
|
|
|
def __init__(self, sockaddr):
|
2012-03-03 12:55:38 -05:00
|
|
|
self.sockaddr = sockaddr
|
|
|
|
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
|
|
|
|
self._cleanup_file()
|
|
|
|
self.socket.bind(self.sockaddr)
|
|
|
|
self.socket.listen(1)
|
|
|
|
|
|
|
|
def _cleanup_file(self):
|
|
|
|
try:
|
|
|
|
os.remove(self.sockaddr)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
self.conn, addr = self.socket.accept()
|
|
|
|
|
|
|
|
def send(self, message):
|
|
|
|
self.conn.send(_pack(message))
|
|
|
|
|
|
|
|
def recv(self):
|
2012-03-04 13:17:03 -05:00
|
|
|
maxlen = 2048
|
2012-03-03 12:55:38 -05:00
|
|
|
packet = self.conn.recv(maxlen)
|
2012-03-04 13:17:03 -05:00
|
|
|
if len(packet) < 1:
|
|
|
|
return None
|
2012-03-03 12:55:38 -05:00
|
|
|
if len(packet) >= maxlen:
|
|
|
|
raise PacketTooLarge
|
|
|
|
return _unpack(packet)
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
if hasattr(self, "conn"):
|
2012-03-06 09:00:02 -05:00
|
|
|
self.conn.shutdown(socket.SHUT_RDWR)
|
2012-03-03 12:55:38 -05:00
|
|
|
self.conn.close()
|
|
|
|
if hasattr(self, "socket"):
|
2012-03-06 09:00:02 -05:00
|
|
|
self.socket.shutdown(socket.SHUT_RDWR)
|
2012-03-03 12:55:38 -05:00
|
|
|
self.socket.close()
|
|
|
|
self._cleanup_file()
|