-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ping relay when no data received in five minutes
If no data is received after an additional 30 seconds, disconnect.
- Loading branch information
Showing
5 changed files
with
159 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
weechat-android/src/main/java/com/ubergeek42/WeechatAndroid/service/PingActionReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/******************************************************************************* | ||
* Copyright 2014 Matthew Horan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
******************************************************************************/ | ||
package com.ubergeek42.WeechatAndroid.service; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.os.SystemClock; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class PingActionReceiver extends BroadcastReceiver { | ||
private static Logger logger = LoggerFactory.getLogger("PingActionReceiver"); | ||
private RelayService relayService; | ||
public static final String PING_ACTION = "com.ubergeek42.WeechatAndroid.PING_ACTION"; | ||
|
||
public PingActionReceiver(RelayService relayService) { | ||
super(); | ||
this.relayService = relayService; | ||
} | ||
|
||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
logger.debug("onReceive intent: {}, sentPing: {}", intent, intent.getBooleanExtra("sentPing", false)); | ||
|
||
if (!relayService.isConnection(RelayService.CONNECTED)) | ||
return; | ||
|
||
long triggerAt; | ||
Bundle extras = new Bundle(); | ||
|
||
if (SystemClock.elapsedRealtime() - relayService.lastMessageReceivedAt > 60 * 5 * 1000) { | ||
if (!intent.getBooleanExtra("sentPing", false)) { | ||
logger.debug("last message too old, sending ping"); | ||
relayService.connection.sendMsg("ping"); | ||
triggerAt = SystemClock.elapsedRealtime() + 30 * 1000; | ||
extras.putBoolean("sentPing", true); | ||
} else { | ||
logger.debug("no message received, disconnecting"); | ||
relayService.startThreadedDisconnect(false); | ||
return; | ||
} | ||
} else { | ||
triggerAt = relayService.lastMessageReceivedAt + 60 * 5 * 1000; | ||
} | ||
|
||
relayService.schedulePing(triggerAt, extras); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters