Search in sources :

Example 1 with JingleContent

use of org.jivesoftware.smackx.jingle.element.JingleContent in project Smack by igniterealtime.

the class JingleProvider method parse.

@Override
public Jingle parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    Jingle.Builder builder = Jingle.builder(iqData);
    String actionString = parser.getAttributeValue("", Jingle.ACTION_ATTRIBUTE_NAME);
    if (actionString != null) {
        JingleAction action = JingleAction.fromString(actionString);
        builder.setAction(action);
    }
    FullJid initiator = ParserUtils.getFullJidAttribute(parser, Jingle.INITIATOR_ATTRIBUTE_NAME);
    builder.setInitiator(initiator);
    FullJid responder = ParserUtils.getFullJidAttribute(parser, Jingle.RESPONDER_ATTRIBUTE_NAME);
    builder.setResponder(responder);
    String sessionId = parser.getAttributeValue("", Jingle.SESSION_ID_ATTRIBUTE_NAME);
    builder.setSessionId(sessionId);
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String tagName = parser.getName();
                switch(tagName) {
                    case JingleContent.ELEMENT:
                        JingleContent content = parseJingleContent(parser, parser.getDepth());
                        builder.addJingleContent(content);
                        break;
                    case JingleReason.ELEMENT:
                        JingleReason reason = parseJingleReason(parser);
                        builder.setReason(reason);
                        break;
                    default:
                        LOGGER.severe("Unknown Jingle element: " + tagName);
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    return builder.build();
}
Also used : Jingle(org.jivesoftware.smackx.jingle.element.Jingle) JingleAction(org.jivesoftware.smackx.jingle.element.JingleAction) JingleReason(org.jivesoftware.smackx.jingle.element.JingleReason) FullJid(org.jxmpp.jid.FullJid) JingleContent(org.jivesoftware.smackx.jingle.element.JingleContent)

Example 2 with JingleContent

use of org.jivesoftware.smackx.jingle.element.JingleContent in project Smack by igniterealtime.

the class JingleProvider method parseJingleContent.

public static JingleContent parseJingleContent(XmlPullParser parser, final int initialDepth) throws XmlPullParserException, IOException, SmackParsingException {
    JingleContent.Builder builder = JingleContent.getBuilder();
    String creatorString = parser.getAttributeValue("", JingleContent.CREATOR_ATTRIBUTE_NAME);
    JingleContent.Creator creator = JingleContent.Creator.valueOf(creatorString);
    builder.setCreator(creator);
    String disposition = parser.getAttributeValue("", JingleContent.DISPOSITION_ATTRIBUTE_NAME);
    builder.setDisposition(disposition);
    String name = parser.getAttributeValue("", JingleContent.NAME_ATTRIBUTE_NAME);
    builder.setName(name);
    String sendersString = parser.getAttributeValue("", JingleContent.SENDERS_ATTRIBUTE_NAME);
    if (sendersString != null) {
        JingleContent.Senders senders = JingleContent.Senders.valueOf(sendersString);
        builder.setSenders(senders);
    }
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String tagName = parser.getName();
                String namespace = parser.getNamespace();
                switch(tagName) {
                    case JingleContentDescription.ELEMENT:
                        {
                            JingleContentDescription description;
                            JingleContentDescriptionProvider<?> provider = JingleContentProviderManager.getJingleContentDescriptionProvider(namespace);
                            if (provider == null) {
                                StandardExtensionElement standardExtensionElement = StandardExtensionElementProvider.INSTANCE.parse(parser);
                                description = new UnknownJingleContentDescription(standardExtensionElement);
                            } else {
                                description = provider.parse(parser);
                            }
                            builder.setDescription(description);
                            break;
                        }
                    case JingleContentTransport.ELEMENT:
                        {
                            JingleContentTransport transport;
                            JingleContentTransportProvider<?> provider = JingleContentProviderManager.getJingleContentTransportProvider(namespace);
                            if (provider == null) {
                                StandardExtensionElement standardExtensionElement = StandardExtensionElementProvider.INSTANCE.parse(parser);
                                transport = new UnknownJingleContentTransport(standardExtensionElement);
                            } else {
                                transport = provider.parse(parser);
                            }
                            builder.setTransport(transport);
                            break;
                        }
                    default:
                        LOGGER.severe("Unknown Jingle content element: " + tagName);
                        break;
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    return builder.build();
}
Also used : UnknownJingleContentDescription(org.jivesoftware.smackx.jingle.element.UnknownJingleContentDescription) JingleContentDescription(org.jivesoftware.smackx.jingle.element.JingleContentDescription) StandardExtensionElement(org.jivesoftware.smack.packet.StandardExtensionElement) UnknownJingleContentDescription(org.jivesoftware.smackx.jingle.element.UnknownJingleContentDescription) JingleContent(org.jivesoftware.smackx.jingle.element.JingleContent) JingleContentTransport(org.jivesoftware.smackx.jingle.element.JingleContentTransport) UnknownJingleContentTransport(org.jivesoftware.smackx.jingle.element.UnknownJingleContentTransport) UnknownJingleContentTransport(org.jivesoftware.smackx.jingle.element.UnknownJingleContentTransport)

Example 3 with JingleContent

use of org.jivesoftware.smackx.jingle.element.JingleContent in project Smack by igniterealtime.

the class JingleS5BTransportSession method initiateSession.

private void initiateSession() {
    Socks5Proxy.getSocks5Proxy().addTransfer(createTransport().getDestinationAddress());
    JingleContent content = jingleSession.getContents().get(0);
    UsedCandidate usedCandidate = chooseFromProposedCandidates(theirProposal);
    if (usedCandidate == null) {
        ourChoice = CANDIDATE_FAILURE;
        Jingle candidateError = transportManager().createCandidateError(jingleSession.getRemote(), jingleSession.getInitiator(), jingleSession.getSessionId(), content.getSenders(), content.getCreator(), content.getName(), theirProposal.getStreamId());
        try {
            jingleSession.getConnection().sendStanza(candidateError);
        } catch (SmackException.NotConnectedException | InterruptedException e) {
            LOGGER.log(Level.WARNING, "Could not send candidate-error.", e);
        }
    } else {
        ourChoice = usedCandidate;
        Jingle jingle = transportManager().createCandidateUsed(jingleSession.getRemote(), jingleSession.getInitiator(), jingleSession.getSessionId(), content.getSenders(), content.getCreator(), content.getName(), theirProposal.getStreamId(), ourChoice.candidate.getCandidateId());
        try {
            jingleSession.getConnection().sendIqRequestAndWaitForResponse(jingle);
        } catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
            LOGGER.log(Level.WARNING, "Could not send candidate-used.", e);
        }
    }
    connectIfReady();
}
Also used : Jingle(org.jivesoftware.smackx.jingle.element.Jingle) JingleContent(org.jivesoftware.smackx.jingle.element.JingleContent)

Example 4 with JingleContent

use of org.jivesoftware.smackx.jingle.element.JingleContent in project Smack by igniterealtime.

the class JingleS5BTransportSession method connectIfReady.

/**
 * Determine, which candidate (ours/theirs) is the nominated one.
 * Connect to this candidate. If it is a proxy and it is ours, activate it and connect.
 * If its a proxy and it is theirs, wait for activation.
 * If it is not a proxy, just connect.
 */
private void connectIfReady() {
    JingleContent content = jingleSession.getContents().get(0);
    if (ourChoice == null || theirChoice == null) {
        // Not yet ready.
        LOGGER.log(Level.INFO, "Not ready.");
        return;
    }
    if (ourChoice == CANDIDATE_FAILURE && theirChoice == CANDIDATE_FAILURE) {
        LOGGER.log(Level.INFO, "Failure.");
        jingleSession.onTransportMethodFailed(getNamespace());
        return;
    }
    LOGGER.log(Level.INFO, "Ready.");
    // Determine nominated candidate.
    UsedCandidate nominated;
    if (ourChoice != CANDIDATE_FAILURE && theirChoice != CANDIDATE_FAILURE) {
        if (ourChoice.candidate.getPriority() > theirChoice.candidate.getPriority()) {
            nominated = ourChoice;
        } else if (ourChoice.candidate.getPriority() < theirChoice.candidate.getPriority()) {
            nominated = theirChoice;
        } else {
            nominated = jingleSession.isInitiator() ? ourChoice : theirChoice;
        }
    } else if (ourChoice != CANDIDATE_FAILURE) {
        nominated = ourChoice;
    } else {
        nominated = theirChoice;
    }
    if (nominated == theirChoice) {
        LOGGER.log(Level.INFO, "Their choice, so our proposed candidate is used.");
        boolean isProxy = nominated.candidate.getType() == JingleS5BTransportCandidate.Type.proxy;
        try {
            nominated = connectToOurCandidate(nominated.candidate);
        } catch (InterruptedException | IOException | XMPPException | SmackException | TimeoutException e) {
            LOGGER.log(Level.INFO, "Could not connect to our candidate.", e);
            // TODO: Proxy-Error
            return;
        }
        if (isProxy) {
            LOGGER.log(Level.INFO, "Is external proxy. Activate it.");
            Bytestream activate = new Bytestream(ourProposal.getStreamId());
            activate.setMode(null);
            activate.setType(IQ.Type.set);
            activate.setTo(nominated.candidate.getJid());
            activate.setToActivate(jingleSession.getRemote());
            activate.setFrom(jingleSession.getLocal());
            try {
                jingleSession.getConnection().sendIqRequestAndWaitForResponse(activate);
            } catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
                LOGGER.log(Level.WARNING, "Could not activate proxy.", e);
                return;
            }
            LOGGER.log(Level.INFO, "Send candidate-activate.");
            Jingle candidateActivate = transportManager().createCandidateActivated(jingleSession.getRemote(), jingleSession.getInitiator(), jingleSession.getSessionId(), content.getSenders(), content.getCreator(), content.getName(), nominated.transport.getStreamId(), nominated.candidate.getCandidateId());
            try {
                jingleSession.getConnection().sendIqRequestAndWaitForResponse(candidateActivate);
            } catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
                LOGGER.log(Level.WARNING, "Could not send candidate-activated", e);
                return;
            }
        }
        LOGGER.log(Level.INFO, "Start transmission.");
        Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, !isProxy);
        callback.onSessionInitiated(bs);
    } else // Our choice
    {
        LOGGER.log(Level.INFO, "Our choice, so their candidate was used.");
        boolean isProxy = nominated.candidate.getType() == JingleS5BTransportCandidate.Type.proxy;
        if (!isProxy) {
            LOGGER.log(Level.INFO, "Direct connection.");
            Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, true);
            callback.onSessionInitiated(bs);
        } else {
            LOGGER.log(Level.INFO, "Our choice was their external proxy. wait for candidate-activate.");
        }
    }
}
Also used : SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) Socks5BytestreamSession(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession) Jingle(org.jivesoftware.smackx.jingle.element.Jingle) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) JingleContent(org.jivesoftware.smackx.jingle.element.JingleContent) XMPPException(org.jivesoftware.smack.XMPPException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with JingleContent

use of org.jivesoftware.smackx.jingle.element.JingleContent in project Smack by igniterealtime.

the class JingleTransportSession method processJingle.

public void processJingle(Jingle jingle) {
    if (jingle.getContents().size() == 0) {
        return;
    }
    JingleContent content = jingle.getContents().get(0);
    JingleContentTransport t = content.getTransport();
    if (t != null && t.getNamespace().equals(getNamespace())) {
        setTheirProposal(t);
    }
}
Also used : JingleContent(org.jivesoftware.smackx.jingle.element.JingleContent) JingleContentTransport(org.jivesoftware.smackx.jingle.element.JingleContentTransport)

Aggregations

JingleContent (org.jivesoftware.smackx.jingle.element.JingleContent)7 Jingle (org.jivesoftware.smackx.jingle.element.Jingle)3 JingleContentTransport (org.jivesoftware.smackx.jingle.element.JingleContentTransport)3 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1 SmackException (org.jivesoftware.smack.SmackException)1 XMPPException (org.jivesoftware.smack.XMPPException)1 StandardExtensionElement (org.jivesoftware.smack.packet.StandardExtensionElement)1 Socks5BytestreamSession (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession)1 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)1 JingleAction (org.jivesoftware.smackx.jingle.element.JingleAction)1 JingleContentDescription (org.jivesoftware.smackx.jingle.element.JingleContentDescription)1 JingleReason (org.jivesoftware.smackx.jingle.element.JingleReason)1 UnknownJingleContentDescription (org.jivesoftware.smackx.jingle.element.UnknownJingleContentDescription)1 UnknownJingleContentTransport (org.jivesoftware.smackx.jingle.element.UnknownJingleContentTransport)1 Test (org.junit.jupiter.api.Test)1 FullJid (org.jxmpp.jid.FullJid)1