Network status aware apps

Am I online or not?

There is a summary at the end for the quick readers.

It is in many modern applications important to be able to more or less gracefully handle if the application user is online or not. For example, there is no need to try to fetch emails if the device (computer, phone, tablet, laptop, …) doesn’t have network (wifi, cable, umts, ip over avian carriers, ..).

KDE has of course made nice functionality for this, and here I will try to describe how to make your app network status aware and what to be aware of in the process.

Solid background

All this is going on in Solid, using two parts of Solid and a bit of KDE’s infrastructure:

  • libsolid
  • networkstatus KDED module

The networkstatus KDED module is trying it best to gather information from the computer to see if one is connected or not. It has the possibility to gather information from various sources, and handle changes and such.

The as of writing available sources that the networkstatus module gathers information from is

  • Network Manager
  • wicd
  • asking the linux kernel thru the abstraction layers ntrack and libnl
  • …connman might be work in progress

Collecting from different sources is important, because some people might have configurations like “Manage wireless thru wicd”, “manage umts dongle with Network Manager” and “manage wired network with /etc/network/interfaces” and all of it needs to be combined.

Solid reports, if wicd thinks “online”, and network manager thinks “offline” that the user is Connected..

Solid States

There is a enum with 5 values in Solid to describe the current state of the network:

Connected is the easy state. Here, Solid knows that you are having some kind of connection according to at least one of the sources. But it might just be a local network connection, and not actually a internet connection. So one here should handle connection failures gracefully.

Unconnected is a similar easy state. Here, Solid knows that you don’t have any internet connection according to the sources it knows about. One should here not try to do any networking.

Connecting and Disconnecting is that a state change is happening, so that you can prepare to react on it.

Solid also has the state of Unknown, used for cases where Solid doesn’t have the required data to actually know it. A example could be a system without any of the data providers. You should here probably try to connect, and be able to handle failures gracefully. Also described as ‘Proceed with caution’.

The bright reader might here notice that one both in the Unknown and Connected case actually should do the same.

Querying for network status

At any given time:

Here, we first query Solid for current status, and then if the state is Connected OR Unknown, then we proceed.

Getting notified

Maybe you don’t want the roundtrip of asking (resulting in a dbus rountrip each time) when you need to know the state changes. Then you of course can get notified.

I guess this should be pretty self explaining.

Summary

  • Ask Solid::Networking::status() for network status
  • Connect to statusChanged(Solid::Networking::Status) from Solid::Networking::Notifier to be aware of changes
  • Treat the states Unknown and Connected the same way.

Oh. And I’m out looking for a job.

6 comments on “Network status aware apps
  1. if(currentStatus==Connected || currentStatus==Unknown) {
    //try to do networking things
    } else {
    //mark application as online.
    }

    else {mark application as offline} surely?

  2. Med says:

    Hi,

    Note thata wicd is not handled properly with 4.7.0. It should normally be fixed for 4.7.1. I cannot wait to test it, i find this is one of the nicest features in a modern desktop.

  3. Noughmad says:

    Hello, thanks for this post, I just implemented the same in Knights (chess program, it enables or disables the ‘play on server’ option).

    However, I am not sure whether the information is always corrent. I’m afraid of my application preventing the user from playing when the computer is connected but Solid reports it isn’t. Can I really rely on Solid doing the right thing?

    I was thinking of adding an ‘override’ option in the configuration, but that would only add clutter.

    • sune says:

      Noughmad: As long as you remember to treat ‘Unknown’ as a possibly Connected state, then you should be pretty safe.

  4. Alejandro Nova says:

    Wish: Akonadi should be the first network-aware app. Background mail checking should STOP when I am not connected, and all mail accounts should be checked when I start a connection (Connecting + Connected events).

1 Pings/Trackbacks for "Network status aware apps"
  1. […] Recently I blogged about Network status aware apps, and some time later, I got asked by a developer “How do I see if I’m online if […]