use of org.jivesoftware.smack.SmackException in project Smack by igniterealtime.
the class PingManager method pingServerIfNecessary.
/**
* Ping the server if deemed necessary because automatic server pings are
* enabled ({@link #setPingInterval(int)}) and the ping interval has expired.
*/
public synchronized void pingServerIfNecessary() {
// 1 seconds
final int DELTA = 1000;
// 3 tries
final int TRIES = 3;
final XMPPConnection connection = connection();
if (connection == null) {
// which means we can stop the thread by breaking the loop
return;
}
if (pingInterval <= 0) {
// Ping has been disabled
return;
}
long lastStanzaReceived = connection.getLastStanzaReceived();
if (lastStanzaReceived > 0) {
long now = System.currentTimeMillis();
// Delta since the last stanza was received
int deltaInSeconds = (int) ((now - lastStanzaReceived) / 1000);
// If the delta is small then the ping interval, then we can defer the ping
if (deltaInSeconds < pingInterval) {
maybeSchedulePingServerTask(deltaInSeconds);
return;
}
}
if (connection.isAuthenticated()) {
boolean res = false;
for (int i = 0; i < TRIES; i++) {
if (i != 0) {
try {
Thread.sleep(DELTA);
} catch (InterruptedException e) {
// This only happens if we should stop pinging
return;
}
}
try {
res = pingMyServer(false);
} catch (InterruptedException | SmackException e) {
// Note that we log the connection here, so that it is not GC'ed between the call to isAuthenticated
// a few lines above and the usage of the connection within pingMyServer(). In order to prevent:
// https://community.igniterealtime.org/thread/59369
LOGGER.log(Level.WARNING, "Exception while pinging server of " + connection, e);
res = false;
}
// stop when we receive a pong back
if (res) {
break;
}
}
if (!res) {
for (PingFailedListener l : pingFailedListeners) {
l.pingFailed();
}
} else {
// Ping was successful, wind-up the periodic task again
maybeSchedulePingServerTask();
}
} else {
LOGGER.warning("XMPPConnection was not authenticated");
}
}
use of org.jivesoftware.smack.SmackException in project Smack by igniterealtime.
the class AffiliationProvider method parse.
@Override
public Affiliation parse(XmlPullParser parser, int initialDepth) throws Exception {
String node = parser.getAttributeValue(null, "node");
BareJid jid = ParserUtils.getBareJidAttribute(parser);
String affiliationString = parser.getAttributeValue(null, "affiliation");
Affiliation.Type affiliationType = null;
if (affiliationString != null) {
affiliationType = Affiliation.Type.valueOf(affiliationString);
}
Affiliation affiliation;
if (node != null && jid == null) {
// affiliationType may be empty
affiliation = new Affiliation(node, affiliationType);
} else if (node == null && jid != null) {
// TODO
PubSubNamespace namespace = null;
affiliation = new Affiliation(jid, affiliationType, namespace);
} else {
throw new SmackException("Invalid affililation. Either one of 'node' or 'jid' must be set" + ". Node: " + node + ". Jid: " + jid + '.');
}
return affiliation;
}
use of org.jivesoftware.smack.SmackException in project Smack by igniterealtime.
the class PingTest method checkSendingPing.
@Test
public void checkSendingPing() throws InterruptedException, SmackException, IOException, XMPPException {
DummyConnection dummyCon = getAuthentiactedDummyConnection();
PingManager pinger = PingManager.getInstanceFor(dummyCon);
try {
pinger.ping(DUMMY_AT_EXAMPLE_ORG);
} catch (SmackException e) {
// Ignore the fact the server won't answer for this unit test.
}
Stanza sentPacket = dummyCon.getSentPacket();
assertTrue(sentPacket instanceof Ping);
}
use of org.jivesoftware.smack.SmackException in project Smack by igniterealtime.
the class JingleContentDescriptionProvider method parse.
/**
* Parse a iq/jingle/description element.
*
* @param parser the input to parse
* @return a description element
* @throws IOException
* @throws XmlPullParserException
* @throws SmackException
*/
@Override
public JingleContentDescription parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException {
boolean done = false;
JingleContentDescription desc = getInstance();
while (!done) {
int eventType = parser.next();
String name = parser.getName();
if (eventType == XmlPullParser.START_TAG) {
if (name.equals(JingleContentDescription.JinglePayloadType.NODENAME)) {
desc.addJinglePayloadType(parsePayload(parser));
} else {
throw new SmackException("Unknow element \"" + name + "\" in content.");
}
} else if (eventType == XmlPullParser.END_TAG) {
if (name.equals(JingleContentDescription.NODENAME)) {
done = true;
}
}
}
return desc;
}
use of org.jivesoftware.smack.SmackException in project Smack by igniterealtime.
the class JingleDescriptionProvider method parse.
/**
* Parse a iq/jingle/description element.
*
* @param parser
* the input to parse
* @return a description element
* @throws SmackException
* @throws IOException
* @throws XmlPullParserException
*/
@Override
public JingleDescription parse(XmlPullParser parser, int initialDepth) throws SmackException, XmlPullParserException, IOException {
boolean done = false;
JingleDescription desc = getInstance();
while (!done) {
int eventType = parser.next();
String name = parser.getName();
if (eventType == XmlPullParser.START_TAG) {
if (name.equals(PayloadType.NODENAME)) {
desc.addPayloadType(parsePayload(parser));
} else {
throw new SmackException("Unknow element \"" + name + "\" in content.");
}
} else if (eventType == XmlPullParser.END_TAG) {
if (name.equals(JingleDescription.NODENAME)) {
done = true;
}
}
}
return desc;
}
Aggregations