Commit f4f259b3 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] Moving the code in the simple chat client to more clearly separate responsbilities

parent 76a982dd
Loading
Loading
Loading
Loading
+71 −36
Changes for src/main/java/ch15/SimpleChatClient.java: 71 added lines, 36 removed lines.
Original line number Diff line number Diff line
@@ -11,16 +11,64 @@ import java.io.PrintWriter;
import java.net.Socket;

public class SimpleChatClient {
  private JTextArea incoming;
  private JTextField outgoing;
  private BufferedReader reader;
  private PrintWriter writer;

  public static void main(String[] args) {
    new SimpleChatClient().go();
    Socket socket = setUpNetworking();

    ClientUI clientUI = new ClientUI();
    clientUI.go(socket);

    IncomingReader incomingReader = new IncomingReader(clientUI.getIncoming(), socket);
    Thread readerThread = new Thread(incomingReader);
    readerThread.start();
  }

  public void go() {

  private static Socket setUpNetworking() {
    try {
      Socket sock = new Socket("127.0.0.1", 5000);
      System.out.println("networking established");
      return sock;
    } catch (IOException ex) {
      throw new RuntimeException(ex);
    }
  } // close setUpNetworking

  public static class IncomingReader implements Runnable {
    private final BufferedReader reader;
    private final JTextArea incomingMessageArea;

    public IncomingReader(JTextArea incomingMessageArea, Socket socket) {
      this.incomingMessageArea = incomingMessageArea;
      try {
        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }

    public void run() {
      processMessages();
    }

    private void processMessages() {
      String message;
      try {
        while ((message = reader.readLine()) != null) {
          System.out.println("read " + message);
          incomingMessageArea.append(message + "\n");
        } // close while
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
  } // close inner class
} // close outer class

class ClientUI {
  private JTextArea incoming;

  public void go(Socket socket) {
    JFrame frame = new JFrame("Ludicrously Simple Chat Client");
    JPanel mainPanel = new JPanel();
    incoming = new JTextArea(15, 50);
@@ -30,33 +78,34 @@ public class SimpleChatClient {
    JScrollPane qScroller = new JScrollPane(incoming);
    qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    outgoing = new JTextField(20);
    JTextField outgoing = new JTextField(20);
    JButton sendButton = new JButton("Send");
    sendButton.addActionListener(new SendButtonListener());
    sendButton.addActionListener(new SendButtonListener(outgoing, socket));
    mainPanel.add(qScroller);
    mainPanel.add(outgoing);
    mainPanel.add(sendButton);
    setUpNetworking();
    Thread readerThread = new Thread(new IncomingReader());
    readerThread.start();
    frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
    frame.setSize(800, 500);
    frame.setVisible(true);
  } // close go

  private void setUpNetworking() {
  public JTextArea getIncoming() {
    return incoming;
  }

  public static class SendButtonListener implements ActionListener {
    private final JTextField outgoing;
    private final PrintWriter writer;

    public SendButtonListener(JTextField outgoingMessage, Socket socket) {
      try {
      Socket sock = new Socket("127.0.0.1", 5000);
      InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
      reader = new BufferedReader(streamReader);
      writer = new PrintWriter(sock.getOutputStream());
      System.out.println("networking established");
    } catch (IOException ex) {
      ex.printStackTrace();
        writer = new PrintWriter(socket.getOutputStream());
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
      outgoing = outgoingMessage;
    }
  } // close setUpNetworking

  public class SendButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent ev) {
      try {
        writer.println(outgoing.getText());
@@ -67,19 +116,5 @@ public class SimpleChatClient {
      outgoing.setText("");
      outgoing.requestFocus();
    }
  } // close inner class

  public class IncomingReader implements Runnable {
    public void run() {
      String message;
      try {
        while ((message = reader.readLine()) != null) {
          System.out.println("read " + message);
          incoming.append(message + "\n");
        } // close while
      } catch (Exception ex) {
        ex.printStackTrace();
  }
    } // close run
  } // close inner class
} // close outer class
 No newline at end of file
}
 No newline at end of file