/** 
 * 
 * Copyright 2004 Hiram Chirino
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.activeio.net;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.URI;

import org.activeio.SynchChannel;
import org.activeio.SynchChannelFactory;
import org.activeio.SynchChannelServer;

/**
 * A TcpSynchChannelFactory creates {@see org.activeio.net.TcpSynchChannel}
 * and {@see org.activeio.net.TcpSynchChannelServer} objects.
 * 
 * @version $Revision$
 */
public class DatagramSocketSynchChannelFactory implements SynchChannelFactory {

    /**
     * Uses the {@param location}'s host and port to create a tcp connection to a remote host.
     * 
     * @see org.activeio.SynchChannelFactory#openSynchChannel(java.net.URI)
     */
    public SynchChannel openSynchChannel(URI location) throws IOException {
        DatagramSocket socket=null;
        socket = new DatagramSocket();
        if( location != null ) {
            InetAddress address = InetAddress.getByName(location.getHost());
            socket.connect(address, location.getPort());
        }
        return createSynchChannel(socket);
    }

    /**
     * Uses the {@param location}'s host and port to create a tcp connection to a remote host.
     * 
     */
    public SynchChannel openSynchChannel(URI location, URI localLocation) throws IOException {
        DatagramSocket socket=null;
        InetAddress address = InetAddress.getByName(localLocation.getHost());
        socket = new DatagramSocket(localLocation.getPort(), address);

        if( location != null ) {
            address = InetAddress.getByName(location.getHost());
            socket.connect(address, location.getPort());
        }
        return createSynchChannel(socket);
    }

    /**
     * @param socket
     * @return
     * @throws IOException
     */
    protected SynchChannel createSynchChannel(DatagramSocket socket) throws IOException {
        return new DatagramSocketSynchChannel(socket);
    }

    /**
     * @throws IOException allways thrown.
     * @see org.activeio.SynchChannelFactory#bindSynchChannel(java.net.URI)
     */
    public SynchChannelServer bindSynchChannel(URI location) throws IOException {
        throw new IOException("A SynchChannelServer is not available for this channel.");
    }

}
