Attempt at server multi-threading

Does this look good to you @NorbiPeti? I have no clue how to thread, but
I followed a tutorial. Does this seem like a safe thread?  Locks and all
that stuff.
This commit is contained in:
alisolarflare 2017-06-10 19:11:48 -07:00
parent ad2958b59c
commit e19291139c

View file

@ -17,28 +17,46 @@ public abstract class Page implements HttpHandler {
@Override @Override
public void handle(HttpExchange exchange) { public void handle(HttpExchange exchange) {
try {
exchange.getResponseHeaders().add("Access-Control-Allow-Origin", "https://tbmcplugins.github.io"); //Creates a new thread to handle the request
if (exchange.getRequestURI().getPath().equals("/" + GetName())) Handler handler = new Handler(exchange);
IOHelper.SendResponse(handlePage(exchange)); handler.start();
else { }
IOHelper.SendResponse(404, "404 Not found", exchange);
} class Handler extends Thread{
} catch (Exception e) {
TBMCCoreAPI.SendException("Internal Server Error in ButtonWebsiteModule!", e); HttpExchange exchange;
public Handler(HttpExchange exchange){
this.exchange = exchange;
}
@Override
public void run(){
try { try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); exchange.getResponseHeaders().add("Access-Control-Allow-Origin", "https://tbmcplugins.github.io");
PrintStream str = new PrintStream(baos); if (exchange.getRequestURI().getPath().equals("/" + GetName()))
str.print("<h1>500 Internal Server Error</h1><pre>"); IOHelper.SendResponse(handlePage(exchange));
e.printStackTrace(str); else {
str.print("</pre>"); IOHelper.SendResponse(404, "404 Not found", exchange);
IOHelper.SendResponse(500, baos.toString("UTF-8"), exchange); }
} catch (Exception e1) { } catch (Exception e) {
e1.printStackTrace(); TBMCCoreAPI.SendException("Internal Server Error in ButtonWebsiteModule!", e);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream str = new PrintStream(baos);
str.print("<h1>500 Internal Server Error</h1><pre>");
e.printStackTrace(str);
str.print("</pre>");
IOHelper.SendResponse(500, baos.toString("UTF-8"), exchange);
} catch (Exception e1) {
e1.printStackTrace();
}
} }
} }
} }
/** /**
* The main logic of the endpoint. Use IOHelper to retrieve the message sent and other things. * The main logic of the endpoint. Use IOHelper to retrieve the message sent and other things.
*/ */