FileDocCategorySizeDatePackage
ChatServlet.javaAPI DocExample5266Thu Apr 05 20:16:04 BST 2001None

ChatServlet

public class ChatServlet extends com.oreilly.servlet.RemoteDaemonHttpServlet implements ChatServer

Fields Summary
MessageSource
source
Vector
socketClients
Vector
rmiClients
Constructors Summary
Methods Summary
public voidaddClient(ChatClient client)

    // We have a new RMI client.  Add it to our list.
    rmiClients.addElement(client);
  
public voidbroadcastMessage(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 voiddeleteClient(ChatClient client)

    // Remote the specified client from our list.
    rmiClients.removeElement(client);
  
public voiddoGet(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 voiddoPost(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.StringgetNextMessage()

    // Create a message sink to wait for a new message from the
    // message source.
    return new MessageSink().getNextMessage(source);
  
protected intgetSocketPort()

    // We listen on port 2428 (look at a phone to see why)
    return 2428;
  
public voidhandleClient(java.net.Socket client)

    // We have a new socket client.  Add it to our list.
    socketClients.addElement(client);