Methods Summary |
---|
public void | addClient(ChatClient client)
// We have a new RMI client. Add it to our list.
rmiClients.addElement(client);
|
public void | broadcastMessage(java.lang.String message)
// Send the message to all the HTTP-connected clients by giving the
// message to the message source
source.sendMessage(message);
// Directly send the message to all the socket-connected clients
Enumeration enum = socketClients.elements();
while (enum.hasMoreElements()) {
Socket client = null;
try {
client = (Socket)enum.nextElement();
PrintStream out = new PrintStream(client.getOutputStream());
out.println(message);
}
catch (IOException e) {
// Problem with a client, close and remote it
try {
if (client != null) client.close();
}
catch (IOException ignored) { }
socketClients.removeElement(client);
}
}
// Directly send the message to all RMI clients
enum = rmiClients.elements();
while (enum.hasMoreElements()) {
ChatClient chatClient = null;
try {
chatClient = (ChatClient)enum.nextElement();
chatClient.setNextMessage(message);
}
catch (RemoteException e) {
// Problem communicating with a client, remove it
deleteClient(chatClient);
}
}
|
public void | deleteClient(ChatClient client)
// Remote the specified client from our list.
rmiClients.removeElement(client);
|
public void | doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
// doGet() returns the next message. It blocks until there is one.
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
// Return the next message (blocking)
out.println(getNextMessage());
|
public void | doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
// Accept the new message as the "message" parameter
String message = req.getParameter("message");
// Broadcast it to all listening clients
if (message != null) broadcastMessage(message);
// Set the status code to indicate there will be no response
res.setStatus(res.SC_NO_CONTENT);
|
public java.lang.String | getNextMessage()
// Create a message sink to wait for a new message from the
// message source.
return new MessageSink().getNextMessage(source);
|
protected int | getSocketPort()
// We listen on port 2428 (look at a phone to see why)
return 2428;
|
public void | handleClient(java.net.Socket client)
// We have a new socket client. Add it to our list.
socketClients.addElement(client);
|