slight cleanup, remove thread sleep, low risk of spinning

This commit is contained in:
Daniel
2024-10-23 20:50:38 +03:00
parent 707f726b1c
commit da5e8d8c9a
3 changed files with 8 additions and 16 deletions

View File

@@ -4,20 +4,14 @@ import org.lumijiez.core.http.HttpServer;
public class Main {
public static void main(String[] args) {
HttpServer httpServer = new HttpServer(8080);
HttpServer server = new HttpServer(8080);
httpServer.GET("/hello", (req, res) -> {
res.sendResponse(200, "Hello, World!");
});
server.GET("/hello", (req, res) -> res.sendResponse(200, "Hello, World!"));
httpServer.GET("/goodbye", (req, res) -> {
res.sendResponse(200, "Goodbye, World!");
});
server.GET("/goodbye", (req, res) -> res.sendResponse(200, "Goodbye, World!"));
httpServer.POST("/data", (req, res) -> {
res.sendResponse(200, "Data received");
});
server.POST("/data", (req, res) -> res.sendResponse(200, "Data received"));
httpServer.start();
server.start();
}
}

View File

@@ -4,6 +4,7 @@ import org.lumijiez.logging.Logger;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class HttpResponse {
private final BufferedWriter out;
@@ -20,7 +21,7 @@ public class HttpResponse {
out.write("Content-Type: text/plain");
out.write("\r\n");
out.write("Content-Length: " + message.getBytes("UTF-8").length);
out.write("Content-Length: " + message.getBytes(StandardCharsets.UTF_8).length);
out.write("\r\n");
out.write("Connection: keep-alive");
out.write("\r\n");

View File

@@ -82,7 +82,7 @@ public class HttpServer {
while (keepAlive && requestCount < MAX_REQUESTS_PER_CONNECTION && running) {
try {
if (!in.ready()) {
Thread.sleep(10);
// Thread.sleep(10);
continue;
}
@@ -111,9 +111,6 @@ public class HttpServer {
} catch (SocketTimeoutException e) {
Logger.info("HTTP", "Keep-alive timeout reached");
break;
} catch (InterruptedException e) {
Logger.info("HTTP", "Connection handling interrupted");
break;
} catch (IOException e) {
if (running) {
Logger.error("HTTP", "Error processing request: " + e.getMessage());