package client; import java.rmi.*; import server.ServerInterface; import javax.swing.*; import java.awt.*; /**** * * Class Client is a simple illustration of a client that communicates with a * remote server using RMI. The client provides server-setup and computation * methods, detailed descriptions of which are given in the method * documentation. *
* The main method of the client accepts two command-line arguments. * The first argument is the name of the host on which the server is running. * The host name may be suffixed with a colon-delimited port number. If the * port number is not present, the rmiregistry on the remote host is assumed to * be running on the well-known port 1099. So, e.g., a legal hostname could be * "waldorf.csc.calpoly.edu" or "waldorf.csc.calpoly.edu:1098", the latter * selecting alternate registry port 1098. *
* The second command-line argument is an optional flag indicating if the * client's GUI should be displayed when it runs. If the argument is missing, * the default behavior is to display the GUI. If the second argument is * "-nd", then no GUI display is shown. Using the "-nd" argument is * useful when the client is run in a context where there is no support for a * Java GUI display, for example, running the client on a UNIX machine like * waldorf when logged into waldorf via telnet from a Windows PC. *
* See the Server class for a description * of the server that this client uses. * */ public class Client { /** * Establish communication with the server running on the given * hostname. As explained in the class documentation, the * hostname may be suffixed with colon-delimited port number. The hostname * is used to create a server name of the form * "//hostname/Server". See the documentation for the * server.Server.setupServer method for a explanation of this * server-name syntax. */ protected static void establishCommunication(String hostname) { /* * Set up the security manager if necessary. */ if (System.getSecurityManager() == null) { System.setSecurityManager(new RMISecurityManager()); } /* * Establish communication with the server. */ try { server = (ServerInterface) Naming.lookup( "//" + hostname + "/Server"); } /* * Handle any remote exception that may occur during server set up or * execution. */ catch (Exception e) { System.err.println("Server exception: " + e.getMessage()); e.printStackTrace(); } } /** * Set up the display as a JFrame with a labeled text field. The * hostname input is the command-line argument from main. */ protected static void setupDisplay(String hostname) { /* * Allocate the frame and its innards. */ JFrame frame = new JFrame(); JPanel panel = new JPanel(); Box box = Box.createHorizontalBox(); /* * Add the text field. */ textField = new JTextField(8); box.add(new JLabel("Result of server computation: ")); box.add(textField); /* * Add border for some spacing. */ panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); panel.add(box, BorderLayout.CENTER); /* * Add to, pack, and make visible the frame. */ frame.setTitle("Client of " + hostname); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } /** * Call the following methods on the remote server: *