/**
 * 
 * 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.SocketException;
import java.net.SocketTimeoutException;
import java.net.URI;

import org.activeio.AsynchChannel;
import org.activeio.Channel;
import org.activeio.SynchChannelServer;
import org.activeio.filter.WriteBufferedAsynchChannel;
import org.activeio.packet.ByteBufferPacket;

import com.ibm.io.async.AsyncServerSocketChannel;

/**
 * @version $Revision$
 */
public class AIOSynchChannelServer implements SynchChannelServer {

    private final AsyncServerSocketChannel serverSocket;
    private final URI bindURI;
    private final URI connectURI;
    private int curentSoTimeout;

    public AIOSynchChannelServer(AsyncServerSocketChannel serverSocket, URI bindURI, URI connectURI) throws IOException {
        this.serverSocket=serverSocket;
        this.bindURI=bindURI;
        this.connectURI=connectURI;
        this.curentSoTimeout = serverSocket.socket().getSoTimeout();
    }
    
    public URI getBindURI() {
        return bindURI;
    }

    public URI getConnectURI() {
        return this.connectURI;
    }

    public void dispose() {
        try {
            serverSocket.close();
        } catch (IOException e) {
        }
    }

    synchronized public void start() throws IOException {
    }

    synchronized public void stop(long timeout) {
    }

    public Channel accept(long timeout) throws IOException {
        try {
	        
            if (timeout == SynchChannelServer.WAIT_FOREVER_TIMEOUT)
	            setSoTimeout(0);
	        else if (timeout == SynchChannelServer.NO_WAIT_TIMEOUT)
	            setSoTimeout(1);
	        else
	            setSoTimeout((int) timeout);
	
            AsynchChannel channel = new AIOAsynchChannel(serverSocket.accept());
            channel = new WriteBufferedAsynchChannel(channel, ByteBufferPacket.createDefaultBuffer(true), false);
            return channel;
	        
        } catch (SocketTimeoutException ignore) {
        }
        return null;	        
    }

    private void setSoTimeout(int i) throws SocketException {
        if (curentSoTimeout != i) {
            serverSocket.socket().setSoTimeout(i);
            curentSoTimeout = i;
        }
    }
    
    public Object narrow(Class target) {
        if( target.isAssignableFrom(getClass()) ) {
            return this;
        }
        return null;
    }
    
    public String toString() {
        return "AIO Server: "+getConnectURI();
    }
    
}