sykwer/cli_chat

Server minimum specification

Opened this issue · 0 comments

Skelton

public class ChatServer {
  private static ChatServer app;

  public static ChatServer getApp() {
    if (app == null) {
      app = new ChatServer();
      return app;
    }

    return this.app;
  }

  private ArrayList<ClientServant> clientServants;
  private ServerSocket serverSocket;

  private void ChatServer();

  public void start() {
    this.serverSocket = new ServerSocket(port);

    while (!this.serverSocket.isClosed()) {
      Socket clientSocket = this.serverSocket.accept();

      ClientServant cs = new ClientServant(clientSocket);
      this.clientServants.add(cs);

      cs.start();
    }
  }

  public static void main(String[] args) {
    ChatServer.getApp().start();
  }
}

class ClientServant extends Thread {
  private Socket clientSocket;
  private String username;
  private ChatServer server = ChatServer.getApp();

  public void ClientServant(Socket socket) {
    this.clientSocket = socket;
  }

  public void run() {
    InputStream is = this.clientSocket.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));

    while (!this.clientSocket.isClosed()) {
      String[] msg = reader.readLine().split(" ", 2);
      handleCommand(msg[0], msg[1]);
    }
  }

  private void handleCommand(String cmd, String value) {
    // call sendMsg() or logout() or login()
  }

  private void broadcastMsg(String username, String msg) {
  }

  private void logout();

  private void login(String username);

  // ------------------------------------------------------

  private void sendString(String str) {
    try {
      OutputStream os = this.clientSocket.getOutputStream();
      // TODO: write to output stream
    } catch (Exception err) {
    }
  }
}