john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

Manager

///////////////////////////////////////////////////////////////
// Class MANAGER
// In charge of trading messages between clientThreads and managing
// who is in the room.

import java.net.*;
import java.io.*;
import java.lang.*;
import java.util.*;


class Manager
{
    HashTable userlist = HashTable();   // Keeps current list of users
    roomManager rooms = null;
    ClientThread temp = null;
    string roomName;


    /////////////////////////////////////////////////////////////////////
    // This now requires constructors..
    //
    // This first one is the constructor for the first room that is created.
    // It is called from the Server class and it is the only time that it is
    // called this way.
    public void Manager(roomManager rooms)  {
        this.rooms = rooms;
        roomName = "default";
    }

    // Every other time a new Manager is created, it is created by someone
    // making a new room from thet roomManager class.  This will automatically
    // change the name of the room, add the user, and still keep track of where
    // the roomManager is.
    public void Manager(String roomName, ClientThread user, roomManager rooms)
    {
        this.rooms = rooms;
        this.roomName = roomName;
        userlist.put(user.userName, user);
    }


    ///////////////////////////////////////////////////////////////////
    // ADDUSER-
    // Adds the user.  The error checking is done by the Manager class
    public boolean addUser(String newUserName, ClientThread clientName)
    {
        //check if name is only characters (no numbers/symbols)
        if ( userName.equals("WHISPER") || userName.equals("WHOIS") )   //etc etc.
        {
            clientName.push("Cannot be a username, reserved keyword, please try another.");
            return (false);
        }

        Enumeration iter = userlist.Elements();   //name already taken?
        while (iter.hasMoreElements())
        {
            temp = (ClientThread) iter.nextElement();
            if (temp.userName == newUserName)
                {    clientName.push("Username already taken, please try another.");
                     return (false);    }
        }
        userlist.put(newUserName, clientName);   // If valid add it to the list
        clientName.push(userName + " is validated...");
        clientName.push("Welcome to: " + roomName);
        return (true);
    }

    //////////////////////////////////////////////////////////////////////
    // REMOVEUSER
    // removes the user and lets everyone else in the room know they left
    public void removeUser(String userName)
    {
            userlist.remove(userName);
            broadcast(userName + " has just left the chat room");
    }

    //////////////////////////////////////////////////////////////////////
    // BROADCAST
    // Sends to all users in the current room (IE the ones that this manager
    // has in its hashtable)
    public synchronized void broadcast(String message)
    {
        Enumeration iter = userlist.Elements();
        while (iter.hasMoreElements())  {
            temp = (ClientThread) iter.nextElement();
            temp.push(message);
        }
    }

    //////////////////////////////////////////////////////////////////////
    // WHISPER
    // sent to only the destination user
    public synchronized void whisper(String message, String receiverName, String senderName)
    {
        Enumeration iter = userlist.Elements();
        while (iter.hasMoreElements())
        {
            temp = (ClientThread) iter.nextElement();
            if (temp.userName.equals(receiverName))
            {   temp.push(message);
                return; }
            else
            {   continue;       }
        }
        temp = (ClientThread) userlist.get(senderName);
        temp.push(recieverName + " does not exist in this chatroom");
    }

    ///////////////////////////////////////////////////////////////////////
    // WHOIS
    // Replies by pushing a String with all the clients on the clients FIFOQ
    public void whois(ClientThread clientName)
    {
        String tempstring = null;
        Enumeration iter = userlist.Elements();
        while (iter.hasMoreElements())
        {
            temp = (ClientThread) iter.NextElement();
            tempstring = (tempstring + ", " + temp.userName);
        }

        (userlist.getElement(clientName)).push(temp);
    }


    ////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////
    // These last three functions deal with the roomManager.  The
    public void listRooms(ClientThread clientName)
    {
        clientThread.push(rooms.listRooms(clientName)));    // ech- messy but works..
    }

    public void switchRoom(String roomName, ClientThread clientName)
    {
        removeUser(clientName.userName);
        rooms.switchRoom(roomName, clientThread);
    }

    public void addRoom (....

        // Havent' done this one yet...
    ...}

}//close of Manager class

  • « Java Chat Server Slave
  • Iofun »

Published

Apr 22, 2001

Category

java-chat-university-v2

~395 words

Tags

  • chat 19
  • java 252
  • manager class 1