diff --git a/bin/org/eu/lumiere/net/RequestListener.class b/bin/org/eu/lumiere/net/RequestListener.class new file mode 100644 index 0000000..6d7301f Binary files /dev/null and b/bin/org/eu/lumiere/net/RequestListener.class differ diff --git a/bin/org/eu/lumiere/utils/SimpleResponse.class b/bin/org/eu/lumiere/utils/SimpleResponse.class new file mode 100644 index 0000000..ce74134 Binary files /dev/null and b/bin/org/eu/lumiere/utils/SimpleResponse.class differ diff --git a/src/org/eu/lumiere/net/RequestListener.java b/src/org/eu/lumiere/net/RequestListener.java new file mode 100644 index 0000000..6e7976c --- /dev/null +++ b/src/org/eu/lumiere/net/RequestListener.java @@ -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; + } + +} diff --git a/src/org/eu/lumiere/utils/SimpleResponse.java b/src/org/eu/lumiere/utils/SimpleResponse.java new file mode 100644 index 0000000..2c71951 --- /dev/null +++ b/src/org/eu/lumiere/utils/SimpleResponse.java @@ -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); + } + +}