From 0bf43d67babdf3733969c60ed9d2d952c3e2e808 Mon Sep 17 00:00:00 2001 From: Enrico Faulhaber Date: Tue, 16 Jul 2019 14:19:33 +0200 Subject: [PATCH] interface/tcp: provide properties Change-Id: I1145a862bc523b61b77c84dbacece0f0a5ca816b Reviewed-on: https://forge.frm2.tum.de/review/20918 Tested-by: JenkinsCodeReview Reviewed-by: Markus Zolliker Reviewed-by: Sandra Seger Reviewed-by: Enrico Faulhaber --- secop/protocol/interface/tcp.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/secop/protocol/interface/tcp.py b/secop/protocol/interface/tcp.py index 8934db7..8eb6996 100644 --- a/secop/protocol/interface/tcp.py +++ b/secop/protocol/interface/tcp.py @@ -25,9 +25,11 @@ import collections import socket import sys +from secop.datatypes import StringType, IntRange, BoolType from secop.errors import SECoPError from secop.lib import formatException, \ formatExtendedStack, formatExtendedTraceback +from secop.properties import HasProperties, Property from secop.protocol.interface import decode_msg, encode_msg_frame, get_msg from secop.protocol.messages import ERRORPREFIX, \ HELPREPLY, HELPREQUEST, HelpMessage @@ -180,22 +182,37 @@ class TCPRequestHandler(socketserver.BaseRequestHandler): self.request.close() -class TCPServer(socketserver.ThreadingTCPServer): +class TCPServer(HasProperties, socketserver.ThreadingTCPServer): daemon_threads = True allow_reuse_address = True + properties = { + 'bindto' : Property(StringType(), default='localhost:%d' % DEF_PORT, export=False), + 'bindport' : Property(IntRange(1,65535), default=DEF_PORT, export=False), + 'detailed_errors': Property(BoolType(), default=False, export=False), + } + + # XXX: create configurables from Metaclass! + configurables = properties + def __init__(self, name, logger, options, srv): self.dispatcher = srv.dispatcher self.name = name self.log = logger + super(TCPServer, self).__init__() bindto = options.pop('bindto', 'localhost') - portnum = int(options.pop('bindport', DEF_PORT)) - self.detailed_errors = options.pop('detailed_errors', False) + bindport = int(options.pop('bindport', DEF_PORT)) + detailed_errors = options.pop('detailed_errors', False) if ':' in bindto: bindto, _port = bindto.rsplit(':') - portnum = int(_port) + bindport = int(_port) - self.log.info("TCPServer %s binding to %s:%d" % (name, bindto, portnum)) + self.setProperty('bindto', bindto) + self.setProperty('bindport', bindport) + self.setProperty('detailed_errors', detailed_errors) + self.checkProperties() + + self.log.info("TCPServer %s binding to %s:%d" % (name, self.bindto, self.bindport)) socketserver.ThreadingTCPServer.__init__( - self, (bindto, portnum), TCPRequestHandler, bind_and_activate=True) + self, (self.bindto, self.bindport), TCPRequestHandler, bind_and_activate=True) self.log.info("TCPServer initiated")