1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 20:42:06 +01:00

Allow session-removal and use threads

This commit is contained in:
vanitasvitae 2017-07-28 00:55:44 +02:00
parent d37bdb5d20
commit b0c8a8027a
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
5 changed files with 84 additions and 65 deletions

View file

@ -253,4 +253,8 @@ public final class JingleManager extends Manager {
jingleSessions.put(new FullJidAndSessionId(peer, session.getSessionId()), session); jingleSessions.put(new FullJidAndSessionId(peer, session.getSessionId()), session);
return session; return session;
} }
public void removeSession(JingleSession session) {
jingleSessions.remove(new FullJidAndSessionId(session.getPeer(), session.getSessionId()));
}
} }

View file

@ -18,6 +18,8 @@ package org.jivesoftware.smackx.jingle.components;
import org.jivesoftware.smackx.bytestreams.BytestreamSession; import org.jivesoftware.smackx.bytestreams.BytestreamSession;
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionElement; import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionElement;
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionInfoElement;
import org.jivesoftware.smackx.jingle.element.JingleElement;
/** /**
* Class that represents a contents description component. * Class that represents a contents description component.
@ -34,6 +36,8 @@ public abstract class JingleDescription<D extends JingleContentDescriptionElemen
} }
} }
public abstract JingleElement handleDescriptionInfo(JingleContentDescriptionInfoElement info);
public JingleContent getParent() { public JingleContent getParent() {
return parent; return parent;
} }

View file

@ -29,6 +29,7 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.Async;
import org.jivesoftware.smackx.jingle.JingleDescriptionManager; import org.jivesoftware.smackx.jingle.JingleDescriptionManager;
import org.jivesoftware.smackx.jingle.JingleManager; import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.adapter.JingleTransportAdapter; import org.jivesoftware.smackx.jingle.adapter.JingleTransportAdapter;
@ -166,46 +167,50 @@ public class JingleSession {
} }
} }
private IQ handleTransportReplace(JingleElement request) { private IQ handleTransportReplace(final JingleElement request) {
List<JingleContentElement> affectedContents = request.getContents(); Async.go(new Runnable() {
List<JingleElement> responses = new ArrayList<>(); @Override
public void run() {
List<JingleContentElement> affectedContents = request.getContents();
List<JingleElement> responses = new ArrayList<>();
for (JingleContentElement affected : affectedContents) { for (JingleContentElement affected : affectedContents) {
JingleContent content = contents.get(affected.getName()); JingleContent content = contents.get(affected.getName());
JingleContentTransportElement newTransport = affected.getTransport(); JingleContentTransportElement newTransport = affected.getTransport();
Set<String> blacklist = content.getTransportBlacklist(); Set<String> blacklist = content.getTransportBlacklist();
// Proposed transport method might already be on the blacklist (eg. because of previous failures) // Proposed transport method might already be on the blacklist (eg. because of previous failures)
if (blacklist.contains(newTransport.getNamespace())) { if (blacklist.contains(newTransport.getNamespace())) {
responses.add(JingleElement.createTransportReject(getInitiator(), getPeer(), getSessionId(), responses.add(JingleElement.createTransportReject(getInitiator(), getPeer(), getSessionId(),
content.getCreator(), content.getName(), newTransport)); content.getCreator(), content.getName(), newTransport));
continue; continue;
}
JingleTransportAdapter<?> transportAdapter = JingleManager.getJingleTransportAdapter(
newTransport.getNamespace());
// This might be an unknown transport.
if (transportAdapter == null) {
responses.add(JingleElement.createTransportReject(getInitiator(), getPeer(), getSessionId(),
content.getCreator(), content.getName(), newTransport));
continue;
}
//Otherwise, when all went well so far, accept the transport-replace
content.setTransport(JingleManager.getJingleTransportAdapter(newTransport.getNamespace())
.transportFromElement(newTransport));
responses.add(JingleElement.createTransportAccept(getInitiator(), getPeer(), getSessionId(),
content.getCreator(), content.getName(), newTransport));
}
for (JingleElement response : responses) {
try {
jingleManager.getConnection().createStanzaCollectorAndSend(response).nextResultOrThrow();
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException | SmackException.NotConnectedException e) {
LOGGER.log(Level.SEVERE, "Could not send response to transport-replace: " + e, e);
}
}
} }
});
JingleTransportAdapter<?> transportAdapter = JingleManager.getJingleTransportAdapter(
newTransport.getNamespace());
// This might be an unknown transport.
if (transportAdapter == null) {
responses.add(JingleElement.createTransportReject(getInitiator(), getPeer(), getSessionId(),
content.getCreator(), content.getName(), newTransport));
continue;
}
//Otherwise, when all went well so far, accept the transport-replace
content.setTransport(JingleManager.getJingleTransportAdapter(newTransport.getNamespace())
.transportFromElement(newTransport));
responses.add(JingleElement.createTransportAccept(getInitiator(), getPeer(), getSessionId(),
content.getCreator(), content.getName(), newTransport));
}
//TODO: Put in Thread?
for (JingleElement response : responses) {
try {
jingleManager.getConnection().createStanzaCollectorAndSend(response).nextResultOrThrow();
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException | SmackException.NotConnectedException e) {
LOGGER.log(Level.SEVERE, "Could not send response to transport-replace: " + e, e);
}
}
return IQ.createResultIQ(request); return IQ.createResultIQ(request);
} }
@ -217,32 +222,10 @@ public class JingleSession {
throw new AssertionError("Reason MUST not be null! (I guess)..."); throw new AssertionError("Reason MUST not be null! (I guess)...");
} }
switch (reason.asEnum()) { //TODO: Inform client.
case alternative_session:
case busy: jingleManager.removeSession(this);
case cancel:
case connectivity_error:
case decline:
// :(
case expired:
case failed_application:
case failed_transport:
case general_error:
// well... shit.
case gone:
case incompatible_parameters:
case media_error:
case security_error:
case success:
// Weeeeeh
break;
case timeout:
case unsupported_applications:
case unsupported_transports:
break;
default:
throw new AssertionError("Unknown reason enum: " + reason.asEnum().toString());
}
return IQ.createResultIQ(request); return IQ.createResultIQ(request);
} }
@ -270,7 +253,6 @@ public class JingleSession {
HashMap<JingleContentElement, JingleContent> affectedContents = getAffectedContents(request); HashMap<JingleContentElement, JingleContent> affectedContents = getAffectedContents(request);
for (Map.Entry<JingleContentElement, JingleContent> entry : affectedContents.entrySet()) { for (Map.Entry<JingleContentElement, JingleContent> entry : affectedContents.entrySet()) {
JingleTransport<?> transport = entry.getValue().getTransport(); JingleTransport<?> transport = entry.getValue().getTransport();
JingleContentTransportInfoElement info = entry.getKey().getTransport().getInfo(); JingleContentTransportInfoElement info = entry.getKey().getTransport().getInfo();
transport.handleTransportInfo(info, request); transport.handleTransportInfo(info, request);
@ -328,7 +310,22 @@ public class JingleSession {
} }
private IQ handleDescriptionInfo(JingleElement request) { private IQ handleDescriptionInfo(JingleElement request) {
return null; HashMap<JingleContentElement, JingleContent> affectedContents = getAffectedContents(request);
List<JingleElement> responses = new ArrayList<>();
for (Map.Entry<JingleContentElement, JingleContent> entry : affectedContents.entrySet()) {
responses.add(entry.getValue().getDescription().handleDescriptionInfo(entry.getKey().getDescription().getDescriptionInfo()));
}
for (JingleElement response : responses) {
try {
getJingleManager().getConnection().createStanzaCollectorAndSend(response).nextResultOrThrow();
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.SEVERE, "Could not send response to description-info: " + e, e);
}
}
return IQ.createResultIQ(request);
} }
private IQ handleContentRemove(JingleElement request) { private IQ handleContentRemove(JingleElement request) {

View file

@ -57,6 +57,10 @@ public abstract class JingleContentDescriptionElement implements ExtensionElemen
return payloads; return payloads;
} }
public JingleContentDescriptionInfoElement getDescriptionInfo() {
return null; //TODO
}
protected void addExtraAttributes(XmlStringBuilder xml) { protected void addExtraAttributes(XmlStringBuilder xml) {
} }

View file

@ -0,0 +1,10 @@
package org.jivesoftware.smackx.jingle.element;
import org.jivesoftware.smack.packet.NamedElement;
/**
* Created by vanitas on 28.07.17.
*/
public abstract class JingleContentDescriptionInfoElement implements NamedElement {
}