plumber: fix regex to allow colons in agent names, add file logging for debugging
This commit is contained in:
parent
df0b38aedf
commit
5ac2e67769
|
|
@ -12,6 +12,8 @@
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <cstdarg>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
// Canonicalise the X11 display the way plan9 getns() does: "xxx:0.0" -> "xxx:0"
|
// Canonicalise the X11 display the way plan9 getns() does: "xxx:0.0" -> "xxx:0"
|
||||||
// and '/' -> '_'. Only the trailing ".0" is stripped.
|
// and '/' -> '_'. Only the trailing ".0" is stripped.
|
||||||
|
|
@ -87,24 +89,53 @@ Q_SIGNALS:
|
||||||
protected:
|
protected:
|
||||||
void run() override
|
void run() override
|
||||||
{
|
{
|
||||||
|
static constexpr int PollMs = 200;
|
||||||
|
static constexpr int ReconnectMs = 2000;
|
||||||
|
|
||||||
|
// Log to file for debugging
|
||||||
|
FILE *logf = fopen("/tmp/ollie-plumber.log", "a");
|
||||||
|
auto log = [logf](const char *fmt, ...) {
|
||||||
|
if (!logf) return;
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
vfprintf(logf, fmt, ap);
|
||||||
|
fprintf(logf, "\n");
|
||||||
|
fflush(logf);
|
||||||
|
va_end(ap);
|
||||||
|
};
|
||||||
|
|
||||||
|
log("PlumbReader starting for port %s on %s", qPrintable(m_portName), qPrintable(m_socketPath));
|
||||||
|
|
||||||
|
while (!m_stop.loadRelaxed()) {
|
||||||
NineP nine;
|
NineP nine;
|
||||||
|
log("Connecting to %s...", qPrintable(m_socketPath));
|
||||||
if (!nine.connectAndAttach(m_socketPath, currentUser())) {
|
if (!nine.connectAndAttach(m_socketPath, currentUser())) {
|
||||||
Q_EMIT failed(QStringLiteral("plumb connect: %1").arg(nine.errorString()));
|
log("Connect failed: %s", qPrintable(nine.errorString()));
|
||||||
return;
|
QThread::msleep(ReconnectMs);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
log("Connected, walking to %s...", qPrintable(m_portName));
|
||||||
static constexpr uint32_t PortFid = 1;
|
static constexpr uint32_t PortFid = 1;
|
||||||
if (!nine.walk(PortFid, m_portName) || !nine.open(PortFid, NineP::OREAD)) {
|
if (!nine.walk(PortFid, m_portName)) {
|
||||||
Q_EMIT failed(QStringLiteral("plumb open %1: %2").arg(m_portName, nine.errorString()));
|
log("Walk failed: %s", qPrintable(nine.errorString()));
|
||||||
return;
|
QThread::msleep(ReconnectMs);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
log("Opening %s for read...", qPrintable(m_portName));
|
||||||
|
if (!nine.open(PortFid, NineP::OREAD)) {
|
||||||
|
log("Open failed: %s", qPrintable(nine.errorString()));
|
||||||
|
QThread::msleep(ReconnectMs);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr int PollMs = 200;
|
log("Port %s connected and open!", qPrintable(m_portName));
|
||||||
|
|
||||||
bool pending = false;
|
bool pending = false;
|
||||||
while (!m_stop.loadRelaxed()) {
|
while (!m_stop.loadRelaxed()) {
|
||||||
if (!pending) {
|
if (!pending) {
|
||||||
if (!nine.beginRead(PortFid, 0, nine.msize())) {
|
if (!nine.beginRead(PortFid, 0, nine.msize())) {
|
||||||
Q_EMIT failed(QStringLiteral("plumb read: %1").arg(nine.errorString()));
|
log("beginRead failed: %s", qPrintable(nine.errorString()));
|
||||||
break;
|
break; // reconnect
|
||||||
}
|
}
|
||||||
pending = true;
|
pending = true;
|
||||||
}
|
}
|
||||||
|
|
@ -114,19 +145,29 @@ protected:
|
||||||
if (timedOut)
|
if (timedOut)
|
||||||
continue;
|
continue;
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
if (!m_stop.loadRelaxed())
|
log("Read error: %s", qPrintable(nine.errorString()));
|
||||||
Q_EMIT failed(QStringLiteral("plumb read: %1").arg(nine.errorString()));
|
break; // reconnect
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
pending = false;
|
pending = false;
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
continue;
|
continue;
|
||||||
|
log("Received message: %d bytes", buf.size());
|
||||||
PlumbMsg m;
|
PlumbMsg m;
|
||||||
if (PlumbMsg::unpack(buf, &m)) {
|
if (PlumbMsg::unpack(buf, &m)) {
|
||||||
|
log("Emitting message: %s", m.data.constData());
|
||||||
Q_EMIT message(QString::fromUtf8(m.data), m.lookup(QStringLiteral("addr")),
|
Q_EMIT message(QString::fromUtf8(m.data), m.lookup(QStringLiteral("addr")),
|
||||||
m.wdir);
|
m.wdir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connection lost, wait before reconnecting
|
||||||
|
if (!m_stop.loadRelaxed()) {
|
||||||
|
log("Disconnected, reconnecting in %dms...", ReconnectMs);
|
||||||
|
QThread::msleep(ReconnectMs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log("PlumbReader exiting");
|
||||||
|
if (logf) fclose(logf);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue