Edit the name

This commit is contained in:
DevSnaith 2023-04-14 02:37:04 +03:00
parent 596e2d9a23
commit a8b4c3eb1f
4 changed files with 113 additions and 0 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,90 @@
package org.eu.lumiere.net;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import org.eu.lumiere.loggers.GlobalLogger;
import org.eu.lumiere.loggers.GlobalLogger.LogLevel;
public class RequestListener implements Runnable {
private GlobalLogger l = GlobalLogger.getLogger();
private String threadName = "Lumiere server thread";
private boolean isRunning = false;
private Thread thread;
private int port = 8080;
private ServerSocket server;
private ServerEvents e;
public RequestListener(String name, ServerEvents e, int port) {
this.threadName = name == null ? this.threadName : name;
this.port = port;
this.e = e;
}
public synchronized void start() {
if(isRunning || thread != null) {
l.printf(LogLevel.WARNING, threadName + " is already running", null);
return;
}
try {
server = new ServerSocket(port);
} catch (Exception ex) {
l.printf(LogLevel.ERROR, ex instanceof IllegalArgumentException ?
"Server port must be between 0 and 65535" : ex.getMessage(), null);
}
thread = new Thread(this, threadName);
isRunning = true;
thread.start();
}
public synchronized void stop() {
if(!isRunning || thread == null) {
l.printf(LogLevel.WARNING, threadName + " is already running", null);
return;
}
thread.interrupt();
isRunning = false;
thread = null;
if(server != null) {
try {
server.close();
} catch (IOException e) {
l.printf(LogLevel.ERROR, e.getMessage(), null);
}
}
}
@Override
public void run() {
l.printf(LogLevel.INFO, threadName + " using port " + getPort(), null);
while(isRunning && !thread.isInterrupted()) {
try {
if(server == null) {
stop();
continue;
}
Socket s = server.accept();
if(e != null)
e.onConnection(s);
// l.printf(LogLevel.INFO, "New Connection " + s.getInetAddress().getHostAddress(), null);
} catch (IOException ex) {
l.printf(LogLevel.ERROR, ex.getMessage(), null);
}
}
l.printf(LogLevel.INFO, threadName + " has been stopped", null);
}
public int getPort() {
return this.port;
}
}

View File

@ -0,0 +1,23 @@
package org.eu.lumiere.utils;
import org.eu.lumiere.net.http.HttpRequest;
import org.eu.lumiere.net.http.HttpRequestHandler;
import org.eu.lumiere.net.http.HttpResponse;
public class SimpleResponse implements HttpRequestHandler {
private String body = "BasicResponse is running";
private boolean html_response = false;
public SimpleResponse(String body, boolean rhtml) {
this.body = body == null ? this.body : body;
this.html_response = rhtml;
}
@Override
public void onRequestReceived(HttpRequest request, HttpResponse response) {
response.setContentType(String.format("text/%s", html_response ? "html" : "plan"));
response.push(body);
}
}