def thread_main(self): try: with self.lock: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.should_close = True ciel.log("Connecting %s:%s" % (self.otherend_hostname, self.ref.socket_port), "TCP_FETCH", logging.DEBUG) subscribe_remote_output_nopost(self.ref.id, self) self.sock.connect((self.otherend_hostname, self.ref.socket_port)) self.sock.sendall("%s %s %d\n" % (self.ref.id, get_own_netloc(), self.chunk_size)) ciel.log("%s:%s connected: requesting %s (chunk size %d)" % (self.otherend_hostname, self.ref.socket_port, self.ref.id, self.chunk_size), "TCP_FETCH", logging.DEBUG) fp = self.sock.makefile("r", bufsize=0) response = fp.readline().strip() fp.close() with self.lock: self.should_close = False if response.find("GO") != -1: ciel.log("TCP-fetch %s: transfer started" % self.ref.id, "TCP_FETCH", logging.DEBUG) new_fd = os.dup(self.sock.fileno()) self.sock.close() self.fetch_ctx.set_fd(new_fd, True) else: ciel.log("TCP-fetch %s: request failed: other end said '%s'" % (self.ref.id, response), "TCP_FETCH", logging.WARNING) unsubscribe_remote_output_nopost(self.ref.id) self.done = True self.sock.close() self.fetch_ctx.result(False) except Exception as e: unsubscribe_remote_output_nopost(self.ref.id) ciel.log("TCP-fetch %s: failed due to exception %s" % (self.ref.id, repr(e)), "TCP_FETCH", logging.ERROR) with self.lock: if self.should_close: self.sock.close() self.done = True self.should_close = False self.fetch_ctx.result(False)
def unsubscribe(self, fetcher): should_callback = False with self.lock: if self.done: return else: if self.should_close: self.sock.close() self.done = True should_callback = True unsubscribe_remote_output_nopost(self.ref.id) if should_callback: self.fetch_ctx.result(False)