JingleSession.java

  1. /**
  2.  *
  3.  * Copyright 2017 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.jingle;

  18. import java.util.ArrayList;
  19. import java.util.HashSet;
  20. import java.util.List;
  21. import java.util.concurrent.Future;

  22. import org.jivesoftware.smack.XMPPConnection;
  23. import org.jivesoftware.smack.packet.IQ;
  24. import org.jivesoftware.smack.util.EqualsUtil;
  25. import org.jivesoftware.smack.util.HashCode;

  26. import org.jivesoftware.smackx.jingle.element.Jingle;
  27. import org.jivesoftware.smackx.jingle.element.JingleContent;
  28. import org.jivesoftware.smackx.jingle.transports.JingleTransportSession;

  29. import org.jxmpp.jid.FullJid;

  30. public abstract class JingleSession implements JingleSessionHandler {
  31.     protected HashSet<String> failedTransportMethods = new HashSet<>();

  32.     protected final FullJid local;

  33.     protected final FullJid remote;

  34.     protected final Role role;

  35.     protected final String sid;

  36.     protected final List<JingleContent> contents = new ArrayList<>();

  37.     protected ArrayList<Future<?>> queued = new ArrayList<>();
  38.     protected JingleTransportSession<?> transportSession;

  39.     public JingleSession(FullJid initiator, FullJid responder, Role role, String sid) {
  40.         this(initiator, responder, role, sid, null);
  41.     }

  42.     public JingleSession(FullJid initiator, FullJid responder, Role role, String sid, List<JingleContent> contents) {
  43.         if (role == Role.initiator) {
  44.             this.local = initiator;
  45.             this.remote = responder;
  46.         } else {
  47.             this.local = responder;
  48.             this.remote = initiator;
  49.         }
  50.         this.sid = sid;
  51.         this.role = role;

  52.         if (contents != null) {
  53.             this.contents.addAll(contents);
  54.         }
  55.     }

  56.     public FullJid getInitiator() {
  57.         return isInitiator() ? local : remote;
  58.     }

  59.     public boolean isInitiator() {
  60.         return role == Role.initiator;
  61.     }

  62.     public FullJid getResponder() {
  63.         return isResponder() ? local : remote;
  64.     }

  65.     public boolean isResponder() {
  66.         return role == Role.responder;
  67.     }

  68.     public FullJid getRemote() {
  69.         return remote;
  70.     }

  71.     public FullJid getLocal() {
  72.         return local;
  73.     }

  74.     public String getSessionId() {
  75.         return sid;
  76.     }

  77.     public FullJidAndSessionId getFullJidAndSessionId() {
  78.         return new FullJidAndSessionId(remote, sid);
  79.     }

  80.     public List<JingleContent> getContents() {
  81.         return contents;
  82.     }

  83.     public JingleTransportSession<?> getTransportSession() {
  84.         return transportSession;
  85.     }

  86.     protected void setTransportSession(JingleTransportSession<?> transportSession) {
  87.         this.transportSession = transportSession;
  88.     }

  89.     @Override
  90.     public int hashCode() {
  91.         return HashCode.builder()
  92.                        .append(getInitiator())
  93.                        .append(getResponder())
  94.                        .append(getSessionId())
  95.                        .build();
  96.     }

  97.     @Override
  98.     public boolean equals(Object other) {
  99.         return EqualsUtil.equals(this, other, (e, o) ->
  100.             e.append(getInitiator(), o.getInitiator())
  101.               .append(getResponder(), o.getResponder())
  102.               .append(sid, o.sid)
  103.         );
  104.     }

  105.     @Override
  106.     public IQ handleJingleSessionRequest(Jingle jingle) {
  107.         switch (jingle.getAction()) {
  108.         case content_accept:
  109.             return handleContentAccept(jingle);
  110.         case content_add:
  111.             return handleContentAdd(jingle);
  112.         case content_modify:
  113.             return handleContentModify(jingle);
  114.         case content_reject:
  115.             return handleContentReject(jingle);
  116.         case content_remove:
  117.             return handleContentRemove(jingle);
  118.         case description_info:
  119.             return handleDescriptionInfo(jingle);
  120.         case session_info:
  121.             return handleSessionInfo(jingle);
  122.         case security_info:
  123.             return handleSecurityInfo(jingle);
  124.         case session_accept:
  125.             return handleSessionAccept(jingle);
  126.         case transport_accept:
  127.             return handleTransportAccept(jingle);
  128.         case transport_info:
  129.             return transportSession.handleTransportInfo(jingle);
  130.         case session_initiate:
  131.             return handleSessionInitiate(jingle);
  132.         case transport_reject:
  133.             return handleTransportReject(jingle);
  134.         case session_terminate:
  135.             return handleSessionTerminate(jingle);
  136.         case transport_replace:
  137.             return handleTransportReplace(jingle);
  138.         default:
  139.             return IQ.createResultIQ(jingle);
  140.         }
  141.     }

  142.     protected IQ handleSessionInitiate(Jingle sessionInitiate) {
  143.         return IQ.createResultIQ(sessionInitiate);
  144.     }

  145.     protected IQ handleSessionTerminate(Jingle sessionTerminate) {
  146.         return IQ.createResultIQ(sessionTerminate);
  147.     }

  148.     protected IQ handleSessionInfo(Jingle sessionInfo) {
  149.         return IQ.createResultIQ(sessionInfo);
  150.     }

  151.     protected IQ handleSessionAccept(Jingle sessionAccept) {
  152.         return IQ.createResultIQ(sessionAccept);
  153.     }

  154.     protected IQ handleContentAdd(Jingle contentAdd) {
  155.         return IQ.createResultIQ(contentAdd);
  156.     }

  157.     protected IQ handleContentAccept(Jingle contentAccept) {
  158.         return IQ.createResultIQ(contentAccept);
  159.     }

  160.     protected IQ handleContentModify(Jingle contentModify) {
  161.         return IQ.createResultIQ(contentModify);
  162.     }

  163.     protected IQ handleContentReject(Jingle contentReject) {
  164.         return IQ.createResultIQ(contentReject);
  165.     }

  166.     protected IQ handleContentRemove(Jingle contentRemove) {
  167.         return IQ.createResultIQ(contentRemove);
  168.     }

  169.     protected IQ handleDescriptionInfo(Jingle descriptionInfo) {
  170.         return IQ.createResultIQ(descriptionInfo);
  171.     }

  172.     protected IQ handleSecurityInfo(Jingle securityInfo) {
  173.         return IQ.createResultIQ(securityInfo);
  174.     }

  175.     protected IQ handleTransportAccept(Jingle transportAccept) {
  176.         return IQ.createResultIQ(transportAccept);
  177.     }

  178.     protected IQ handleTransportReplace(Jingle transportReplace) {
  179.         return IQ.createResultIQ(transportReplace);
  180.     }

  181.     protected IQ handleTransportReject(Jingle transportReject) {
  182.         return IQ.createResultIQ(transportReject);
  183.     }

  184.     public abstract XMPPConnection getConnection();

  185.     public abstract void onTransportMethodFailed(String namespace);

  186. }