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;
026import org.jivesoftware.smackx.jingleold.JingleSession;
027import org.jivesoftware.smackx.jingleold.listeners.CreatedJingleSessionListener;
028import org.jivesoftware.smackx.jingleold.listeners.JingleSessionListener;
029import org.jivesoftware.smackx.jingleold.media.PayloadType;
030import org.jivesoftware.smackx.jingleold.nat.ICECandidate.Type;
031
032public class ICETransportManager extends JingleTransportManager implements JingleSessionListener, CreatedJingleSessionListener {
033    private static final Logger LOGGER = Logger.getLogger(ICETransportManager.class.getName());
034
035    ICEResolver iceResolver = null;
036
037    public ICETransportManager(XMPPConnection xmppConnection, String server, int port) {
038        iceResolver = new ICEResolver(xmppConnection, server, port);
039        try {
040            iceResolver.initializeAndWait();
041        }
042        catch (Exception e) {
043            LOGGER.log(Level.WARNING, "exception", e);
044        }
045    }
046
047    @Override
048    protected TransportResolver createResolver(JingleSession session) throws SmackException, InterruptedException {
049        try {
050            iceResolver.resolve(session);
051        }
052        catch (XMPPException e) {
053            LOGGER.log(Level.WARNING, "exception", e);
054        }
055        return iceResolver;
056    }
057
058    // Implement a Session Listener to relay candidates after establishment
059
060    @Override
061    public void sessionEstablished(PayloadType pt, TransportCandidate rc, TransportCandidate lc, JingleSession jingleSession) throws NotConnectedException, InterruptedException {
062        if (lc instanceof ICECandidate) {
063            if (((ICECandidate) lc).getType().equals(Type.relay)) {
064                RTPBridge rtpBridge = RTPBridge.relaySession(lc.getConnection(), lc.getSessionId(), lc.getPassword(), rc, lc);
065            }
066        }
067    }
068
069    @Override
070    public void sessionDeclined(String reason, JingleSession jingleSession) {
071    }
072
073    @Override
074    public void sessionRedirected(String redirection, JingleSession jingleSession) {
075    }
076
077    @Override
078    public void sessionClosed(String reason, JingleSession jingleSession) {
079    }
080
081    @Override
082    public void sessionClosedOnError(XMPPException e, JingleSession jingleSession) {
083    }
084
085    @Override
086    public void sessionMediaReceived(JingleSession jingleSession, String participant) {
087        // Do Nothing
088    }
089
090    // Session Created
091
092    @Override
093    public void sessionCreated(JingleSession jingleSession) {
094        jingleSession.addListener(this);
095    }
096}