import edu.vt.cs.collab.cork.ObjectID; import edu.vt.cs.collab.cork.ReplicatedObjectClient; import edu.vt.cs.collab.cork.impl.inet.InetClient; import edu.vt.cs.collab.cork.search.RetrievableCriteria; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.beans.*; /** * Simple CORK client that retrieves or creates a replicated TestBean. * Should be executed with server host and port number as arguments. */ public class Client3 { /** * Our replicated object client */ private ReplicatedObjectClient client = null; /** * Replicated TestBean instance */ private TestBean testBean = null; public Client3(String host, int port) { try { client = new InetClient(host, port, InetClient.SOCKET); client.login("my-id", "my-password"); // Connection and login succeeded if we got to this point. See // if a TestBean has already been created. RetrievableCriteria crit = new RetrievableCriteria(client.getUser(), TestBean.class); ObjectID ids[] = client.getIdentifiers(crit); if ((ids == null) || (ids.length == 0)) { // The search didn't find TestBeans, so we create one testBean = (TestBean) client.getNewObject(TestBean.class, null); } else { // We have at least one TestBean replica in the server. We // could do other tests if we had more than one, but for // this demo we just take the first one. testBean = (TestBean) client.getObject(ids[0]); } // Attach a listener to ensure that remote replicas find // out about local changes: testBean.addPropertyChangeListener(new TestBeanListener(client)); } catch(SecurityException se) { System.err.println("Login failed: "); se.printStackTrace(); } catch(Exception e) { System.err.println("Client connection error: "); e.printStackTrace(); } } /** * Displays a simple UI for viewing and changing the replicated * TestBean's state. */ public void showUI() { // Make a window JFrame f = new JFrame("TestBean demo"); // Kill the client when the window closes f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } }); Container pane = f.getContentPane(); pane.setLayout(new GridLayout(2, 1, 3, 3)); JPanel top = new JPanel(); pane.add(top); top.setLayout(new BorderLayout()); top.add(new JLabel("value: "), BorderLayout.WEST); final JTextField current = new JTextField(); top.add(current, BorderLayout.CENTER); current.setEditable(false); // Initialize the current value field if (testBean.getValue() != null) current.setText(testBean.getValue()); else current.setText(""); // Listen for changes to the "value" property, update the // field when changes occurr. testBean.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent pce) { if (testBean.getValue() != null) current.setText(testBean.getValue()); else current.setText(""); } }); JPanel bottom = new JPanel(); pane.add(bottom); bottom.setLayout(new BorderLayout()); // Add a field to the bottom for entering a new value final JTextField newValue = new JTextField(); bottom.add(newValue, BorderLayout.CENTER); JButton button = new JButton("Set"); bottom.add(button, BorderLayout.EAST); // An action listener (for both the button and the field) that // updates the replica. ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent evt) { testBean.setValue(newValue.getText()); } }; button.addActionListener(al); newValue.addActionListener(al); f.pack(); f.setSize(300, f.getSize().height); f.show(); } public static void main(String[] args) { int port = Integer.parseInt(args[1]); Client3 c = new Client3(args[0], port); c.showUI(); } }