Smack/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleSession.java

243 lines
8.1 KiB
Java
Raw Normal View History

/**
*
* Copyright 2017 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.jingle;
2017-06-23 22:48:28 +02:00
import java.util.ArrayList;
2017-06-29 21:53:57 +02:00
import java.util.HashSet;
2017-06-24 17:46:03 +02:00
import java.util.List;
2017-06-23 22:48:28 +02:00
import java.util.concurrent.Future;
2017-07-06 14:00:36 +02:00
import java.util.logging.Level;
2017-06-22 14:47:39 +02:00
import java.util.logging.Logger;
2017-06-19 19:22:59 +02:00
import org.jivesoftware.smack.SmackException;
2017-06-23 22:48:28 +02:00
import org.jivesoftware.smack.XMPPConnection;
2017-06-19 19:22:59 +02:00
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
2017-07-19 15:17:12 +02:00
import org.jivesoftware.smackx.jingle3.element.JingleContentElement;
import org.jivesoftware.smackx.jingle3.element.JingleElement;
2017-06-23 22:48:28 +02:00
import org.jivesoftware.smackx.jingle.transports.JingleTransportSession;
2017-06-18 16:47:49 +02:00
import org.jxmpp.jid.FullJid;
public abstract class JingleSession implements JingleSessionHandler {
2017-06-22 14:47:39 +02:00
private static final Logger LOGGER = Logger.getLogger(JingleSession.class.getName());
2017-06-29 21:53:57 +02:00
protected HashSet<String> failedTransportMethods = new HashSet<>();
2017-06-18 16:47:49 +02:00
protected final FullJid local;
2017-06-18 16:47:49 +02:00
protected final FullJid remote;
2017-06-18 16:47:49 +02:00
protected final Role role;
2017-06-18 16:47:49 +02:00
protected final String sid;
2017-07-19 15:17:12 +02:00
protected final List<JingleContentElement> contents = new ArrayList<>();
2017-06-24 17:46:03 +02:00
2017-06-23 22:48:28 +02:00
protected ArrayList<Future<?>> queued = new ArrayList<>();
protected JingleTransportSession<?> transportSession;
2017-06-22 14:47:39 +02:00
2017-06-18 16:47:49 +02:00
public JingleSession(FullJid initiator, FullJid responder, Role role, String sid) {
2017-06-24 17:46:03 +02:00
this(initiator, responder, role, sid, null);
}
2017-07-19 15:17:12 +02:00
public JingleSession(FullJid initiator, FullJid responder, Role role, String sid, List<JingleContentElement> contents) {
2017-06-18 16:47:49 +02:00
if (role == Role.initiator) {
this.local = initiator;
this.remote = responder;
} else {
this.local = responder;
this.remote = initiator;
}
this.sid = sid;
2017-06-18 16:47:49 +02:00
this.role = role;
2017-06-24 17:46:03 +02:00
if (contents != null) {
this.contents.addAll(contents);
}
2017-06-18 16:47:49 +02:00
}
public FullJid getInitiator() {
return isInitiator() ? local : remote;
}
public boolean isInitiator() {
return role == Role.initiator;
}
public FullJid getResponder() {
return isResponder() ? local : remote;
}
public boolean isResponder() {
return role == Role.responder;
}
2017-06-23 22:48:28 +02:00
public FullJid getRemote() {
return remote;
}
public FullJid getLocal() {
return local;
}
2017-06-18 16:47:49 +02:00
public String getSessionId() {
return sid;
}
public FullJidAndSessionId getFullJidAndSessionId() {
return new FullJidAndSessionId(remote, sid);
}
2017-07-19 15:17:12 +02:00
public List<JingleContentElement> getContents() {
2017-06-24 17:46:03 +02:00
return contents;
}
2017-06-23 22:48:28 +02:00
public JingleTransportSession<?> getTransportSession() {
return transportSession;
}
protected void setTransportSession(JingleTransportSession<?> transportSession) {
this.transportSession = transportSession;
}
@Override
public int hashCode() {
2017-06-18 16:47:49 +02:00
int hashCode = 31 + getInitiator().hashCode();
hashCode = 31 * hashCode + getResponder().hashCode();
hashCode = 31 * hashCode + getSessionId().hashCode();
return hashCode;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof JingleSession)) {
return false;
}
JingleSession otherJingleSession = (JingleSession) other;
2017-06-18 16:47:49 +02:00
return getInitiator().equals(otherJingleSession.getInitiator())
&& getResponder().equals(otherJingleSession.getResponder())
&& sid.equals(otherJingleSession.sid);
}
@Override
2017-07-19 15:17:12 +02:00
public IQ handleJingleSessionRequest(JingleElement jingle) {
2017-06-21 14:11:42 +02:00
try {
switch (jingle.getAction()) {
case content_accept:
return handleContentAccept(jingle);
case content_add:
return handleContentAdd(jingle);
case content_modify:
return handleContentModify(jingle);
case content_reject:
return handleContentReject(jingle);
case content_remove:
return handleContentRemove(jingle);
case description_info:
return handleDescriptionInfo(jingle);
case session_info:
return handleSessionInfo(jingle);
case security_info:
return handleSecurityInfo(jingle);
case session_accept:
return handleSessionAccept(jingle);
case transport_accept:
return handleTransportAccept(jingle);
case transport_info:
return transportSession.handleTransportInfo(jingle);
case session_initiate:
return handleSessionInitiate(jingle);
case transport_reject:
return handleTransportReject(jingle);
case session_terminate:
return handleSessionTerminate(jingle);
case transport_replace:
return handleTransportReplace(jingle);
default:
return IQ.createResultIQ(jingle);
}
2017-06-21 14:11:42 +02:00
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
2017-07-06 14:00:36 +02:00
LOGGER.log(Level.SEVERE, "Caught an Exception: ", e);
return null; //TODO: Handle better?
2017-06-21 14:11:42 +02:00
}
}
2017-07-19 15:17:12 +02:00
protected IQ handleSessionInitiate(JingleElement sessionInitiate) throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException {
return IQ.createResultIQ(sessionInitiate);
}
2017-07-19 15:17:12 +02:00
protected IQ handleSessionTerminate(JingleElement sessionTerminate) {
return IQ.createResultIQ(sessionTerminate);
}
2017-07-19 15:17:12 +02:00
protected IQ handleSessionInfo(JingleElement sessionInfo) {
return IQ.createResultIQ(sessionInfo);
}
2017-07-19 15:17:12 +02:00
protected IQ handleSessionAccept(JingleElement sessionAccept) throws SmackException.NotConnectedException, InterruptedException {
return IQ.createResultIQ(sessionAccept);
}
2017-07-19 15:17:12 +02:00
protected IQ handleContentAdd(JingleElement contentAdd) {
return IQ.createResultIQ(contentAdd);
}
2017-07-19 15:17:12 +02:00
protected IQ handleContentAccept(JingleElement contentAccept) {
return IQ.createResultIQ(contentAccept);
}
2017-07-19 15:17:12 +02:00
protected IQ handleContentModify(JingleElement contentModify) {
return IQ.createResultIQ(contentModify);
}
2017-07-19 15:17:12 +02:00
protected IQ handleContentReject(JingleElement contentReject) {
return IQ.createResultIQ(contentReject);
}
2017-07-19 15:17:12 +02:00
protected IQ handleContentRemove(JingleElement contentRemove) {
return IQ.createResultIQ(contentRemove);
}
2017-07-19 15:17:12 +02:00
protected IQ handleDescriptionInfo(JingleElement descriptionInfo) {
return IQ.createResultIQ(descriptionInfo);
}
2017-07-19 15:17:12 +02:00
protected IQ handleSecurityInfo(JingleElement securityInfo) {
return IQ.createResultIQ(securityInfo);
}
2017-07-19 15:17:12 +02:00
protected IQ handleTransportAccept(JingleElement transportAccept) throws SmackException.NotConnectedException, InterruptedException {
return IQ.createResultIQ(transportAccept);
}
2017-07-19 15:17:12 +02:00
protected IQ handleTransportReplace(JingleElement transportReplace)
2017-06-21 00:16:47 +02:00
throws InterruptedException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, SmackException.NoResponseException {
return IQ.createResultIQ(transportReplace);
}
2017-07-19 15:17:12 +02:00
protected IQ handleTransportReject(JingleElement transportReject) {
return IQ.createResultIQ(transportReject);
}
2017-06-23 22:48:28 +02:00
public abstract XMPPConnection getConnection();
2017-06-29 21:53:57 +02:00
public abstract void onTransportMethodFailed(String namespace);
}