import java.net.*;
import java.io.*;

public class TelnetServe extends TelnetTTY {
    StringBuffer buf = new StringBuffer();
// This constructor just calls the base class
    public TelnetServe(Socket s) throws IOException {
	super(s);
    }
// on connection, print a sign on message
    protected void connected() {
	try {
	    send("Welcome to the Java Telnet Server\n");
	}
	catch (IOException e) {}
    }
// process an incoming line
    protected void processLine(String line) {
	try {
	    // this is just a simple example
	    if (line.equals("status"))
		send("\nStatus OK\n");
	    else if (line.equals("time"))
		send("\n"+new java.util.Date().toString()+"\n");
	    else if (line.equals("help"))
		send("\nCommands are: status, time, help, quit\n");
	    else if (line.equals("quit")) {
		send("\nGoodbye\n");
		sock.close();
		stopthread=true;
	    }
	    else 
		send("Unknown command\n");
	}
	catch (Exception e) {}
    }
// This overrides the base class and builds a line
// that will be handled by processLine
    protected synchronized void processChar(int c) {
	if (c=='\r') {
	    String line=buf.toString();
	    buf=new StringBuffer();
	    // process line
	    processLine(line);
	}
	else {
	    if (c!='\n') buf.append((char)c);
	}

    }

// Test main
    public static void main(String args[]) throws Exception {
	TelnetTTYServer(TelnetServe.class);
    }
}
