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