def connectionLost(self, reason=ConnectionDone): if isinstance(reason, ConnectionLost): self.errorOut('connection lost: {}'.format(reason)) elif self.waiting_for_response: self.errorOut('Server closed connection without sending a response.') else: log.debug('connection terminated.')
def requestTimedOut(self): self.retry_count += 1 if self.retry_count > self.retries: self.errorOut("Request timed out; server failed to respond.") else: log.debug('Retrying...') self.connectionMade()
def sendRequest(self, command, **params): self.sent_request = DaqServerRequest(command, params) request_string = self.sent_request.serialize() log.debug('sending request: {}'.format(request_string)) self.transport.write(''.join([request_string, '\r\n'])) self.timeoutCallback = reactor.callLater(self.timeout, self.requestTimedOut) self.waiting_for_response = True
def waitForTransfersToCompleteAndExit(self): if self.transfers_in_progress: reactor.callLater(self.wait_delay, self.waitForTransfersToCompleteAndExit) else: log.debug('Stopping the reactor.') reactor.stop()
def resumeProducing(self): if not self.proto: raise ProtocolError('resumeProducing called with no protocol set.') self._paused = False try: while not self._paused: line = self.fh.next().rstrip('\n') + '\r\n' self.proto.transport.write(line) except StopIteration: log.debug('Sent everything.') self.stopProducing()
def resumeProducing(self): if not self.proto: raise ProtocolError('resumeProducing called with no protocol set.') self._paused = False try: while not self._paused: line = next(self.fh).rstrip('\n') + '\r\n' if sys.version_info[0] == 3: self.proto.transport.write(line.encode('utf-8')) else: self.proto.transport.write(line) except StopIteration: log.debug('Sent everything.') self.stopProducing()
def __init__(self, config, command, timeout=10, retries=1): self.config = config self.command = command self.timeout = timeout self.retries = retries self.result = CommandResult() self.done = False self.transfers_in_progress = {} if command.name == 'get_data': if 'output_directory' not in command.params: self.errorOut('output_directory not specifed for get_data command.') self.output_directory = command.params['output_directory'] if not os.path.isdir(self.output_directory): log.debug('Creating output directory {}'.format(self.output_directory)) os.makedirs(self.output_directory)
def perform_cleanup(self): """ Cleanup and old uncollected data files to recover disk space. """ log.msg('Performing cleanup of the output directory...') base_directory = self.server.base_output_directory current_time = datetime.now() for entry in os.listdir(base_directory): entry_path = os.path.join(base_directory, entry) entry_ctime = datetime.fromtimestamp(os.path.getctime(entry_path)) existence_time = current_time - entry_ctime if existence_time > self.cleanup_threshold: log.debug('Removing {} (existed for {})'.format(entry, existence_time)) shutil.rmtree(entry_path) else: log.debug('Keeping {} (existed for {})'.format(entry, existence_time)) log.msg('Cleanup complete.')
def perform_cleanup(self): """ Cleanup and old uncollected data files to recover disk space. """ log.msg('Performing cleanup of the output directory...') base_directory = self.server.base_output_directory current_time = datetime.now() for entry in os.listdir(base_directory): entry_path = os.path.join(base_directory, entry) entry_ctime = datetime.fromtimestamp(os.path.getctime(entry_path)) existence_time = current_time - entry_ctime if existence_time > self.cleanup_threshold: log.debug('Removing {} (existed for {})'.format( entry, existence_time)) shutil.rmtree(entry_path) else: log.debug('Keeping {} (existed for {})'.format( entry, existence_time)) log.msg('Cleanup complete.')
def stop(self): self.is_running = False log.debug('Stopping DAQ Task.') self.task.StopTask() log.debug('Stopping sample processor.') self.processor.stop() log.debug('Runner stopped.')
def start(self): log.debug('Starting sample processor.') self.processor.start() log.debug('Starting DAQ Task.') self.task.StartTask() self.is_running = True log.debug('Runner started.')
def clientConnectionLost(self, connector, reason): if self.transfers_in_progress: log.debug('Waiting for the transfer(s) to complete.') self.waitForTransfersToCompleteAndExit()
def transferComplete(self, session): connector = self.transfers_in_progress[session] log.debug('Transfer on port {} complete.'.format(connector.port)) del self.transfers_in_progress[session]
def initiateFileTransfer(self, filename, port): log.debug('Downloading {} from port {}'.format(filename, port)) filepath = os.path.join(self.output_directory, filename) session = FileReceiverFactory(filepath, self) connector = reactor.connectTCP(self.config.host, port, session) self.transfers_in_progress[session] = connector