Commit 32b88860 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] DailyAdviceClient as it appears in the book.

parent 9f04c945
Loading
Loading
Loading
Loading
+10 −7
Changes for src/main/java/ch15/DailyAdviceClient.java: 10 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -2,24 +2,27 @@ package ch15;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.net.InetSocketAddress;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

public class DailyAdviceClient {
  public static void main(String[] args) {
    DailyAdviceClient client = new DailyAdviceClient();
    client.go();
  }

  public void go() {
    try (SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 4242))) {
      BufferedReader reader = new BufferedReader(Channels.newReader(socketChannel, StandardCharsets.UTF_8));
    InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.1", 4242);
    try (SocketChannel socketChannel = SocketChannel.open(serverAddress)) {
      Reader channelReader = Channels.newReader(socketChannel, StandardCharsets.UTF_8);
      BufferedReader reader = new BufferedReader(channelReader);
      String advice = reader.readLine();
      System.out.println("Today you should: " + advice);
      reader.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    new DailyAdviceClient().go();
  }
}
 No newline at end of file
+98 −0
Changes for src/test/java/ch15/ConceptsTest.java: 98 added lines, 0 removed lines.
Original line number Diff line number Diff line
package ch15;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.channels.Channels;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class ConceptsTest {

  @Test
    // This test doesn't pass or fail. I was using it to see if I could generate exceptions
  void testConnect() throws IOException {
    SocketAddress remote1 = new InetSocketAddress("127.0.0.1", 4242);

//    SocketChannel.open(remote1);
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.bind(remote1);

    SocketAddress remote2 = new InetSocketAddress("127.0.0.1", 4242);
    SocketChannel.open(remote2);

    Socket socket = new Socket("localhost", 4242);
  }

  @Test
  void testBlockingVsNonBlocking() throws IOException {
    int serverPort = 3600;
    ServerSocketChannel server = ServerSocketChannel.open();
    server.bind(new InetSocketAddress("localhost", serverPort));
    System.out.println("server.isBlocking() = " + server.isBlocking());

    SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", serverPort));
    System.out.println("socketChannel.isBlocking() = " + socketChannel.isBlocking());

    socketChannel.configureBlocking(true);
    System.out.println("socketChannel.isBlocking() = " + socketChannel.isBlocking());

    socketChannel.configureBlocking(false);
    System.out.println("socketChannel.isBlocking() = " + socketChannel.isBlocking());
  }

  @Test
  void testInputStreamsWithNonBlockingChannel() throws IOException, InterruptedException {
    ExecutorService executorService = Executors.newFixedThreadPool(2);
    CountDownLatch latch = new CountDownLatch(1);
    int serverPort = 3600;
    ServerSocketChannel server = ServerSocketChannel.open();
    server.bind(new InetSocketAddress("localhost", serverPort));
    executorService.submit(() -> {
      try {
        while (true) {

          SocketChannel client = server.accept();
          Writer writer = Channels.newWriter(client, StandardCharsets.UTF_8);
          writer.write("hey there");
          writer.flush();
          System.out.println("done");
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    });


    SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", serverPort));
    socketChannel.configureBlocking(false);
//    InputStream inputStream = Channels.newInputStream(socketChannel);
//    // throws an exception
//    inputStream.read();

    executorService.submit(() -> {
      Reader reader = Channels.newReader(socketChannel, StandardCharsets.UTF_8);
      try {
        while (socketChannel.isOpen()) {
          System.out.println(reader.read());
          latch.countDown();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    });

    latch.await();


  }
}
 No newline at end of file