|
|
@ -111,7 +111,7 @@ function CalibreWireless:find_calibre_server()
|
|
|
|
if dgram and host then
|
|
|
|
if dgram and host then
|
|
|
|
-- replied diagram has greet message from calibre and calibre hostname
|
|
|
|
-- replied diagram has greet message from calibre and calibre hostname
|
|
|
|
-- calibre opds port and calibre socket port we will later connect to
|
|
|
|
-- 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
|
|
|
|
return host, replied_port
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
@ -122,13 +122,13 @@ function CalibreWireless:checkCalibreServer(host, port)
|
|
|
|
local socket = require("socket")
|
|
|
|
local socket = require("socket")
|
|
|
|
local tcp = socket.tcp()
|
|
|
|
local tcp = socket.tcp()
|
|
|
|
tcp:settimeout(5)
|
|
|
|
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.
|
|
|
|
-- 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()
|
|
|
|
tcp:close()
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
return false, err
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Standard JSON/control opcodes receive callback
|
|
|
|
-- Standard JSON/control opcodes receive callback
|
|
|
@ -146,12 +146,15 @@ function CalibreWireless:JSONReceiveCallback(host, port)
|
|
|
|
msg = _("Invalid password")
|
|
|
|
msg = _("Invalid password")
|
|
|
|
this.invalid_password = nil
|
|
|
|
this.invalid_password = nil
|
|
|
|
this:disconnect()
|
|
|
|
this:disconnect()
|
|
|
|
|
|
|
|
logger.warn("invalid password, disconnecting")
|
|
|
|
elseif this.disconnected_by_server then
|
|
|
|
elseif this.disconnected_by_server then
|
|
|
|
msg = _("Disconnected by calibre")
|
|
|
|
msg = _("Disconnected by calibre")
|
|
|
|
this.disconnected_by_server = nil
|
|
|
|
this.disconnected_by_server = nil
|
|
|
|
|
|
|
|
logger.info("disconnected by calibre")
|
|
|
|
else
|
|
|
|
else
|
|
|
|
msg = T(_("Connected to calibre server at %1"),
|
|
|
|
msg = T(_("Connected to calibre server at %1"),
|
|
|
|
BD.ltr(T("%1:%2", this.calibre_socket.host, this.calibre_socket.port)))
|
|
|
|
BD.ltr(T("%1:%2", this.calibre_socket.host, this.calibre_socket.port)))
|
|
|
|
|
|
|
|
logger.info("connected successfully")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = msg,
|
|
|
|
text = msg,
|
|
|
@ -160,10 +163,6 @@ function CalibreWireless:JSONReceiveCallback(host, port)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
this.connect_message = true
|
|
|
|
this.connect_message = true
|
|
|
|
UIManager:scheduleIn(1, this.password_check_callback)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
@ -179,7 +178,7 @@ function CalibreWireless:initCalibreMQ(host, port)
|
|
|
|
self.calibre_socket:start()
|
|
|
|
self.calibre_socket:start()
|
|
|
|
self.calibre_messagequeue = UIManager:insertZMQ(self.calibre_socket)
|
|
|
|
self.calibre_messagequeue = UIManager:insertZMQ(self.calibre_socket)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
logger.info("connected to calibre", host, port)
|
|
|
|
logger.info(string.format("connecting to calibre @ %s:%s", host, port))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- will callback initCalibreMQ if inbox is confirmed to be set
|
|
|
|
-- will callback initCalibreMQ if inbox is confirmed to be set
|
|
|
@ -234,26 +233,36 @@ function CalibreWireless:connect()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
self.connect_message = false
|
|
|
|
self.connect_message = false
|
|
|
|
local host, port
|
|
|
|
local address_type, host, port, ok, err
|
|
|
|
if G_reader_settings:hasNot("calibre_wireless_url") then
|
|
|
|
if G_reader_settings:hasNot("calibre_wireless_url") then
|
|
|
|
host, port = self:find_calibre_server()
|
|
|
|
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
|
|
|
|
else
|
|
|
|
local calibre_url = G_reader_settings:readSetting("calibre_wireless_url")
|
|
|
|
local calibre_url = G_reader_settings:readSetting("calibre_wireless_url")
|
|
|
|
host, port = calibre_url["address"], calibre_url["port"]
|
|
|
|
host, port = calibre_url["address"], calibre_url["port"]
|
|
|
|
if not self:checkCalibreServer(host, port) then
|
|
|
|
address_type = "specified"
|
|
|
|
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
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
if host and port then
|
|
|
|
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")
|
|
|
|
local inbox_dir = G_reader_settings:readSetting("inbox_dir")
|
|
|
|
if inbox_dir then
|
|
|
|
if inbox_dir then
|
|
|
|
CalibreMetadata:init(inbox_dir)
|
|
|
|
CalibreMetadata:init(inbox_dir)
|
|
|
@ -261,17 +270,11 @@ function CalibreWireless:connect()
|
|
|
|
else
|
|
|
|
else
|
|
|
|
self:setInboxDir(host, port)
|
|
|
|
self:setInboxDir(host, port)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
|
|
|
|
logger.info("cannot connect to calibre server")
|
|
|
|
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
|
|
|
|
text = _("Cannot connect to calibre server."),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function CalibreWireless:disconnect()
|
|
|
|
function CalibreWireless:disconnect()
|
|
|
|
logger.info("disconnect from calibre")
|
|
|
|
logger.info("disconnecting from calibre")
|
|
|
|
self.connect_message = false
|
|
|
|
self.connect_message = false
|
|
|
|
if self.calibre_socket then
|
|
|
|
if self.calibre_socket then
|
|
|
|
self.calibre_socket:stop()
|
|
|
|
self.calibre_socket:stop()
|
|
|
|