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     * @param ip the IP address.
039     * @param port the port number.
040     */
041    public FixedResolver(String ip, int port) {
042        super();
043        setFixedCandidate(ip, port);
044    }
045
046    /**
047     * Create a basic resolver, where we provide the IP and port.
048     *
049     * @param ip   an IP address
050     * @param port a port
051     */
052    public void setFixedCandidate(String ip, int port) {
053        fixedCandidate = new TransportCandidate.Fixed(ip, port);
054    }
055
056    /**
057     * Resolve the IP address.
058     * @throws NotConnectedException if the XMPP connection is not connected.
059     * @throws InterruptedException if the calling thread was interrupted.
060     */
061    @Override
062    public synchronized void resolve(JingleSession session) throws XMPPException, NotConnectedException, InterruptedException {
063        if (!isResolving()) {
064            setResolveInit();
065
066            clearCandidates();
067
068            if (fixedCandidate.getLocalIp() == null)
069                fixedCandidate.setLocalIp(fixedCandidate.getIp());
070
071            if (fixedCandidate != null) {
072                addCandidate(fixedCandidate);
073            }
074
075            setResolveEnd();
076        }
077    }
078
079    /**
080     * Initialize the resolver.
081     *
082     * @throws XMPPException if an XMPP protocol error was received.
083     */
084    @Override
085    public void initialize() throws XMPPException {
086        setInitialized();
087    }
088
089    @Override
090    public void cancel() throws XMPPException {
091        // Nothing to do here
092    }
093}