From e19291139cd3f4e93d3a1c80af2a2c5f69aea091 Mon Sep 17 00:00:00 2001 From: alisolarflare Date: Sat, 10 Jun 2017 19:11:48 -0700 Subject: [PATCH] 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. --- src/buttondevteam/website/page/Page.java | 54 ++++++++++++++++-------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/src/buttondevteam/website/page/Page.java b/src/buttondevteam/website/page/Page.java index 66633f8..362408a 100644 --- a/src/buttondevteam/website/page/Page.java +++ b/src/buttondevteam/website/page/Page.java @@ -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("

500 Internal Server Error

");
-				e.printStackTrace(str);
-				str.print("
"); - 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("

500 Internal Server Error

");
+					e.printStackTrace(str);
+					str.print("
"); + 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. */