/*
* Copyright 1999 by dreamBean Software,
* All rights reserved.
*/
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.server.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;
import java.util.Iterator;
import java.util.StringTokenizer;
import javax.swing.ListModel;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.SwingUtilities;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/*import masteringrmi.chat.interfaces.Message;
import masteringrmi.chat.interfaces.MessageListener;
import masteringrmi.chat.interfaces.Topic;
import masteringrmi.chat.interfaces.TopicInfo;
import masteringrmi.chat.interfaces.TopicServer;
import masteringrmi.chat.interfaces.ListenerInfo;
*/
/**
* This is the chat client model and controller. This is used
* by the GUI, but can also be used as a standalone application
* for testing purposes.
*
* @see ChatGUI
* @author Rickard �berg (
[email protected])
* @version $Revision:$
*/
public class ChatClient
extends UnicastRemoteObject
implements MessageListener
{
// Constants -----------------------------------------------------
static final String TOPIC_SERVER = "topics";
// Attributes ----------------------------------------------------
TopicServer server;
DefaultComboBoxModel topics;
DefaultListModel users;
Topic currentTopic;
TopicInfo currentTopicInfo;
ListenerInfo info;
String title;
MessageReceiver messageReceiver;
// Static --------------------------------------------------------
/* public static void main(String[] args)
throws IOException
{
// Performance tests
// This will test the throughput of the chat system
// Typically you should get about 0 ms/message (i.e. very low)
// The reason for this is that since the message delivery is
// batched there will be a number of messages for each RMI call
// Set security policy and security manager
// Policy allows everything
System.setProperty("java.security.policy",ChatClient.class.getResource("/client.policy").toString());
System.setSecurityManager(new SecurityManager());
/*
// Get test parameters
int clientCount = new Integer(args[0]).intValue();
int messageCount = new Integer(args[1]).intValue();
int topicIndex = args.length == 3 ? new Integer(args[2]).intValue()-1 : 0;
// Create test clients and subscribe them to the default topic
Collection clients = new ArrayList();
ChatClient client = null;
for (int i = 0; i < clientCount; i++)
{
client = new ChatClient();
client.login("Hello"+i);
client.subscribe(((TopicInfo)client.getTopics().getElementAt(topicIndex)));
clients.add(client);
System.out.println("Client "+i+" created");
}
System.out.println("Clients created");
// Use the last client to send messages
long start = System.currentTimeMillis();
for (int i = 0; i < messageCount; i++)
{
Message message = new Message("Hello"+(clientCount-1),"Text","Hello "+i+"!");
client.publishMessage(message);
if (i % 100 == 0)
System.out.println(i+" messages sent");
}
long end = System.currentTimeMillis();
long time = end - start;
System.out.println("Test done");
// Log off test clients
Iterator enum = clients.iterator();
while (enum.hasNext())
{
client = (ChatClient)enum.next();
client.logout();
}
System.out.println("Clients removed");
// Show results
System.out.println("Total time:"+time);
System.out.println("Nr of clients:"+clientCount);
System.out.println("Total nr of messages:"+(messageCount*(clientCount+1)));
System.out.println("Time/message:"+(time/messageCount));
System.out.println("Time/(message*nr of test clients):"+(time/(messageCount*clientCount)));
System.out.println("Time/(message*(nr of test clients + 1)):"+(time/(messageCount*(clientCount+1))));
}*/
// Constructors --------------------------------------------------
public ChatClient()
throws IOException
{
getTopicServer();
}
// Public --------------------------------------------------------
public void login(String name)
throws RemoteException
{
info = new ListenerInfo(name);
getTopicServer().addListener(info, this);
}
public void logout()
throws RemoteException
{
if (currentTopic != null)
unsubscribe();
getTopicServer().removeListener(info);
server = null;
}
public void subscribe(TopicInfo topicInfo)
throws RemoteException
{
if (currentTopic != null)
unsubscribe();
currentTopic = server.subscribe(topicInfo, info);
currentTopicInfo = topicInfo;
// Force user list to be loaded
getUsers();
}
public void unsubscribe()
throws RemoteException
{
server.unsubscribe(currentTopicInfo, info);
currentTopic = null;
currentTopicInfo = null;
users = null;
}
public void publishMessage(Message message)
throws RemoteException
{
currentTopic.publishMessage(message);
}
public void addMessageReceiver(MessageReceiver mr)
{
this.messageReceiver = mr;
}
public void removeMessageReceiver(MessageReceiver mr)
{
if (this.messageReceiver == mr)
this.messageReceiver = null;
}
public TopicServer getTopicServer()
throws RemoteException
{
if (server == null)
{
try
{
String url="rmi://localhost/";
server = (TopicServer)Naming.lookup(url+"topics");
}
catch (Exception e)
{
//throw new ServerException("Could not load jndi.properties", e);
}
// Get topic list from server
topics = new DefaultComboBoxModel();
Iterator enum = getTopicServer().getTopicInfos().iterator();
while(enum.hasNext())
{
topics.addElement(enum.next());
}
}
return server;
}
public ComboBoxModel getTopics()
throws RemoteException
{
return topics;
}
public ListModel getUsers()
throws RemoteException
{
// Get list from server
if (users == null)
{
users = new DefaultListModel();
Iterator enum = currentTopic.getListenerInfos().iterator();
while(enum.hasNext())
{
users.addElement(enum.next());
}
}
return users;
}
public ListenerInfo getClientInfo()
{
return info;
}
// MessageListener implementation --------------------------------
public synchronized void messagePublished(final Collection messages)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
Iterator enum = messages.iterator();
while (enum.hasNext())
{
messagePublished((Message)enum.next());
}
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public synchronized void messagePublished(Message message)
{
if (server == null)
{
// Not connected - ignore
return;
}
try
{
if (message.getSender().equals(Message.SYSTEM))