ping stuff

This commit is contained in:
Daniel Gultsch 2014-03-11 15:44:22 +01:00
parent 08023210ba
commit 0d516b0603
5 changed files with 240 additions and 188 deletions

View file

@ -144,7 +144,11 @@ public class MucOptions {
if (split.length == 2) {
return split[1];
} else {
if (conversation.getAccount()!=null) {
return conversation.getAccount().getUsername();
} else {
return null;
}
}
}

View file

@ -8,11 +8,10 @@ public class EventReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent mIntentForService = new Intent(context,
XmppConnectionService.class);
mIntentForService.putExtra("ping", intent.getBooleanExtra("ping",false));
if ((intent.getAction() != null)
&& (intent.getAction()
.equals("android.intent.action.BOOT_COMPLETED"))) {
if (intent.getAction() != null) {
mIntentForService.setAction(intent.getAction());
} else {
mIntentForService.setAction("other");
}
context.startService(mIntentForService);
}

View file

@ -7,7 +7,6 @@ import java.util.Hashtable;
import java.util.List;
import java.util.Random;
import org.json.JSONException;
import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection;
@ -70,7 +69,8 @@ public class XmppConnectionService extends Service {
public long startDate;
private static final int PING_INTERVAL = 300;
private static final int PING_MAX_INTERVAL = 30;
private static final int PING_MIN_INTERVAL = 10;
private static final int PING_TIMEOUT = 2;
private List<Account> accounts;
@ -80,7 +80,8 @@ public class XmppConnectionService extends Service {
private OnAccountListChangedListener accountChangedListener = null;
private OnTLSExceptionReceived tlsException = null;
public void setOnTLSExceptionReceivedListener(OnTLSExceptionReceived listener) {
public void setOnTLSExceptionReceivedListener(
OnTLSExceptionReceived listener) {
tlsException = listener;
}
@ -171,7 +172,8 @@ public class XmppConnectionService extends Service {
if (convChangedListener != null) {
convChangedListener.onConversationListChanged();
} else {
UIHelper.updateNotification(getApplicationContext(), getConversations(), notify);
UIHelper.updateNotification(getApplicationContext(),
getConversations(), notify);
}
}
};
@ -197,7 +199,7 @@ public class XmppConnectionService extends Service {
if (convChangedListener != null) {
convChangedListener.onConversationListChanged();
}
scheduleWakeupCall(PING_INTERVAL, true);
scheduleWakeupCall(PING_MAX_INTERVAL, true);
} else if (account.getStatus() == Account.STATUS_OFFLINE) {
Log.d(LOGTAG, "onStatusChanged offline");
databaseBackend.clearPresences(account);
@ -215,12 +217,16 @@ public class XmppConnectionService extends Service {
@Override
public void onPresencePacketReceived(Account account,
PresencePacket packet) {
if (packet.hasChild("x")&&(packet.findChild("x").getAttribute("xmlns").startsWith("http://jabber.org/protocol/muc"))) {
Conversation muc = findMuc(packet.getAttribute("from").split("/")[0]);
if (packet.hasChild("x")
&& (packet.findChild("x").getAttribute("xmlns")
.startsWith("http://jabber.org/protocol/muc"))) {
Conversation muc = findMuc(packet.getAttribute("from").split(
"/")[0]);
if (muc != null) {
int error = muc.getMucOptions().getError();
muc.getMucOptions().processPacket(packet);
if ((muc.getMucOptions().getError()!=error)&&(convChangedListener!=null)) {
if ((muc.getMucOptions().getError() != error)
&& (convChangedListener != null)) {
Log.d(LOGTAG, "muc error status changed");
convChangedListener.onConversationListChanged();
}
@ -250,10 +256,12 @@ public class XmppConnectionService extends Service {
if (pgp != null) {
Element x = packet.findChild("x");
if ((x != null)
&& (x.getAttribute("xmlns").equals("jabber:x:signed"))) {
&& (x.getAttribute("xmlns")
.equals("jabber:x:signed"))) {
try {
contact.setPgpKeyId(pgp.fetchKeyId(packet.findChild("status")
.getContent(), x.getContent()));
contact.setPgpKeyId(pgp.fetchKeyId(packet
.findChild("status").getContent(), x
.getContent()));
} catch (OpenPgpException e) {
Log.d(LOGTAG, "faulty pgp. just ignore");
}
@ -309,6 +317,8 @@ public class XmppConnectionService extends Service {
private OpenPgpServiceConnection pgpServiceConnection;
private PgpEngine mPgpEngine = null;
private Intent pingIntent;
private PendingIntent pendingPingIntent = null;
public PgpEngine getPgpEngine() {
if (pgpServiceConnection.isBound()) {
@ -386,35 +396,58 @@ public class XmppConnectionService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
Log.d(LOGTAG,"calling start service. caller was:"+intent.getAction());
// internet and online last_received - list_ping >= max_ping : ping
// internet and online last_ping - last_received >= ping_timeout :
// reconnect
// internet and offline and enabled : connect (Threat start)
// no internet - set no internet
ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null
&& activeNetwork.isConnected();
for (Account account : accounts) {
if (!account.isOptionSet(Account.OPTION_DISABLED)) {
if (!isConnected) {
account.setStatus(Account.STATUS_NO_INTERNET);
} else {
if (account.getStatus() == Account.STATUS_NO_INTERNET) {
account.setStatus(Account.STATUS_OFFLINE);
}
// TODO 3 remaining cases
if (account.getStatus() == Account.STATUS_ONLINE) {
long lastReceived = account.getXmppConnection().lastPaketReceived;
long lastSent = account.getXmppConnection().lastPingSent;
if (lastSent - lastReceived >= PING_TIMEOUT * 1000) {
Log.d(LOGTAG, account.getJid() + ": ping timeout");
this.reconnectAccount(account);
} else if (SystemClock.elapsedRealtime() - lastReceived >= PING_MIN_INTERVAL * 1000) {
account.getXmppConnection().sendPing();
account.getXmppConnection().lastPingSent = SystemClock.elapsedRealtime();
this.scheduleWakeupCall(2, false);
}
} else if (account.getStatus() == Account.STATUS_OFFLINE) {
if (account.getXmppConnection() == null) {
account.setXmppConnection(this
.createConnection(account));
}
account.getXmppConnection().lastPingSent = SystemClock.elapsedRealtime();
new Thread(account.getXmppConnection()).start();
} else {
Log.d(LOGTAG,account.getJid()+": status="+account.getStatus());
}
//in any case. reschedule wakup call
this.scheduleWakeupCall(PING_MAX_INTERVAL, true);
}
if (accountChangedListener != null) {
accountChangedListener.onAccountListChangedListener();
}
if ((!account.isOptionSet(Account.OPTION_DISABLED))&&(isConnected)) {
if (account.getXmppConnection() == null) {
account.setXmppConnection(this.createConnection(account));
}
if (account.getStatus()==Account.STATUS_OFFLINE) {
Thread thread = new Thread(account.getXmppConnection());
thread.start();
} else {
if (intent.getBooleanExtra("ping", false)) {
Log.d(LOGTAG,"start service ping");
ping(account,PING_TIMEOUT);
}
}
}
}
return START_STICKY;
@ -432,7 +465,6 @@ public class XmppConnectionService extends Service {
getApplicationContext(), "org.sufficientlysecure.keychain");
this.pgpServiceConnection.bindToService();
}
@Override
@ -446,18 +478,39 @@ public class XmppConnectionService extends Service {
}
protected void scheduleWakeupCall(int seconds, boolean ping) {
long timeToWake = SystemClock.elapsedRealtime() + seconds * 1000;
Context context = getApplicationContext();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, EventReceiver.class);
intent.putExtra("ping", ping);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
seconds * 1000, alarmIntent);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
if (ping) {
if (this.pingIntent==null) {
this.pingIntent = new Intent(context, EventReceiver.class);
this.pingIntent.setAction("ping");
this.pingIntent.putExtra("time", timeToWake);
this.pendingPingIntent = PendingIntent.getBroadcast(context, 0,
this.pingIntent, 0);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,timeToWake, pendingPingIntent);
Log.d(LOGTAG,"schedule ping in "+seconds+" seconds");
} else {
Log.d(LOGTAG,"schedule reconnect in "+seconds+" seconds");
long scheduledTime = this.pingIntent.getLongExtra("time", 0);
if (scheduledTime<SystemClock.elapsedRealtime() || (scheduledTime > timeToWake)) {
this.pingIntent.putExtra("time", timeToWake);
alarmManager.cancel(this.pendingPingIntent);
this.pendingPingIntent = PendingIntent.getBroadcast(context, 0,
this.pingIntent, 0);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,timeToWake, pendingPingIntent);
Log.d(LOGTAG,"reschedule old ping to ping in "+seconds+" seconds");
}
}
} else {
Intent intent = new Intent(context, EventReceiver.class);
intent.setAction("ping_check");
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0,
intent, 0);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,timeToWake, alarmIntent);
}
}
@ -470,13 +523,16 @@ public class XmppConnectionService extends Service {
connection.setOnPresencePacketReceivedListener(this.presenceListener);
connection
.setOnUnregisteredIqPacketReceivedListener(this.unknownIqListener);
connection.setOnTLSExceptionReceivedListener(new OnTLSExceptionReceived() {
connection
.setOnTLSExceptionReceivedListener(new OnTLSExceptionReceived() {
@Override
public void onTLSExceptionReceived(String fingerprint, Account account) {
public void onTLSExceptionReceived(String fingerprint,
Account account) {
Log.d(LOGTAG, "tls exception arrived in service");
if (tlsException != null) {
tlsException.onTLSExceptionReceived(fingerprint,account);
tlsException.onTLSExceptionReceived(fingerprint,
account);
}
}
});
@ -624,7 +680,8 @@ public class XmppConnectionService extends Service {
Element query = new Element("query");
query.setAttribute("xmlns", "jabber:iq:roster");
if (!"".equals(account.getRosterVersion())) {
Log.d(LOGTAG,account.getJid()+ ": fetching roster version "+account.getRosterVersion());
Log.d(LOGTAG, account.getJid() + ": fetching roster version "
+ account.getRosterVersion());
} else {
Log.d(LOGTAG, account.getJid() + ": fetching roster");
}
@ -638,7 +695,8 @@ public class XmppConnectionService extends Service {
IqPacket packet) {
Element roster = packet.findChild("query");
if (roster != null) {
Log.d(LOGTAG,account.getJid()+": processing roster");
Log.d(LOGTAG, account.getJid()
+ ": processing roster");
processRosterItems(account, roster);
StringBuilder mWhere = new StringBuilder();
mWhere.append("jid NOT IN(");
@ -663,7 +721,8 @@ public class XmppConnectionService extends Service {
}
} else {
Log.d(LOGTAG,account.getJid()+": empty roster returend");
Log.d(LOGTAG, account.getJid()
+ ": empty roster returend");
}
mergePhoneContactsWithRoster(new OnPhoneContactsMerged() {
@ -908,8 +967,7 @@ public class XmppConnectionService extends Service {
nick = conversation.getAccount().getUsername();
}
PresencePacket packet = new PresencePacket();
packet.setAttribute("to", muc + "/"
+ nick);
packet.setAttribute("to", muc + "/" + nick);
Element x = new Element("x");
x.setAttribute("xmlns", "http://jabber.org/protocol/muc");
if (conversation.getMessages().size() != 0) {
@ -926,6 +984,7 @@ public class XmppConnectionService extends Service {
private OnRenameListener renameListener = null;
private boolean pongReceived;
public void setOnRenameListener(OnRenameListener listener) {
this.renameListener = listener;
}
@ -946,13 +1005,16 @@ public class XmppConnectionService extends Service {
}
});
PresencePacket packet = new PresencePacket();
packet.setAttribute("to", conversation.getContactJid().split("/")[0]+"/"+nick);
packet.setAttribute("to",
conversation.getContactJid().split("/")[0] + "/" + nick);
packet.setAttribute("from", conversation.getAccount().getFullJid());
conversation.getAccount().getXmppConnection().sendPresencePacket(packet, new OnPresencePacketReceived() {
conversation.getAccount().getXmppConnection()
.sendPresencePacket(packet, new OnPresencePacketReceived() {
@Override
public void onPresencePacketReceived(Account account, PresencePacket packet) {
public void onPresencePacketReceived(Account account,
PresencePacket packet) {
final boolean changed;
String type = packet.getAttribute("type");
changed = (!"error".equals(type));
@ -960,8 +1022,10 @@ public class XmppConnectionService extends Service {
options.getOnRenameListener().onRename(false);
} else {
if (type == null) {
options.getOnRenameListener().onRename(true);
options.setNick(packet.getAttribute("from").split("/")[1]);
options.getOnRenameListener()
.onRename(true);
options.setNick(packet.getAttribute("from")
.split("/")[1]);
} else {
options.processPacket(packet);
}
@ -969,7 +1033,8 @@ public class XmppConnectionService extends Service {
}
});
} else {
String jid = conversation.getContactJid().split("/")[0]+"/"+nick;
String jid = conversation.getContactJid().split("/")[0] + "/"
+ nick;
conversation.setContactJid(jid);
databaseBackend.updateConversation(conversation);
if (conversation.getAccount().getStatus() == Account.STATUS_ONLINE) {
@ -983,11 +1048,13 @@ public class XmppConnectionService extends Service {
packet.setAttribute("to", conversation.getContactJid());
packet.setAttribute("from", conversation.getAccount().getFullJid());
packet.setAttribute("type", "unavailable");
conversation.getAccount().getXmppConnection().sendPresencePacket(packet);
conversation.getAccount().getXmppConnection()
.sendPresencePacket(packet);
conversation.getMucOptions().setOffline();
}
public void disconnect(final Account account, boolean blocking) {
if (account.getStatus() == Account.STATUS_ONLINE) {
List<Conversation> conversations = getConversations();
for (int i = 0; i < conversations.size(); i++) {
Conversation conversation = conversations.get(i);
@ -1020,6 +1087,7 @@ public class XmppConnectionService extends Service {
account.setXmppConnection(null);
}
}
}
@Override
public IBinder onBind(Intent intent) {
@ -1141,6 +1209,7 @@ public class XmppConnectionService extends Service {
this.tlsException = null;
}
//TODO dont let thread sleep but schedule wake up
public void reconnectAccount(final Account account) {
new Thread(new Runnable() {
@ -1159,46 +1228,4 @@ public class XmppConnectionService extends Service {
}
}).start();
}
public void ping(final Account account,final int timeout) {
account.getXmppConnection().r();
Log.d(LOGTAG,account.getJid()+": sending ping");
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
Element ping = new Element("ping");
iq.setAttribute("from",account.getFullJid());
ping.setAttribute("xmlns", "urn:xmpp:ping");
iq.addChild(ping);
pongReceived = false;
account.getXmppConnection().sendIqPacket(iq, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
pongReceived = true;
}
});
new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while(i <= (5 * timeout)) {
if (pongReceived) {
scheduleWakeupCall(PING_INTERVAL,true);
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
++i;
}
if (!pongReceived) {
Log.d("xmppService",account.getJid()+" no pong after "+timeout+" seconds");
reconnectAccount(account);
}
}
}).start();
}
}

View file

@ -42,8 +42,9 @@ public abstract class XmppActivity extends Activity {
protected void onStart() {
super.onStart();
if (!xmppConnectionServiceBound) {
startService(new Intent(this, XmppConnectionService.class));
Intent intent = new Intent(this, XmppConnectionService.class);
intent.setAction("ui");
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
}

View file

@ -29,9 +29,9 @@ import javax.net.ssl.X509TrustManager;
import org.json.JSONException;
import org.xmlpull.v1.XmlPullParserException;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.util.Log;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.utils.CryptoHelper;
@ -72,6 +72,9 @@ public class XmppConnection implements Runnable {
private int stanzasReceived = 0;
private int stanzasSent = 0;
public long lastPaketReceived = 0;
public long lastPingSent = 0;
private static final int PACKET_IQ = 0;
private static final int PACKET_MESSAGE = 1;
private static final int PACKET_PRESENCE = 2;
@ -214,6 +217,7 @@ public class XmppConnection implements Runnable {
tagWriter.writeStanzaAsync(ack);
} else if (nextTag.isStart("a")) {
Element ack = tagReader.readElement(nextTag);
lastPaketReceived = SystemClock.elapsedRealtime();
int serverSequence = Integer.parseInt(ack.getAttribute("h"));
if (serverSequence>this.stanzasSent) {
this.stanzasSent = serverSequence;
@ -265,6 +269,7 @@ public class XmppConnection implements Runnable {
nextTag = tagReader.readTag();
}
++stanzasReceived;
lastPaketReceived = SystemClock.elapsedRealtime();
return element;
}
@ -580,6 +585,7 @@ public class XmppConnection implements Runnable {
}
private synchronized void sendPacket(final AbstractStanza packet, PacketReceived callback) {
// TODO dont increment stanza count if packet = request packet or ack;
++stanzasSent;
tagWriter.writeStanzaAsync(packet);
if (callback != null) {
@ -590,6 +596,21 @@ public class XmppConnection implements Runnable {
}
}
public void sendPing() {
if (streamFeatures.hasChild("sm")) {
Log.d(LOGTAG,"sending r as ping");
tagWriter.writeStanzaAsync(new RequestPacket());
} else {
Log.d(LOGTAG,"sending iq as ping");
IqPacket iq = new IqPacket(IqPacket.TYPE_GET);
Element ping = new Element("ping");
iq.setAttribute("from",account.getFullJid());
ping.setAttribute("xmlns", "urn:xmpp:ping");
iq.addChild(ping);
this.sendIqPacket(iq, null);
}
}
public void setOnMessagePacketReceivedListener(
OnMessagePacketReceived listener) {
this.messageListener = listener;