Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Connection lost on first ping after switching to wifi
Connection lost on first ping after switching to wifi [message #1711599] Sat, 17 October 2015 23:02 Go to next message
Vitalii Stakhov is currently offline Vitalii StakhovFriend
Messages: 3
Registered: October 2015
Junior Member
I'm using Android paho from the develop branch.

I've set keepAlive to 15 seconds just to see log output more frequently.

Here is the behavior I see.

1. I connect using 3g
2. I see successful ping messages sent by AlarmPingSender every 15 seconds.
3. I turn on Airplane mode and see AlarmPingSender unregisters alarmreceiver.
4. I turn Airplane mode off. 3g comes back, new alarmreceiver is registered, client connected and I can see successful ping messages.
5. I turn on wifi. Right after the first ping message (which looks to be successful), alarmreceiver is unregistered and connectionLost is called. The client is disconnected and won't try to reconnect.

From what I see in logs, I make the following conclusions how paho works. It connects and sends ping messages once every keepAlive seconds. If a network changes (from 3g to wifi to lte to no connection), the client will try to reconnect.

If connection is lost while using the same network (while being on 3g for example), paho wouldn't reconnect. It will reconnect either when connect method is called explicitly or when network type switches from one type to another.

The problems I see are:
a) paho doesn't automatically try to reconnect when switching from 3g to wifi. It just invokes connectionLost and sits there without connection
b) if I invoke connect method from connectionLost so that the client is reconnected after switching to wifi it works well, but screws other places, for example, a future connect attempt (in connectionLost) may fail with the 'client is disconnecting' error and after that paho is not trying to reconnect even on network type switches.

So what is the solution for having the client connected when connection is lost after switching to wifi? Is it a bug?

Thanks!
Re: Connection lost on first ping after switching to wifi [message #1711914 is a reply to message #1711599] Tue, 20 October 2015 09:50 Go to previous messageGo to next message
James Sutton is currently offline James SuttonFriend
Messages: 71
Registered: July 2015
Member
Hi,


You're right, when the network on the device changes state, it will try to reconnect, 3G -> WiFi, Aeroplane mode -> 3G etc.. however if the connection is completely lost then it calls the connectionLost callback.

It sounds like it tried to reconnect when then phone joined the WiFi network, but the connection timed out or failed independently. This would always cause connectionLost to be called.

I don't quite understand what you mean in your second point about causing problems when re-connecting from within connectionLost though..
Re: Connection lost on first ping after switching to wifi [message #1712180 is a reply to message #1711914] Wed, 21 October 2015 18:55 Go to previous message
Vitalii Stakhov is currently offline Vitalii StakhovFriend
Messages: 3
Registered: October 2015
Junior Member
James,

So what is the correct approach to have the mqtt client always connected?

Should we let paho reconnect if network type changes (3g -> wifi, wifi -> 3g etc) and reconnect 'manually' if network didn't change?

is this code for having the client always connected correct (assuming the client is already connected):

void connectMqttClient(Runnable onFailure) {
client.connect(conOpt, options, new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken iMqttToken) {
            }

            @Override
            public void onFailure(IMqttToken iMqttToken, Throwable throwable) {
                onFailure.run();
            }
        });
}

@Override
public void connectionLost(Throwable throwable) {
   tryToConnectUntilSuccess(); 
}

public void tryToConnectUntilSuccess() {
    if(isNetworkAvailable) {
        // network is available - try to reconnect.
        // If network is not available - do nothing as
        // mqttservice will reconnect itself
        // when network type changes
        connectMqttClient(() -> tryToConnectUntilSuccess());
    }
}

Previous Topic:openwrt: stdinpub: waitfor unexpectedly is NULL
Next Topic:MQTT Client has lost connection
Goto Forum:
  


Current Time: Tue Nov 20 00:58:35 GMT 2018

Powered by FUDForum. Page generated in 0.01849 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top