master
io42630 1 year ago
parent 264b576171
commit 174a86b89b

@ -20,6 +20,7 @@ services:
environment: environment:
- forward.url=${FORWARD_URL} - forward.url=${FORWARD_URL}
- app.url=${APP_URL} - app.url=${APP_URL}
- check.supply.interval.ms=${CHECK_SUPPLY_INTERVAL_MS}
mirror: mirror:

@ -24,9 +24,9 @@
</dependencyManagement> </dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.olexyn.misp.helper</groupId> <groupId>com.olexyn.misp</groupId>
<artifactId>helper</artifactId> <artifactId>helper</artifactId>
<version>0.1</version> <version>0.2</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>

@ -16,6 +16,8 @@ import java.io.PrintWriter;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static com.olexyn.misp.helper.Constants.AVAILABLE;
@RestController @RestController
public class Forward { public class Forward {
@ -59,8 +61,8 @@ public class Forward {
} }
// ride exists only locally, thus safe // ride exists only locally, thus safe
ride = available.entrySet().iterator().next().getValue(); ride = available.entrySet().iterator().next().getValue();
// ride exists only in "available", access through which is sync, thus safe // ride exists only in AVAILABLE, access through which is sync, thus safe
available.remove(ride.getID()); available.remove(ride.getId());
// needed because POST (Ride) wait()s // needed because POST (Ride) wait()s
available.notify(); available.notify();
} }
@ -68,7 +70,7 @@ public class Forward {
synchronized (booked) { synchronized (booked) {
// ride exists only locally, thus safe // ride exists only locally, thus safe
booked.put(ride.getID(), ride); booked.put(ride.getId(), ride);
// ride exists only in "booked", access through which is sync, thus safe // ride exists only in "booked", access through which is sync, thus safe
ride.setRequest(parsedRequest); ride.setRequest(parsedRequest);
// POST (Ride) wait()s // POST (Ride) wait()s
@ -78,7 +80,7 @@ public class Forward {
synchronized (loaded) { synchronized (loaded) {
while (!loaded.containsKey(ride.getID())) { while (!loaded.containsKey(ride.getId())) {
loaded.notify(); loaded.notify();
if (loaded.size() > 0) { break; } if (loaded.size() > 0) { break; }
@ -89,7 +91,7 @@ public class Forward {
// what if ride exists in another map, e.g. "available' // what if ride exists in another map, e.g. "available'
// in that case illegal access is possible // in that case illegal access is possible
// be carefull to removing ride from all other references, when adding it to "loaded". // be carefull to removing ride from all other references, when adding it to "loaded".
ride.setData(loaded.remove(ride.getID()).getData()); ride.setData(loaded.remove(ride.getId()).getData());
} }
response.setStatus(200); response.setStatus(200);
@ -111,7 +113,7 @@ public class Forward {
JSONObject obj = new JSONObject(payload); JSONObject obj = new JSONObject(payload);
if (obj.has("available")) { if (obj.has(AVAILABLE)) {
Thread handlePostAvailableT = new Thread(() -> { handlePostAvailable(request, response); }); Thread handlePostAvailableT = new Thread(() -> { handlePostAvailable(request, response); });
handlePostAvailableT.setName("handlePostAvailableT"); handlePostAvailableT.setName("handlePostAvailableT");
handlePostAvailableT.start(); handlePostAvailableT.start();
@ -129,7 +131,7 @@ public class Forward {
} }
if (obj.has("id") && hasData) { if (obj.has("id") && hasData) {
Thread handlePostRideRequestDataT = new Thread(() -> { handlePostRideRequestData(request, response, payload); }); Thread handlePostRideRequestDataT = new Thread(() -> { handlePostRideRequestData(payload); });
handlePostRideRequestDataT.setName("handlePostRideRequestDataT"); handlePostRideRequestDataT.setName("handlePostRideRequestDataT");
handlePostRideRequestDataT.start(); handlePostRideRequestDataT.start();
try {handlePostRideRequestDataT.join(); } catch (InterruptedException ignored) { } try {handlePostRideRequestDataT.join(); } catch (InterruptedException ignored) { }
@ -141,16 +143,16 @@ public class Forward {
* Handle POST (Ride)(Request)(Data) * Handle POST (Ride)(Request)(Data)
* Move the Ride from `booked` to `loaded`, so it can be picked up by OK (Data) of GET (Request). * Move the Ride from `booked` to `loaded`, so it can be picked up by OK (Data) of GET (Request).
*/ */
private void handlePostRideRequestData(HttpServletRequest request, HttpServletResponse response, String payload) { private void handlePostRideRequestData(String payload) {
final Ride ride = new Ride(payload); final Ride ride = new Ride(payload);
synchronized (booked) { synchronized (booked) {
booked.remove(ride.getID()); booked.remove(ride.getId());
} }
synchronized (loaded) { synchronized (loaded) {
loaded.put(ride.getID(), ride); loaded.put(ride.getId(), ride);
loaded.notify(); loaded.notify();
} }
} }
@ -160,17 +162,17 @@ public class Forward {
* Handle POST (Available). * Handle POST (Available).
* Send current # of available Rides to `reverse`. * Send current # of available Rides to `reverse`.
*/ */
private void handlePostAvailable(HttpServletRequest request, HttpServletResponse response) { private void handlePostAvailable(HttpServletRequest req, HttpServletResponse res) {
JSONObject obj = new JSONObject().put("available", available.size()); JSONObject obj = new JSONObject().put(AVAILABLE, available.size());
response.setStatus(200); res.setStatus(200);
try { try {
PrintWriter writer = response.getWriter(); PrintWriter writer = res.getWriter();
writer.write(obj.toString()); writer.write(obj.toString());
writer.flush(); writer.flush();
writer.close(); writer.close();
} catch (Exception ignored) {} } catch (Exception ignored) { /* ignored */ }
} }
@ -186,18 +188,18 @@ public class Forward {
final Ride ride = new Ride(payload); final Ride ride = new Ride(payload);
synchronized (available) { synchronized (available) {
available.put(ride.getID(), ride); available.put(ride.getId(), ride);
available.notify(); available.notify();
} }
// ID is final/threadsafe // ID is final/threadsafe
while (!(booked.containsKey(ride.getID()))) { while (!(booked.containsKey(ride.getId()))) {
Thread.sleep(WAIT_FOR_USER_REQUEST); Thread.sleep(WAIT_FOR_USER_REQUEST);
} }
synchronized (booked) { synchronized (booked) {
// ride = booked.get(ride.getID()); // ride = booked.get(ride.getId());
ride.setRequest(booked.get(ride.getID()).getRequest()); ride.setRequest(booked.get(ride.getId()).getRequest());
} }
response.setStatus(200); response.setStatus(200);
@ -206,6 +208,6 @@ public class Forward {
writer.flush(); writer.flush();
writer.close(); writer.close();
} catch (Exception ignored) {} } catch (Exception ignored) { /* ignored */ }
} }
} }

@ -8,7 +8,7 @@
</parent> </parent>
<groupId>com.olexyn.misp</groupId> <groupId>com.olexyn.misp</groupId>
<artifactId>helper</artifactId> <artifactId>helper</artifactId>
<version>0.1</version> <version>0.2</version>
<name>helper</name> <name>helper</name>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>

@ -0,0 +1,9 @@
package com.olexyn.misp.helper;
public interface Constants {
String AVAILABLE = "available";
String POST = "POST";
String GET = "GET";
String EMPTY = "";
}

@ -26,7 +26,7 @@
<dependency> <dependency>
<groupId>com.olexyn.misp</groupId> <groupId>com.olexyn.misp</groupId>
<artifactId>helper</artifactId> <artifactId>helper</artifactId>
<version>0.1</version> <version>0.2</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>

@ -0,0 +1,7 @@
#!/bin/bash
docker push io42630/forward:0.1

@ -29,9 +29,9 @@
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.olexyn.misp.helper</groupId> <groupId>com.olexyn.misp</groupId>
<artifactId>helper</artifactId> <artifactId>helper</artifactId>
<version>0.1</version> <version>0.2</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>

@ -8,17 +8,21 @@ public class Reverse implements Runnable {
public static final String FORWARD_URL = System.getenv("forward.url"); public static final String FORWARD_URL = System.getenv("forward.url");
public static final String APP_URL = System.getenv("app.url"); public static final String APP_URL = System.getenv("app.url");
public static final int CHECK_SUPPLY_INTERVAL_MS = Integer.parseInt(System.getenv("check.supply.interval.ms"));
public static final int OVERHEAD = 8;
public static final int CHECK_DEPLETION_INTERVAL = 500;
public static final int START_NEW_JOURNEY_INTERVAL = 100;
public void start() { public void start() {
CheckSuppyR checkSuppyR = new CheckSuppyR(this); CheckSuppyR checkSuppyR = new CheckSuppyR();
Thread checkSupplyT = new Thread(checkSuppyR); Thread checkSupplyT = new Thread(checkSuppyR);
checkSupplyT.setName("checkSupplyT"); checkSupplyT.setName("checkSupplyT");
checkSupplyT.start(); checkSupplyT.start();
Thread journeyGeneratorT = new Thread(new JourneyGeneratorR(this, checkSuppyR)); Thread journeyGeneratorT = new Thread(new JourneyGeneratorR(checkSuppyR));
journeyGeneratorT.setName("journeyGeneratorT"); journeyGeneratorT.setName("journeyGeneratorT");
journeyGeneratorT.start(); journeyGeneratorT.start();
} }

@ -1,39 +1,32 @@
package com.olexyn.misp.reverse.runnable; package com.olexyn.misp.reverse.runnable;
import com.olexyn.misp.reverse.Reverse;
import com.olexyn.misp.reverse.Tools; import com.olexyn.misp.reverse.Tools;
import lombok.Getter; import lombok.Getter;
import org.json.JSONObject; import org.json.JSONObject;
import static com.olexyn.misp.helper.Constants.POST;
import static com.olexyn.misp.reverse.Reverse.CHECK_SUPPLY_INTERVAL_MS;
import static com.olexyn.misp.reverse.Reverse.FORWARD_URL; import static com.olexyn.misp.reverse.Reverse.FORWARD_URL;
import static com.olexyn.misp.helper.Constants.AVAILABLE;
public class CheckSuppyR implements Runnable { public class CheckSuppyR implements Runnable {
@Getter @Getter
private int available; private int available;
public int CHECK_SUPPLY_INTERVAL_MILLI = 100;
private Reverse reverse;
public CheckSuppyR(Reverse reverse) {
this.reverse = reverse;
}
@Override @Override
public void run() { public void run() {
while (true) { while (true) {
JSONObject obj = new JSONObject().put("available", 0); JSONObject obj = new JSONObject().put(AVAILABLE, 0);
try { try {
String result = Tools.send("POST", FORWARD_URL, obj.toString()); String result = Tools.send(POST, FORWARD_URL, obj.toString());
JSONObject resultObj = new JSONObject(result); JSONObject resultObj = new JSONObject(result);
available = resultObj.getInt("available"); available = resultObj.getInt(AVAILABLE);
Thread.sleep(CHECK_SUPPLY_INTERVAL_MILLI); Thread.sleep(CHECK_SUPPLY_INTERVAL_MS);
} catch (Exception ignored) { } } catch (Exception ignored) { }
} }

@ -1,31 +1,25 @@
package com.olexyn.misp.reverse.runnable; package com.olexyn.misp.reverse.runnable;
import com.olexyn.misp.reverse.Reverse; import static com.olexyn.misp.reverse.Reverse.CHECK_DEPLETION_INTERVAL;
import static com.olexyn.misp.reverse.Reverse.OVERHEAD;
import static com.olexyn.misp.reverse.Reverse.START_NEW_JOURNEY_INTERVAL;
public class JourneyGeneratorR implements Runnable { public class JourneyGeneratorR implements Runnable {
public int OVERHEAD = 8;
public int CHECK_DEPLETION_INTERVAL = 500;
public int START_NEW_JOURNEY_INTERVAL = 100;
private final Reverse reverse;
private final CheckSuppyR checkSuppyR; private final CheckSuppyR checkSuppyR;
public JourneyGeneratorR(Reverse reverse , CheckSuppyR checkSuppyR) { public JourneyGeneratorR(CheckSuppyR checkSuppyR) {
this.reverse = reverse;
this.checkSuppyR = checkSuppyR; this.checkSuppyR = checkSuppyR;
} }
@Override @Override
public void run() { public void run() {
int LIMIT = 0; int LIMIT = 0;
while (true) { while (true) {
try { try {
Thread journeyT = new Thread(new JourneyR(reverse)); Thread journeyT = new Thread(new JourneyR());
journeyT.setName("journeyT"); journeyT.setName("journeyT");
journeyT.start(); journeyT.start();
LIMIT++; LIMIT++;
@ -39,7 +33,7 @@ public class JourneyGeneratorR implements Runnable {
} }
// TODO rework this, so it sends - but not too much, and so it wais - but not too long. // TODO rework this, so it sends - but not too much, and so it wais - but not too long.
} catch (Exception ignored) { } } catch (Exception ignored) { /* ignored */ }
} }
} }
} }

@ -1,55 +1,49 @@
package com.olexyn.misp.reverse.runnable; package com.olexyn.misp.reverse.runnable;
import com.olexyn.misp.helper.Ride; import com.olexyn.misp.helper.Ride;
import com.olexyn.misp.reverse.Reverse;
import com.olexyn.misp.reverse.Tools; import com.olexyn.misp.reverse.Tools;
import java.io.IOException; import java.io.IOException;
public class JourneyR implements Runnable { import static com.olexyn.misp.helper.Constants.EMPTY;
import static com.olexyn.misp.helper.Constants.GET;
private Reverse reverse; import static com.olexyn.misp.helper.Constants.POST;
import static com.olexyn.misp.reverse.Reverse.APP_URL;
public JourneyR(Reverse reverse) { import static com.olexyn.misp.reverse.Reverse.FORWARD_URL;
this.reverse = reverse;
}
public class JourneyR implements Runnable {
@Override @Override
public void run() { public void run() {
try { try {
var ride = sendPostRide(); var ride = sendPostRide();
ride = sendGetRequest(ride); sendGetRequest(ride);
sendPostRideRequestData(ride); sendPostRideRequestData(ride);
} catch (Exception ignored) { /* ignored */ } } catch (Exception ignored) { /* ignored */ }
} }
Ride sendPostRide() throws IOException { private Ride sendPostRide() throws IOException {
final Ride ride = new Ride(); Ride ride = new Ride();
final String result = Tools.send("POST", reverse.FORWARD_URL, ride.json()); String result = Tools.send(POST, FORWARD_URL, ride.json());
String _req = new Ride(result).getRequest(); String reqStr = new Ride(result).getRequest();
String request = (_req == null) ? "" : _req; reqStr = (reqStr == null) ? EMPTY : reqStr;
ride.setRequest(request); ride.setRequest(reqStr);
return ride; return ride;
} }
private Ride sendGetRequest(Ride ride) throws IOException {
Ride sendGetRequest(Ride ride) throws IOException { String result = Tools.send(GET, APP_URL, ride.getRequest());
final String result = Tools.send("GET", reverse.APP_URL, ride.getRequest());
ride.setData(result); ride.setData(result);
return ride; return ride;
} }
private void sendPostRideRequestData(Ride ride) throws IOException {
void sendPostRideRequestData(Ride ride) throws IOException { Tools.send(POST, FORWARD_URL, ride.json());
Tools.send("POST", reverse.FORWARD_URL, ride.json());
} }
} }
Loading…
Cancel
Save