john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

slaveOverseer

public class slaveOverseer extends Thread {

    private HashTable users = new HashTable();          // Keeps current list of users


    public boolean addUser(Slave slaveName, String userName)  {

        Enumeration iter = lines.Elements();
        while (iter.hasMoreElements())  {           // checks against all the other names
            Slave s = (Slave) iter.getNextElement();
            if (s.slaveName == userName)
                {    slaveName.push("Username already taken, please try another.");
                     return (false);    }
        }

        if (userName == WHOIS)                  // Does several checks. We can add as many as we want
            {    slaveName.push("WHOIS cannot be a username");
                 return (false);    }
        else if (userName.startsWith("/"))
            {    slaveName.push("Username cannot start with a slash");
                     return (false);    }


        users.put(userName, slaveName);
        broadcast(userName + " has just entered the chat room");
        return (true);                                       // If everything checks out, add it to the list and notify everyone

    }


    public void removeUser(String userName)  {
        if (users.containsKey(userName) )
            {   users.remove(userName);                      // Simply remove the user and let everyone else know they left
                broadcast(userName + " has just left the chat room");   }
        else
            broadcast("Fatal Error!!!");
            return;
    }


    public synchronized void broadcast(String message, String senderName) {
        Enumeration iter = users.Elements();
        while (iter.hasMoreElements())  {
            Slave s = (Slave) iter.getNextElement();
            if (s.slaveName == senderName)               // Enumerate through a list and put the message on
            {   continue;   }                    // everyone's outgoing stack except the senders
            else
            {   s.push(message);}
        }
        return;
    }


    public synchronized void whisper(String message, String receiverName, userName) {    // sends message to only recieverName

        Slave s = (Slave) users.get(recieverName);
        if (s == null)      {
            t = users.get(userName);
            t.push(recieverName + " does not exist in this chatroom!"); }
        else
        {   s.push(message);    }


        return;
    }


    public void whois(Slave slaveName)  {
        String temp = new String();
        Enumeration iter = users.Elements();                 // Enumerate through the list and keep adding it to the
        while (iter.hasMoreElements())  {                    // String.  Then stick the String on the user's stack.
            Slave s = (Slave) iter.getNextElement();     // I'm not sure this will work!!
            temp = (temp + ", " + s.slaveName);
        }

        (users.getElement(slaveName)).push(temp);

        return;

    }

}

  • « ChatServer
  • Slave »

Published

Apr 18, 2001

Category

java-chat-university

~248 words

Tags

  • chatcrap 23
  • slaveoverseer 1