def run(self): logs.print_info("Oczekiwanie na handshake...") msg = self.conn.read() Thread(target=self.__pinger).start() buff = "" while not self.__was_stopped: buff += msg self.last_seen = datetime.datetime.now() if "\n" in msg: esc_string = buff[: buff.index("\n")] buff = buff[buff.index("\n") + 1 :] data = json.loads(un_escape(esc_string)) logs.print_debug("RECEIVED: %s" % data) if "request" in data: Thread(target=partial(self.binder.handle_message, data, self.conn)).start() while not self.__is_connected: pass try: msg = self.conn.read() except socket.error as e: logs.print_warning("socket.error while waiting for server request: %s" % e) self.__is_connected = False if not msg: logs.print_warning("Serwer zamknął połączenie") self.__is_connected = False
def __pinger(self): while not self.__was_stopped: if datetime.datetime.now() - self.last_seen > timeout_time: logs.print_debug("Serwer przekroczył czas oczekiwania na odpowiedz") self.reconnect() # TODO dodać wysyłanie pingów if not self.__is_connected: self.reconnect() time.sleep(1)
def handle_message(self, data, conn): target = self.binds[data["request"]]["target"] datacpy = dict(data) del datacpy["request"] if "msgid" in datacpy: del datacpy["msgid"] ret = target(**datacpy) if "msgid" in data: ret["msgid"] = data["msgid"] logs.print_debug("RETURNING: %s" % ret) conn.send(escape(json.dumps(ret)))
def progress_hook(self, data): if data[u"status"] == u"downloading": self.__speed = data[u"speed"] self.__progress = (data[u'downloaded_bytes'] / data[u'total_bytes']) * 0.99 self.__eta = data[u'eta'] + 10 elif data[u"status"] == u"finished": logs.print_debug(data) self.__progress = 0.99 self.__eta = 10 self.__tmpfilename = data[u'filename'] elif data[u"status"] == u"error": self.success = False self.ended = True
def reconnect(self): logs.print_info("Próba ponownego nawiązania połączenia") try: self.conn.close() except Exception as e: logs.print_debug("exception while closing connection in reconnecting: %s" % e) self.conn = ssl.wrap_socket(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) self.binder.set_connection(self.conn) try: self.conn.settimeout(10) self.conn.connect((config.server_host, config.server_port)) self.conn.settimeout(None) self.__is_connected = True self.last_seen = datetime.datetime.now() logs.print_info("Nawiązano połączenie ponownie") except socket.error as e: logs.print_warning("exception while trying to reconnect: %s " % e)
def start_download(self): if self.downloader is None: try: method = self.queue[0].url.split(';')[0] url = self.queue[0].url.split(';')[1] if method == "youtube-dl": self.downloader = YoutubeDLDownloadThread(url) logs.print_debug("Starting download of %s with youtube-dl" % url) else: logs.print_error("Unknown download method %s" % method) return self.downloader.start() except IndexError as e: logs.print_error("Syntax error in URL: %s" % e) else: logs.print_warning("Download thread already exists")
def add_download(self, url, artist="", album="", track=""): self.queue.append(DownloadObject(url, artist, album, track)) logs.print_debug("added new url to download") if self.downloader is None: self.start_download()