/** Connects the 0MQ sockets */
bool StatusSubscribe::startSocket()
{
    m_socket = m_context->createSocket(ZMQSocket::TYP_SUB, this);
    m_socket->setLinger(0);

    try {
        m_socket->connectTo(m_socketUri);
    }
    catch (const zmq::error_t &e) {
        const QString errorString = QString("Error %1: ").arg(e.num()) + QString(e.what());
        qCritical() << m_debugName << ":" << errorString;
        return false;
    }

    connect(m_socket, &ZMQSocket::messageReceived,
            this, &StatusSubscribe::processSocketMessage);


    for (const auto &topic: m_socketTopics)
    {
        m_socket->subscribeTo(topic);
    }

#ifdef QT_DEBUG
    DEBUG_TAG(1, m_debugName, "sockets connected" << m_socketUri);
#endif

    return true;
}
void ServiceDiscovery::stopQuery(const QString &serviceType)
{
    int queryId = 0; // prevents compiler warning
    bool found = false;

    QMapIterator<int, QString> i(m_queryIdServiceMap);
    while (i.hasNext()) {
        i.next();
        if (i.value() == serviceType)  // query type matching
        {
            found = true;
            queryId = i.key();
            break;
        }
    }

    if (!found)
    {
        return;
    }

    m_jdns->queryCancel(queryId);
    m_queryIdTypeMap.remove(queryId);
    m_queryIdServiceMap.remove(queryId);
    clearAlItems(serviceType);

    DEBUG_TAG(1, "SD", "Stopped query" << queryId << serviceType);
}
/** Processes all message received on socket */
void PreviewSubscribe::processSocketMessage(const QList<QByteArray> &messageList)
{
    Container &rx = m_socketRx;

    if (messageList.length() < 2)  // in case we received insufficient data
    {
        return;
    }

    // we only handle the first two messges
    const auto &topic = messageList.first();
    rx.ParseFromArray(messageList.last().data(), messageList.last().size());

#ifdef QT_DEBUG
    std::string s;
    gpb::TextFormat::PrintToString(rx, &s);
    DEBUG_TAG(3, m_debugName, "received message" << QString::fromStdString(s));
#endif

    // react to any incoming message

    if (m_state == State::Up)
    {
        emit fsmUpMessageReceived(QPrivateSignal());
    }

    emit socketMessageReceived(topic, rx);
}
void ServiceDiscovery::refreshQuery(const QString &serviceType)
{
    int queryId = 0; // prevents compiler warning
    bool found = false;

    QMapIterator<int, QString> i(m_queryIdServiceMap);
    while (i.hasNext()) {
        i.next();
        if (i.value() == serviceType)  // query type matching
        {
            found = true;
            queryId = i.key();
            break;
        }
    }

    if (!found)
    {
        return;
    }

    QJDns::Type queryType = m_serviceTypeMap.value(serviceType);

    m_jdns->queryCancel(queryId);                                       // stop old query
    m_queryIdTypeMap.remove(queryId);
    m_queryIdServiceMap.remove(queryId);

    purgeAllItems(serviceType);                                                   // purge outdated items

    queryId = m_jdns->queryStart(serviceType.toLocal8Bit(), queryType);       // start a new query
    m_queryIdTypeMap.insert(queryId, queryType);
    m_queryIdServiceMap.insert(queryId, serviceType);

    DEBUG_TAG(2, "SD", "Refreshed query" << queryId << serviceType);
}
void QServiceDiscovery::deinitializeMdns()
{
    if (m_jdns == NULL)
    {
        return;
    }

#ifdef QT_DEBUG
    DEBUG_TAG(1, "SD", "Deinitializing JDNS");
#endif

    if (m_running)
    {
        if (m_lookupMode == UnicastDNS)
        {
            m_unicastLookupTimer->stop();
        }

        removeAllServiceTypes();
        m_queryIdItemMap.clear();
        m_queryIdServiceMap.clear();
        m_queryIdTypeMap.clear();
    }

    m_jdns->deleteLater();
    m_jdns = NULL;

#if defined(Q_OS_ANDROID)
    QAndroidJniObject::callStaticMethod<void>("io/machinekit/service/MulticastActivator",
                                              "disable");
#endif

    m_lookupReady = false;                      // lookup no ready anymore
    emit lookupReadyChanged(m_lookupReady);
}
Exemple #6
0
void ConfigBase::fsmLoading()
{
#ifdef QT_DEBUG
    DEBUG_TAG(1, m_debugName, "State LOADING");
#endif
    m_state = State::Loading;
    emit stateChanged(m_state);
}
void LogServiceBase::fsmUp()
{
#ifdef QT_DEBUG
    DEBUG_TAG(1, m_debugName, "State UP");
#endif
    m_state = State::Up;
    emit stateChanged(m_state);
}
void LogServiceBase::fsmDown()
{
#ifdef QT_DEBUG
    DEBUG_TAG(1, m_debugName, "State DOWN");
#endif
    m_state = State::Down;
    emit stateChanged(m_state);
}
Exemple #9
0
void LauncherBase::fsmSynced()
{
#ifdef QT_DEBUG
    DEBUG_TAG(1, m_debugName, "State SYNCED");
#endif
    m_state = State::Synced;
    emit stateChanged(m_state);
}
void HalrcompSubscribe::fsmTrying()
{
#ifdef QT_DEBUG
    DEBUG_TAG(1, m_debugName, "State TRYING");
#endif
    m_state = State::Trying;
    emit stateChanged(m_state);
}
Exemple #11
0
void ConfigBase::fsmTrying()
{
#ifdef QT_DEBUG
    DEBUG_TAG(1, m_debugName, "State TRYING");
#endif
    m_state = State::Trying;
    emit stateChanged(m_state);
}
void StatusSubscribe::fsmUp()
{
#ifdef QT_DEBUG
    DEBUG_TAG(1, m_debugName, "State UP");
#endif
    m_state = State::Up;
    emit stateChanged(m_state);
}
void StatusSubscribe::fsmDown()
{
#ifdef QT_DEBUG
    DEBUG_TAG(1, m_debugName, "State DOWN");
#endif
    m_state = State::Down;
    emit stateChanged(m_state);
}
void ServiceDiscovery::openNetworkSession()
{
    DEBUG_TAG(3, "SD", "trying to open network session");

    // use the default network configuration and make sure that the link is open
    QList<QNetworkConfiguration> availableConfigs;
    QNetworkConfiguration defaultConfig = m_networkConfigManager->defaultConfiguration();

    if (defaultConfig.isValid()) {
        availableConfigs.append(defaultConfig);
    }
    availableConfigs.append(m_networkConfigManager->allConfigurations(QNetworkConfiguration::Discovered));

    DEBUG_TAG(2, "SD", "number of configs: " << availableConfigs.size());

    for (const QNetworkConfiguration &config: availableConfigs)
    {
        if (networkConfigIsQualified(config))
        {
            DEBUG_TAG(2, "SD", "network config: " << config.bearerTypeName() << config.bearerTypeFamily() << config.name());

            if (!m_networkSession.isNull())
            {
                m_networkSession->deleteLater();
            }

            m_networkSession = new QNetworkSession(config, this);

            connect(m_networkSession, &QNetworkSession::opened,
                    this, &ServiceDiscovery::networkSessionOpened);
            connect(m_networkSession, &QNetworkSession::closed,
                    this, &ServiceDiscovery::networkSessionClosed);
            connect(m_networkSession, static_cast<void (QNetworkSession::*)(QNetworkSession::SessionError)>(&QNetworkSession::error),
                    this, &ServiceDiscovery::networkSessionError);

            m_networkSession->open();

            return;
        }
        else
        {
            DEBUG_TAG(2, "SD", "unsupported network config: " << config.bearerTypeName() << config.bearerTypeFamily() << config.name());
        }
    }
}
void QApplicationStatus::stop()
{
#ifdef QT_DEBUG
    DEBUG_TAG(1, "status", "stop")
#endif

    cleanup();
    updateState(Disconnected);  // clears also the error
}
void QApplicationLauncher::stop()
{
#ifdef QT_DEBUG
    DEBUG_TAG(1, m_commandIdentity, "stop")
#endif

    cleanup();

    updateState(Service::Disconnected);  // clears also the error
}
void PreviewSubscribe::fsmUpMessageReceivedEvent()
{
    if (m_state == State::Up)
    {
#ifdef QT_DEBUG
        DEBUG_TAG(1, m_debugName, "Event MESSAGE RECEIVED");
#endif
        // execute actions
     }
}
void QApplicationCommand::stop()
{
#ifdef QT_DEBUG
    DEBUG_TAG(1, "command", "stop")
#endif

    cleanup();

    updateState(Disconnected);
    updateError(NoError, "");   // clear the error here
}
void StatusSubscribe::fsmUpHeartbeatTickEvent()
{
    if (m_state == State::Up)
    {
#ifdef QT_DEBUG
        DEBUG_TAG(1, m_debugName, "Event HEARTBEAT TICK");
#endif
        // execute actions
        resetHeartbeatTimer();
     }
}
void StatusSubscribe::fsmUpAnyMsgReceivedEvent()
{
    if (m_state == State::Up)
    {
#ifdef QT_DEBUG
        DEBUG_TAG(1, m_debugName, "Event ANY MSG RECEIVED");
#endif
        // execute actions
        resetHeartbeatLiveness();
        resetHeartbeatTimer();
     }
}
void QApplicationStatus::start()
{
#ifdef QT_DEBUG
    DEBUG_TAG(1, "status", "start")
#endif
    updateState(Connecting);

    if (connectSockets())
    {
        subscribe();
    }
}
void QApplicationLauncher::start(int index)
{
    if (!m_connected) {
        return;
    }

#ifdef QT_DEBUG
    DEBUG_TAG(1, m_commandIdentity, "starting launcher" << index)
#endif

    m_tx.set_index(index);
    sendCommandMessage(pb::MT_LAUNCHER_START);
}
void ApplicationLauncher::start(int index)
{
    if (!m_synced) {
        return;
    }

#ifdef QT_DEBUG
    DEBUG_TAG(1, debugName(), "starting launcher" << index)
#endif

    m_tx.set_index(index);
    sendLauncherStart(m_tx);
}
Exemple #24
0
void LauncherBase::fsmSyncedLauncherTryingEvent()
{
    if (m_state == State::Synced)
    {
#ifdef QT_DEBUG
        DEBUG_TAG(1, m_debugName, "Event LAUNCHER TRYING");
#endif
        // handle state change
        emit fsmSyncedExited(QPrivateSignal());
        fsmSyncing();
        emit fsmSyncingEntered(QPrivateSignal());
        // execute actions
     }
}
void PreviewSubscribe::fsmTryingConnectedEvent()
{
    if (m_state == State::Trying)
    {
#ifdef QT_DEBUG
        DEBUG_TAG(1, m_debugName, "Event CONNECTED");
#endif
        // handle state change
        emit fsmTryingExited(QPrivateSignal());
        fsmUp();
        emit fsmUpEntered(QPrivateSignal());
        // execute actions
     }
}
Exemple #26
0
void CommandBase::fsmUpCommandTryingEvent()
{
    if (m_state == State::Up)
    {
#ifdef QT_DEBUG
        DEBUG_TAG(1, m_debugName, "Event COMMAND TRYING");
#endif
        // handle state change
        emit fsmUpExited(QPrivateSignal());
        fsmTrying();
        emit fsmTryingEntered(QPrivateSignal());
        // execute actions
     }
}
Exemple #27
0
void ConfigBase::fsmLoadingConfigTryingEvent()
{
    if (m_state == State::Loading)
    {
#ifdef QT_DEBUG
        DEBUG_TAG(1, m_debugName, "Event CONFIG TRYING");
#endif
        // handle state change
        emit fsmLoadingExited(QPrivateSignal());
        fsmTrying();
        emit fsmTryingEntered(QPrivateSignal());
        // execute actions
     }
}
Exemple #28
0
void ConfigBase::fsmLoadingApplicationLoadedEvent()
{
    if (m_state == State::Loading)
    {
#ifdef QT_DEBUG
        DEBUG_TAG(1, m_debugName, "Event APPLICATION LOADED");
#endif
        // handle state change
        emit fsmLoadingExited(QPrivateSignal());
        fsmUp();
        emit fsmUpEntered(QPrivateSignal());
        // execute actions
     }
}
/** Processes all message received on socket */
void StatusSubscribe::processSocketMessage(const QList<QByteArray> &messageList)
{
    Container &rx = m_socketRx;

    if (messageList.length() < 2)  // in case we received insufficient data
    {
        return;
    }

    // we only handle the first two messges
    const auto &topic = messageList.first();
    rx.ParseFromArray(messageList.last().data(), messageList.last().size());

#ifdef QT_DEBUG
    std::string s;
    gpb::TextFormat::PrintToString(rx, &s);
    DEBUG_TAG(3, m_debugName, "received message" << QString::fromStdString(s));
#endif

    // react to any incoming message

    if (m_state == State::Up)
    {
        emit fsmUpAnyMsgReceived(QPrivateSignal());
    }

    // react to ping message
    if (rx.type() == MT_PING)
    {
        return; // ping is uninteresting
    }

    // react to emcstat full update message
    else if (rx.type() == MT_EMCSTAT_FULL_UPDATE)
    {
        if (rx.has_pparams())
        {
            ProtocolParameters pparams = rx.pparams();
            m_heartbeatInterval = pparams.keepalive_timer();
        }

        if (m_state == State::Trying)
        {
            emit fsmTryingFullUpdateReceived(QPrivateSignal());
        }
    }

    emit socketMessageReceived(topic, rx);
}
void QApplicationLauncher::start()
{
#ifdef QT_DEBUG
   DEBUG_TAG(1, m_commandIdentity, "start")
#endif
    m_commandSocketState = Service::Trying;
    updateState(Service::Connecting);

    if (connectSockets())
    {
        subscribe("launcher");
        startCommandHeartbeat();
        sendCommandMessage(pb::MT_PING);
    }
}