Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
commit 6a74b0c70c1b4f9003ac550afcca43175ca27dd1 Author: Peter Hatina phatina@redhat.com Date: Fri Jul 25 12:04:17 2014 +0200
fix synchro methods indication listener startup
cli/lmi/shell/LMIMethod.py | 33 ++++++++---------------- cli/lmi/shell/compat/CIMIndicationListener.py | 23 +++++++++++++---- 2 files changed, 28 insertions(+), 28 deletions(-)
diff --git a/cli/lmi/shell/LMIMethod.py b/cli/lmi/shell/LMIMethod.py index 7b9a316..885f251 100644 --- a/cli/lmi/shell/LMIMethod.py +++ b/cli/lmi/shell/LMIMethod.py @@ -334,36 +334,25 @@ class LMIMethod(LMIWrapperBaseObject): cond.notify() cond.release()
- # Start indication listener. cond = threading.Condition() job_finished = LMIPassByRef(JOB_NOT_FINISHED) job_exception = LMIPassByRef(None) # There needs to be a pattern of at least 8 "X" in a row at the end of # the indication_name. indication_name = "synchro-method-call-XXXXXXXX" - listener = LMIIndicationListener() + + # Start the indication listener + listener = LMIIndicationListener( + "0.0.0.0", + self._INDICATION_DESTINATION_PORT, + LMIShellConfig().cert_file, + LMIShellConfig().key_file) indication_name = listener.add_handler( indication_name, handle_job, cond, job_finished, job_exception) - # Search for free port. - port = LMIMethod._INDICATION_DESTINATION_PORT - for i in xrange(LMIMethod._INDICATION_BIND_TRIES): - try: - cert_file = LMIShellConfig - listener.start( - port, - LMIShellConfig().cert_file, - LMIShellConfig().key_file) - break - except wbem.ConnectionError, e: - port += 1 - except wbem.AuthError, e: - port += 1 - else: - raise LMIIndicationListenerError( - "Can not start indication listener") + listener.start(self._INDICATION_BIND_TRIES) logger.debug( "Choosing port for Sync%s() indications: %d" % - (self._method_name, port)) + (self._method_name, listener.port))
# Search for necessary static filter filter_name = "LMI:%s:Changed" % job_inst.classname @@ -387,7 +376,7 @@ class LMIMethod(LMIWrapperBaseObject): # LMIMethod() and PreferPolling. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: - s.connect((netloc, port)) + s.connect((netloc, listener.port)) except socket.gaierror, e: listener.stop() errorstr = "Can not determine IP address of this machine" @@ -403,7 +392,7 @@ class LMIMethod(LMIWrapperBaseObject): "Name": indication_name, "Destination": "%s://%s:%d/%s%s" % ( "https" if listener.uses_ssl else "http", - destination, port, indication_prefix, indication_name), + destination, listener.port, indication_prefix, indication_name), } cim_handler, _, _ = self._conn.client.create_instance( "CIM_IndicationHandlerCIMXML", diff --git a/cli/lmi/shell/compat/CIMIndicationListener.py b/cli/lmi/shell/compat/CIMIndicationListener.py index 79f49ee..6b8a2d1 100644 --- a/cli/lmi/shell/compat/CIMIndicationListener.py +++ b/cli/lmi/shell/compat/CIMIndicationListener.py @@ -27,6 +27,8 @@ from SocketServer import ThreadingMixIn from BaseHTTPServer import HTTPServer from BaseHTTPServer import BaseHTTPRequestHandler
+from lmi.shell.LMIExceptions import ConnectionError +
class CIMIndicationHandlerCallback(object): """ @@ -138,17 +140,26 @@ class CIMIndicationListener(object): self._server = None self._server_thread = None
- def start(self): + def start(self, retries=1): """ Starts a indication listener.
The indication listener runs in a newly-created thread. + + :param int retries: number of bind retries. """ - try: - self._server = CIMIndicationServer( - (self._hostname, self._port), CIMIndicationHandler) - except socket.error, e: - raise ConnectionError(e) + # Try to create a listener for retries-times. + for i in xrange(retries): + try: + self._server = CIMIndicationServer( + (self._hostname, self._port), CIMIndicationHandler) + break + except socket.error as e: + # We raise the exception, when we run out of retries or TCP port + # is out of range. + self._port += 1 + if i == retries - 1 or self._port >= 65536: + raise ConnectionError(e) self._server._handlers = self._handlers self._server_thread = threading.Thread( target=self._server.serve_forever)
cura-tools-devel@lists.fedorahosted.org