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