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) { if (split.length == 2) {
return split[1]; return split[1];
} else { } else {
return conversation.getAccount().getUsername(); 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) { public void onReceive(Context context, Intent intent) {
Intent mIntentForService = new Intent(context, Intent mIntentForService = new Intent(context,
XmppConnectionService.class); XmppConnectionService.class);
mIntentForService.putExtra("ping", intent.getBooleanExtra("ping",false)); if (intent.getAction() != null) {
if ((intent.getAction() != null) mIntentForService.setAction(intent.getAction());
&& (intent.getAction() } else {
.equals("android.intent.action.BOOT_COMPLETED"))) { mIntentForService.setAction("other");
} }
context.startService(mIntentForService); context.startService(mIntentForService);
} }

View file

@ -7,7 +7,6 @@ import java.util.Hashtable;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import org.json.JSONException;
import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpServiceConnection; import org.openintents.openpgp.util.OpenPgpServiceConnection;
@ -70,9 +69,10 @@ public class XmppConnectionService extends Service {
public long startDate; 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 static final int PING_TIMEOUT = 2;
private List<Account> accounts; private List<Account> accounts;
private List<Conversation> conversations = null; private List<Conversation> conversations = null;
@ -80,10 +80,11 @@ public class XmppConnectionService extends Service {
private OnAccountListChangedListener accountChangedListener = null; private OnAccountListChangedListener accountChangedListener = null;
private OnTLSExceptionReceived tlsException = null; private OnTLSExceptionReceived tlsException = null;
public void setOnTLSExceptionReceivedListener(OnTLSExceptionReceived listener) { public void setOnTLSExceptionReceivedListener(
OnTLSExceptionReceived listener) {
tlsException = listener; tlsException = listener;
} }
private Random mRandom = new Random(System.currentTimeMillis()); private Random mRandom = new Random(System.currentTimeMillis());
private ContentObserver contactObserver = new ContentObserver(null) { private ContentObserver contactObserver = new ContentObserver(null) {
@ -115,7 +116,7 @@ public class XmppConnectionService extends Service {
&& (packet.getBody().startsWith("?OTR"))) { && (packet.getBody().startsWith("?OTR"))) {
message = MessageParser.parseOtrChat(packet, account, message = MessageParser.parseOtrChat(packet, account,
service); service);
if (message!=null) { if (message != null) {
message.markUnread(); message.markUnread();
} }
} else if (packet.hasChild("body")) { } else if (packet.hasChild("body")) {
@ -126,7 +127,7 @@ public class XmppConnectionService extends Service {
|| (packet.hasChild("sent"))) { || (packet.hasChild("sent"))) {
message = MessageParser.parseCarbonMessage(packet, account, message = MessageParser.parseCarbonMessage(packet, account,
service); service);
if (message!=null) { if (message != null) {
message.getConversation().markRead(); message.getConversation().markRead();
} }
notify = false; notify = false;
@ -146,7 +147,7 @@ public class XmppConnectionService extends Service {
} else if (packet.getType() == MessagePacket.TYPE_ERROR) { } else if (packet.getType() == MessagePacket.TYPE_ERROR) {
message = MessageParser.parseError(packet, account, service); message = MessageParser.parseError(packet, account, service);
} else { } else {
//Log.d(LOGTAG, "unparsed message " + packet.toString()); // Log.d(LOGTAG, "unparsed message " + packet.toString());
} }
if (message == null) { if (message == null) {
return; return;
@ -171,7 +172,8 @@ public class XmppConnectionService extends Service {
if (convChangedListener != null) { if (convChangedListener != null) {
convChangedListener.onConversationListChanged(); convChangedListener.onConversationListChanged();
} else { } else {
UIHelper.updateNotification(getApplicationContext(), getConversations(), notify); UIHelper.updateNotification(getApplicationContext(),
getConversations(), notify);
} }
} }
}; };
@ -197,13 +199,13 @@ public class XmppConnectionService extends Service {
if (convChangedListener != null) { if (convChangedListener != null) {
convChangedListener.onConversationListChanged(); convChangedListener.onConversationListChanged();
} }
scheduleWakeupCall(PING_INTERVAL, true); scheduleWakeupCall(PING_MAX_INTERVAL, true);
} else if (account.getStatus() == Account.STATUS_OFFLINE) { } else if (account.getStatus() == Account.STATUS_OFFLINE) {
Log.d(LOGTAG,"onStatusChanged offline"); Log.d(LOGTAG, "onStatusChanged offline");
databaseBackend.clearPresences(account); databaseBackend.clearPresences(account);
if (!account.isOptionSet(Account.OPTION_DISABLED)) { if (!account.isOptionSet(Account.OPTION_DISABLED)) {
int timeToReconnect = mRandom.nextInt(50)+10; int timeToReconnect = mRandom.nextInt(50) + 10;
scheduleWakeupCall(timeToReconnect,false); scheduleWakeupCall(timeToReconnect, false);
} }
} }
@ -215,13 +217,17 @@ public class XmppConnectionService extends Service {
@Override @Override
public void onPresencePacketReceived(Account account, public void onPresencePacketReceived(Account account,
PresencePacket packet) { PresencePacket packet) {
if (packet.hasChild("x")&&(packet.findChild("x").getAttribute("xmlns").startsWith("http://jabber.org/protocol/muc"))) { if (packet.hasChild("x")
Conversation muc = findMuc(packet.getAttribute("from").split("/")[0]); && (packet.findChild("x").getAttribute("xmlns")
if (muc!=null) { .startsWith("http://jabber.org/protocol/muc"))) {
Conversation muc = findMuc(packet.getAttribute("from").split(
"/")[0]);
if (muc != null) {
int error = muc.getMucOptions().getError(); int error = muc.getMucOptions().getError();
muc.getMucOptions().processPacket(packet); muc.getMucOptions().processPacket(packet);
if ((muc.getMucOptions().getError()!=error)&&(convChangedListener!=null)) { if ((muc.getMucOptions().getError() != error)
Log.d(LOGTAG,"muc error status changed"); && (convChangedListener != null)) {
Log.d(LOGTAG, "muc error status changed");
convChangedListener.onConversationListChanged(); convChangedListener.onConversationListChanged();
} }
} }
@ -247,15 +253,17 @@ public class XmppConnectionService extends Service {
contact.updatePresence(fromParts[1], Presences.DND); contact.updatePresence(fromParts[1], Presences.DND);
} }
PgpEngine pgp = getPgpEngine(); PgpEngine pgp = getPgpEngine();
if (pgp!=null) { if (pgp != null) {
Element x = packet.findChild("x"); Element x = packet.findChild("x");
if ((x != null) if ((x != null)
&& (x.getAttribute("xmlns").equals("jabber:x:signed"))) { && (x.getAttribute("xmlns")
.equals("jabber:x:signed"))) {
try { try {
contact.setPgpKeyId(pgp.fetchKeyId(packet.findChild("status") contact.setPgpKeyId(pgp.fetchKeyId(packet
.getContent(), x.getContent())); .findChild("status").getContent(), x
.getContent()));
} catch (OpenPgpException e) { } catch (OpenPgpException e) {
Log.d(LOGTAG,"faulty pgp. just ignore"); Log.d(LOGTAG, "faulty pgp. just ignore");
} }
} }
} }
@ -309,6 +317,8 @@ public class XmppConnectionService extends Service {
private OpenPgpServiceConnection pgpServiceConnection; private OpenPgpServiceConnection pgpServiceConnection;
private PgpEngine mPgpEngine = null; private PgpEngine mPgpEngine = null;
private Intent pingIntent;
private PendingIntent pendingPingIntent = null;
public PgpEngine getPgpEngine() { public PgpEngine getPgpEngine() {
if (pgpServiceConnection.isBound()) { if (pgpServiceConnection.isBound()) {
@ -325,7 +335,7 @@ public class XmppConnectionService extends Service {
} }
protected Conversation findMuc(String name) { protected Conversation findMuc(String name) {
for(Conversation conversation : this.conversations) { for (Conversation conversation : this.conversations) {
if (conversation.getContactJid().split("/")[0].equals(name)) { if (conversation.getContactJid().split("/")[0].equals(name)) {
return conversation; return conversation;
} }
@ -386,34 +396,57 @@ public class XmppConnectionService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { 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(); NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null boolean isConnected = activeNetwork != null
&& activeNetwork.isConnected(); && activeNetwork.isConnected();
for (Account account : accounts) { for (Account account : accounts) {
if (!isConnected) { if (!account.isOptionSet(Account.OPTION_DISABLED)) {
account.setStatus(Account.STATUS_NO_INTERNET); if (!isConnected) {
} else { account.setStatus(Account.STATUS_NO_INTERNET);
if (account.getStatus() == Account.STATUS_NO_INTERNET) {
account.setStatus(Account.STATUS_OFFLINE);
}
}
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 { } else {
if (intent.getBooleanExtra("ping", false)) { if (account.getStatus() == Account.STATUS_NO_INTERNET) {
Log.d(LOGTAG,"start service ping"); account.setStatus(Account.STATUS_OFFLINE);
ping(account,PING_TIMEOUT);
} }
// 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();
} }
} }
} }
@ -431,8 +464,7 @@ public class XmppConnectionService extends Service {
this.pgpServiceConnection = new OpenPgpServiceConnection( this.pgpServiceConnection = new OpenPgpServiceConnection(
getApplicationContext(), "org.sufficientlysecure.keychain"); getApplicationContext(), "org.sufficientlysecure.keychain");
this.pgpServiceConnection.bindToService(); this.pgpServiceConnection.bindToService();
} }
@Override @Override
@ -440,26 +472,47 @@ public class XmppConnectionService extends Service {
super.onDestroy(); super.onDestroy();
for (Account account : accounts) { for (Account account : accounts) {
if (account.getXmppConnection() != null) { if (account.getXmppConnection() != null) {
disconnect(account,true); disconnect(account, true);
} }
} }
} }
protected void scheduleWakeupCall(int seconds,boolean ping) { protected void scheduleWakeupCall(int seconds, boolean ping) {
long timeToWake = SystemClock.elapsedRealtime() + seconds * 1000;
Context context = getApplicationContext(); Context context = getApplicationContext();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); AlarmManager alarmManager = (AlarmManager) context
Intent intent = new Intent(context, EventReceiver.class); .getSystemService(Context.ALARM_SERVICE);
intent.putExtra("ping", ping);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
seconds * 1000, alarmIntent);
if (ping) {
Log.d(LOGTAG,"schedule ping in "+seconds+" seconds");
} else {
Log.d(LOGTAG,"schedule reconnect in "+seconds+" seconds");
}
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 {
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);
}
} }
public XmppConnection createConnection(Account account) { public XmppConnection createConnection(Account account) {
@ -470,16 +523,19 @@ public class XmppConnectionService extends Service {
connection.setOnPresencePacketReceivedListener(this.presenceListener); connection.setOnPresencePacketReceivedListener(this.presenceListener);
connection connection
.setOnUnregisteredIqPacketReceivedListener(this.unknownIqListener); .setOnUnregisteredIqPacketReceivedListener(this.unknownIqListener);
connection.setOnTLSExceptionReceivedListener(new OnTLSExceptionReceived() { connection
.setOnTLSExceptionReceivedListener(new OnTLSExceptionReceived() {
@Override
public void onTLSExceptionReceived(String fingerprint, Account account) { @Override
Log.d(LOGTAG,"tls exception arrived in service"); public void onTLSExceptionReceived(String fingerprint,
if (tlsException!=null) { Account account) {
tlsException.onTLSExceptionReceived(fingerprint,account); Log.d(LOGTAG, "tls exception arrived in service");
} if (tlsException != null) {
} tlsException.onTLSExceptionReceived(fingerprint,
}); account);
}
}
});
return connection; return connection;
} }
@ -624,9 +680,10 @@ public class XmppConnectionService extends Service {
Element query = new Element("query"); Element query = new Element("query");
query.setAttribute("xmlns", "jabber:iq:roster"); query.setAttribute("xmlns", "jabber:iq:roster");
if (!"".equals(account.getRosterVersion())) { 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 { } else {
Log.d(LOGTAG,account.getJid()+": fetching roster"); Log.d(LOGTAG, account.getJid() + ": fetching roster");
} }
query.setAttribute("ver", account.getRosterVersion()); query.setAttribute("ver", account.getRosterVersion());
iqPacket.addChild(query); iqPacket.addChild(query);
@ -638,7 +695,8 @@ public class XmppConnectionService extends Service {
IqPacket packet) { IqPacket packet) {
Element roster = packet.findChild("query"); Element roster = packet.findChild("query");
if (roster != null) { if (roster != null) {
Log.d(LOGTAG,account.getJid()+": processing roster"); Log.d(LOGTAG, account.getJid()
+ ": processing roster");
processRosterItems(account, roster); processRosterItems(account, roster);
StringBuilder mWhere = new StringBuilder(); StringBuilder mWhere = new StringBuilder();
mWhere.append("jid NOT IN("); mWhere.append("jid NOT IN(");
@ -663,7 +721,8 @@ public class XmppConnectionService extends Service {
} }
} else { } else {
Log.d(LOGTAG,account.getJid()+": empty roster returend"); Log.d(LOGTAG, account.getJid()
+ ": empty roster returend");
} }
mergePhoneContactsWithRoster(new OnPhoneContactsMerged() { mergePhoneContactsWithRoster(new OnPhoneContactsMerged() {
@ -859,7 +918,7 @@ public class XmppConnectionService extends Service {
public void deleteAccount(Account account) { public void deleteAccount(Account account) {
Log.d(LOGTAG, "called delete account"); Log.d(LOGTAG, "called delete account");
if (account.getXmppConnection() != null) { if (account.getXmppConnection() != null) {
this.disconnect(account,false); this.disconnect(account, false);
} }
databaseBackend.deleteAccount(account); databaseBackend.deleteAccount(account);
this.accounts.remove(account); this.accounts.remove(account);
@ -908,8 +967,7 @@ public class XmppConnectionService extends Service {
nick = conversation.getAccount().getUsername(); nick = conversation.getAccount().getUsername();
} }
PresencePacket packet = new PresencePacket(); PresencePacket packet = new PresencePacket();
packet.setAttribute("to", muc + "/" packet.setAttribute("to", muc + "/" + nick);
+ nick);
Element x = new Element("x"); Element x = new Element("x");
x.setAttribute("xmlns", "http://jabber.org/protocol/muc"); x.setAttribute("xmlns", "http://jabber.org/protocol/muc");
if (conversation.getMessages().size() != 0) { if (conversation.getMessages().size() != 0) {
@ -923,21 +981,22 @@ public class XmppConnectionService extends Service {
conversation.getAccount().getXmppConnection() conversation.getAccount().getXmppConnection()
.sendPresencePacket(packet); .sendPresencePacket(packet);
} }
private OnRenameListener renameListener = null; private OnRenameListener renameListener = null;
private boolean pongReceived; private boolean pongReceived;
public void setOnRenameListener(OnRenameListener listener) { public void setOnRenameListener(OnRenameListener listener) {
this.renameListener = listener; this.renameListener = listener;
} }
public void renameInMuc(final Conversation conversation, final String nick) { public void renameInMuc(final Conversation conversation, final String nick) {
final MucOptions options = conversation.getMucOptions(); final MucOptions options = conversation.getMucOptions();
if (options.online()) { if (options.online()) {
options.setOnRenameListener(new OnRenameListener() { options.setOnRenameListener(new OnRenameListener() {
@Override @Override
public void onRename(boolean success) { public void onRename(boolean success) {
if (renameListener!=null) { if (renameListener != null) {
renameListener.onRename(success); renameListener.onRename(success);
} }
if (success) { if (success) {
@ -946,30 +1005,36 @@ public class XmppConnectionService extends Service {
} }
}); });
PresencePacket packet = new PresencePacket(); 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()); 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) { @Override
final boolean changed; public void onPresencePacketReceived(Account account,
String type = packet.getAttribute("type"); PresencePacket packet) {
changed = (!"error".equals(type)); final boolean changed;
if (!changed) { String type = packet.getAttribute("type");
options.getOnRenameListener().onRename(false); changed = (!"error".equals(type));
} else { if (!changed) {
if (type==null) { options.getOnRenameListener().onRename(false);
options.getOnRenameListener().onRename(true); } else {
options.setNick(packet.getAttribute("from").split("/")[1]); if (type == null) {
} else { options.getOnRenameListener()
options.processPacket(packet); .onRename(true);
options.setNick(packet.getAttribute("from")
.split("/")[1]);
} else {
options.processPacket(packet);
}
}
} }
} });
}
});
} else { } else {
String jid = conversation.getContactJid().split("/")[0]+"/"+nick; String jid = conversation.getContactJid().split("/")[0] + "/"
+ nick;
conversation.setContactJid(jid); conversation.setContactJid(jid);
databaseBackend.updateConversation(conversation); databaseBackend.updateConversation(conversation);
if (conversation.getAccount().getStatus() == Account.STATUS_ONLINE) { if (conversation.getAccount().getStatus() == Account.STATUS_ONLINE) {
@ -982,42 +1047,45 @@ public class XmppConnectionService extends Service {
PresencePacket packet = new PresencePacket(); PresencePacket packet = new PresencePacket();
packet.setAttribute("to", conversation.getContactJid()); packet.setAttribute("to", conversation.getContactJid());
packet.setAttribute("from", conversation.getAccount().getFullJid()); packet.setAttribute("from", conversation.getAccount().getFullJid());
packet.setAttribute("type","unavailable"); packet.setAttribute("type", "unavailable");
conversation.getAccount().getXmppConnection().sendPresencePacket(packet); conversation.getAccount().getXmppConnection()
.sendPresencePacket(packet);
conversation.getMucOptions().setOffline(); conversation.getMucOptions().setOffline();
} }
public void disconnect(final Account account, boolean blocking) { public void disconnect(final Account account, boolean blocking) {
List<Conversation> conversations = getConversations(); if (account.getStatus() == Account.STATUS_ONLINE) {
for (int i = 0; i < conversations.size(); i++) { List<Conversation> conversations = getConversations();
Conversation conversation = conversations.get(i); for (int i = 0; i < conversations.size(); i++) {
if (conversation.getAccount() == account) { Conversation conversation = conversations.get(i);
if (conversation.getMode() == Conversation.MODE_MULTI) { if (conversation.getAccount() == account) {
leaveMuc(conversation); if (conversation.getMode() == Conversation.MODE_MULTI) {
} else { leaveMuc(conversation);
try { } else {
conversation.endOtrIfNeeded(); try {
} catch (OtrException e) { conversation.endOtrIfNeeded();
Log.d(LOGTAG, "error ending otr session for " } catch (OtrException e) {
+ conversation.getName()); Log.d(LOGTAG, "error ending otr session for "
+ conversation.getName());
}
} }
} }
} }
} if (!blocking) {
if (!blocking) { new Thread(new Runnable() {
new Thread(new Runnable() {
@Override
@Override public void run() {
public void run() { account.getXmppConnection().disconnect(false);
account.getXmppConnection().disconnect(false); Log.d(LOGTAG, "disconnected account: " + account.getJid());
Log.d(LOGTAG, "disconnected account: " + account.getJid()); account.setXmppConnection(null);
account.setXmppConnection(null); }
} }).start();
}).start(); } else {
} else { account.getXmppConnection().disconnect(false);
account.getXmppConnection().disconnect(false); Log.d(LOGTAG, "disconnected account: " + account.getJid());
Log.d(LOGTAG, "disconnected account: " + account.getJid()); account.setXmppConnection(null);
account.setXmppConnection(null); }
} }
} }
@ -1099,7 +1167,7 @@ public class XmppConnectionService extends Service {
Log.d(LOGTAG, packet.toString()); Log.d(LOGTAG, packet.toString());
contact.getAccount().getXmppConnection().sendPresencePacket(packet); contact.getAccount().getXmppConnection().sendPresencePacket(packet);
} }
public void sendPgpPresence(Account account, String signature) { public void sendPgpPresence(Account account, String signature) {
PresencePacket packet = new PresencePacket(); PresencePacket packet = new PresencePacket();
packet.setAttribute("from", account.getFullJid()); packet.setAttribute("from", account.getFullJid());
@ -1129,7 +1197,7 @@ public class XmppConnectionService extends Service {
public Contact findContact(String uuid) { public Contact findContact(String uuid) {
Contact contact = this.databaseBackend.getContact(uuid); Contact contact = this.databaseBackend.getContact(uuid);
for(Account account : getAccounts()) { for (Account account : getAccounts()) {
if (contact.getAccountUuid().equals(account.getUuid())) { if (contact.getAccountUuid().equals(account.getUuid())) {
contact.setAccount(account); contact.setAccount(account);
} }
@ -1141,63 +1209,22 @@ public class XmppConnectionService extends Service {
this.tlsException = null; this.tlsException = null;
} }
//TODO dont let thread sleep but schedule wake up
public void reconnectAccount(final Account account) { public void reconnectAccount(final Account account) {
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
if (account.getXmppConnection() != null) { if (account.getXmppConnection() != null) {
disconnect(account,true); disconnect(account, true);
} }
if (!account.isOptionSet(Account.OPTION_DISABLED)) { if (!account.isOptionSet(Account.OPTION_DISABLED)) {
if (account.getXmppConnection()==null) { if (account.getXmppConnection() == null) {
account.setXmppConnection(createConnection(account)); account.setXmppConnection(createConnection(account));
} }
Thread thread = new Thread(account.getXmppConnection()); Thread thread = new Thread(account.getXmppConnection());
thread.start(); thread.start();
}
}
}).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(); }).start();
} }

View file

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

View file

@ -29,9 +29,9 @@ import javax.net.ssl.X509TrustManager;
import org.json.JSONException; import org.json.JSONException;
import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserException;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.SystemClock;
import android.util.Log; import android.util.Log;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
@ -71,6 +71,9 @@ public class XmppConnection implements Runnable {
private int stanzasReceived = 0; private int stanzasReceived = 0;
private int stanzasSent = 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_IQ = 0;
private static final int PACKET_MESSAGE = 1; private static final int PACKET_MESSAGE = 1;
@ -214,6 +217,7 @@ public class XmppConnection implements Runnable {
tagWriter.writeStanzaAsync(ack); tagWriter.writeStanzaAsync(ack);
} else if (nextTag.isStart("a")) { } else if (nextTag.isStart("a")) {
Element ack = tagReader.readElement(nextTag); Element ack = tagReader.readElement(nextTag);
lastPaketReceived = SystemClock.elapsedRealtime();
int serverSequence = Integer.parseInt(ack.getAttribute("h")); int serverSequence = Integer.parseInt(ack.getAttribute("h"));
if (serverSequence>this.stanzasSent) { if (serverSequence>this.stanzasSent) {
this.stanzasSent = serverSequence; this.stanzasSent = serverSequence;
@ -265,6 +269,7 @@ public class XmppConnection implements Runnable {
nextTag = tagReader.readTag(); nextTag = tagReader.readTag();
} }
++stanzasReceived; ++stanzasReceived;
lastPaketReceived = SystemClock.elapsedRealtime();
return element; return element;
} }
@ -580,6 +585,7 @@ public class XmppConnection implements Runnable {
} }
private synchronized void sendPacket(final AbstractStanza packet, PacketReceived callback) { private synchronized void sendPacket(final AbstractStanza packet, PacketReceived callback) {
// TODO dont increment stanza count if packet = request packet or ack;
++stanzasSent; ++stanzasSent;
tagWriter.writeStanzaAsync(packet); tagWriter.writeStanzaAsync(packet);
if (callback != null) { if (callback != null) {
@ -589,6 +595,21 @@ public class XmppConnection implements Runnable {
packetCallbacks.put(packet.getId(), callback); packetCallbacks.put(packet.getId(), callback);
} }
} }
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( public void setOnMessagePacketReceivedListener(
OnMessagePacketReceived listener) { OnMessagePacketReceived listener) {