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  
20  package jgroup.test.performance;
21  
22  import java.io.IOException;
23  import java.rmi.RemoteException;
24  import java.rmi.registry.LocateRegistry;
25  import java.rmi.registry.Registry;
26  import java.rmi.server.UnicastRemoteObject;
27  
28  import jgroup.core.JgroupException;
29  import jgroup.util.Abort;
30  
31  /**
32   *  RMI server 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 RMIServer 
40  implements SpeedTest
41  {
42  
43    ////////////////////////////////////////////////////////////////////////////////////////////
44    // Main
45    ////////////////////////////////////////////////////////////////////////////////////////////
46  
47    /** 
48     * Start server from command line
49     */
50    public static void main(String[] argv)
51      throws Exception
52    {
53      int port = Registry.REGISTRY_PORT;
54      for (int i = 0 ; i < argv.length ; i++) {
55        if  (argv[i].equals("-rport")) {
56          if ((i + 1) == argv.length) {
57            System.err.println("Options -rport must be followed by a port number");
58            usage();
59            return;
60          }
61          try {
62            port = Integer.parseInt(argv[++i]);
63          } catch (Exception e) {
64            System.err.println("Value " + argv[i] + " is not a number");
65            usage();
66            return;
67          }
68        } else {
69          System.err.println("Unknown option " + argv[i]);
70          usage();
71        }    
72      }
73      RMIServer server = new RMIServer();
74      Object[] objs = { new Integer(port) };
75      server.start(objs);
76    }
77  
78    private static void usage()
79    {
80      Abort.usage("Usage: rmispeedserver [-rport <port number>]");
81    }
82  
83    ////////////////////////////////////////////////////////////////////////////////////////////
84    // Constructor
85    ////////////////////////////////////////////////////////////////////////////////////////////
86  
87    /**
88     * Export the server; create a local registry in order
89     * to bind the server.
90     */
91    public RMIServer()
92      throws RemoteException, JgroupException, IOException
93    {
94      UnicastRemoteObject.exportObject(this);
95    }
96  
97    ////////////////////////////////////////////////////////////////////////////////////////////
98    // Methods from Server
99    ////////////////////////////////////////////////////////////////////////////////////////////
100 
101   /**
102    *  Initialization; nothing to do
103    */
104   public void start(Object[] args)
105     throws Exception
106   {
107       int port = ((Integer) args[0]).intValue();
108     Registry reg = LocateRegistry.createRegistry(port);
109     reg.rebind("Jgroup/SpeedTest", this);
110     System.out.println("SpeedTest registered");
111   }
112 
113   /**
114    *  Initialization; nothing to do
115    */
116   public void halt()
117     throws Exception
118   {
119     UnicastRemoteObject.unexportObject(this, false);
120   }
121 
122 
123   ////////////////////////////////////////////////////////////////////////////////////////////
124   // Methods from SpeedTest
125   ////////////////////////////////////////////////////////////////////////////////////////////
126 
127   public void test() throws RemoteException { }
128 
129   public byte[] test(byte[] x) throws RemoteException { return x; }
130 
131   public String test(String x) throws RemoteException { return x; }
132 
133   public void mtest() throws RemoteException { }
134 
135   public byte[] mtest(byte[] x) throws RemoteException { return x; }
136 
137   public void vmtest(byte[] x) throws RemoteException { }
138 
139   public String mtest(String x) throws RemoteException { return x; }
140 
141   /**
142    *  Shut down the server
143    */
144   public void shutdown()
145     throws RemoteException
146   {
147     new Thread() {
148       public void run() {
149         try {
150           sleep(3000);
151         } catch (InterruptedException e) {
152         }
153         Abort.exit(0);
154       }
155     }.start();
156   }
157 
158 }