2012-03-23 11:41:30 -04:00
|
|
|
# Copyright (C) 2012 Vermeer Manufacturing Co.
|
|
|
|
# License: GPLv3 with additional permissions (see README).
|
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
import socket
|
|
|
|
import os
|
2015-05-09 09:09:32 -04:00
|
|
|
import sys
|
|
|
|
import struct
|
|
|
|
|
2015-05-12 19:20:57 -04:00
|
|
|
if sys.platform == "win32":
|
|
|
|
header_len = 2
|
2012-03-03 12:55:38 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# Message classes
|
|
|
|
#
|
|
|
|
|
2012-03-06 13:29:39 -05:00
|
|
|
class Int32(int):
|
2015-04-13 14:07:07 -04:00
|
|
|
pass
|
2012-03-06 13:29:39 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
class Message:
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self, *pvalues):
|
|
|
|
for parameter, value in zip(self.parameters, pvalues):
|
|
|
|
setattr(self, parameter[1], parameter[0](value))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
p = []
|
|
|
|
for parameter in self.parameters:
|
|
|
|
p.append(parameter[1] + "=" + str(getattr(self, parameter[1])))
|
|
|
|
if p:
|
|
|
|
pf = " " + " ".join(p)
|
|
|
|
else:
|
|
|
|
pf = ""
|
|
|
|
return "<" + self.__class__.__name__ + pf + ">"
|
2012-03-03 12:55:38 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
class MessageTick(Message):
|
2015-04-13 14:07:07 -04:00
|
|
|
code = 0
|
|
|
|
parameters = []
|
2012-03-03 12:55:38 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
class MessageGo(Message):
|
2015-04-13 14:07:07 -04:00
|
|
|
code = 1
|
|
|
|
parameters = []
|
2012-03-03 12:55:38 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
class MessageWrite(Message):
|
2015-04-13 14:07:07 -04:00
|
|
|
code = 2
|
|
|
|
parameters = [(str, "name"), (Int32, "index"), (int, "value")]
|
2012-03-03 12:55:38 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
class MessageRead(Message):
|
2015-04-13 14:07:07 -04:00
|
|
|
code = 3
|
|
|
|
parameters = [(str, "name"), (Int32, "index")]
|
2012-03-03 12:55:38 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
class MessageReadReply(Message):
|
2015-04-13 14:07:07 -04:00
|
|
|
code = 4
|
|
|
|
parameters = [(int, "value")]
|
2012-03-03 12:55:38 -05:00
|
|
|
|
|
|
|
message_classes = [MessageTick, MessageGo, MessageWrite, MessageRead, MessageReadReply]
|
|
|
|
|
2015-05-09 09:09:32 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
#
|
|
|
|
# Packing
|
|
|
|
#
|
|
|
|
|
|
|
|
def _pack_int(v):
|
2015-04-13 14:07:07 -04:00
|
|
|
if v == 0:
|
|
|
|
p = [1, 0]
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
def _pack_str(v):
|
2015-04-13 14:07:07 -04:00
|
|
|
p = [ord(c) for c in v]
|
|
|
|
p.append(0)
|
|
|
|
return p
|
2012-03-03 12:55:38 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-05-09 09:09:32 -04:00
|
|
|
def _pack_int16(v):
|
|
|
|
return [v & 0xff,
|
|
|
|
(v & 0xff00) >> 8]
|
|
|
|
|
|
|
|
|
2012-03-06 13:29:39 -05:00
|
|
|
def _pack_int32(v):
|
2015-04-13 14:07:07 -04:00
|
|
|
return [
|
|
|
|
v & 0xff,
|
|
|
|
(v & 0xff00) >> 8,
|
|
|
|
(v & 0xff0000) >> 16,
|
|
|
|
(v & 0xff000000) >> 24
|
|
|
|
]
|
2012-03-06 13:29:39 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
def _pack(message):
|
2015-04-13 14:07:07 -04:00
|
|
|
r = [message.code]
|
|
|
|
for t, p in message.parameters:
|
|
|
|
value = getattr(message, p)
|
|
|
|
assert(isinstance(value, t))
|
|
|
|
if t == int:
|
|
|
|
r += _pack_int(value)
|
|
|
|
elif t == str:
|
|
|
|
r += _pack_str(value)
|
|
|
|
elif t == Int32:
|
|
|
|
r += _pack_int32(value)
|
|
|
|
else:
|
|
|
|
raise TypeError
|
2015-05-09 09:09:32 -04:00
|
|
|
if sys.platform == "win32":
|
2015-05-12 19:20:57 -04:00
|
|
|
size = _pack_int16(len(r) + header_len)
|
2015-05-09 09:09:32 -04:00
|
|
|
r = size + r
|
2015-04-13 14:07:07 -04:00
|
|
|
return bytes(r)
|
2012-03-03 12:55:38 -05:00
|
|
|
|
2015-05-09 09:09:32 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
#
|
|
|
|
# Unpacking
|
|
|
|
#
|
|
|
|
|
2012-03-06 13:29:39 -05:00
|
|
|
def _unpack_int(i, nchunks=None):
|
2015-04-13 14:07:07 -04:00
|
|
|
v = 0
|
|
|
|
power = 1
|
|
|
|
if nchunks is None:
|
|
|
|
nchunks = next(i)
|
|
|
|
for j in range(nchunks):
|
|
|
|
v += power*next(i)
|
|
|
|
power *= 256
|
|
|
|
return v
|
2012-03-03 12:55:38 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
def _unpack_str(i):
|
2015-04-13 14:07:07 -04:00
|
|
|
v = ""
|
|
|
|
c = next(i)
|
|
|
|
while c:
|
|
|
|
v += chr(c)
|
|
|
|
c = next(i)
|
|
|
|
return v
|
2012-03-03 12:55:38 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
def _unpack(message):
|
2015-04-13 14:07:07 -04:00
|
|
|
i = iter(message)
|
|
|
|
code = next(i)
|
|
|
|
msgclass = next(filter(lambda x: x.code == code, message_classes))
|
|
|
|
pvalues = []
|
|
|
|
for t, p in msgclass.parameters:
|
|
|
|
if t == int:
|
|
|
|
v = _unpack_int(i)
|
|
|
|
elif t == str:
|
|
|
|
v = _unpack_str(i)
|
|
|
|
elif t == Int32:
|
|
|
|
v = _unpack_int(i, 4)
|
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
pvalues.append(v)
|
|
|
|
return msgclass(*pvalues)
|
2012-03-03 12:55:38 -05:00
|
|
|
|
2015-05-09 09:09:32 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
#
|
|
|
|
# I/O
|
|
|
|
#
|
|
|
|
|
|
|
|
class PacketTooLarge(Exception):
|
2015-04-13 14:07:07 -04:00
|
|
|
pass
|
2014-10-17 05:08:37 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2012-03-03 12:55:38 -05:00
|
|
|
class Initiator:
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self, sockaddr):
|
|
|
|
self.sockaddr = sockaddr
|
2015-05-09 09:09:32 -04:00
|
|
|
if sys.platform == "win32":
|
|
|
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
else:
|
|
|
|
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
|
|
|
|
self._cleanup_file()
|
2015-04-13 14:07:07 -04:00
|
|
|
self.socket.bind(self.sockaddr)
|
|
|
|
self.socket.listen(1)
|
|
|
|
|
2015-05-12 19:20:57 -04:00
|
|
|
self.ipc_rxbuffer = bytearray()
|
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
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):
|
|
|
|
maxlen = 2048
|
2015-05-09 09:09:32 -04:00
|
|
|
if sys.platform == "win32":
|
2015-05-12 19:20:57 -04:00
|
|
|
def recv_packet():
|
|
|
|
while len(self.ipc_rxbuffer) < header_len:
|
|
|
|
self.ipc_rxbuffer += self.conn.recv(maxlen)
|
|
|
|
packet_len = struct.unpack("<H", self.ipc_rxbuffer[:header_len])[0]
|
|
|
|
while len(self.ipc_rxbuffer) < packet_len:
|
|
|
|
self.ipc_rxbuffer += self.conn.recv(maxlen)
|
|
|
|
packet = self.ipc_rxbuffer[header_len:packet_len]
|
|
|
|
self.ipc_rxbuffer = self.ipc_rxbuffer[packet_len:]
|
|
|
|
return packet
|
|
|
|
packet = recv_packet()
|
2015-05-09 09:09:32 -04:00
|
|
|
else:
|
|
|
|
packet = self.conn.recv(maxlen)
|
2015-05-12 19:20:57 -04:00
|
|
|
if len(packet) < 1:
|
|
|
|
return None
|
|
|
|
if len(packet) >= maxlen:
|
|
|
|
raise PacketTooLarge
|
2015-04-13 14:07:07 -04:00
|
|
|
return _unpack(packet)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if hasattr(self, "conn"):
|
|
|
|
self.conn.shutdown(socket.SHUT_RDWR)
|
|
|
|
self.conn.close()
|
|
|
|
if hasattr(self, "socket"):
|
2015-05-09 09:09:32 -04:00
|
|
|
if sys.platform == "win32":
|
2015-05-12 19:20:57 -04:00
|
|
|
# don't shutdown our socket since closing connection
|
|
|
|
# seems to already have done it. (trigger an error
|
|
|
|
# otherwise)
|
2015-05-09 09:09:32 -04:00
|
|
|
self.socket.close()
|
|
|
|
else:
|
|
|
|
self.socket.shutdown(socket.SHUT_RDWR)
|
|
|
|
self.socket.close()
|
|
|
|
self._cleanup_file()
|