|
|
@ -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(); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|