def acquire_network_resources(self): """ Configure a network connection to the server. The resulting socket is handled by two instances of ServerConnectionReader and ServerConnectionWriter, which are created and run as well. It is possibile to send/receive message to/from them through the queues self.output_message_queue and self._input_queue (servermessage). Note: this private method should be actually "protected" for the ServerSession states, but Python doesn't have such a protection level. """ try: self.logger.debug(u"Creating a socket on %s:%s", self.host, self.port) sock = socket.create_connection((self.host, self.port), timeout=10) ca_chain = os.path.abspath(self.server_certificate) self.sock = ssl.wrap_socket(sock, cert_reqs=ssl.CERT_REQUIRED, ca_certs=ca_chain, ssl_version=ssl.PROTOCOL_TLSv1) self.sock.setblocking(True) match_hostname(self.sock.getpeercert(), self.host) except CertificateError as e: self.logger.critical(u"SSL certificate validation failed: %s" % e) raise ssl.SSLError(e) except socket.error as exception: self.logger.debug(u"Error opening SSL socket: %s" % exception) self.logger.warning( u"Unable to connect, re-trying in %s seconds." % self.reconnection_time) self.reconnection_time = stoppable_exponential_backoff_waiting( self.reconnection_time, self.must_die, 10) self.num_connection_attempts += 1 return False except socket.timeout as exception: self.logger.debug(u"Socket timeout: %s" % exception) self.logger.warning( u"Unable to connect, re-trying in %s seconds." % self.reconnection_time) self.reconnection_time = stoppable_exponential_backoff_waiting( self.reconnection_time, self.must_die, 10) self.num_connection_attempts += 1 return False self.connection_reader = ServerConnectionReader( self._input_queue, self.input_keepalive_queue, self.sock) self.connection_writer = ServerConnectionWriter( self._input_queue, self.output_message_queue, self.sock) self.connection_reader.start() self.connection_writer.start() self.reconnection_time = 1 return True
def acquire_network_resources(self): """ Configure a network connection to the server. The resulting socket is handled by two instances of ServerConnectionReader and ServerConnectionWriter, which are created and run as well. It is possibile to send/receive message to/from them through the queues self.output_message_queue and self._input_queue (servermessage). Note: this private method should be actually "protected" for the ServerSession states, but Python doesn't have such a protection level. """ try: self.logger.debug(u"Creating a socket on %s:%s", self.host, self.port) sock = socket.create_connection((self.host, self.port), timeout=10) ca_chain = os.path.abspath(self.server_certificate) self.sock = ssl.wrap_socket( sock, cert_reqs=ssl.CERT_REQUIRED, ca_certs=ca_chain, ssl_version=ssl.PROTOCOL_TLSv1) self.sock.setblocking(True) match_hostname(self.sock.getpeercert(), self.host) except CertificateError as e: self.logger.critical(u"SSL certificate validation failed: %s" % e) raise ssl.SSLError(e) except socket.error as exception: self.logger.debug(u"Error opening SSL socket: %s" % exception) self.logger.warning( u"Unable to connect, re-trying in %s seconds." % self.reconnection_time) self.reconnection_time = stoppable_exponential_backoff_waiting( self.reconnection_time, self.must_die, 10) self.num_connection_attempts += 1 return False except socket.timeout as exception: self.logger.debug(u"Socket timeout: %s" % exception) self.logger.warning(u"Unable to connect, re-trying in %s seconds." % self.reconnection_time) self.reconnection_time = stoppable_exponential_backoff_waiting( self.reconnection_time, self.must_die, 10) self.num_connection_attempts += 1 return False self.connection_reader = ServerConnectionReader( self._input_queue, self.input_keepalive_queue, self.sock) self.connection_writer = ServerConnectionWriter( self._input_queue, self.output_message_queue, self.sock) self.connection_reader.start() self.connection_writer.start() self.reconnection_time = 1 return True
def _perform_network_transfer(self, transfer_strategy, file_operation): """Does a limited number of attempts to perform the given transfer. In case of failure a certain time interval is awaited and another attempt is performed. The waiting time is doubled each time until the maximum amount of attempts is reached. The transfer could be interrupted in any time by setting terminationEvent. @param transfer_strategy: lambda function wrapping the transfer method. @param file_operation: instance of filerockclient.pathname_operation. """ max_attempts = 10 waiting_time = 1 attempts = 0 while not self.terminationEvent.is_set() and attempts <= max_attempts: self.logger.debug(u'Started network transfer for: %s "%s":' % (file_operation.verb, file_operation.pathname)) response = transfer_strategy(self.terminationEvent) if response['success']: self.logger.debug( u'Successfully ended network transfer' ' for: %s "%s":' % (file_operation.verb, file_operation.pathname)) result = {'status': SUCCESS} if file_operation.verb == 'DOWNLOAD': result['actual_etag'] = response['etag'] return result elif 'termination' in response['details']: result = {'status': INTERRUPTED} return result self.logger.warning( u'HTTP %s failed for operation: %s. ' 'Retrying in %s seconds...' % (file_operation.verb, file_operation, waiting_time)) self.logger.debug(u'Response details: %s' % (response['details'])) waiting_time = stoppable_exponential_backoff_waiting( waiting_time, self.terminationEvent) attempts += 1 if self.terminationEvent.is_set(): # termination requested from outside result = {'status': INTERRUPTED} return result self.logger.error(u'Ok, I have tried performing %s for %d times.' ' I am done now. Put that stuff in a FedEx box and' ' send it via mail.' % (file_operation, max_attempts)) result = {'status': FAILED} return result
def _perform_network_transfer(self, transfer_strategy, file_operation): """Does a limited number of attempts to perform the given transfer. In case of failure a certain time interval is awaited and another attempt is performed. The waiting time is doubled each time until the maximum amount of attempts is reached. The transfer could be interrupted in any time by setting terminationEvent. @param transfer_strategy: lambda function wrapping the transfer method. @param file_operation: instance of filerockclient.pathname_operation. """ max_attempts = 10 waiting_time = 1 attempts = 0 while not self.terminationEvent.is_set() and attempts <= max_attempts: self.logger.debug(u'Started network transfer for: %s "%s":' % (file_operation.verb, file_operation.pathname)) response = transfer_strategy(self.terminationEvent) if response['success']: self.logger.debug(u'Successfully ended network transfer' ' for: %s "%s":' % (file_operation.verb, file_operation.pathname)) result = {'status': SUCCESS} if file_operation.verb == 'DOWNLOAD': result['actual_etag'] = response['etag'] return result elif 'termination' in response['details']: result = {'status': INTERRUPTED} return result self.logger.warning(u'HTTP %s failed for operation: %s. ' 'Retrying in %s seconds...' % (file_operation.verb, file_operation, waiting_time)) self.logger.debug(u'Response details: %s' % (response['details'])) waiting_time = stoppable_exponential_backoff_waiting( waiting_time, self.terminationEvent) attempts += 1 if self.terminationEvent.is_set(): # termination requested from outside result = {'status': INTERRUPTED} return result self.logger.error(u'Ok, I have tried performing %s for %d times.' ' I am done now. Put that stuff in a FedEx box and' ' send it via mail.' % (file_operation, max_attempts)) result = {'status': FAILED} return result
def _perform_network_transfer(self, transfer_strategy, file_operation): """ Does a limited number of attempts to perform the given transfer. In case of failure a certain time interval is awaited and another attempt is performed. The waiting time is doubled each time until the maximum amount of attempts is reached. The transfer could be interrupted in any time by setting terminationEvent @param transfer_strategy: lambda function wrapping the transfer method @param file_operation: instance of filerockclient.pathname_operation """ # # if self.terminationQueue is not None: # event = threading.Event() # termination = TerminationThread(self.terminationQueue, event) # termination.start() max_attempts = 10 waiting_time = 1 attempts = 0 while not self.terminationEvent.is_set() and attempts <= max_attempts: self.logger.debug(u'Started network transfer for: %s "%s":' % (file_operation.verb, file_operation.pathname)) response = transfer_strategy(self.terminationEvent) if response['success']: self.logger.debug(u'Successfully ended network transfer for: %s "%s":' % (file_operation.verb, file_operation.pathname)) return (True, False) else: if 'termination' in response['details']: return (False, True) self.logger.warning(u'HTTP %s failed for operation: %s. Retrying in %s seconds...' % (file_operation.verb, file_operation, waiting_time)) self.logger.debug(u'Response details: %s' % (response['details'])) waiting_time = stoppable_exponential_backoff_waiting(waiting_time, self.terminationEvent) attempts += 1 if self.terminationEvent.is_set(): # termination required from outside return (False, True) self.logger.error(u'Ok, I have tried performing %s for %d times. I am done now. Put that stuff in a FedEx box and send it via mail.' % (file_operation, max_attempts)) return (False, False)