john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

ChatProtocol

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


public class ChatProtocol {
    private static final int WAITING = 0;
    private static final int ENTERNAME = 1;
    private static final int CHATTING = 2;
    private static final int BYE = 3;
    private static final int WHOIS = 4;

    private int state = WAITING;

    public String processInput(String theInput) throws IOException
    {
        String theOutput = null;

        if(theInput == "LOGGING ON");   //TO START IT IN THE WAIT STATE
        {
            System.out.println("Connecting...");
            state = WAITING;
        }

        if (state == WAITING)
        {
            theOutput = "Welcome to ChatServe .03, Type in your Chat Name:";

            state = ENTERNAME;
        }
        else if (state == ENTERNAME)
        {

        /*Check Name against userlist

            File userlistFile = null;
            FileWriter outFile = null;
*/
            theOutput = "Don't worry about it.";

            state = CHATTING;

        }
        else if (state == CHATTING)
        {
            if(theInput.equalsIgnoreCase("LOGOFF"))
            {
                state = BYE;
            }
            else if(theInput.equalsIgnoreCase("WHOIS"))
            {
                state = WHOIS;
            }
            else
            {
                theOutput = "testing";
            }

        }
        else if(state == WHOIS)
        {

            theOutput = "not implemented yet";
            state = CHATTING;
/*          int eof;
            File userlistFile = new File("userlist.txt");
            FileReader userlistin = new FileReader(userlistFile);
            while ((eof = userlistin.read()) != -1)
                out.write(c);
*/

        }
        else
        {
            theOutput = "Bye.";
            state = WAITING;
        }

        return theOutput;
    }//processInput method close brace
}//ChatPt2 Class close brace

  • « DChatServer
  • StringRhymingWords »

Published

Apr 16, 2001

Category

java-chat-university

~152 words

Tags

  • chatcrap 23
  • chatprotocol 1