// Calculator client, written by Dan around 4/1/98 // modified by Mustafa Dagtekin 3/4/1999 import java.awt.*; import java.awt.event.*; import java.rmi.*; import java.rmi.server.*; class Calculator extends Frame implements ActionListener // This class implements the add2 client. { // class variables private TextField op1_field, op2_field;// Declare it at this scope so that private Label result_field; private CheckboxGroup op_type; private Checkbox opAdd, opSub,opMul; private CheckboxGroup lang_type; private Checkbox langC, langJava,hardware; private CheckboxGroup net_type; private Checkbox netRemote, netLocal; private Button energize,quit; private int addend1, addend2, result; private TextArea textfield; private remoteMath mathserver; private static String the_server_name; // end of class variables public Calculator(String title ) // This is where the interface is set up. { setTitle(title); setLayout(new GridLayout(0,1)); // set up field 1 op1_field = new TextField("0", 10); // create new field op1_field.setEditable(true); // set a property // set up field 2 op2_field = new TextField("0", 10); op2_field.setEditable(true); // set up field 3 (result field) result_field = new Label("0 "); // make space for largest possible answer // set up field panel Panel fields = new Panel(); fields.setBackground(Color.lightGray); fields.add(op1_field); // add the fields to the panel fields.add(op2_field); fields.add(result_field); add(fields); // when this panel is instantiated, Panel op_check = new Panel(); //set up Option Boxes: op_type = new CheckboxGroup(); opAdd = new Checkbox("Add",op_type,true); opSub = new Checkbox("Subtract",op_type,false); opMul = new Checkbox("Multiply",op_type,false); op_check.add(opAdd); op_check.add(opSub); op_check.add(opMul); add(op_check); Panel lang_check = new Panel(); lang_type = new CheckboxGroup(); langC = new Checkbox("C", lang_type,true); langJava = new Checkbox("Java", lang_type, false); hardware = new Checkbox("Hardware", lang_type, false); lang_check.add(langC); lang_check.add(langJava); lang_check.add(hardware); add(lang_check); //lang_check.setBackground(Color.white); Panel net_check = new Panel(); net_type = new CheckboxGroup(); netRemote = new Checkbox("Remote",net_type,true); netLocal = new Checkbox("Local", net_type,false); net_check.add(netRemote); net_check.add(netLocal); add(net_check); // put it at the top of the frame //set up energize button energize = new Button("Execute!!"); energize.addActionListener(this); // set up quit button quit = new Button("Quit"); quit.addActionListener(this); // set up a panel for the buttons Panel controls = new Panel(); controls.setBackground(Color.lightGray); controls.add(energize); controls.add(quit); add(controls); // set up the text field textfield = new TextArea("", 8, 40, 1); textfield.setEditable(false); textfield.setBackground(Color.black); // necessary because non-editable // fields are greyed out by default textfield.setForeground(Color.green); textfield.append( "Welcome to the Calculator Demo! \n\n" + "Individual Studies Project Assignment\n" + "This program demonstrates the use of " + "Remote Method Invocation (RMI) and native methods. \n\n" ); // set up the textfield's panel Panel textpanel = new Panel(); textpanel.setBackground(Color.lightGray); textpanel.add(textfield); add(textpanel); setResizable(true); setSize(300,100); pack(); // arrange components and size window accordingly addWindowListener( new windowWatcher() ); // listen for events from // this object's window // Cause the window to appear on the display setVisible(true); // start a security manager System.setSecurityManager(new RMISecurityManager()); // Attach to the RMI server mathserver = attach(the_server_name); } // end of constructor method "Calculator" private remoteMath attach(String server_name) { remoteMath the_server; try { the_server = (remoteMath)Naming.lookup(server_name); } catch ( Exception e ) { textfield.append("Unable to attach to " + server_name + "\n" + e ); return null; } textfield.append("Attached to " + server_name + "\n"); return the_server; } // end of method "attach" private class windowWatcher extends WindowAdapter // put any windowlistener extension in here. { public void windowClosing(WindowEvent we) // This is what happens when the destroy box in the // upper right corner or the "close" // option from the menu has been picked. { dispose(); // uninstantiate the window } public void windowClosed(WindowEvent we) // This is what happens after the window has been closed // (it's gone from the screen by this time). { System.exit(0); // end execution } } // end of subclass "windowWatcher" public void actionPerformed(ActionEvent ae) // Put any action code in here. { Object source = ae.getSource(); // find out what generated the event // software add action if (source == energize) { if ( get_addends() ) { if (netLocal.getState() && langC.getState() && opAdd.getState()) { textfield.append("Local software add (C) \n"); result = new Arithmetic().Add(addend1, addend2); result_field.setText( Integer.toString(result) ); } if (netLocal.getState() && langC.getState() && opSub.getState()) { textfield.append("Local software subtract (C) \n"); result = new Arithmetic().Sub(addend1, addend2); result_field.setText( Integer.toString(result) ); } if (netLocal.getState() && langC.getState() && opMul.getState()) { textfield.append("Local software multiply (C) \n"); result = new Arithmetic().Mul(addend1, addend2); result_field.setText( Integer.toString(result) ); } if (netLocal.getState() && langJava.getState() && opAdd.getState()) { textfield.append("Local software add (JAVA) \n"); result = new Arithmetic().JavaAdd(addend1, addend2); result_field.setText( Integer.toString(result) ); } if (netLocal.getState() && langJava.getState() && opSub.getState()) { textfield.append("Local software subtract (JAVA) \n"); result = new Arithmetic().JavaSub(addend1, addend2); result_field.setText( Integer.toString(result) ); } if (netLocal.getState() && langJava.getState() && opMul.getState()) { textfield.append("Local software multiply (JAVA) \n"); result = new Arithmetic().JavaMul(addend1, addend2); result_field.setText( Integer.toString(result) ); } if (netRemote.getState() && langC.getState() && opAdd.getState()) { textfield.append("Remote software add (C) \n"); try { result = mathserver.AddTwoC( addend1, addend2 ); } catch ( Exception e ) { textfield.append( "Operation Failed!\nRMI error:\n" + e ); } result_field.setText( Integer.toString(result) ); } if (netRemote.getState() && langC.getState() && opSub.getState()) { textfield.append("Remote software subtract (C) \n"); try { result = mathserver.SubTwoC( addend1, addend2 ); } catch ( Exception e ) { textfield.append( "Operation Failed!\nRMI error:\n" + e ); } result_field.setText( Integer.toString(result) ); } if (netRemote.getState() && langC.getState() && opMul.getState()) { textfield.append("Remote software multiply (C) \n"); try { result = mathserver.MulTwoC( addend1, addend2 ); } catch ( Exception e ) { textfield.append( "Operation Failed!\nRMI error:\n" + e ); } result_field.setText( Integer.toString(result) ); } if (netRemote.getState() && langJava.getState() && opAdd.getState()) { textfield.append("Remote software add (JAVA) \n"); try { result = mathserver.AddTwoJava( addend1, addend2 ); } catch ( Exception e ) { textfield.append( "Operation Failed!\nRMI error:\n" + e ); } result_field.setText( Integer.toString(result) ); } if (netRemote.getState() && langJava.getState() && opSub.getState()) { textfield.append("Remote software subtract (JAVA) \n"); try { result = mathserver.SubTwoJava( addend1, addend2 ); } catch ( Exception e ) { textfield.append( "Operation Failed!\nRMI error:\n" + e ); } result_field.setText( Integer.toString(result) ); } if (netRemote.getState() && langJava.getState() && opMul.getState()) { textfield.append("Remote software multiply (JAVA) \n"); try { result = mathserver.MulTwoJava( addend1, addend2 ); } catch ( Exception e ) { textfield.append( "Operation Failed!\nRMI error:\n" + e ); } result_field.setText( Integer.toString(result) ); } // Hardware CALLS! if (netRemote.getState() && hardware.getState() && opAdd.getState()) { textfield.append("Remote hardware add \n"); try { result = mathserver.AddTwoHW( addend1, addend2 ); } catch ( Exception e ) { textfield.append( "Operation Failed!\nRMI error:\n" + e ); } result_field.setText( Integer.toString(result) ); } if (netRemote.getState() && hardware.getState() && opSub.getState()) { textfield.append("Remote hardware subtract \n"); try { result = mathserver.SubTwoHW( addend1, addend2 ); } catch ( Exception e ) { textfield.append( "Operation Failed!\nRMI error:\n" + e ); } result_field.setText( Integer.toString(result) ); } if (netRemote.getState() && hardware.getState() && opMul.getState()) { textfield.append("Remote hardware multiply \n"); try { result = mathserver.MulTwoHW( addend1, addend2 ); } catch ( Exception e ) { textfield.append( "Operation Failed!\nRMI error:\n" + e ); } result_field.setText( Integer.toString(result) ); } if (netLocal.getState() && hardware.getState() && opAdd.getState()) { textfield.append("Local hardware add \n"); result = new Arithmetic().HW_Add(addend1, addend2); result_field.setText( Integer.toString(result) ); } if (netLocal.getState() && hardware.getState() && opSub.getState()) { textfield.append("Local hardware subtract \n"); result = new Arithmetic().HW_Sub(addend1, addend2); result_field.setText( Integer.toString(result) ); } if (netLocal.getState() && hardware.getState() && opMul.getState()) { textfield.append("Local hardware multiply \n"); result = new Arithmetic().HW_Mul(addend1, addend2); result_field.setText( Integer.toString(result) ); } } } // quit action else if (source == quit) System.exit(0); } // end of method "actionPerformed" private boolean get_addends() // this grabs values from the input fields and handles errors { try { addend1 = Integer.parseInt( op1_field.getText().trim() ); addend2 = Integer.parseInt( op2_field.getText().trim() ); } catch (Exception e) { // it would be nice to print this in red textfield.setForeground(Color.red); textfield.append("Improper input, try again\n"); textfield.setForeground(Color.green); return false; } if ( is16(addend1) && is16 (addend2) ) return true; textfield.append("Number must be 16 bits ( -32766 <= x <= 32767 )\n"); return false; } // end of method "get_addends" private boolean is16(int number) { if (number < 32768 && number >-32767) return true; return false; } // end of method "is16" public static void main(String[] args) { // The length field of an array is a variable, not a method: if ( args.length == 0 ) the_server_name = new String("rmi://bowlingball.cacc.ncsu.edu/math"); else the_server_name = new String("rmi://" + args[0] + "/math"); System.out.println("The server is: " + the_server_name); Frame window = new Calculator("Calculator Client"); } // end of method "main" } // End of class "Calculator"