parent
55abcbbba1
commit
d519725651
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 42 KiB |
@ -0,0 +1,39 @@
|
||||
package com.olexyn.misp.reverse;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class CheckSuppyR implements Runnable {
|
||||
|
||||
private int available;
|
||||
public int CHECK_SUPPLY_INTERVAL = 100;
|
||||
|
||||
private Reverse reverse;
|
||||
|
||||
public CheckSuppyR(Reverse reverse) {
|
||||
this.reverse = reverse;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
JSONObject obj = new JSONObject().put("available", 0);
|
||||
|
||||
try {
|
||||
String result = Tools.send("POST", reverse.FORWARD_URL, obj.toString());
|
||||
|
||||
JSONObject resultObj = new JSONObject(result);
|
||||
|
||||
available = resultObj.getInt("available");
|
||||
|
||||
Thread.sleep(CHECK_SUPPLY_INTERVAL);
|
||||
|
||||
} catch (Exception ignored) { }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public int getAvailable() { return available; }
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.olexyn.misp.reverse;
|
||||
|
||||
|
||||
public class JourneyGenerator implements Runnable {
|
||||
|
||||
public int OVERHEAD = 8;
|
||||
public int CHECK_DEPLETION_INTERVAL = 500;
|
||||
public int START_NEW_JOURNEY_INTERVAL = 100;
|
||||
|
||||
private Reverse reverse;
|
||||
private CheckSuppyR checkSuppyR;
|
||||
|
||||
public JourneyGenerator(Reverse reverse , CheckSuppyR checkSuppyR) {
|
||||
this.reverse = reverse;
|
||||
this.checkSuppyR = checkSuppyR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
Thread journeyT = new Thread(new JourneyR(reverse));
|
||||
journeyT.setName("journeyT");
|
||||
journeyT.start();
|
||||
|
||||
while (checkSuppyR.getAvailable() > OVERHEAD) { Thread.sleep(CHECK_DEPLETION_INTERVAL); }
|
||||
|
||||
Thread.sleep(START_NEW_JOURNEY_INTERVAL);
|
||||
|
||||
} catch (Exception ignored) { }
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.olexyn.misp.reverse;
|
||||
|
||||
import com.olexyn.misp.helper.Ride;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class JourneyR implements Runnable {
|
||||
|
||||
private Reverse reverse;
|
||||
|
||||
public JourneyR(Reverse reverse) {
|
||||
this.reverse = reverse;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Ride _ride = sendPostRide();
|
||||
_ride = sendGetRequest(_ride);
|
||||
sendPostRideRequestData(_ride);
|
||||
} catch (Exception ignored) { }
|
||||
}
|
||||
|
||||
|
||||
Ride sendPostRide() throws IOException {
|
||||
|
||||
final Ride ride = new Ride();
|
||||
|
||||
final String result = Tools.send("POST", reverse.FORWARD_URL, ride.json());
|
||||
|
||||
String _req = new Ride(result).getRequest();
|
||||
String request = (_req == null) ? "" : _req;
|
||||
ride.setRequest(request);
|
||||
|
||||
return ride;
|
||||
}
|
||||
|
||||
|
||||
Ride sendGetRequest(Ride ride) throws IOException {
|
||||
|
||||
final String result = Tools.send("GET", reverse.APP_URL, ride.getRequest());
|
||||
ride.setData(result);
|
||||
|
||||
return ride;
|
||||
}
|
||||
|
||||
|
||||
void sendPostRideRequestData(Ride ride) throws IOException {
|
||||
|
||||
Tools.send("POST", reverse.FORWARD_URL, ride.json());
|
||||
}
|
||||
}
|
@ -1,3 +1,16 @@
|
||||
* Run from `ReverseApp` :
|
||||
* as `main()`
|
||||
* or as `Runnable` from somewhere else.
|
||||
#### Overview
|
||||
* `Reverse`
|
||||
* Core
|
||||
* Starts Threads
|
||||
* Coordinates
|
||||
* Launch from here
|
||||
* as `main()`
|
||||
* or as `Runnable` from somewhere else.
|
||||
* `CheckSupply`
|
||||
* Runnable
|
||||
* Asks *forward* about supply.
|
||||
* `Journey`
|
||||
* Runnable
|
||||
* Journey fo a `Ride` through the App.
|
||||
* `Tools`
|
||||
* Performs the actual request.
|
@ -1,35 +0,0 @@
|
||||
package com.olexyn.misp.reverse;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ReverseApp implements Runnable {
|
||||
|
||||
|
||||
public static void main(String... args) throws IOException {
|
||||
new ReverseApp().doRun();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
doRun();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void doRun() throws IOException {
|
||||
Reverse reverse = new Reverse();
|
||||
|
||||
reverse.FORWARD_URL = "http://localhost:8090/forward";
|
||||
reverse.APP_URL = "http://localhost:8090/app";
|
||||
|
||||
|
||||
|
||||
reverse.start();
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.olexyn.misp.reverse;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class Tools {
|
||||
|
||||
|
||||
static String send(String method, String urlString, String body) throws IOException {
|
||||
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod(method);
|
||||
|
||||
|
||||
boolean getToForward = method.equals("GET") && urlString.contains("forward");
|
||||
|
||||
if (method.equals("POST") || getToForward) {
|
||||
connection.setDoOutput(true);
|
||||
|
||||
|
||||
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
|
||||
|
||||
if (body != null) {
|
||||
outputStream.writeBytes(body);
|
||||
}
|
||||
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
|
||||
}
|
||||
|
||||
|
||||
int i = connection.getResponseCode();
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String out;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while ((out = in.readLine()) != null) { sb.append(out); }
|
||||
in.close();
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue