import java.io.BufferedReader; import java.io.InputStreamReader; import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.server.UnicastRemoteObject; public class ClientImpl extends UnicastRemoteObject implements Client { private Server server; public ClientImpl () throws RemoteException { super (); } public void option1_from_server () throws RemoteException { System.out.println ("In option 1 from server"); } public String option2_from_server () throws RemoteException { System.out.print ("In option 2 from server, enter text: "); return getUserInput (); } public void go () { int id = 0; try { server = (Server) Naming.lookup ("//localhost:4236/Server"); id = server.connect (this); } catch (Exception e1) { e1.printStackTrace(); } /* * Start the main client loop, which allows players to view * the list of other players, get statistics, or start a game. */ try { while (true) { int choice = getMenuChoice (); switch (choice) { case 1: server.option1 (id); break; case 2: { String result = server.option2 (id); System.out.println ("Server returned " + result); break; } case 3: System.exit (0); } } } catch (RemoteException e) { e.printStackTrace(); } } public static void main (String[] args) { try { ClientImpl client = new ClientImpl (); client.go (); } catch (Exception e) { e.printStackTrace (); } } private static String getUserInput () { BufferedReader keyboard = new BufferedReader (new InputStreamReader (System.in)); String userInput = null; try { userInput = keyboard.readLine (); } catch (Exception e) { e.printStackTrace (); } return userInput; } private int getMenuChoice () { int result; do { displayMenu (); try { String line = getUserInput (); System.out.println ("Getting menu choice, input was " + line); result = Integer.parseInt (line); } catch (NumberFormatException e) { System.out.println ("You must enter a number between 1 and 3"); result = 0; } } while (result < 1 || result > 3); return result; } private void displayMenu () { System.out.println ("Input Blocking Example"); System.out.println ("1. Option 1 (no input in server call)"); System.out.println ("2. Option 2 (input in server call)"); System.out.println ("3. Quit"); } }