mirror of
https://github.com/danog/WarehouseServer.git
synced 2024-11-26 20:04:50 +01:00
Fixes
This commit is contained in:
parent
29ce5b819a
commit
89d87a2fd5
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
0
warehouse.txt
Normal file
0
warehouse.txt
Normal file
Loading…
Reference in New Issue
Block a user