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

import test.MultipleTestRunner;
import test.OnewayInvokerClientTest;

/**
 * Makes oneway (client side thread pooling) call a number of times using multiple threads
 *
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
 */
public class ServerOnewayInvocationTest extends OnewayTest
{
   public ServerOnewayInvocationTest(String name)
   {
      super(name);
   }

   public ServerOnewayInvocationTest(int numberOfInstances)
   {
      super(numberOfInstances);
   }

   public ServerOnewayInvocationTest(String transport, int port)
   {
      super(transport, port);
   }

   public ServerOnewayInvocationTest(String transport, int port, int numberOfInstances)
   {
      super(transport, port, numberOfInstances);
   }

   protected void printStartMessage()
   {
      System.out.println("\n*****************************************************\n" +
            "Starting oneway preformance test with server oneway.\n" +
            "*****************************************************\n");
   }

   protected Runnable getRunner(String method)
   {
      return new ServerRunner(method);
   }

   public void testOnewayClientInvocation() throws Throwable
   {
      //NO OP since don't want to run this one in this case
   }


   public class ServerRunner implements Runnable
   {
      private String method;

      public ServerRunner(String method)
      {
         this.method = method;
      }

      public void run()
      {
         for (int i = 0; i < NUM_OF_CALLS; i++)
         {
            try
            {
               superMakeServerOnewayInvocation(this.method,
                     String.valueOf(incrementCallCount()));
            }
            catch (Throwable throwable)
            {
               throwable.printStackTrace();
            }
         }
      }
   }


   public static void main(String[] args)
   {
      ServerOnewayInvocationTest client = null;
      if (args.length == 1)
      {
         int instances = Integer.parseInt(args[0]);
         client = new ServerOnewayInvocationTest(instances);
      }
      else if (args.length == 2)
      {
         String transport = args[0];
         int port = Integer.parseInt(args[1]);
         client = new ServerOnewayInvocationTest(transport, port);
      }
      else if (args.length == 3)
      {
         String transport = args[0];
         int port = Integer.parseInt(args[1]);
         int instances = Integer.parseInt(args[2]);
         client = new ServerOnewayInvocationTest(transport, port, instances);
      }
      else
      {
         client = new ServerOnewayInvocationTest(OnewayInvokerClientTest.class.getName());
         System.out.println("Using default transport (" + client.getTransport() +
               ") and default port (" + client.getPort() + ") and " +
               "default number of instances (" + client.getNumberOfInstances() + ")" +
               "\nCan enter transport, port, and instances via command line.");
      }

      try
      {
         //regular class run
         //client.runInvokers();
         MultipleTestRunner runner = new MultipleTestRunner();
         runner.doRun(client, true);
      }
      catch (Throwable e)
      {
         e.printStackTrace();
         System.exit(1);
      }
      System.exit(0);
   }

}