master
io42630 1 year ago
parent 264b576171
commit 174a86b89b

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

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

@ -16,6 +16,8 @@ import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import static com.olexyn.misp.helper.Constants.AVAILABLE;
@RestController
public class Forward {
@ -59,8 +61,8 @@ public class Forward {
}
// ride exists only locally, thus safe
ride = available.entrySet().iterator().next().getValue();
// ride exists only in "available", access through which is sync, thus safe
available.remove(ride.getID());
// ride exists only in AVAILABLE, access through which is sync, thus safe
available.remove(ride.getId());
// needed because POST (Ride) wait()s
available.notify();
}
@ -68,7 +70,7 @@ public class Forward {
synchronized (booked) {
// 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.setRequest(parsedRequest);
// POST (Ride) wait()s
@ -78,7 +80,7 @@ public class Forward {
synchronized (loaded) {
while (!loaded.containsKey(ride.getID())) {
while (!loaded.containsKey(ride.getId())) {
loaded.notify();
if (loaded.size() > 0) { break; }
@ -89,7 +91,7 @@ public class Forward {
// what if ride exists in another map, e.g. "available'
// in that case illegal access is possible
// 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);
@ -111,7 +113,7 @@ public class Forward {
JSONObject obj = new JSONObject(payload);
if (obj.has("available")) {
if (obj.has(AVAILABLE)) {
Thread handlePostAvailableT = new Thread(() -> { handlePostAvailable(request, response); });
handlePostAvailableT.setName("handlePostAvailableT");
handlePostAvailableT.start();
@ -129,7 +131,7 @@ public class Forward {
}
if (obj.has("id") && hasData) {
Thread handlePostRideRequestDataT = new Thread(() -> { handlePostRideRequestData(request, response, payload); });
Thread handlePostRideRequestDataT = new Thread(() -> { handlePostRideRequestData(payload); });
handlePostRideRequestDataT.setName("handlePostRideRequestDataT");
handlePostRideRequestDataT.start();
try {handlePostRideRequestDataT.join(); } catch (InterruptedException ignored) { }
@ -141,16 +143,16 @@ public class Forward {
* Handle POST (Ride)(Request)(Data)
* 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);
synchronized (booked) {
booked.remove(ride.getID());
booked.remove(ride.getId());
}
synchronized (loaded) {
loaded.put(ride.getID(), ride);
loaded.put(ride.getId(), ride);
loaded.notify();
}
}
@ -160,17 +162,17 @@ public class Forward {
* Handle POST (Available).
* 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 {
PrintWriter writer = response.getWriter();
PrintWriter writer = res.getWriter();
writer.write(obj.toString());
writer.flush();
writer.close();
} catch (Exception ignored) {}
} catch (Exception ignored) { /* ignored */ }
}
@ -186,18 +188,18 @@ public class Forward {
final Ride ride = new Ride(payload);
synchronized (available) {
available.put(ride.getID(), ride);
available.put(ride.getId(), ride);
available.notify();
}
// ID is final/threadsafe
while (!(booked.containsKey(ride.getID()))) {
while (!(booked.containsKey(ride.getId()))) {
Thread.sleep(WAIT_FOR_USER_REQUEST);
}
synchronized (booked) {
// ride = booked.get(ride.getID());
ride.setRequest(booked.get(ride.getID()).getRequest());
// ride = booked.get(ride.getId());
ride.setRequest(booked.get(ride.getId()).getRequest());
}
response.setStatus(200);
@ -206,6 +208,6 @@ public class Forward {
writer.flush();
writer.close();
} catch (Exception ignored) {}
} catch (Exception ignored) { /* ignored */ }
}
}

@ -8,7 +8,7 @@
</parent>
<groupId>com.olexyn.misp</groupId>
<artifactId>helper</artifactId>
<version>0.1</version>
<version>0.2</version>
<name>helper</name>
<dependencyManagement>
<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>
<groupId>com.olexyn.misp</groupId>
<artifactId>helper</artifactId>
<version>0.1</version>
<version>0.2</version>
<scope>compile</scope>
</dependency>
<dependency>

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

@ -29,9 +29,9 @@
<dependencies>
<dependency>
<groupId>com.olexyn.misp.helper</groupId>
<groupId>com.olexyn.misp</groupId>
<artifactId>helper</artifactId>
<version>0.1</version>
<version>0.2</version>
<scope>compile</scope>
</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 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() {
CheckSuppyR checkSuppyR = new CheckSuppyR(this);
CheckSuppyR checkSuppyR = new CheckSuppyR();
Thread checkSupplyT = new Thread(checkSuppyR);
checkSupplyT.setName("checkSupplyT");
checkSupplyT.start();
Thread journeyGeneratorT = new Thread(new JourneyGeneratorR(this, checkSuppyR));
Thread journeyGeneratorT = new Thread(new JourneyGeneratorR(checkSuppyR));
journeyGeneratorT.setName("journeyGeneratorT");
journeyGeneratorT.start();
}

@ -1,39 +1,32 @@
package com.olexyn.misp.reverse.runnable;
import com.olexyn.misp.reverse.Reverse;
import com.olexyn.misp.reverse.Tools;
import lombok.Getter;
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.helper.Constants.AVAILABLE;
public class CheckSuppyR implements Runnable {
@Getter
private int available;
public int CHECK_SUPPLY_INTERVAL_MILLI = 100;
private Reverse reverse;
public CheckSuppyR(Reverse reverse) {
this.reverse = reverse;
}
@Override
public void run() {
while (true) {
JSONObject obj = new JSONObject().put("available", 0);
JSONObject obj = new JSONObject().put(AVAILABLE, 0);
try {
String result = Tools.send("POST", FORWARD_URL, obj.toString());
String result = Tools.send(POST, FORWARD_URL, obj.toString());
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) { }
}

@ -1,31 +1,25 @@
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 int OVERHEAD = 8;
public int CHECK_DEPLETION_INTERVAL = 500;
public int START_NEW_JOURNEY_INTERVAL = 100;
private final Reverse reverse;
private final CheckSuppyR checkSuppyR;
public JourneyGeneratorR(Reverse reverse , CheckSuppyR checkSuppyR) {
this.reverse = reverse;
public JourneyGeneratorR(CheckSuppyR checkSuppyR) {
this.checkSuppyR = checkSuppyR;
}
@Override
public void run() {
int LIMIT = 0;
while (true) {
try {
Thread journeyT = new Thread(new JourneyR(reverse));
Thread journeyT = new Thread(new JourneyR());
journeyT.setName("journeyT");
journeyT.start();
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.
} catch (Exception ignored) { }
} catch (Exception ignored) { /* ignored */ }
}
}
}

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