/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.remoting.transport.async;

import java.io.IOException;

import org.jboss.logging.Logger;
import org.jboss.remoting.ConnectionFailedException;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.RemoteClientInvoker;

/**
 * AsyncClientInvoker uses sockets to remotely connect to the 
 * a remote AsyncServerInvoker.  Requests are sent asynchronously 
 * to allow more concurrent requests to be sent to the server
 * while using fewer sockets.  This is also known as the 'async'
 * protocol.
 * 
 * TODO:
 * If you are running on Java 1.4, this transport
 * transport will take advantage of the NIO 
 * classes to further reduce the resources used on the server.
 *
 * @author <a href="mailto:hiram@coredevelopers.net">Hiram Chirino</a>
 */
public class AsyncClientInvoker extends RemoteClientInvoker  {

	static final Logger log = Logger.getLogger(AsyncClientInvoker.class);
	private ChannelPool pool;
	InvokerLocator uri;

	/**
	 * @param cl
	 * @param locator
	 */
	public AsyncClientInvoker(InvokerLocator locator) {
		super(locator);
		uri = locator;
	}

	/* (non-Javadoc)
	 * @see org.jboss.remoting.ClientInvoker#handleConnect()
	 */
	protected void handleConnect() throws ConnectionFailedException {
		try {
			log.trace("Connecting to: " + uri.getLocatorURI());
			this.pool = Registry.getChannelPool(uri);
			this.pool.connect();
		} catch (IOException e) {
			this.connected = false;
			throw new ConnectionFailedException(
				"Failed to connect to: " + uri.getLocatorURI()+" : "+e);
		}
	}

	/* (non-Javadoc)
	 * @see org.jboss.remoting.ClientInvoker#handleDisconnect()
	 */
	protected void handleDisconnect() {
		if (this.pool != null) {
			this.pool.disconnect();
			this.pool = null;
		}
	}

	protected void finalize() throws Throwable {
		disconnect();
		super.finalize();
	}

	/* (non-Javadoc)
	 * @see org.jboss.remoting.ClientInvoker#transport(java.lang.String, byte[])
	 */
	protected byte[] transport(String sessionId, byte[] buffer)
		throws IOException, ConnectionFailedException {
		if (connected == false) 
			connect();
			
		return pool.sendRequest(buffer);
	}

}
