From f6421abab01558d36428e27abcc53fc10039e589 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Sat, 17 Dec 2022 22:37:23 +0100 Subject: [PATCH] Calibre: Log errors on wireless connection failures (#9914) Also, get rid of the weird and clunky `failed_connect_callback` thingy, because it makes no sense? Re: #9908 --- frontend/ui/message/streammessagequeue.lua | 2 +- plugins/calibre.koplugin/wireless.lua | 61 ++++++++++++---------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/frontend/ui/message/streammessagequeue.lua b/frontend/ui/message/streammessagequeue.lua index 8058b5ce5..8560ac4fd 100644 --- a/frontend/ui/message/streammessagequeue.lua +++ b/frontend/ui/message/streammessagequeue.lua @@ -17,7 +17,7 @@ function StreamMessageQueue:start() self.socket = czmq.zsocket_new(self.context, C.ZMQ_STREAM) self.poller = czmq.zpoller_new(self.socket, nil) local endpoint = string.format("tcp://%s:%d", self.host, self.port) - logger.warn("connect to endpoint", endpoint) + logger.dbg("connecting to endpoint", endpoint) local rc = czmq.zsocket_connect(self.socket, endpoint) if rc ~= 0 then error("cannot connect to " .. endpoint) diff --git a/plugins/calibre.koplugin/wireless.lua b/plugins/calibre.koplugin/wireless.lua index 8617a6904..103dbbdde 100644 --- a/plugins/calibre.koplugin/wireless.lua +++ b/plugins/calibre.koplugin/wireless.lua @@ -111,7 +111,7 @@ function CalibreWireless:find_calibre_server() if dgram and host then -- replied diagram has greet message from calibre and calibre hostname -- calibre opds port and calibre socket port we will later connect to - local _, _, _, replied_port = dgram:match("(.-)%(on (.-)%);(.-),(.-)$") + local _, _, replied_port = dgram:match("calibre wireless device client %(on (.-)%);(%d+),(%d+)$") return host, replied_port end end @@ -122,13 +122,13 @@ function CalibreWireless:checkCalibreServer(host, port) local socket = require("socket") local tcp = socket.tcp() tcp:settimeout(5) - local client = tcp:connect(host, port) -- In case of error, the method returns nil followed by a string describing the error. In case of success, the method returns 1. - if client then + local ok, err = tcp:connect(host, port) + if ok then tcp:close() return true end - return false + return false, err end -- Standard JSON/control opcodes receive callback @@ -146,12 +146,15 @@ function CalibreWireless:JSONReceiveCallback(host, port) msg = _("Invalid password") this.invalid_password = nil this:disconnect() + logger.warn("invalid password, disconnecting") elseif this.disconnected_by_server then msg = _("Disconnected by calibre") this.disconnected_by_server = nil + logger.info("disconnected by calibre") else msg = T(_("Connected to calibre server at %1"), BD.ltr(T("%1:%2", this.calibre_socket.host, this.calibre_socket.port))) + logger.info("connected successfully") end UIManager:show(InfoMessage:new{ text = msg, @@ -160,10 +163,6 @@ function CalibreWireless:JSONReceiveCallback(host, port) end this.connect_message = true UIManager:scheduleIn(1, this.password_check_callback) - if this.failed_connect_callback then - -- Don't disconnect if we connect in 10 seconds - UIManager:unschedule(this.failed_connect_callback) - end end end end @@ -179,7 +178,7 @@ function CalibreWireless:initCalibreMQ(host, port) self.calibre_socket:start() self.calibre_messagequeue = UIManager:insertZMQ(self.calibre_socket) end - logger.info("connected to calibre", host, port) + logger.info(string.format("connecting to calibre @ %s:%s", host, port)) end -- will callback initCalibreMQ if inbox is confirmed to be set @@ -234,26 +233,36 @@ function CalibreWireless:connect() end self.connect_message = false - local host, port + local address_type, host, port, ok, err if G_reader_settings:hasNot("calibre_wireless_url") then host, port = self:find_calibre_server() + if host and port then + address_type = "discovered" + else + ok = false + err = _("Couldn't discover a calibre instance on the local network") + address_type = "unavailable" + end else local calibre_url = G_reader_settings:readSetting("calibre_wireless_url") host, port = calibre_url["address"], calibre_url["port"] - if not self:checkCalibreServer(host, port) then - host = nil - else - self.failed_connect_callback = function() - UIManager:show(InfoMessage:new{ - text = _("Cannot connect to calibre server."), - }) - self:disconnect() - end - -- wait 10 seconds to connect to calibre - UIManager:scheduleIn(10, self.failed_connect_callback) - end + address_type = "specified" end + if host and port then + ok, err = self:checkCalibreServer(host, port) + end + + if not ok then + host = host or "????" + port = port or "??" + err = err or _("N/A") + logger.warn(string.format("Cannot connect to %s calibre server at %s:%s (%s)", address_type, host, port, err)) + UIManager:show(InfoMessage:new{ + text = T(_("Cannot connect to calibre server at %1 (%2)"), + BD.ltr(T("%1:%2", host, port)), err) + }) + else local inbox_dir = G_reader_settings:readSetting("inbox_dir") if inbox_dir then CalibreMetadata:init(inbox_dir) @@ -261,17 +270,11 @@ function CalibreWireless:connect() else self:setInboxDir(host, port) end - else - logger.info("cannot connect to calibre server") - UIManager:show(InfoMessage:new{ - text = _("Cannot connect to calibre server."), - }) - return end end function CalibreWireless:disconnect() - logger.info("disconnect from calibre") + logger.info("disconnecting from calibre") self.connect_message = false if self.calibre_socket then self.calibre_socket:stop()