mirror of
https://github.com/devsnaith/lumiere-server.git
synced 2024-11-23 17:13:14 +03:00
Edit the name
This commit is contained in:
parent
596e2d9a23
commit
a8b4c3eb1f
BIN
bin/org/eu/lumiere/net/RequestListener.class
Normal file
BIN
bin/org/eu/lumiere/net/RequestListener.class
Normal file
Binary file not shown.
BIN
bin/org/eu/lumiere/utils/SimpleResponse.class
Normal file
BIN
bin/org/eu/lumiere/utils/SimpleResponse.class
Normal file
Binary file not shown.
90
src/org/eu/lumiere/net/RequestListener.java
Normal file
90
src/org/eu/lumiere/net/RequestListener.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
23
src/org/eu/lumiere/utils/SimpleResponse.java
Normal file
23
src/org/eu/lumiere/utils/SimpleResponse.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user