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 org.jivesoftware.smack.SmackException.NotConnectedException;
020import org.jivesoftware.smack.XMPPException;
021
022import org.jivesoftware.smackx.jingleold.JingleSession;
023
024/**
025 * The FixedResolver is a resolver where
026 * the external address and port are previously known when the object is
027 * initialized.
028 *
029 * @author Alvaro Saurin
030 */
031public class FixedResolver extends TransportResolver {
032
033    TransportCandidate fixedCandidate;
034
035    /**
036     * Constructor.
037     */
038    public FixedResolver(String ip, int port) {
039        super();
040        setFixedCandidate(ip, port);
041    }
042
043    /**
044     * Create a basic resolver, where we provide the IP and port.
045     *
046     * @param ip   an IP address
047     * @param port a port
048     */
049    public void setFixedCandidate(String ip, int port) {
050        fixedCandidate = new TransportCandidate.Fixed(ip, port);
051    }
052
053    /**
054     * Resolve the IP address.
055     * @throws NotConnectedException 
056     * @throws InterruptedException 
057     */
058    @Override
059    public synchronized void resolve(JingleSession session) throws XMPPException, NotConnectedException, InterruptedException {
060        if (!isResolving()) {
061            setResolveInit();
062
063            clearCandidates();
064
065            if (fixedCandidate.getLocalIp() == null)
066                fixedCandidate.setLocalIp(fixedCandidate.getIp());
067
068            if (fixedCandidate != null) {
069                addCandidate(fixedCandidate);
070            }
071
072            setResolveEnd();
073        }
074    }
075
076    /**
077     * Initialize the resolver.
078     *
079     * @throws XMPPException
080     */
081    @Override
082    public void initialize() throws XMPPException {
083        setInitialized();
084    }
085
086    @Override
087    public void cancel() throws XMPPException {
088        // Nothing to do here
089    }
090}