001/**
002 *
003 * Copyright 2003-2006 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.jingleold.nat;
018
019import java.util.logging.Level;
020import java.util.logging.Logger;
021
022import org.jivesoftware.smack.SmackException;
023import org.jivesoftware.smack.SmackException.NotConnectedException;
024import org.jivesoftware.smack.XMPPConnection;
025import org.jivesoftware.smack.XMPPException;
026
027import org.jivesoftware.smackx.jingleold.JingleSession;
028import org.jivesoftware.smackx.jingleold.listeners.CreatedJingleSessionListener;
029import org.jivesoftware.smackx.jingleold.listeners.JingleSessionListener;
030import org.jivesoftware.smackx.jingleold.media.PayloadType;
031import org.jivesoftware.smackx.jingleold.nat.ICECandidate.Type;
032
033@SuppressWarnings("UnusedVariable")
034public class ICETransportManager extends JingleTransportManager implements JingleSessionListener, CreatedJingleSessionListener {
035    private static final Logger LOGGER = Logger.getLogger(ICETransportManager.class.getName());
036
037    ICEResolver iceResolver = null;
038
039    public ICETransportManager(XMPPConnection xmppConnection, String server, int port) {
040        iceResolver = new ICEResolver(xmppConnection, server, port);
041        try {
042            iceResolver.initializeAndWait();
043        }
044        catch (Exception e) {
045            LOGGER.log(Level.WARNING, "exception", e);
046        }
047    }
048
049    @Override
050    protected TransportResolver createResolver(JingleSession session) throws SmackException, InterruptedException {
051        try {
052            iceResolver.resolve(session);
053        }
054        catch (XMPPException e) {
055            LOGGER.log(Level.WARNING, "exception", e);
056        }
057        return iceResolver;
058    }
059
060    // Implement a Session Listener to relay candidates after establishment
061
062    @Override
063    public void sessionEstablished(PayloadType pt, TransportCandidate rc, TransportCandidate lc, JingleSession jingleSession) throws NotConnectedException, InterruptedException {
064        if (lc instanceof ICECandidate) {
065            if (((ICECandidate) lc).getType().equals(Type.relay)) {
066                RTPBridge rtpBridge = RTPBridge.relaySession(lc.getConnection(), lc.getSessionId(), lc.getPassword(), rc, lc);
067            }
068        }
069    }
070
071    @Override
072    public void sessionDeclined(String reason, JingleSession jingleSession) {
073    }
074
075    @Override
076    public void sessionRedirected(String redirection, JingleSession jingleSession) {
077    }
078
079    @Override
080    public void sessionClosed(String reason, JingleSession jingleSession) {
081    }
082
083    @Override
084    public void sessionClosedOnError(XMPPException e, JingleSession jingleSession) {
085    }
086
087    @Override
088    public void sessionMediaReceived(JingleSession jingleSession, String participant) {
089        // Do Nothing
090    }
091
092    // Session Created
093
094    @Override
095    public void sessionCreated(JingleSession jingleSession) {
096        jingleSession.addListener(this);
097    }
098}