Server Implementation
Listing 11.2
RemoteServerImpl.java (Complete code)
import java.rmi.*;
import java.rmi.server.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;
//server class
public class RemoteServerImpl extends
UnicastRemoteObject
implements ServerInterface {
//instance variables
private JButton btnSend;
private JTextArea txtReceive,txtPass;
private ClientInterface client;
//vector used to store messages
private Vector messages = new Vector();
private ThreadHandler run;
//constructor
public RemoteServerImpl()
throws RemoteException{
System.out.println("Creating remote object");
this.setup();
this.bindIt();
this.startThread();
}
//{{Method block}}//
//method to create various components
private void setup(){
//create a frame
JFrame frame = new JFrame("Remote Server");
//get the content pane object
Container cp = frame.getContentPane();
//panel for buttons
JPanel btnPanel = new JPanel();
//text area for messages received.
txtReceive = new JTextArea(15,45);
//set the border of text area
txtReceive.setBorder(BorderFactory.createTitledBorder(
"Messages Received"));
txtReceive.setEditable(false);
//text area for typing messages.
txtPass = new JTextArea(5,45);
txtPass.setBorder(BorderFactory.createTitledBorder(
"Type your message here"));
//button used to send a message
btnSend = new JButton("Send Message");
btnSend.addActionListener(new ButtonHandler());
JScrollPane
cPane = new JScrollPane(txtReceive),
sPane = new JScrollPane(txtPass);
//add all the components
btnPanel.add(btnSend);
cp.add(cPane,BorderLayout.NORTH);
cp.add(sPane,BorderLayout.CENTER);
cp.add(btnPanel,BorderLayout.SOUTH);
//set default close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//pack the frame and show it
frame.pack();
frame.show();
//set focus
txtPass.requestFocus();
}
//method used to bind the server
private void bindIt(){
try{
Naming.rebind("Server",this);
}catch(Exception ex){
txtReceive.append(ex.getMessage());
}
}
//method used to register the client. It gets a reference
//to the remote client that is used to identify the client.
public String getRegistered(ClientInterface ci)
throws RemoteException{
client = ci;
txtReceive.append("Registered a remote client");
return "Registered";
}
//remote method used to post a message to the
//server.
public void passMessage(String message)
throws RemoteException {
txtReceive.append(message);
}
//method used to start the thread
private void startThread(){
messages = new Vector();
run = new ThreadHandler();
Thread th = new Thread(run);
th.start();
}
//main method creates an instance of the
//remote object implementation class
public static void main(String args[]){
try{
RemoteServerImpl obj = new RemoteServerImpl();
}catch(Exception ex){
System.out.println(ex.getMessage());
}
}
//{{Private classes}}///
//event handling class.
private class ButtonHandler
implements ActionListener{
//method invoked when the button is pressed
public void actionPerformed(ActionEvent ae){
//add the message to vector
messages.add(txtPass.getText());
txtPass.setText("");
txtPass.requestFocus();
//method used to notify the waiting thread
run.wakeUp();
}
}
// Class used to handle threading
private class ThreadHandler
implements Runnable {
//method used to notify the waiting thread
public synchronized void wakeUp(){
notifyAll();
}
//method used to pass messages to remote client
public synchronized void submit()
throws Exception{
if(messages.size()<1){
//no data available please wait
wait();
}
else{
try{
for(int i=0;i<messages.size();i++){
client.passMessage("\n" + messages.elementAt(0));
messages.removeElementAt(0);
}
}catch(Exception ex){
wait();
}
}
}
public void run(){
while(true){
try{
submit();
}catch(Exception ex){
txtReceive.append(ex.getMessage());
}
}
}
}
}
|