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
public void handle(HttpExchange exchange) {
try {
exchange.getResponseHeaders().add("Access-Control-Allow-Origin", "https://tbmcplugins.github.io");
if (exchange.getRequestURI().getPath().equals("/" + GetName()))
IOHelper.SendResponse(handlePage(exchange));
else {
IOHelper.SendResponse(404, "404 Not found", exchange);
}
} catch (Exception e) {
TBMCCoreAPI.SendException("Internal Server Error in ButtonWebsiteModule!", e);
//Creates a new thread to handle the request
Handler handler = new Handler(exchange);
handler.start();
}
class Handler extends Thread{
HttpExchange exchange;
public Handler(HttpExchange exchange){
this.exchange = exchange;
}
@Override
public void run(){
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();
exchange.getResponseHeaders().add("Access-Control-Allow-Origin", "https://tbmcplugins.github.io");
if (exchange.getRequestURI().getPath().equals("/" + GetName()))
IOHelper.SendResponse(handlePage(exchange));
else {
IOHelper.SendResponse(404, "404 Not found", exchange);
}
} catch (Exception e) {
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.
*/