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