#include "BackendConnection.h" #include #include #include #include #include #include namespace PhpQml::Bridge { namespace { constexpr int kInitialProbeMs = 0; constexpr int kProbeIntervalMs = 5000; constexpr int kProbeTimeoutMs = 2000; } // namespace BackendConnection::BackendConnection(QObject* parent) : QObject(parent) , m_nam(new QNetworkAccessManager(this)) , m_retryTimer(new QTimer(this)) { m_url = QString::fromUtf8(qgetenv("BRIDGE_URL")); m_token = QString::fromUtf8(qgetenv("BRIDGE_TOKEN")); if (m_url.isEmpty()) { // Dev-mode fallback: matches the spike's hardcoded port and // documents the convention. See PLAN.md §11 *Open Questions* // (system FrankenPHP collision on :8080). m_url = QStringLiteral("http://127.0.0.1:8765"); } m_retryTimer->setSingleShot(false); m_retryTimer->setInterval(kProbeIntervalMs); connect(m_retryTimer, &QTimer::timeout, this, &BackendConnection::probe); QTimer::singleShot(kInitialProbeMs, this, &BackendConnection::probe); m_retryTimer->start(); } BackendConnection::~BackendConnection() = default; BackendConnection* BackendConnection::create(QQmlEngine* engine, QJSEngine*) { return new BackendConnection(engine); } void BackendConnection::restart() { // No-op in dev mode (Phase 1). Phase 4 re-spawns the bundled child. probe(); } void BackendConnection::probe() { if (m_pendingReply) return; QNetworkRequest req; req.setUrl(QUrl(m_url + QStringLiteral("/healthz"))); req.setTransferTimeout(kProbeTimeoutMs); if (!m_token.isEmpty()) { req.setRawHeader("Authorization", "Bearer " + m_token.toUtf8()); } m_pendingReply = m_nam->get(req); connect(m_pendingReply, &QNetworkReply::finished, this, &BackendConnection::onProbeFinished); } void BackendConnection::onProbeFinished() { QNetworkReply* reply = m_pendingReply; m_pendingReply = nullptr; if (!reply) return; reply->deleteLater(); bool ok = false; if (reply->error() == QNetworkReply::NoError) { const int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); if (status == 200) { ok = true; } else { setError(QStringLiteral("/healthz returned HTTP %1").arg(status)); } } else { setError(reply->errorString()); } if (ok) { setError(QString()); m_firstFailureSinceOnline.invalidate(); setState(ConnectionState::Online); return; } // Probe failed. Decide between Reconnecting and Offline based on how // long we've been failing since the last Online. (PLAN.md §5.) if (!m_firstFailureSinceOnline.isValid()) { m_firstFailureSinceOnline.start(); } if (m_firstFailureSinceOnline.hasExpired(m_offlineThresholdMs)) { setState(ConnectionState::Offline); } else { setState(ConnectionState::Reconnecting); } } void BackendConnection::setState(ConnectionState s) { if (m_state == s) return; m_state = s; emit connectionStateChanged(); } void BackendConnection::setError(const QString& msg) { if (m_error == msg) return; m_error = msg; emit errorChanged(); } } // namespace PhpQml::Bridge