/** * CIS 525 Project #3 * David Henderson * Java Program * This applet called "VamlorClient.java" uses RMI (Remote Method Invocation) * to communicate with a server. The applet reads data from the server then * presents it to the user (see documentation) * * @version 1.0 * @author Copyright 2000 - David Henderson */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.rmi.Naming; import java.rmi.RemoteException; import java.net.MalformedURLException; import java.rmi.NotBoundException; import java.util.*; public class VamlorClient extends Applet { VamlorRMI vamlor; // A vamlor is an E-animal. It virtually walks, // runs, sleeps, and eats. Label locationLabel; // The label displaying the vamlor's location // (0-249, 0-249) Label energyLabel; // The label displaying the vamlor's energy // 0-3000 Label healthLabel; // The label displaying the vamlor's health // 0-3000 Label stateLabel; // The label displaying the vamlor's state // Walking, Running, Sleeping Choice menu; // Vamlor selection menu Timer timer = new Timer(); // This timer updates the Vamlor values Timer menutimer = new Timer();// This timer updates the Vamlor menu /** Redraw - Inner class used as a timer. At a scheduled interval, this will fire then update all Vamlor values */ private class Redraw extends TimerTask { public VamlorClient vam; // Hang on to this applet so we can access the members public Redraw(VamlorClient v) { vam = v; } // This gets called every time the timer expires public void run() { vam.redrawit(); } } /** MenuDraw - Inner class used as a timer. At a scheduled interval, this will fire then update Vamlor menu */ private class MenuDraw extends TimerTask { public VamlorClient vam; // Hang on to this applet so we can access the members public MenuDraw(VamlorClient v) { vam = v; } // This gets called every time the timer expires public void run() { vam.menudraw(); } } /** createMenuItems - Applet method that gets called to re-create the menu with all of the alive Vamlors. The server will send an integer list indicating the IDs of the existing Vamlors */ private void createMenuItems() { int [] vlist; // List of existing vamlors String vname; // Vamlor name menu.removeAll(); // Clean out all menu items first // Whenever we use RMI, we must catch the exceptions try { vlist = vamlor.getIDList(); // Get the list of Vamlor IDs from server // As long as we have at least one, build up the menu of Vamlors. // The format is "ID - Name" if (vlist != null && vlist.length > 0) { for (int i = 0; i < vlist.length; i++) { vname = vamlor.getName(vlist[i]); if (vname != null) menu.addItem(vlist[i] + " - " + vname); } } else { menu.addItem("No Vamlors Alive"); // Display this when no Vamlors } } catch (Exception e) { // Indicate that we could not connect to server menu.addItem("Nothing to Select"); add("South", new Label("Error-Cannot Connect To Server", Label.CENTER)); System.out.println(e); } } /** menudraw - Applet method that gets called at specified intervals to update the menu */ private void menudraw() { // Preserve the current selection String currentSelection = menu.getSelectedItem(); createMenuItems(); menu.select(currentSelection); } /** redrawit - Applet method that gets called at specified intervals to update the Vamlor values */ private void redrawit() { String locstr = ""; String energystr = ""; String healthstr = ""; String statestr = ""; int v_id; // Get the currently selected Vamlor String currentSelection = menu.getSelectedItem(); // Determine the ID of the Vamlor by parsing the String StringTokenizer st = new StringTokenizer(currentSelection); try { v_id = Integer.parseInt(st.nextToken()); } catch (Exception e) { v_id = 0; } // If we did not get back any Vamlors, indicate that there is a problem if (v_id == 0) { locstr = "Not Initialized"; energystr = "Not Initialized"; healthstr = "Not Initialized"; statestr = "Not Initialized"; } else { try { // Four server methods are called to get the status of the selected // Vamlor. //getX, getY - Gives its location (0-249, 0-249) locstr = "Location: " + "(" + vamlor.getX(v_id) + ", " + vamlor.getY(v_id) + ")"; // getEnergyLevel - Gives its energy level 0-3000 energystr = "Energy Level: " + vamlor.getEnergyLevel(v_id); // getHealthLevel - Gives its health level 0-3000 healthstr = "Health Level: " + vamlor.getHealthLevel(v_id); // getState - Gives its state (Running, Walking, or Sleeping) String returned = vamlor.getState(v_id); // Check for valid returned string if (returned != null) statestr = "Life State: " + returned; } catch (Exception e) { // Indicate that we could not connect to server add("South", new Label("Error-Cannot Connect To Server", Label.CENTER)); System.out.println(e); } } // Update the four status labels locationLabel.setText(locstr); energyLabel.setText(energystr); healthLabel.setText(healthstr); stateLabel.setText(statestr); } /** init - Applet method. This is called when the applet is first loaded. */ public void init() { // Try to connect to the server. We lookup the registered service using // the "Naming" mechanism. try { vamlor = (VamlorRMI)Naming.lookup("//localhost/VamlorRMIService"); } // Catch all of the different reasons why we could not connect. // NOTE: This is for debugging purposes since the user does not need // to know about the details. catch (MalformedURLException murle) { System.out.println(); System.out.println("MalformedURLException"); System.out.println(murle); add("South", new Label("Error-Cannot Connect To Server", Label.CENTER)); } catch (RemoteException re) { System.out.println(); System.out.println("RemoteException"); System.out.println(re); add("South", new Label("Error-Cannot Connect To Server", Label.CENTER)); } catch (NotBoundException nbe) { System.out.println(); System.out.println("NotBoundException"); System.out.println(nbe); add("South", new Label("Error-Cannot Connect To Server", Label.CENTER)); } catch ( java.lang.ArithmeticException ae) { System.out.println(); System.out.println("java.lang.ArithmeticException"); System.out.println(ae); add("South", new Label("Error-Cannot Connect To Server", Label.CENTER)); } // Gray background setBackground(Color.gray); // Set up a Border Layout for the Applet setLayout(new BorderLayout()); // Add all of the Applet components addComponents(); // Query the server for the selected Vamlor's values every 3 seconds timer.scheduleAtFixedRate(new Redraw(this), 3000, 3000); // Create Vamlor Menu Items for the first time createMenuItems(); // Select the first item for now menu.select(0); // Query the server for the Vamlor list every 10 seconds timer.scheduleAtFixedRate(new MenuDraw(this), 10000, 10000); } /** addComponents - Applet method which adds all of the components to the Applet */ private void addComponents() { // First change the Font for the eintire applet setFont(new Font("Helvetica", Font.BOLD, 14)); // This panel contains components for the menu Panel menuPanel = new Panel(); // This panel contains components for the Vamlor values Panel workPanel = new Panel(); // We select a 2 by 2 grid for holding the Vamlor status values GridLayout grid = new GridLayout(2,2); grid.setVgap(0); // Vertical gap is zero for each row workPanel.setLayout(grid); // Create the Vamlor menu listing all available Vamlors from server menu = new Choice(); // This just makes menu large enough to hold IDs + Names. // Setting the width does not work menu.addItem("DUMMYDUMMYDUMMYDUMMY"); // Add the menu label to the panel menuPanel.add(new Label("Choose Vamlor:", Label.LEFT)); // Then add the menu itself menuPanel.add(menu); // Add each Vamlor label to the Applet // Health, Energy, Location, State locationLabel = new Label("", Label.LEFT); workPanel.add(locationLabel); energyLabel = new Label("", Label.LEFT); workPanel.add(energyLabel ); healthLabel = new Label("", Label.LEFT); workPanel.add(healthLabel); stateLabel = new Label("", Label.LEFT); workPanel.add(stateLabel); add("North", menuPanel); add("Center", workPanel); //Update the values for the first time redrawit(); } }