View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.test.performance;
20  
21  import java.io.FileOutputStream;
22  import java.io.PrintWriter;
23  import java.rmi.registry.LocateRegistry;
24  import java.rmi.registry.Registry;
25  import java.util.Calendar;
26  import java.util.GregorianCalendar;
27  
28  import jgroup.util.Abort;
29  import jgroup.util.Network;
30  
31  /**
32   *  Test for measuring the performance of standard RMI invocations.
33   *  Can be executed remotely through the <t>Executor</t> class,
34   *  since it implements the <t>Server</t> interface.
35   *
36   *  @author       Alberto Montresor
37   *  @since        Jgroup 1.1
38   */
39  public class RMIClient 
40  extends Thread
41  {
42  
43    ////////////////////////////////////////////////////////////////////////////////////////////
44    // Main
45    ////////////////////////////////////////////////////////////////////////////////////////////
46  
47    /** 
48     * Start client from command line
49     */
50    public static void main(String[] argv)
51      throws Exception
52    {
53      String hostname = null;
54      int port = Registry.REGISTRY_PORT;
55  
56      for (int i = 0 ; i < argv.length ; i++) {
57        if  (argv[i].equals("-rport")) {
58          if ((i + 1) == argv.length) {
59            System.err.println("Options -rport must be followed by a port number");
60            usage();
61            return;
62          }
63          try {
64            port = Integer.parseInt(argv[++i]);
65          } catch (Exception e) {
66            System.err.println("Value " + argv[i] + " is not a number");
67            usage();
68            return;
69          }
70        } else if (argv[i].startsWith("-")) {
71          System.err.println("Unknown option " + argv[i]);
72          usage();
73        } else {
74          if (hostname != null) {
75            System.err.println("Hostfile already specified");
76            usage();
77            return;
78          }
79          hostname = argv[i];
80        }
81      }
82  
83      RMIClient client = new RMIClient();
84      Object[] objs = new Object[] { hostname, new Integer(port) };
85      client.start(objs);
86    }
87    
88    private static void usage()
89    {
90      Abort.usage("Usage: rmispeedclient <hostname> [-rport <port number>]");
91    }
92    
93    ////////////////////////////////////////////////////////////////////////////////////////////
94    // Fields
95    ////////////////////////////////////////////////////////////////////////////////////////////
96  
97    /** Remote reference for the RMI server */
98    SpeedTest test;
99  
100   /** True iff the test is completed */
101   boolean completed = false;
102 
103   
104   ////////////////////////////////////////////////////////////////////////////////////////////
105   // Constructor
106   ////////////////////////////////////////////////////////////////////////////////////////////
107 
108   /**
109    * Default constructor, used by the executor service
110    * to create a service.
111    */
112   public RMIClient()
113   {
114   }
115 
116 
117   ////////////////////////////////////////////////////////////////////////////////////////////
118   // Method
119   ////////////////////////////////////////////////////////////////////////////////////////////
120 
121   /**
122    * Start the service associated with this server object.
123    */
124   public void start(Object[] args)
125     throws Exception
126   {
127     // Check arguments
128     if (args == null || args.length != 2)
129       throw new IllegalArgumentException("Arguments required: String");
130     String hostname = (String) args[0];
131     int port = ((Integer) args[1]).intValue();
132     
133     // Locate server
134     Registry reg = LocateRegistry.getRegistry(hostname);
135     test = (SpeedTest) reg.lookup("Jgroup/SpeedTest");
136     System.out.println("Obtained remote reference: " + test);
137 
138     this.start();
139   }
140   
141   /*
142    * Run method of the thread
143    */
144   public synchronized void run() 
145   {
146     try {
147       // Open log file
148       PrintWriter out = null;
149       try {
150         GregorianCalendar gc = new GregorianCalendar();
151         String name = "client." + Network.getLocalHostName() + gc.get(Calendar.YEAR) +
152           gc.get(Calendar.MONTH) + gc.get(Calendar.DAY_OF_MONTH) +
153           "-" + gc.get(Calendar.HOUR_OF_DAY) + gc.get(Calendar.MINUTE)+".log";
154         out = new PrintWriter(new FileOutputStream(name));
155       } catch (Exception e) {
156         e.printStackTrace();
157       }
158 
159       // Perform test
160       ClientTest.test1(test, out);
161       ClientTest.test2(test, out);
162   
163       // Close log file
164       out.close();
165       completed = true;
166       notify();
167     } catch (Exception e) {
168       e.printStackTrace();
169     }
170   }
171 
172   /**
173    * Stop the service associated with this server object.
174    */
175   public synchronized void halt()
176   {
177     while (!completed)
178       try { wait(); } catch (Exception e) { }
179   }
180 
181 } // END RMICLient
182