use of org.jivesoftware.smack.SmackException.NotConnectedException in project Smack by igniterealtime.
the class Roster method reload.
/**
* Reloads the entire roster from the server. This is an asynchronous operation,
* which means the method will return immediately, and the roster will be
* reloaded at a later point when the server responds to the reload request.
* @throws NotLoggedInException If not logged in.
* @throws NotConnectedException
* @throws InterruptedException
*/
public void reload() throws NotLoggedInException, NotConnectedException, InterruptedException {
final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
RosterPacket packet = new RosterPacket();
if (rosterStore != null && isRosterVersioningSupported()) {
packet.setVersion(rosterStore.getRosterVersion());
}
rosterState = RosterState.loading;
connection.sendIqWithResponseCallback(packet, new RosterResultListener(), new ExceptionCallback() {
@Override
public void processException(Exception exception) {
rosterState = RosterState.uninitialized;
Level logLevel;
if (exception instanceof NotConnectedException) {
logLevel = Level.FINE;
} else {
logLevel = Level.SEVERE;
}
LOGGER.log(logLevel, "Exception reloading roster", exception);
for (RosterLoadedListener listener : rosterLoadedListeners) {
listener.onRosterLoadingFailed(exception);
}
}
});
}
use of org.jivesoftware.smack.SmackException.NotConnectedException in project Smack by igniterealtime.
the class CloseListener method handleIQRequest.
@Override
public IQ handleIQRequest(IQ iqRequest) {
Close closeRequest = (Close) iqRequest;
InBandBytestreamSession ibbSession = this.manager.getSessions().get(closeRequest.getSessionID());
if (ibbSession == null) {
try {
this.manager.replyItemNotFoundPacket(closeRequest);
} catch (InterruptedException | NotConnectedException e) {
return null;
}
} else {
try {
ibbSession.closeByPeer(closeRequest);
} catch (InterruptedException | NotConnectedException e) {
return null;
}
this.manager.getSessions().remove(closeRequest.getSessionID());
}
return null;
}
use of org.jivesoftware.smack.SmackException.NotConnectedException in project Smack by igniterealtime.
the class DataListener method handleIQRequest.
@Override
public IQ handleIQRequest(IQ iqRequest) {
Data data = (Data) iqRequest;
InBandBytestreamSession ibbSession = this.manager.getSessions().get(data.getDataPacketExtension().getSessionID());
try {
if (ibbSession == null) {
this.manager.replyItemNotFoundPacket(data);
} else {
ibbSession.processIQPacket(data);
}
} catch (NotConnectedException | InterruptedException e) {
return null;
}
return null;
}
use of org.jivesoftware.smack.SmackException.NotConnectedException in project Smack by igniterealtime.
the class TransportNegotiator method delayedCheckBestCandidate.
/**
* Launch a thread that checks, after some time, if any of the candidates
* offered by the other endpoint is usable. The thread does not check the
* candidates: it just checks if we have got a valid one and sends an Accept
* in that case.
*/
private void delayedCheckBestCandidate(final JingleSession js, final Jingle jin) {
//
if (resultThread == null && !getRemoteCandidates().isEmpty()) {
resultThread = new Thread(new Runnable() {
@Override
public void run() {
// Sleep for some time, waiting for the candidates checks
int totalTime = (CANDIDATES_ACCEPT_PERIOD + TransportResolver.CHECK_TIMEOUT);
int tries = (int) Math.ceil(totalTime / 1000);
for (int i = 0; i < tries - 1; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
// Once we are in pending state, look for any valid remote
// candidate, and send an "accept" if we have one...
TransportCandidate bestRemote = getBestRemoteCandidate();
if ((bestRemote != null) && ((getNegotiatorState() == JingleNegotiatorState.PENDING))) {
// Accepting the remote candidate
if (!acceptedRemoteCandidates.contains(bestRemote)) {
Jingle jout = new Jingle(JingleActionEnum.CONTENT_ACCEPT);
JingleContent content = parentNegotiator.getJingleContent();
content.addJingleTransport(getJingleTransport(bestRemote));
jout.addContent(content);
// Send the packet
try {
js.sendFormattedJingle(jin, jout);
} catch (InterruptedException | NotConnectedException e) {
throw new IllegalStateException(e);
}
acceptedRemoteCandidates.add(bestRemote);
}
if ((isEstablished()) && (getNegotiatorState() == JingleNegotiatorState.PENDING)) {
setNegotiatorState(JingleNegotiatorState.SUCCEEDED);
try {
triggerTransportEstablished(getAcceptedLocalCandidate(), bestRemote);
} catch (InterruptedException | NotConnectedException e) {
throw new IllegalStateException(e);
}
break;
}
}
}
// Once we are in pending state, look for any valid remote
// candidate, and send an "accept" if we have one...
TransportCandidate bestRemote = getBestRemoteCandidate();
if (bestRemote == null) {
boolean foundRemoteRelay = false;
for (TransportCandidate candidate : remoteCandidates) {
if (candidate instanceof ICECandidate) {
ICECandidate iceCandidate = (ICECandidate) candidate;
if (iceCandidate.getType().equals(Type.relay)) {
//TODO Check if the relay is reacheable
addValidRemoteCandidate(iceCandidate);
foundRemoteRelay = true;
}
}
}
// We should accept the Public One if we received it, otherwise, accepts any.
if (!foundRemoteRelay) {
boolean foundLocalRelay = false;
for (TransportCandidate candidate : offeredCandidates) {
if (candidate instanceof ICECandidate) {
ICECandidate iceCandidate = (ICECandidate) candidate;
if (iceCandidate.getType().equals(Type.relay)) {
foundLocalRelay = true;
}
}
}
if (foundLocalRelay) {
boolean foundRemotePublic = false;
for (TransportCandidate candidate : remoteCandidates) {
if (candidate instanceof ICECandidate) {
ICECandidate iceCandidate = (ICECandidate) candidate;
if (iceCandidate.getType().equals(ICECandidate.Type.srflx)) {
addValidRemoteCandidate(iceCandidate);
foundRemotePublic = true;
}
}
}
if (!foundRemotePublic) {
for (TransportCandidate candidate : remoteCandidates) {
if (candidate instanceof ICECandidate) {
ICECandidate iceCandidate = (ICECandidate) candidate;
addValidRemoteCandidate(iceCandidate);
}
}
}
}
}
}
for (int i = 0; i < 6; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
bestRemote = getBestRemoteCandidate();
//State state = getState();
if ((bestRemote != null) && ((getNegotiatorState() == JingleNegotiatorState.PENDING))) {
if (!acceptedRemoteCandidates.contains(bestRemote)) {
Jingle jout = new Jingle(JingleActionEnum.CONTENT_ACCEPT);
JingleContent content = parentNegotiator.getJingleContent();
content.addJingleTransport(getJingleTransport(bestRemote));
jout.addContent(content);
// Send the packet
try {
js.sendFormattedJingle(jin, jout);
} catch (InterruptedException | NotConnectedException e) {
throw new IllegalStateException(e);
}
acceptedRemoteCandidates.add(bestRemote);
}
if (isEstablished()) {
setNegotiatorState(JingleNegotiatorState.SUCCEEDED);
break;
}
}
}
if (getNegotiatorState() != JingleNegotiatorState.SUCCEEDED) {
try {
session.terminate("Unable to negotiate session. This may be caused by firewall configuration problems.");
} catch (Exception e) {
LOGGER.log(Level.WARNING, "exception", e);
}
}
}
}, "Waiting for all the transport candidates checks...");
resultThread.setName("Transport Resolver Result");
resultThread.start();
}
}
use of org.jivesoftware.smack.SmackException.NotConnectedException in project Smack by igniterealtime.
the class BasicResolver method resolve.
/**
* Resolve the IP address.
* <p/>
* The BasicResolver takes the IP addresses of the interfaces and uses the
* first non-loopback, non-linklocal and non-sitelocal address.
* @throws NotConnectedException
* @throws InterruptedException
*/
@Override
public synchronized void resolve(JingleSession session) throws XMPPException, NotConnectedException, InterruptedException {
setResolveInit();
clearCandidates();
Enumeration<NetworkInterface> ifaces = null;
try {
ifaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
while (ifaces.hasMoreElements()) {
NetworkInterface iface = ifaces.nextElement();
Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
while (iaddresses.hasMoreElements()) {
InetAddress iaddress = iaddresses.nextElement();
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress() && !iaddress.isSiteLocalAddress()) {
TransportCandidate tr = new TransportCandidate.Fixed(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName(), getFreePort());
tr.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName());
addCandidate(tr);
setResolveEnd();
return;
}
}
}
try {
ifaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
LOGGER.log(Level.WARNING, "exception", e);
}
while (ifaces.hasMoreElements()) {
NetworkInterface iface = ifaces.nextElement();
Enumeration<InetAddress> iaddresses = iface.getInetAddresses();
while (iaddresses.hasMoreElements()) {
InetAddress iaddress = iaddresses.nextElement();
if (!iaddress.isLoopbackAddress() && !iaddress.isLinkLocalAddress()) {
TransportCandidate tr = new TransportCandidate.Fixed(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName(), getFreePort());
tr.setLocalIp(iaddress.getHostAddress() != null ? iaddress.getHostAddress() : iaddress.getHostName());
addCandidate(tr);
setResolveEnd();
return;
}
}
}
try {
TransportCandidate tr = new TransportCandidate.Fixed(InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName(), getFreePort());
tr.setLocalIp(InetAddress.getLocalHost().getHostAddress() != null ? InetAddress.getLocalHost().getHostAddress() : InetAddress.getLocalHost().getHostName());
addCandidate(tr);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "exception", e);
}
setResolveEnd();
}
Aggregations