The Line Oriented Talker: Telnet Handler

Telnet may be something of a misnomer, but this handler will be capable of accepting Telnet connections (i.e., from a UNIX shell you can type "telnet host 5555").

There are requirements beyond this class to allow the telnet driver to actually communicate with other users. Those will be covered in the Talker Registry.

Telnet.java

import java.io.*;
import java.util.*;

public class Telnet extends Handler
{
    public Telnet()
    {
        super(); 
        System.out.println("Telnet " + this + " instantiated");
        TalkerRegistry.register(this);
    }
    
    public void processLine(String strLine)
    {
        if (strLine != null)
        {
            strLine = strLine.trim();
        
            if (strLine.length() > 0)
            {
                // check for the @ commands
                if (strLine.toUpperCase().indexOf("@QUIT") == 1)
                {
                    TalkerRegistry.unregister(this);
                }
                else
                {
                    // send a message
                    TalkerRegistry.postMessage(this, strLine);
                }
            }
        }
    }
    
    protected boolean register(Vector vRegistry)
    {
        // first login
        vRegistry.addElement(this);
        boolean bRegistered = true;
 
        System.out.println(this + " logged in.");
        TalkerRegistry.postMessage(this, "Logged in.");

        return bRegistered;
    }


    public void unregister(Vector vRegistry)
    {
        if (vRegistry.indexOf(this) != -1)
        {
            vRegistry.removeElement(this);

            System.out.println(this + " logged out.");
            
            TalkerRegistry.postMessage(this, "Logged out.");
        }

        // stop the listening thread in handler
	bListening = false;
    }
}

How the Telnet handler works

In essence:
  1. The Telnet handler is instantiated and registers itself
  2. The Telnet handler processes lines (Strings) by looking for the @QUIT command
  3. Quitting causes the handler to remove the user from the TalkerRegistry
  4. Normal messages are sent to the TalkerRegistry to propogate

The TalkerRegistry is a linchpin to this. It supplies the Vector containing the registered talkers and distributes messages.

What's wrong with this design?

The Telnet handler sits on top of the functionality provided by the generic handler. The combination isn't very flexible, however. Messages are assumed to be Strings and are passed directly to Handler rather than Telnet. The Handler also takes care of ensuring the network connection polls for incoming data. That's what Telnet should be doing. The next version of the Telnet handler will be more flexible, but at the cost of complexity.

The Telnet handler is also insecure. There is no way to distinguish users (such as user names). There is little checking for spamming or other garbage input. And the present version makes it difficult to cleanly expand the command (note that the only command defined is @QUIT).