parent
70b340bb91
commit
9ebe5575fa
Binary file not shown.
Binary file not shown.
@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="module" module-name="misphelper" />
|
||||||
|
<orderEntry type="module" module-name="mispclient" />
|
||||||
|
<orderEntry type="library" name="org.eclipse.jetty.orbit:javax.servlet:3.0.0.v201112011016" level="project" />
|
||||||
|
</component>
|
||||||
|
</module>
|
@ -0,0 +1,184 @@
|
|||||||
|
package com.olexyn.mispl.adapter;
|
||||||
|
|
||||||
|
import com.olexyn.misp.client.ConnectionHelper;
|
||||||
|
import com.olexyn.misp.helper.Ride;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
public class Adapter {
|
||||||
|
|
||||||
|
protected static final String MISP_BRIDGE_URL = "http://localhost:9090/mispbridge/core";
|
||||||
|
protected static final String APP_URL = "http://localhost:9090/mispclient";
|
||||||
|
|
||||||
|
public static final int AVAILABLE_RIDES_OVERHEAD_TRIGGER = 2;
|
||||||
|
public static final int AVAILABLE_RIDES_OVERHEAD = 4;
|
||||||
|
|
||||||
|
|
||||||
|
public final Map<Long, Ride> available = new HashMap<>();
|
||||||
|
public final Map<Long, Ride> booked = new HashMap<>();
|
||||||
|
public final Map<Long, Ride> loaded = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
public Adapter() {
|
||||||
|
|
||||||
|
// Thread : while AvailableRides < 256 , add Ride to AvailableRides , send POST (Ride) [DONE]
|
||||||
|
Thread postRideThread = new Thread(new PostRideRunnable(this));
|
||||||
|
postRideThread.setName("postRideThread");
|
||||||
|
postRideThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generated by Loop.
|
||||||
|
* Prepare payload for the request.
|
||||||
|
* Process the parsed response.
|
||||||
|
*/
|
||||||
|
final void sendPostRide() throws IOException, InterruptedException, ServletException {
|
||||||
|
|
||||||
|
final Ride ride = new Ride();
|
||||||
|
|
||||||
|
synchronized (available) {
|
||||||
|
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, InterruptedException, ServletException {
|
||||||
|
// send POST (Ride)
|
||||||
|
final HttpURLConnection connection = ConnectionHelper.make("POST", MISP_BRIDGE_URL);
|
||||||
|
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
|
||||||
|
outputStream.writeBytes(ride.json());
|
||||||
|
outputStream.flush();
|
||||||
|
outputStream.close();
|
||||||
|
|
||||||
|
Ride rideXa= ConnectionHelper.parseRide(connection);
|
||||||
|
rideXa.setRequest("ff");
|
||||||
|
return rideXa;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare payload for the request.
|
||||||
|
* Process the parsed response.
|
||||||
|
*/
|
||||||
|
final void sendGetRequest(Ride ride) throws IOException, InterruptedException {
|
||||||
|
|
||||||
|
|
||||||
|
ride.setData(doSendGetRequest(ride.getRequest()));
|
||||||
|
|
||||||
|
synchronized (booked) {
|
||||||
|
booked.remove(ride.getID());
|
||||||
|
}
|
||||||
|
synchronized (loaded) {
|
||||||
|
loaded.put(ride.getID(), ride);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendGetRideRequestData(ride);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send GET (Request) to App.
|
||||||
|
* Parse response.
|
||||||
|
*/
|
||||||
|
protected String doSendGetRequest(String request) throws IOException, InterruptedException {
|
||||||
|
|
||||||
|
// send GET (Ride)
|
||||||
|
final HttpURLConnection connection = ConnectionHelper.make("GET", APP_URL);
|
||||||
|
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
|
||||||
|
outputStream.writeBytes(request);
|
||||||
|
outputStream.flush();
|
||||||
|
outputStream.close();
|
||||||
|
|
||||||
|
return ConnectionHelper.parseString(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* While AvailableRides < 256 ,
|
||||||
|
* add Ride to AvailableRides ,
|
||||||
|
* send POST (Ride).
|
||||||
|
*/
|
||||||
|
class PostRideRunnable implements Runnable {
|
||||||
|
|
||||||
|
Adapter adapter;
|
||||||
|
|
||||||
|
public PostRideRunnable(Adapter adapter) {
|
||||||
|
this.adapter = adapter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
if (adapter.available.size() < Adapter.AVAILABLE_RIDES_OVERHEAD_TRIGGER) {
|
||||||
|
for (int i = 0; i < Adapter.AVAILABLE_RIDES_OVERHEAD; i++) {
|
||||||
|
try {adapter.sendPostRide();} catch (IOException | InterruptedException | ServletException e) { e.printStackTrace(); }
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.olexyn.mispl.adapter;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void Main(String... args){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,37 +0,0 @@
|
|||||||
package com.olexyn.misp.bridge;
|
|
||||||
|
|
||||||
import com.olexyn.misp.helper.Ride;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class Debug {
|
|
||||||
|
|
||||||
|
|
||||||
public static String mapTables(BridgeServlet clientServlet){
|
|
||||||
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.append(mapTable(clientServlet.available, "available"));
|
|
||||||
sb.append(mapTable(clientServlet.booked, "booked"));
|
|
||||||
sb.append(mapTable(clientServlet.loaded, "loaded"));
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static String mapTable(Map<Long, Ride> foo, String type){
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.append("<table style=\"width:100%\">");
|
|
||||||
sb.append("<tr>");
|
|
||||||
sb.append("<th>");
|
|
||||||
sb.append("WebPrint: "+ type);
|
|
||||||
sb.append("</th>");
|
|
||||||
sb.append("</tr>");
|
|
||||||
synchronized (foo){
|
|
||||||
for(Map.Entry<Long,Ride> entry : foo.entrySet()){
|
|
||||||
sb.append("<tr><td>"+entry.getValue()+"</td></tr>");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sb.append("</table>");
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
}
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,21 @@
|
|||||||
|
package com.olexyn.misp.helper;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
public class JsonHelper {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static boolean isJson(String string){
|
||||||
|
|
||||||
|
try{
|
||||||
|
new JSONObject(string);
|
||||||
|
}catch (JSONException | NullPointerException e){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,100 @@
|
|||||||
|
package com.olexyn.misp.mock;
|
||||||
|
|
||||||
|
import com.olexyn.misp.client.ClientServlet;
|
||||||
|
import com.olexyn.misp.helper.Ride;
|
||||||
|
import com.olexyn.misp.mock.exchange.ExchangeMock;
|
||||||
|
import com.olexyn.mispl.adapter.Adapter;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps a ClientServlet so it can be debugged easily, i.e. without running Tomcat.
|
||||||
|
*/
|
||||||
|
public class AdapterMock extends Adapter {
|
||||||
|
|
||||||
|
private MockSet mockSet;
|
||||||
|
|
||||||
|
|
||||||
|
public AdapterMock(MockSet mockSet) {
|
||||||
|
super();
|
||||||
|
mockSet.adapterMock = this;
|
||||||
|
this.mockSet = mockSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send POST (Ride).
|
||||||
|
* Parse response.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected Ride doSendPostRide(Ride ride) throws IOException, InterruptedException, ServletException {
|
||||||
|
|
||||||
|
// Mock Exchange
|
||||||
|
final ExchangeMock exchange = new ExchangeMock();
|
||||||
|
exchange.request.setMethod("POST");
|
||||||
|
exchange.request.setContentType("application/json");
|
||||||
|
exchange.request.setContent(ride.json().getBytes());
|
||||||
|
|
||||||
|
synchronized (exchange) {
|
||||||
|
// Mock POST (Ride)
|
||||||
|
exchange.notify();
|
||||||
|
mockSet.bridgeMock.doPost(exchange.request, exchange.response);
|
||||||
|
exchange.wait();
|
||||||
|
exchange.notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle OK (Ride)(Request)
|
||||||
|
return new Ride(exchange.response.getContentAsString());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send GET (Request) to App.
|
||||||
|
* Parse response.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected String doSendGetRequest(String request) throws IOException {
|
||||||
|
|
||||||
|
// Mock Exchange
|
||||||
|
final ExchangeMock exchange = new ExchangeMock();
|
||||||
|
|
||||||
|
exchange.request.setMethod("GET");
|
||||||
|
exchange.request.setContent(request.getBytes());
|
||||||
|
|
||||||
|
synchronized (exchange) {
|
||||||
|
// Mock GET (Request)
|
||||||
|
exchange.notify();
|
||||||
|
mockSet.appMock.doGet(exchange.request, exchange.response);
|
||||||
|
|
||||||
|
// handle OK (Data)
|
||||||
|
exchange.notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
return exchange.response.getContentAsString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send GET (Ride)(Request)(Data).
|
||||||
|
* Parse response.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void doSendGetRideRequest(Ride ride) throws IOException, InterruptedException {
|
||||||
|
|
||||||
|
// Mock Exchange
|
||||||
|
final ExchangeMock exchange = new ExchangeMock();
|
||||||
|
exchange.request.setMethod("GET");
|
||||||
|
exchange.request.setContentType("application/json");
|
||||||
|
exchange.request.setContent(ride.json().getBytes());
|
||||||
|
|
||||||
|
synchronized (exchange) {
|
||||||
|
// Mock GET (Ride)(Request)(Data)
|
||||||
|
exchange.notify();
|
||||||
|
mockSet.bridgeMock.doGet(exchange.request, exchange.response);
|
||||||
|
exchange.wait();
|
||||||
|
exchange.notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.olexyn.misp.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pass the MockSet.
|
||||||
|
* Provide a Runnable.
|
||||||
|
*/
|
||||||
|
public class AdapterRunnable implements Runnable {
|
||||||
|
|
||||||
|
private MockSet mockSet;
|
||||||
|
|
||||||
|
public AdapterRunnable(MockSet mockSet){
|
||||||
|
super();
|
||||||
|
this.mockSet = mockSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
new AdapterMock(mockSet);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue