+ separate the request from the preparation which can be shared between mock and misp.

pull/1/head
Ivan Olexyn 5 years ago
parent 284ebb3a73
commit 30f32fb4d4

@ -14,8 +14,8 @@ public class ClientServlet extends HttpServlet {
protected static final String MISP_BRIDGE_URL = "http://localhost:9090/mispbridge/core"; protected static final String MISP_BRIDGE_URL = "http://localhost:9090/mispbridge/core";
protected static final String APP_URL = "http://localhost:9090"; protected static final String APP_URL = "http://localhost:9090";
public static final int AVAILABLE_RIDES_OVERHEAD_TRIGGER = 16; public static final int AVAILABLE_RIDES_OVERHEAD_TRIGGER = 32;
public static final int AVAILABLE_RIDES_OVERHEAD = 32; public static final int AVAILABLE_RIDES_OVERHEAD = 64;
public final Map<Long, Ride> available = new HashMap<>(); public final Map<Long, Ride> available = new HashMap<>();
@ -33,77 +33,116 @@ public class ClientServlet extends HttpServlet {
/** /**
* # send POST (Ride) * Generated by Loop.
* Generated by Loop * Prepare payload for the request.
* Process the parsed response.
*/ */
void sendPostRide(Ride ride) throws IOException, ServletException, InterruptedException { final void sendPostRide() throws IOException, ServletException, InterruptedException {
HttpURLConnection connection = ConnectionHelper.make("POST", MISP_BRIDGE_URL); final Ride ride = new Ride();
// send POST (Ride) synchronized (available) {
available.put(ride.getID(), ride); available.put(ride.getID(), ride);
}
final Ride parsedRide = doSendPostRide(ride);
synchronized (available) {
available.remove(ride.getID());
ride.setRequest(parsedRide.getRequest());
}
synchronized (booked) {
booked.put(ride.getID(), ride);
}
sendGetRequest(ride);
}
/**
* Send POST (Ride).
* Parse response.
*/
protected Ride doSendPostRide(Ride ride) throws IOException, ServletException, InterruptedException {
// send POST (Ride)
final HttpURLConnection connection = ConnectionHelper.make("POST", MISP_BRIDGE_URL);
connection.setDoOutput(true); connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(ride.json()); outputStream.writeBytes(ride.json());
outputStream.flush(); outputStream.flush();
outputStream.close(); outputStream.close();
// handle OK (Ride) return ConnectionHelper.parseRide(connection);
// remove Ride from AvailableRides
// add Ride to ReservedRides
if (connection.getResponseCode() == 200) {
Ride parsedRide = ConnectionHelper.parseRide(connection);
if (parsedRide.equals(ride)) {
ride.setState(State.BOOKED);
}
}
} }
/** /**
* # send GET (Request) to App * Prepare payload for the request.
* Process the parsed response.
*/ */
void sendGetRequest(Ride ride) throws IOException, ServletException, InterruptedException { final void sendGetRequest(Ride ride) throws IOException, ServletException, InterruptedException {
// send FOO Ride parsedRide = doSendGetRequest(ride);
// TODO make sure as many as possible tyes of requests can be forwarded.
// handle OK (Data)
// remove Ride from PendingRequests
// add Ride to PendingData
// send GET (Ride)(Data)
String data = "DATA";
ride.setData(data);
ride.setState(State.LOADED);
sendGetRideRequestData(ride);
ride.setData(parsedRide.getData());
synchronized (booked) {
booked.remove(ride.getID());
}
synchronized (loaded) {
loaded.put(ride.getID(), ride);
}
sendGetRideRequestData(ride);
} }
/** /**
* # send GET (Ride)(Request)(Data) * Send GET (Request) to App.
* Parse response.
*/ */
void sendGetRideRequestData(Ride oldRide) throws IOException, ServletException, InterruptedException { protected Ride doSendGetRequest(Ride ride) throws IOException, InterruptedException {
HttpURLConnection connection = ConnectionHelper.make("GET", MISP_BRIDGE_URL); // send GET (Ride)
final HttpURLConnection connection = ConnectionHelper.make("GET", APP_URL);
// send GET (Ride)(Request)(Data)
connection.setDoOutput(true); connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(oldRide.json()); outputStream.writeBytes(ride.getRequest());
outputStream.flush(); outputStream.flush();
outputStream.close(); outputStream.close();
// handle OK (RIDE) return ConnectionHelper.parseRide(connection);
if (connection.getResponseCode() == 200) {
Ride shellIdRide = ConnectionHelper.parseRide(connection);
if (shellIdRide.getID() != null) {
loaded.remove(oldRide.getID());
} }
/**
* Prepare payload for the request.
* Process the parsed response.
*/
final protected void sendGetRideRequestData(Ride ride) throws IOException, InterruptedException {
doSendGetRideRequest(ride);
synchronized (loaded) {
loaded.remove(ride.getID());
} }
} }
/**
* Send GET (Ride)(Request)(Data).
* Parse response.
*/
protected void doSendGetRideRequest(Ride ride) throws IOException, InterruptedException {
HttpURLConnection connection = ConnectionHelper.make("GET", MISP_BRIDGE_URL);
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(ride.json());
outputStream.flush();
outputStream.close();
}
} }
@ -125,7 +164,7 @@ class PostRideRunnable implements Runnable {
while (true) { while (true) {
if (clientServlet.available.size() < ClientServlet.AVAILABLE_RIDES_OVERHEAD_TRIGGER) { if (clientServlet.available.size() < ClientServlet.AVAILABLE_RIDES_OVERHEAD_TRIGGER) {
for (int i = 0; i < ClientServlet.AVAILABLE_RIDES_OVERHEAD; i++) { for (int i = 0; i < ClientServlet.AVAILABLE_RIDES_OVERHEAD; i++) {
try {clientServlet.sendPostRide(new Ride());} catch (IOException | ServletException | InterruptedException e) { e.printStackTrace(); } try {clientServlet.sendPostRide();} catch (IOException | ServletException | InterruptedException e) { e.printStackTrace(); }
} }
} }
} }

@ -20,17 +20,12 @@ public class ClientMock extends ClientServlet {
this.mockSet = mockSet; this.mockSet = mockSet;
} }
/**
* Send POST (Ride).
* Parse response.
*/
@Override @Override
void sendPostRide(Ride ride) throws IOException, ServletException, InterruptedException { protected Ride doSendPostRide(Ride ride) throws IOException, ServletException, InterruptedException {
synchronized (available){
available.put(ride.getID(),ride);
}
// Mock Exchange // Mock Exchange
final ExchangeMock exchange = new ExchangeMock(); final ExchangeMock exchange = new ExchangeMock();
@ -43,27 +38,23 @@ public class ClientMock extends ClientServlet {
exchange.notify(); exchange.notify();
mockSet.bridgeMock.doPost(exchange.request, exchange.response); mockSet.bridgeMock.doPost(exchange.request, exchange.response);
exchange.wait(); exchange.wait();
// handle OK (Ride)(Request)
Ride parsedRide = new Ride(exchange.response.getContentAsString());
ride.setRequest(parsedRide.getRequest());
exchange.notify(); exchange.notify();
} }
available.remove(ride.getID());
booked.put(ride.getID(),ride); // handle OK (Ride)(Request)
sendGetRequest(ride); return new Ride(exchange.response.getContentAsString());
} }
/** /**
* # send GET (Request) to App * Send GET (Request) to App.
* Parse response.
*/ */
@Override @Override
void sendGetRequest(Ride ride) throws IOException, ServletException, InterruptedException { protected Ride doSendGetRequest(Ride ride) throws IOException, InterruptedException {
// Mock Exchange // Mock Exchange
ExchangeMock exchange = new ExchangeMock(); final ExchangeMock exchange = new ExchangeMock();
exchange.request.setMethod("GET"); exchange.request.setMethod("GET");
exchange.request.setContentType("application/json"); exchange.request.setContentType("application/json");
@ -73,28 +64,24 @@ public class ClientMock extends ClientServlet {
// Mock GET (Request) // Mock GET (Request)
exchange.notify(); exchange.notify();
mockSet.appMock.doGet(exchange.request, exchange.response); mockSet.appMock.doGet(exchange.request, exchange.response);
//exchange.wait();
// handle OK (Data) // handle OK (Data)
Ride parsedRide = new Ride(exchange.response.getContentAsString());
ride.setData(parsedRide.getData());
exchange.notify(); exchange.notify();
} }
booked.remove(ride.getID());
loaded.put(ride.getID(),ride); return new Ride(exchange.response.getContentAsString());
sendGetRideRequestData(ride);
} }
/** /**
* # send GET (Ride)(Request)(Data) * Send GET (Ride)(Request)(Data).
* Parse response.
*/ */
@Override @Override
void sendGetRideRequestData(Ride ride) throws IOException, ServletException, InterruptedException { protected void doSendGetRideRequest(Ride ride) throws IOException, InterruptedException {
// Mock Exchange // Mock Exchange
ExchangeMock exchange = new ExchangeMock(); final ExchangeMock exchange = new ExchangeMock();
exchange.request.setMethod("GET"); exchange.request.setMethod("GET");
exchange.request.setContentType("application/json"); exchange.request.setContentType("application/json");
exchange.request.setContent(ride.json().getBytes()); exchange.request.setContent(ride.json().getBytes());
@ -104,10 +91,6 @@ public class ClientMock extends ClientServlet {
exchange.notify(); exchange.notify();
mockSet.bridgeMock.doGet(exchange.request, exchange.response); mockSet.bridgeMock.doGet(exchange.request, exchange.response);
exchange.wait(); exchange.wait();
// handle OK (Ride)
Ride parsedRide = new Ride(exchange.response.getContentAsString());
loaded.remove(parsedRide.getID());
exchange.notify(); exchange.notify();
} }
} }

Loading…
Cancel
Save