-
LoginRoster: our remote interface
/*
* LoginRoster.java
*
* Created on May 8, 2003, 12:48 PM
*/
package chat;
import java.rmi.*;
import java.util.Vector;
/** Remote interface.
*
* @author default
* @version 1.0
*/
public interface LoginRoster extends java.rmi.Remote {
public void login(String user) throws RemoteException;
public void logout(String user) throws RemoteException;
public Vector getLoggedIn() throws RemoteException;
}
-
LoginRosterImpl: The server side implementation of our interface.
/*
* LoginRoster_Impl.java
*
* Created on May 8, 2003, 12:51 PM
*/
package chat;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.MalformedURLException;
import java.util.Vector;
/** Unicast remote object implementing LoginRoster_ interface.
*
* @author default
* @version 1.0
*/
public class LoginRoster_Impl extends UnicastRemoteObject implements
LoginRoster {
Vector loggedIn = new Vector();
/** Constructs LoginRoster_Impl object and exports
it on default port.
*/
public LoginRoster_Impl() throws RemoteException
{
super();
}
/** Constructs LoginRoster_Impl object and exports
it on specified port.
* @param port The port for exporting
*/
public LoginRoster_Impl(int port) throws RemoteException
{
super(port);
}
/** Register LoginRoster_Impl 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 {
LoginRoster_Impl obj = new LoginRoster_Impl();
registerToRegistry("LoginRoster_Impl", obj, true);
} catch (RemoteException
ex) {
ex.printStackTrace();
} catch (MalformedURLException
ex) {
ex.printStackTrace();
}
}
public void logout(String user) throws RemoteException
{
System.out.println("Logged
out " + user);
loggedIn.remove(user);
}
public void login(String user) throws RemoteException
{
System.out.println("Logged
in " + user);
loggedIn.add(user);
}
public Vector getLoggedIn() throws RemoteException
{
System.out.println("Returning
logged in vector. size: " + loggedIn.size());
return loggedIn;
}
}
-
Chat with new method to get LoginRoster:
/*
* 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;
public void sendMessage (Message message) throws
RemoteException;
public LoginRoster getLoginRoster() throws RemoteException;
}
/*
* ChatForm.java
*
* Created on May 1, 2003, 1:18 AM
*/
package chat;
import java.rmi.*;
import java.rmi.registry.*;
import javax.swing.JOptionPane;
import java.util.Vector;
/**
*
* @author default
*/
public class ChatForm extends javax.swing.JFrame {
// Globally visible reference to the stub via
interface.
Chat chat;
LoginRoster roster;
String user;
/** Creates new form ChatForm */
public ChatForm() {
initComponents();
// Get the stub.
// Change the URL if
connecting to a different machine.
try {
System.out.println("Getting chat.");
chat = (Chat)Naming.lookup("rmi://localhost/ChatImpl");
System.out.println("Getting roster.");
roster = chat.getLoginRoster();
} catch (Exception e)
{
System.out.println("Exception while attempting to lookup ChatImpl.
Message: " + e.getMessage());
e.printStackTrace();
}
}
/** 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();
btnShowUsers = new javax.swing.JButton();
btnLogin = new javax.swing.JButton();
btnLogout = 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);
btnShowUsers.setText("Show
Users");
btnShowUsers.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnShowUsersActionPerformed(evt);
}
});
jPanel1.add(btnShowUsers);
btnLogin.setText("Login");
btnLogin.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoginActionPerformed(evt);
}
});
jPanel1.add(btnLogin);
btnLogout.setText("Logout");
btnLogout.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLogoutActionPerformed(evt);
}
});
jPanel1.add(btnLogout);
getContentPane().add(jPanel1,
java.awt.BorderLayout.SOUTH);
txtMessage.setRows(4);
jScrollPane1.setViewportView(txtMessage);
getContentPane().add(jScrollPane1,
java.awt.BorderLayout.CENTER);
pack();
}
private void btnLogoutActionPerformed(java.awt.event.ActionEvent
evt) {
// Add your handling code here:
try {
roster.logout(user);
} catch (Exception e) {
System.out.println("Exception while attempting to logout. Message:
" + e.getMessage());
e.printStackTrace();
}
}
private void btnLoginActionPerformed(java.awt.event.ActionEvent
evt) {
try {
// Add your handling code here:
System.out.println("Prompting for login.");
user = JOptionPane.showInputDialog("Enter your username.");
System.out.println("Logging in.");
roster.login(user);
System.out.println("Returning.");
} catch (Exception e) {
System.out.println("Exception while attempting to login. Message:
" + e.getMessage());
e.printStackTrace();
}
}
private void btnShowUsersActionPerformed(java.awt.event.ActionEvent
evt) {
// Add your handling code here:
try {
Vector users = roster.getLoggedIn();
StringBuffer sb = new StringBuffer();
for (int i =0 ; i < users.size(); i++) {
sb.append((String) users.get(i));
}
JOptionPane.showMessageDialog(this, sb.toString());
} catch (RemoteException e) {
System.out.println("RemoteException caught in Show Users. Message:
" + e.getMessage());
e.printStackTrace();
}
}
private void btnSendActionPerformed(java.awt.event.ActionEvent
evt) {
// Send a message to
the bulletin board.
try {
// Make a message. Hardcode some thing in until we prompt the user
for this information.
Message message = new Message(txtMessage.getText(), "BulletinBoard", user);
// chat.sendMessage(txtMessage.getText(), "BulletinBoard");
chat.sendMessage(message);
} 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.JButton btnShowUsers;
private javax.swing.JButton btnLogin;
private javax.swing.JButton btnLogout;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea txtMessage;
// End of variables declaration
}
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 {
BulletinBoard board;
LoginRoster_Impl roster;
/** Constructs ChatImpl object and exports it
on default port.
*/
public ChatImpl() throws RemoteException {
super();
// Fire up our JFrame
here.
board = new BulletinBoard();
board.show();
roster = new LoginRoster_Impl();
}
/** 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();
}
}
/** deprecated */
public void login(String user, String password) {
// roster.login(user);
}
/**
return a reference
to the roster.
*/
public LoginRoster getLoginRoster()
throws RemoteException {
return roster;
}
public void sendMessage(String message, String
user) {
System.out.println("Message:
" + message + " User: " + user);
// call method on JFrame
to add this to our list.
board.addMessage(message);
}
/** For sake of time, just make a convenience
method to the existing sendMessage method. */
public void sendMessage(Message message) {
sendMessage(message.getMessage(),
message.getUser());
}
}
Try it out!
|