mirror of
https://github.com/devsnaith/lumiere-server.git
synced 2024-11-23 17:13:14 +03:00
Change the file name
This commit is contained in:
parent
231830b6b4
commit
6b797f75e4
Binary file not shown.
@ -1,90 +0,0 @@
|
||||
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 WebServer 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 WebServer(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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user