From 89d87a2fd564c0d11f08d34cdb91051010bf34f7 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Sat, 4 Nov 2017 23:22:59 +0300 Subject: [PATCH] Fixes --- src/warehouseserver/ConnectionRunnable.java | 39 +++++++++++++++------ src/warehouseserver/MultiServer.java | 32 ++++++++++++----- src/warehouseserver/WarehouseServer.java | 13 +++++-- src/warehouseserver/warehouse.txt | 4 +-- warehouse.txt | 0 5 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 warehouse.txt diff --git a/src/warehouseserver/ConnectionRunnable.java b/src/warehouseserver/ConnectionRunnable.java index 7cb128f..e5ee22a 100644 --- a/src/warehouseserver/ConnectionRunnable.java +++ b/src/warehouseserver/ConnectionRunnable.java @@ -16,6 +16,8 @@ */ package warehouseserver; +import Main.ClientException; +import Main.Server; import Payloads.ServerException; import Payloads.RequestPayload; import Payloads.ResponsePayload; @@ -25,30 +27,29 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.logging.Level; import java.util.logging.Logger; /** * - * @author Studente + * @author Daniil Gentili */ public class ConnectionRunnable implements Runnable { BufferedReader input; BufferedWriter output; Socket client; + Server warehouse; Boolean run = true; String path = "/root/NetBeansProjects/WarehouseServer/src/warehouseserver/warehouse.txt"; - public ConnectionRunnable(Socket client) throws IOException { + public ConnectionRunnable(Socket client, Server warehouse) throws IOException { this.client = client; + this.warehouse = warehouse; input = new BufferedReader(new InputStreamReader(client.getInputStream())); - output = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); + output = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); } - @Override public void run() { String buffer = ""; @@ -68,6 +69,11 @@ public class ConnectionRunnable implements Runnable { Logger.getLogger(ConnectionRunnable.class.getName()).log(Level.SEVERE, null, ex); } } + try { + client.close(); + } catch (IOException ex) { + Logger.getLogger(ConnectionRunnable.class.getName()).log(Level.SEVERE, null, ex); + } } private ResponsePayload handlePayload(RequestPayload request) throws ServerException, IOException { @@ -75,7 +81,7 @@ public class ConnectionRunnable implements Runnable { throw new ServerException(request, 400, "Bad request", "Wrong protocol"); } run = request.shouldKeepAlive(); - + switch (request.getMethod()) { case "GET": if (!"/".equals(request.getURI())) { @@ -84,9 +90,17 @@ public class ConnectionRunnable implements Runnable { return new ResponsePayload(request, 200, "OK", getDatabase()); case "POST": if (!"/".equals(request.getURI())) { + System.out.println(String.format("%s spent %s", client.getInetAddress().getCanonicalHostName(), request.getPayload())); + return new ResponsePayload(request, 200, "OK"); + } else if ("/".equals(request.getURI())) { + try { + return new ResponsePayload(request, 200, "OK", updateDatabase(request.getPayload())); + } catch (ClientException ex) { + throw new ServerException(request, 500, ex.getMessage(), getDatabase()); + } + } else { throw new ServerException(request, 404, "File not found", "Database not found"); } - return new ResponsePayload(request, 200, "OK", getDatabase()); default: throw new ServerException(request, 400, "Bad Request", "Bad HTTP method"); @@ -94,8 +108,13 @@ public class ConnectionRunnable implements Runnable { } - synchronized private String getDatabase() throws IOException { - return String.join("\n", Files.readAllLines(Paths.get(path))); + synchronized private String updateDatabase(String input) throws ClientException, IOException { + warehouse.checkout(input); + return warehouse.getWarehouse().getPayload(); + } + + synchronized private String getDatabase() { + return warehouse.getWarehouse().getPayload(); } } diff --git a/src/warehouseserver/MultiServer.java b/src/warehouseserver/MultiServer.java index 0f92d81..dfcc9c0 100644 --- a/src/warehouseserver/MultiServer.java +++ b/src/warehouseserver/MultiServer.java @@ -16,6 +16,7 @@ */ package warehouseserver; +import Main.Server; import java.io.IOException; import java.net.ServerSocket; import java.util.concurrent.ExecutorService; @@ -25,23 +26,36 @@ import java.util.logging.Logger; /** * - * @author Studente + * @author Daniil Gentili */ public class MultiServer { - private Integer port; - - MultiServer(Integer port) { - this.port = port; + private final Server warehouse; + private final ServerSocket socketServer; + private final ExecutorService pool; + MultiServer(Integer port, Server server) throws IOException { + this.warehouse = server; + this.pool = newCachedThreadPool(); + this.socketServer = new ServerSocket(port); + } - public void start() { + public void run() { try { - ExecutorService pool = newCachedThreadPool(); - ServerSocket server = new ServerSocket(port); while (true) { - pool.submit(new ConnectionRunnable(server.accept())); + pool.submit(new ConnectionRunnable(socketServer.accept(), warehouse)); } } catch (IOException ex) { Logger.getLogger(MultiServer.class.getName()).log(Level.SEVERE, null, ex); } + pool.shutdown(); + try { + socketServer.close(); + } catch (IOException ex) { + Logger.getLogger(MultiServer.class.getName()).log(Level.SEVERE, null, ex); + } + try { + warehouse.close(); + } catch (IOException ex) { + Logger.getLogger(MultiServer.class.getName()).log(Level.SEVERE, null, ex); + } } } diff --git a/src/warehouseserver/WarehouseServer.java b/src/warehouseserver/WarehouseServer.java index 927ddf7..c6beaf5 100644 --- a/src/warehouseserver/WarehouseServer.java +++ b/src/warehouseserver/WarehouseServer.java @@ -16,9 +16,14 @@ */ package warehouseserver; +import Main.Server; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; + /** * - * @author Studente + * @author Daniil Gentili */ public class WarehouseServer { @@ -26,6 +31,10 @@ public class WarehouseServer { * @param args the command line arguments */ public static void main(String[] args) { - new MultiServer(9090).start(); + try { + new MultiServer(9090, new Server("/root/NetBeansProjects/WarehouseServer/src/warehouseserver/warehouse.txt")).run(); + } catch (IOException ex) { + Logger.getLogger(WarehouseServer.class.getName()).log(Level.SEVERE, null, ex); + } } } diff --git a/src/warehouseserver/warehouse.txt b/src/warehouseserver/warehouse.txt index bac9868..e230d0f 100644 --- a/src/warehouseserver/warehouse.txt +++ b/src/warehouseserver/warehouse.txt @@ -1,2 +1,2 @@ -1;Twilight Sparkle plushie;49.99;20 -666;Tempest Shadow plushie;79.99;10 +666;Tempest Shadow plushie;79.990000;10 +0;Twilight Sparkle plushie;49.990000;0 diff --git a/warehouse.txt b/warehouse.txt new file mode 100644 index 0000000..e69de29