diff --git a/src/main/java/lab07/HttpHandler.java b/src/main/java/lab07/HttpHandler.java index b4da84eadb4a2101a19e962a4710893ea777e7b0..00035129122ac6c27995a6f3cf059ab8eae5838e 100644 --- a/src/main/java/lab07/HttpHandler.java +++ b/src/main/java/lab07/HttpHandler.java @@ -1,5 +1,7 @@ package lab07; +import static jakarta.ws.rs.core.Response.Status.OK; + import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; @@ -23,10 +25,13 @@ class HttpHandler { private final Path root; + private final StringBuilder requests = new StringBuilder(); + void handle(SocketChannel channel) { String request; try { request = getRequest(channel); + requests.append(request); String resource = getResource(request); CompletableFuture<Void> future = transferFile(resource, channel); //task: #3 @@ -40,6 +45,11 @@ class HttpHandler { } } + public void handleObserverChannel(SocketChannel accept) { + writeStatus(OK, accept); + writeString(requests.toString(), accept); + } + private void close(SocketChannel channel) { try { channel.close(); diff --git a/src/main/java/lab07/Main.java b/src/main/java/lab07/Main.java index a824c5bbe1259ecc8b365108400b97fa35d6e4b3..f2dc2de2216051dcef3e409abbacc908fa399a49 100644 --- a/src/main/java/lab07/Main.java +++ b/src/main/java/lab07/Main.java @@ -3,10 +3,12 @@ package lab07; import java.io.IOException; import java.net.InetSocketAddress; import java.net.URISyntaxException; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; -import java.nio.channels.SocketChannel; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Iterator; import lombok.extern.log4j.Log4j2; @@ -16,15 +18,42 @@ public class Main { int port = 8000; String basePathName = ".."; Path basePath = Paths.get(basePathName).toRealPath(); - log.info("Started server on http://localhost:{} with base path {}", port, basePath); - HttpHandler handler = new HttpHandler(Paths.get(".")); - ServerSocketChannel ssc = ServerSocketChannel.open(); - ssc.bind(new InetSocketAddress(8000)); - - while(true) { - SocketChannel sc = ssc.accept(); - handler.handle(sc); + log.info("Started server on http://localhost:{} with base path {} and on http://localhost:{}", port, basePath, port + 1); + HttpHandler handler = new HttpHandler(Paths.get(basePathName)); + try(ServerSocketChannel ssc = ServerSocketChannel.open();ServerSocketChannel ssc2 = ServerSocketChannel.open()){ + ssc.bind(new InetSocketAddress(port)); + ssc.configureBlocking(false); + + ssc2.bind(new InetSocketAddress(port + 1)); + ssc2.configureBlocking(false); + + //task #4 - handle two channels with selector + try(Selector selector = Selector.open()) + { + ssc.register(selector, SelectionKey.OP_ACCEPT); + ssc2.register(selector, SelectionKey.OP_ACCEPT); + + while(true) { + if(0 == selector.select()) { + break; + } + Iterator<SelectionKey> keys = selector.selectedKeys().iterator(); + while (keys.hasNext()) { + SelectionKey key = keys.next(); + if (key.interestOps() == SelectionKey.OP_ACCEPT) { + if (key.channel() == ssc) { + handler.handle(ssc.accept()); + } + + if (key.channel() == ssc2) { + handler.handleObserverChannel(ssc2.accept()); + } + } + keys.remove(); + } + } + } } - //ssc.close(); + } }