20-CS-694 | Advanced Programming Techniques | Spring 2012 |
---|---|---|
Networking |
Remote Method Invocation
import java.rmi.*; import java.rmi.RemoteException; import java.awt.*; import java.io.*; import java.net.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; class RVClients implements Runnable { Thread runner = null; Socket socket = null; BufferedReader in = null; ObjectOutputStream out = null; RVClient rv; String name; public RVClients (RVClient ca, String nm) { rv = ca; name = nm; } public void disconnect () { try { socket.close(); } catch (Exception e) { rv.status.setText(String.valueOf(e)); } rv.status.setText("Connection Closed"); rv.client = null; } public void connectit () { try { socket = new Socket("localhost", 8689); InputStream ins = socket.getInputStream(); in = new BufferedReader(new InputStreamReader(ins)); out = new ObjectOutputStream(socket.getOutputStream()); rv.status.setText("Ready to send"); runner = new Thread(this); runner.start(); } catch (Exception e) { rv.status.setText("Connection Refused - Server Running?"); rv.client = null; } } public void transmitit() { Successor succ1 = null; Times times5 = null; try { //Times times5 = new Times(5, new Successor(1)); try { succ1 = (Successor)Naming.lookup("rmi:///Successor"); times5 = (Times)Naming.lookup("rmi:///Times"); times5.set(5, (Stream)succ1); } catch (Exception e) { System.out.println(e.toString()); } for (int i=1 ; i <= 10 ; i++) { int number = ((IntObject)(times5.next())).valueOf(); rv.monitor.append(String.valueOf(number)); if ((i % 5) == 0) rv.monitor.append("\n"); else rv.monitor.append(" "); } rv.monitor.append("--------------------\n"); out.writeObject(times5); rv.status.setText("Sending to server"); } catch (Exception e) { System.out.println(e.toString()); rv.status.setText("Cannot Send to Server - Server Down?"); rv.client = null; } } public void run () { try { while (true) { String str = in.readLine(); rv.takein.append(str+"\n"); if (str.equals("") || str == null) break; } } catch (Exception e) { } disconnect(); } } public class RVClient extends Applet implements ActionListener { JButton sendit, leave, openit; JTextField status; JTextArea takein, monitor; RVClients client = null; String codebase = ""; String host = ""; public void init () { setLayout(new BorderLayout()); Panel p = new Panel(); p.setLayout(new BorderLayout()); p.add("North", new JLabel("Status")); p.add("Center", status = new JTextField("No connection yet")); add("North", p); Panel q = new Panel(); q.setLayout(new GridLayout(2,1)); p = new Panel(); p.setLayout(new BorderLayout()); p.add("North", new JLabel("Received From Server")); p.add("Center", new JScrollPane(takein = new JTextArea(10,30))); q.add(p); p = new Panel(); p.setLayout(new BorderLayout()); p.add("North", new JLabel("Running locally...")); p.add("Center", new JScrollPane(monitor = new JTextArea(10,30))); q.add(p); add("Center", q); p = new Panel(); p.setLayout(new GridLayout(1,3,10,10)); p.add(sendit = new JButton("Send It")); p.add(openit = new JButton("Connect")); p.add(leave = new JButton("Close")); add("South", p); codebase = getCodeBase().toString(); host = getCodeBase().getHost(); } public void start () { status.setEditable(false); status.setBackground(Color.white); takein.setEditable(false); takein.setBackground(Color.white); sendit.addActionListener(this); openit.addActionListener(this); leave.addActionListener(this); } public void actionPerformed (ActionEvent evt) { if (evt.getSource() == sendit && client != null) client.transmitit(); else if (evt.getSource() == openit && client == null) { client = new RVClients(this, "object_client"); client.connectit(); } else if (evt.getSource() == leave && client != null) { client.disconnect(); client = null; } } }
|