Skip to content
Snippets Groups Projects
Commit a53b2734 authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Implement selector.

parent 1b40e688
Branches solution
No related merge requests found
Pipeline #54 failed with stages
in 0 seconds
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();
......
......@@ -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();
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment