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.
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;
}
}
The TalkerRegistry is a linchpin to this. It supplies the Vector containing the registered talkers and distributes messages.
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).