The Line Oriented Talker: Talker Registry

The Registry is the linchpin of the entire Talker system. It is static because a Talker handler needs to be able to access it without knowing a reference ahead of time. Additionally, only one registry should be in place for each type of server in the VM.

Think of the Registry as a telephone operator. It is responsible for noting who's logged in and passing messages to each user.

TalkerRegistry.java

import java.lang.*;
import java.util.*;

public class TalkerRegistry extends java.lang.Object
{
    private static Vector vTalkers;

    public static synchronized boolean register(Handler handler)
    {
        if (vTalkers == null)
        {
            vTalkers = new Vector();
        }
        
        boolean bReturn = handler.register(vTalkers);
        
        return bReturn;
    }
    
    public static synchronized void unregister(Handler handler)
    {
        if (vTalkers == null)
        {
            vTalkers = new Vector();
        }
        
        handler.unregister(vTalkers);
    }
    
    public static synchronized boolean isRegistered(Handler handler)
    {
        if (vTalkers == null)
        {
            vTalkers = new Vector();
        }
        
        if (vTalkers.indexOf(handler) != -1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public static void postMessage(Handler handler, String strMessage)
    {
        if (vTalkers == null)
        {
            vTalkers = new Vector();
        }
        
        Object obj;
        Telnet tObj, tHandler;
        
        for (Enumeration enHandlers = vTalkers.elements(); enHandlers.hasMoreElements(); )
        {
            obj = enHandlers.nextElement();
            
            if (handler instanceof Telnet)
            {
                tHandler = (Telnet)handler;

                if (obj instanceof Telnet)
                {
                    tObj = (Telnet)obj;
                    tObj.postMessage(new Message(new String("["+tHandler+"] "+strMessage+"\n")));
                }
            }
            // no other cases defined
        }
    }
}

Notes on the Talker Registry

The Talker Registry is implemented as a static. That means that all members must be static as well (not a bad thing, considering how this Registry is used).

Note that the Talker Registry also requires that the Handler class define a boolean register(Vector) and void unregister(Vector) method. The two lines to add to the Handler class are below:

    public abstract boolean register(Vector vRegistry);

    public abstract void unregister(Vector vRegistry);

What the future holds for the Talker Registry

There's a little more work to be done on the Talker Registry. It needs to store some commmonly used messages (welcome, help, etc.) as well as using some of the features of the Telnet handler (like the user name, when that is implemented).