Brainstorming: Detecting network state #348

Closed
opened 2023-06-06 15:01:36 +00:00 by jarolrod · 6 comments
jarolrod commented 2023-06-06 15:01:36 +00:00 (Migrated from github.com)

We need to have the ability to detect network state to allow for states such as the following (specified in the design file):

Paused: No Wifi Peers: No Internet
Mobile Node - Peers

We have two options to go down through, at least with platforms aside from android.

1. Enable the Qt Network API

This gives us access to the QNetworkConfigurationManager class. Here we have access to the following signals and functions that give us the information we need:

It should be noted that with these functions, Qt checks that the computer is connected to any signal, and not if that signal really has an internet connection.

2. Use system specific terminal commands in a Process

For example, on macOS, this could be something like the following:


#include <QProcess>

class NetworkManager : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool isOnline READ isOnline NOTIFY isOnlineChanged)

public:
    explicit NetworkManager(QObject *parent = nullptr)
        : QObject(parent), m_isOnline(false)
    {
        checkNetworkAccess();
    }

    bool isOnline() const { return m_isOnline; }

public slots:
    void checkNetworkAccess() {
        QProcess process;
        process.start("scutil --nwi");
        process.waitForFinished();

        QString output = process.readAllStandardOutput();

        // check if output contains 'No network connection'
        m_isOnline = !output.contains("No network");

        emit isOnlineChanged(m_isOnline);
    }

signals:
    void isOnlineChanged(bool isOnline);

private:
    bool m_isOnline;
};

There should be available commands for macOS, linux, and Windows; but this approach won't work for Android, where we'll just plug into some android api for this information.

We need to have the ability to detect network state to allow for states such as the following (specified in the [design file](https://www.figma.com/file/ek8w3n3upbluw5UL2lGhRx/Bitcoin-Core-App-Design?type=design&node-id=616-0&t=5gUyj44qmIrHNZiE-0)): | [Paused: No Wifi](https://www.figma.com/file/ek8w3n3upbluw5UL2lGhRx/Bitcoin-Core-App-Design?type=design&node-id=2643-66762&t=z37cAAHpelf0fv8z-4) | [Peers: No Internet](https://www.figma.com/file/ek8w3n3upbluw5UL2lGhRx/Bitcoin-Core-App-Design?type=design&node-id=7000-28502&t=5gUyj44qmIrHNZiE-4) | | --------------------| ----------- | | <img width="375" alt="Mobile" src="https://git.fish.foo/attachments/0301b026-89c7-4b28-956a-c8c65bb8fd1b"> | ![Node - Peers](https://git.fish.foo/attachments/6635c11d-99ec-4504-a754-eda37209579c) | We have two options to go down through, at least with platforms aside from android. ## 1. Enable the Qt Network API This gives us access to the [QNetworkConfigurationManager](https://doc.qt.io/qt-5/qnetworkconfigurationmanager.html) class. Here we have access to the following signals and functions that give us the information we need: - [isOnline](https://doc.qt.io/qt-5/qnetworkconfigurationmanager.html#isOnline) - [onlineStateChanged](https://doc.qt.io/qt-5/qnetworkconfigurationmanager.html#onlineStateChanged) It should be noted that with these functions, Qt checks that the computer is connected to any signal, and not if that signal really has an internet connection. ## 2. Use system specific terminal commands in a Process For example, on macOS, this could be something like the following: ```c++ #include <QProcess> class NetworkManager : public QObject { Q_OBJECT Q_PROPERTY(bool isOnline READ isOnline NOTIFY isOnlineChanged) public: explicit NetworkManager(QObject *parent = nullptr) : QObject(parent), m_isOnline(false) { checkNetworkAccess(); } bool isOnline() const { return m_isOnline; } public slots: void checkNetworkAccess() { QProcess process; process.start("scutil --nwi"); process.waitForFinished(); QString output = process.readAllStandardOutput(); // check if output contains 'No network connection' m_isOnline = !output.contains("No network"); emit isOnlineChanged(m_isOnline); } signals: void isOnlineChanged(bool isOnline); private: bool m_isOnline; }; ``` There should be available commands for macOS, linux, and Windows; but this approach won't work for Android, where we'll just plug into some android api for this information.
jarolrod commented 2023-06-06 15:01:55 +00:00 (Migrated from github.com)

cc @johnny9 @hebasto @promag

cc @johnny9 @hebasto @promag
jarolrod commented 2023-06-06 23:29:57 +00:00 (Migrated from github.com)
related: https://github.com/bitcoin-core/gui-qml/issues/324
johnny9 commented 2023-06-10 16:01:03 +00:00 (Migrated from github.com)

We can try out the QNetworkInterface class https://doc.qt.io/qt-5/qnetworkinterface.html with the QNetwork module. Qt 6.1 has QNetworkInformation https://doc.qt.io/qt-6/qnetworkinformation.html. I think we should avoid trying to implement platform specific network tests ourselves.

We can try out the QNetworkInterface class https://doc.qt.io/qt-5/qnetworkinterface.html with the QNetwork module. Qt 6.1 has QNetworkInformation https://doc.qt.io/qt-6/qnetworkinformation.html. I think we should avoid trying to implement platform specific network tests ourselves.
thecompez commented 2023-08-03 22:14:51 +00:00 (Migrated from github.com)

Hi!

We have some limitations for QProcess in some platforms such as Android and iOS.
Have you tried the ping method?

Hi! We have some limitations for QProcess in some platforms such as Android and iOS. Have you tried the ping method?
jarolrod commented 2024-09-25 11:17:15 +00:00 (Migrated from github.com)

We don't need Qt API or terminal commands, we can use platform specific api's that don't ping any server to know if the device has internet access

We don't need Qt API or terminal commands, we can use platform specific api's that don't ping any server to know if the device has internet access
jarolrod commented 2024-09-25 11:18:07 +00:00 (Migrated from github.com)

Closing as brainstorming is not needed, and we can move discussion into https://github.com/bitcoin-core/gui-qml/issues/324 and subsequent designs on the no internet access state

Closing as brainstorming is not needed, and we can move discussion into https://github.com/bitcoin-core/gui-qml/issues/324 and subsequent designs on the no internet access state
Sign in to join this conversation.
No description provided.