/*
 * 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.local;

import org.jboss.remoting.AbstractInvoker;
import org.jboss.remoting.ConnectionFailedException;
import org.jboss.remoting.InvocationRequest;
import org.jboss.remoting.InvokerLocator;
import org.jboss.remoting.InvokerRegistry;
import org.jboss.remoting.ServerInvoker;
import org.jboss.remoting.transport.ClientInvoker;

/**
 * LocalClientInvoker does not use any transport protocol for invoking
 * the ServerInvoker, instead will make call directly on it locally.
 * This increases performance since no serialization required as well
 * as needed for push callbacks where InvokerCallbackHandler is in
 * same JVM as the callback server.
 *
 * @author <a href="mailto:telrod@vocalocity.net">Tom Elrod</a>
 * @version $Revision: 1.3.2.2 $
 */
public class LocalClientInvoker extends AbstractInvoker implements ClientInvoker
{
    private ServerInvoker serverInvoker;
    private long invocationTimestamp;

    public LocalClientInvoker(InvokerLocator locator)
    {
        super(locator);
    }

    /**
     * return the last invocation timestamp when this invoker successfully invoked a
     * method and it returned w/o connection failure (if the remote side raised an Exception during the
     * normal course of an invocation, the timestamp should be updated).
     *
     * @return
     */
    public long getLastInvocationTimestamp()
    {
        return invocationTimestamp;
    }

    /**
     * transport a request against a remote ServerInvoker
     *
     * @param invocation
     * @return
     * @throws Throwable
     */
    public Object invoke(InvocationRequest invocation) throws Throwable
    {
        if(log.isDebugEnabled())
        {
            log.debug("Using local client invoker for invocation.");
        }

        Object ret = null;
        if(serverInvoker != null)
        {
            ret = serverInvoker.invoke(invocation);
            invocationTimestamp = System.currentTimeMillis();
        }
        else
        {
            throw new ConnectionFailedException("Error invoking on server because " +
                                                "no local server to call upon.");
        }

        return ret;
    }

    /**
     * subclasses must provide this method to return true if their remote connection is connected and
     * false if disconnected.  in some transports, such as SOAP, this method may always return true, since the
     * remote connectivity is done on demand and not kept persistent like other transports (such as socket-based
     * transport).
     *
     * @return boolean true if connected, false if not
     */
    public boolean isConnected()
    {
        return serverInvoker != null;
    }

    /**
     * connect to the remote invoker
     *
     * @throws ConnectionFailedException
     */
    public void connect() throws ConnectionFailedException
    {
        // nothing to be done here.  Should already have reference to serverInvoker
    }

    /**
     * disconnect from the remote invoker.  Once disconnect called
     * will not be able to re-connect by calling connect since will
     * loose reference to server invoker.
     */
    public void disconnect()
    {
        /**
         * Need to remove myself from registry so will not keep
         * reference to me since I am of no use now (since serverInvoker
         * reference is null).  Will have to create a new one.
         */
        InvokerRegistry.destroyClientInvoker(getLocator());
        serverInvoker = null;
    }

    /**
     * This will set the local reference to the server invoker.
     * This is needed to so can make calls directly against server.
     * @param svrInvoker
     */
    public void setServerInvoker(ServerInvoker svrInvoker)
    {
        this.serverInvoker = svrInvoker;
    }
}
