250 likes | 363 Views
import java.io.*; public class Customer implements Serializable { public Customer(int theAge, int theSex, String[] theHobbies) { age = theAge; sex = theSex; hobbies = theHobbies; } public int getAge() { return age; } public int getSex() { return sex; }
E N D
import java.io.*; public class Customer implements Serializable { public Customer(int theAge, int theSex, String[] theHobbies) { age = theAge; sex = theSex; hobbies = theHobbies; } public int getAge() { return age; } public int getSex() { return sex; } public boolean hasHobby(String aHobby) { if (aHobby == "") return true; for (int i = 0; i < hobbies.length; i++) if (hobbies[i].equals(aHobby)) return true; return false; }
public void reset() { age = 0; sex = 0; hobbies = null; } public String toString() { String result = "Age: " + age + ", Sex: "; if (sex == Product.MALE) result += "Male"; else if (sex == Product.FEMALE) result += "Female"; else result += "Male or Female"; result += ", Hobbies:"; for (int i = 0; i < hobbies.length; i++) result += " " + hobbies[i]; return result; } private int age; private int sex; private String[] hobbies; }
import java.rmi.*; public interface Product extends Remote { String getDescription() throws RemoteException; static final int MALE = 1; static final int FEMALE = 2; static final int BOTH = MALE + FEMALE; }
import java.rmi.*; import java.rmi.server.*; public class ProductImpl extends UnicastRemoteObject implements Product { public ProductImpl(String n, int s, int age1, int age2, String h) throws RemoteException { name = n; ageLow = age1; ageHigh = age2; sex = s; hobby = h; } public boolean match(Customer c) // local method { if (c.getAge() < ageLow || c.getAge() > ageHigh) return false; if (!c.hasHobby(hobby)) return false; if ((sex & c.getSex()) == 0) return false; return true; }
public String getDescription() throws RemoteException { return "I am a " + name + ". Buy me!"; } private String name; private int ageLow; private int ageHigh; private int sex; private String hobby; }
import java.awt.*; import java.awt.event.*; import java.io.*; import java.rmi.*; import java.rmi.server.*; import java.util.*; import javax.swing.*; public class WarehouseClient { public static void main(String[] args) { JFrame frame = new WarehouseClientFrame(); frame.show(); } }
class WarehouseClientFrame extends JFrame implements ActionListener { public WarehouseClientFrame() { initUI(); System.setSecurityManager(new RMISecurityManager()); try { Properties props = new Properties(); String fileName = "WarehouseClient.properties"; FileInputStream in = new FileInputStream(fileName); props.load(in); String url = props.getProperty("warehouse.url"); if (url == null) url = "rmi://localhost/central_warehouse"; centralWarehouse = (Warehouse)Naming.lookup(url); } catch(Exception e) { System.out.println("Error: Can't connect to warehouse. " + e); } }
private void callWarehouse(Customer c) { try { Vector recommendations = centralWarehouse.find(c); result.setText(c + "\n"); for (int i = 0; i < recommendations.size(); i++) { Product p = (Product)recommendations.get(i); String t = p.getDescription() + "\n"; result.append(t); } } catch(Exception e) { result.setText("Error: " + e); } }
public void actionPerformed(ActionEvent evt) { Object[] hobbyObjects = hobbies.getSelectedValues(); String[] hobbyStrings = new String[hobbyObjects.length]; System.arraycopy(hobbyObjects, 0, hobbyStrings, 0, hobbyObjects.length); Customer c = new Customer(Integer.parseInt(age.getText()), (male.isSelected() ? Product.MALE : 0) + (female.isSelected() ? Product.FEMALE : 0), hobbyStrings); callWarehouse(c); } private void initUI() { setTitle("WarehouseClient"); setSize(300, 300); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.HORIZONTAL; gbc.weightx = 100; gbc.weighty = 0; add(new JLabel("Age:"), gbc, 0, 0, 1, 1); age = new JTextField(4); age.setText("20"); add(age, gbc, 1, 0, 1, 1); male = new JCheckBox("Male", true); female = new JCheckBox("Female", true); add(male, gbc, 0, 1, 1, 1); add(female, gbc, 1, 1, 1, 1); gbc.weighty = 100; add(new JLabel("Hobbies"), gbc, 0, 2, 1, 1); String[] choices = { "Gardening", "Beauty", "Computers", "Household", "Sports" }; gbc.fill = GridBagConstraints.BOTH; hobbies = new JList(choices); add(new JScrollPane(hobbies), gbc, 1, 2, 1, 1);
gbc.weighty = 0; gbc.fill = GridBagConstraints.NONE; JButton submitButton = new JButton("Submit"); add(submitButton, gbc, 0, 3, 2, 1); submitButton.addActionListener(this); gbc.weighty = 100; gbc.fill = GridBagConstraints.BOTH; result = new JTextArea(4, 40); result.setEditable(false); add(result, gbc, 0, 4, 2, 1); } private void add(Component c, GridBagConstraints gbc, int x, int y, int w, int h) { gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = w; gbc.gridheight = h; getContentPane().add(c, gbc); }
private Warehouse centralWarehouse; private JTextField age; private JCheckBox male; private JCheckBox female; private JList hobbies; private JTextArea result; }
RMI Callbacks • Server Calls Back the Client • True Peer-to-Peer Interaction • Client itself must be exportable remote object • Registered with a remote server
Example • Quote Client/Server • Server maintains a list of quotes • Client makes additions to list of quotes • Callback used to allow server to asynchronously update clients with list of quotes
Steps • Specify Remote Server Interface • Specify Remote Client Interface • Develop Remote-server object • Develop Client • Compile • Run