Note that, as of 4/30/2003, this has not been tested yet.
-
Notes
-
The source files.
-
NetBeans was kind enough to run rmic to generate our stub and skeleton
files, as well as the class files.
-
The client.policy files.
-
Chat
/*
* Chat.java
*
* Created on April 28, 2003, 11:55 PM
*/
package chat;
import java.rmi.*;
/** Remote interface.
*
* @author default
* @version 1.0
*/
public interface Chat extends java.rmi.Remote {
public void login (String user, String password)
throws RemoteException;
public void sendMessage (String message, String
user) throws RemoteException;
}
/*
* ChatForm.java
*
* Created on May 1, 2003, 1:18 AM
*/
package chat;
import java.rmi.*;
import java.rmi.registry.*;
/**
*
* @author default
*/
public class ChatForm extends javax.swing.JFrame {
// Globally visible reference to the stub via
interface.
Chat chat;
/** Creates new form ChatForm */
public ChatForm() {
// Get the stub.
// Change the URL if
connecting to a different machine.
try {
chat = (Chat)Naming.lookup("rmi://localhost/ChatImpl");
} catch (Exception e)
{
System.out.println("Exception while attempting to lookup ChatImpl.
Message: " + e.getMessage());
e.printStackTrace();
}
initComponents();
}
/** This method is called from within the constructor
to
* initialize the form.
* WARNING: Do NOT modify this code. The
content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
btnSend = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
txtMessage = new javax.swing.JTextArea();
addWindowListener(new
java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jLabel1.setText("Send
a Message to the Bulletin Board");
getContentPane().add(jLabel1,
java.awt.BorderLayout.NORTH);
btnSend.setText("Send");
btnSend.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSendActionPerformed(evt);
}
});
jPanel1.add(btnSend);
getContentPane().add(jPanel1,
java.awt.BorderLayout.SOUTH);
txtMessage.setRows(4);
jScrollPane1.setViewportView(txtMessage);
getContentPane().add(jScrollPane1,
java.awt.BorderLayout.CENTER);
pack();
}
private void btnSendActionPerformed(java.awt.event.ActionEvent
evt) {
// Send a message to
the bulletin board.
try {
chat.sendMessage(txtMessage.getText(), "BulletinBoard");
} catch (Exception e)
{
System.out.println("RemoteException while attempting to send message to
Bulletin Board. Message: " + e.getMessage());
}
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent
evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
// Set the security
policy and security manager.
System.setProperty("java.security.policy",
"client.policy");
System.setSecurityManager(new
RMISecurityManager());
new ChatForm().show();
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton btnSend;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea txtMessage;
// End of variables declaration
}
-
ChatImpl (indluces ChatServer logic):
package chat;
/*
* ChatImpl.java
*
* Created on April 28, 2003, 11:48 PM
*/
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.MalformedURLException;
/** Unicast remote object implementing Chat interface.
*
* @author default
* @version 1.0
*/
public class ChatImpl extends java.rmi.server.UnicastRemoteObject
implements Chat {
/** Constructs ChatImpl object and exports it
on default port.
*/
public ChatImpl() throws RemoteException {
super();
}
/** Constructs ChatImpl object and exports it
on specified port.
* @param port The port for exporting
*/
public ChatImpl(int port) throws RemoteException
{
super(port);
}
/** Register ChatImpl object with the RMI registry.
* @param name - name identifying the service
in the RMI registry
* @param create - create local registry
if necessary
* @throw RemoteException if cannot be
exported or bound to RMI registry
* @throw MalformedURLException if name
cannot be used to construct a valid URL
* @throw IllegalArgumentException if null
passed as name
*/
public static void registerToRegistry(String
name, Remote obj, boolean create) throws RemoteException, MalformedURLException{
if (name == null) throw
new IllegalArgumentException("registration name can not be null");
try {
Naming.rebind(name, obj);
} catch (RemoteException
ex){
if (create) {
Registry r = LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
r.rebind(name, obj);
} else throw ex;
}
}
/** Main method.
*/
public static void main(String[] args) {
System.setSecurityManager(new
RMISecurityManager());
try {
ChatImpl obj = new ChatImpl();
registerToRegistry("ChatImpl", obj, true);
} catch (RemoteException
ex) {
ex.printStackTrace();
} catch (MalformedURLException
ex) {
ex.printStackTrace();
}
}
public void login(String user, String password)
{
}
public void sendMessage(String message, String
user) {
System.out.println("Message:
" + message + " User: " + user);
}
}
-
client.policy text was provided on slide 8411.
-
Future Enhancements:
-
Login
-
Send messages to other clients in chat mode
-
Beep
-
DSA digital signatures
Fun with Deployment
|