improve Py2/3 compat
Change-Id: I1dfdcb88a492401851d5157c734cd708496bf004 Reviewed-on: https://forge.frm2.tum.de/review/17734 Tested-by: JenkinsCodeReview <bjoern_pedersen@frm2.tum.de> Reviewed-by: Enrico Faulhaber <enrico.faulhaber@frm2.tum.de>
This commit is contained in:
@ -1,4 +1,3 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# *****************************************************************************
|
||||
# This program is free software; you can redistribute it and/or modify it under
|
||||
@ -20,27 +19,81 @@
|
||||
#
|
||||
# *****************************************************************************
|
||||
"""provides tcp interface to the SECoP Server"""
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
import socket
|
||||
import collections
|
||||
import SocketServer
|
||||
|
||||
try:
|
||||
import socketserver # py3
|
||||
except ImportError:
|
||||
import SocketServer as socketserver # py2
|
||||
|
||||
from secop.lib import formatExtendedStack, formatException
|
||||
from secop.protocol.messages import HELPREPLY, Message, HelpMessage
|
||||
|
||||
|
||||
DEF_PORT = 10767
|
||||
MAX_MESSAGE_SIZE = 1024
|
||||
|
||||
from secop.protocol.encoding import ENCODERS
|
||||
from secop.protocol.framing import FRAMERS
|
||||
from secop.protocol.messages import HelpMessage
|
||||
EOL = b'\n'
|
||||
CR = b'\r'
|
||||
SPACE = b' '
|
||||
|
||||
|
||||
class TCPRequestHandler(SocketServer.BaseRequestHandler):
|
||||
def encode_msg_frame(action, specifier=None, data=None):
|
||||
""" encode a msg_tripel into an msg_frame, ready to be sent
|
||||
|
||||
action (and optional specifier) are unicode strings,
|
||||
data may be an json-yfied python object"""
|
||||
action = action.encode('utf-8')
|
||||
if specifier is None:
|
||||
# implicit: data is None
|
||||
return b''.join((action, EOL))
|
||||
specifier = specifier.encode('utf-8')
|
||||
if data:
|
||||
data = data.encode('utf-8')
|
||||
return b''.join((action, SPACE, specifier, SPACE, data, EOL))
|
||||
return b''.join((action, SPACE, specifier, EOL))
|
||||
|
||||
|
||||
def get_msg(_bytes):
|
||||
"""try to deframe the next msg in (binary) input
|
||||
always return a tupel (msg, remaining_input)
|
||||
msg may also be None
|
||||
"""
|
||||
if EOL not in _bytes:
|
||||
return None, _bytes
|
||||
return _bytes.split(EOL, 1)
|
||||
|
||||
|
||||
def decode_msg(msg):
|
||||
"""decode the (binary) msg into a (unicode) msg_tripel"""
|
||||
# check for leading/trailing CR and remove it
|
||||
if msg and msg[0] == CR:
|
||||
msg = msg[1:]
|
||||
if msg and msg[-1] == CR:
|
||||
msg = msg[:-1]
|
||||
|
||||
res = msg.split(b' ', 2)
|
||||
action = res[0].decode('utf-8')
|
||||
if len(res) == 1:
|
||||
return action, None, None
|
||||
specifier = res[1].decode('utf-8')
|
||||
if len(res) == 2:
|
||||
return action, specifier, None
|
||||
data = res[2].decode('utf-8')
|
||||
return action, specifier, data
|
||||
|
||||
|
||||
class TCPRequestHandler(socketserver.BaseRequestHandler):
|
||||
|
||||
def setup(self):
|
||||
self.log = self.server.log
|
||||
# Queue of msgObjects to send
|
||||
self._queue = collections.deque(maxlen=100)
|
||||
self.framing = self.server.framingCLS()
|
||||
self.encoding = self.server.encodingCLS()
|
||||
# self.framing = self.server.framingCLS()
|
||||
# self.encoding = self.server.encodingCLS()
|
||||
|
||||
def handle(self):
|
||||
"""handle a new tcp-connection"""
|
||||
@ -49,6 +102,7 @@ class TCPRequestHandler(SocketServer.BaseRequestHandler):
|
||||
clientaddr = self.client_address
|
||||
serverobj = self.server
|
||||
self.log.info("handling new connection from %s:%d" % clientaddr)
|
||||
data = b''
|
||||
|
||||
# notify dispatcher of us
|
||||
serverobj.dispatcher.add_connection(self)
|
||||
@ -57,14 +111,16 @@ class TCPRequestHandler(SocketServer.BaseRequestHandler):
|
||||
# mysocket.setblocking(False)
|
||||
# start serving
|
||||
while True:
|
||||
# send replys fist, then listen for requests, timing out after 0.1s
|
||||
# send replys first, then listen for requests, timing out after 0.1s
|
||||
while self._queue:
|
||||
# put message into encoder to get frame(s)
|
||||
# put frame(s) into framer to get bytestring
|
||||
# send bytestring
|
||||
outmsg = self._queue.popleft()
|
||||
outframes = self.encoding.encode(outmsg)
|
||||
outdata = self.framing.encode(outframes)
|
||||
#outmsg.mkreply()
|
||||
outdata = encode_msg_frame(*outmsg.serialize())
|
||||
# outframes = self.encoding.encode(outmsg)
|
||||
# outdata = self.framing.encode(outframes)
|
||||
try:
|
||||
mysocket.sendall(outdata)
|
||||
except Exception:
|
||||
@ -72,9 +128,12 @@ class TCPRequestHandler(SocketServer.BaseRequestHandler):
|
||||
|
||||
# XXX: improve: use polling/select here?
|
||||
try:
|
||||
data = mysocket.recv(MAX_MESSAGE_SIZE)
|
||||
except (socket.timeout, socket.error) as e:
|
||||
data = data + mysocket.recv(MAX_MESSAGE_SIZE)
|
||||
except socket.timeout as e:
|
||||
continue
|
||||
except socket.error as e:
|
||||
self.log.exception(e)
|
||||
return
|
||||
# XXX: should use select instead of busy polling
|
||||
if not data:
|
||||
continue
|
||||
@ -82,24 +141,49 @@ class TCPRequestHandler(SocketServer.BaseRequestHandler):
|
||||
# put frames into (de-) coder and if a message appear,
|
||||
# call dispatcher.handle_request(self, message)
|
||||
# dispatcher will queue the reply before returning
|
||||
frames = self.framing.decode(data)
|
||||
if frames is not None:
|
||||
if not frames: # empty list
|
||||
self.queue_reply(HelpMessage(MSGTYPE=reply))
|
||||
for frame in frames:
|
||||
reply = None
|
||||
msg = self.encoding.decode(frame)
|
||||
if msg:
|
||||
serverobj.dispatcher.handle_request(self, msg)
|
||||
while True:
|
||||
origin, data = get_msg(data)
|
||||
if origin is None:
|
||||
break # no more messages to process
|
||||
if not origin: # empty string -> send help message
|
||||
for idx, line in enumerate(HelpMessage.splitlines()):
|
||||
msg = Message(HELPREPLY, specifier='%d' % idx)
|
||||
msg.data = line
|
||||
self.queue_async_reply(msg)
|
||||
continue
|
||||
msg = decode_msg(origin)
|
||||
# construct msgObj from msg
|
||||
try:
|
||||
msgObj = Message(*msg)
|
||||
msgObj.origin = origin.decode('latin-1')
|
||||
msgObj = serverobj.dispatcher.handle_request(self, msgObj)
|
||||
except Exception as err:
|
||||
# create Error Obj instead
|
||||
msgObj.set_error(u'Internal', str(err), {'exception': formatException(),
|
||||
'traceback':formatExtendedStack()})
|
||||
print('--------------------')
|
||||
print(formatException())
|
||||
print('--------------------')
|
||||
print(formatExtendedStack())
|
||||
print('====================')
|
||||
|
||||
if msgObj:
|
||||
self.queue_reply(msgObj)
|
||||
|
||||
def queue_async_reply(self, data):
|
||||
"""called by dispatcher for async data units"""
|
||||
self._queue.append(data)
|
||||
if data:
|
||||
self._queue.append(data)
|
||||
else:
|
||||
self.log.error('should asynq_queue %s' % data)
|
||||
|
||||
def queue_reply(self, data):
|
||||
"""called by dispatcher to queue (sync) replies"""
|
||||
# sync replies go first!
|
||||
self._queue.appendleft(data)
|
||||
if data:
|
||||
self._queue.appendleft(data)
|
||||
else:
|
||||
self.log.error('should queue %s' % data)
|
||||
|
||||
def finish(self):
|
||||
"""called when handle() terminates, i.e. the socket closed"""
|
||||
@ -115,7 +199,7 @@ class TCPRequestHandler(SocketServer.BaseRequestHandler):
|
||||
self.request.close()
|
||||
|
||||
|
||||
class TCPServer(SocketServer.ThreadingTCPServer):
|
||||
class TCPServer(socketserver.ThreadingTCPServer):
|
||||
daemon_threads = True
|
||||
allow_reuse_address = True
|
||||
|
||||
@ -129,12 +213,14 @@ class TCPServer(SocketServer.ThreadingTCPServer):
|
||||
portnum = int(_port)
|
||||
# tcp is a byte stream, so we need Framers (to get frames)
|
||||
# and encoders (to en/decode messages from frames)
|
||||
self.framingCLS = FRAMERS[interfaceopts.pop('framing', 'none')]
|
||||
self.encodingCLS = ENCODERS[interfaceopts.pop('encoding', 'pickle')]
|
||||
interfaceopts.pop('framing') # HACK
|
||||
interfaceopts.pop('encoding') # HACK
|
||||
|
||||
# self.framingCLS = FRAMERS[interfaceopts.pop('framing', 'none')]
|
||||
# self.encodingCLS = ENCODERS[interfaceopts.pop('encoding', 'pickle')]
|
||||
self.log.info("TCPServer binding to %s:%d" % (bindto, portnum))
|
||||
self.log.debug("TCPServer using framing=%s" % self.framingCLS.__name__)
|
||||
self.log.debug("TCPServer using encoding=%s" %
|
||||
self.encodingCLS.__name__)
|
||||
SocketServer.ThreadingTCPServer.__init__(
|
||||
# self.log.debug("TCPServer using framing=%s" % self.framingCLS.__name__)
|
||||
# self.log.debug("TCPServer using encoding=%s" % self.encodingCLS.__name__)
|
||||
socketserver.ThreadingTCPServer.__init__(
|
||||
self, (bindto, portnum), TCPRequestHandler, bind_and_activate=True)
|
||||
self.log.info("TCPServer initiated")
|
||||
|
Reference in New Issue
Block a user