+ create adapter to replace client

pull/1/head
Ivan Olexyn 5 years ago
parent 70b340bb91
commit 9ebe5575fa

@ -2,9 +2,22 @@
cwd=$(pwd) cwd=$(pwd)
cp "${cwd}/mirror/war/mirror.war" /home/user/app/tomcat/webapps
cp "${cwd}/mispbridge/war/mispbridge.war" /home/user/app/tomcat/webapps
cp "${cwd}/mispclient/war/mispclient.war" /home/user/app/tomcat/webapps
/home/user/app/tomcat/bin/shutdown.sh /home/user/app/tomcat/bin/shutdown.sh
echo "================"
echo "END TOMCAT STOP "
echo "================"
cp -v "${cwd}/mirror/war/mirror.war" /home/user/app/tomcat/webapps
cp -v "${cwd}/mispbridge/war/mispbridge.war" /home/user/app/tomcat/webapps
cp -v "${cwd}/mispclient/war/mispclient.war" /home/user/app/tomcat/webapps
echo "================"
echo "END COPY"
echo "================"
/home/user/app/tomcat/bin/startup.sh /home/user/app/tomcat/bin/startup.sh
echo "================"
echo "END TOMCAT START "
echo "================"

@ -1,4 +1,8 @@
#!/bin/bash #!/bin/bash
echo "================"
echo "START RUN"
echo "================"
cwd=$(pwd) cwd=$(pwd)
@ -9,7 +13,9 @@ jar -cvf misphelper.jar ./com/olexyn/misp/helper/*
echo "================"
echo "END MispHelper JAR"
echo "================"
@ -24,16 +30,21 @@ out="/out/production/${n}${pkg}"
wrapper="/${n}/war/wrapper" wrapper="/${n}/war/wrapper"
# copy misphelper.jar to wrapper/.../lib # copy misphelper.jar to wrapper/.../lib
cp "${cwd}/out/production/misphelper/misphelper.jar" "${cwd}${wrapper}/WEB-INF/lib" cp -v"${cwd}/out/production/misphelper/misphelper.jar" "${cwd}${wrapper}/WEB-INF/lib"
# copy compiled code into the wrapper. # copy compiled code into the wrapper.
cp -r "${cwd}${out}" "${cwd}${wrapper}/WEB-INF/classes/com/olexyn/${a}" cp -vr "${cwd}${out}" "${cwd}${wrapper}/WEB-INF/classes/com/olexyn/${a}"
# compress .war # compress .war
cd "${cwd}${wrapper}" || exit cd "${cwd}${wrapper}" || exit
jar -cvf "../${n}.war" * jar -cvf "../${n}.war" *
echo "================"
echo "END MispBridge WAR"
echo "================"
a='misp' a='misp'
b='client' b='client'
@ -44,16 +55,21 @@ out="/out/production/${n}${pkg}"
wrapper="/${n}/war/wrapper" wrapper="/${n}/war/wrapper"
# copy misphelper.jar to wrapper/.../lib # copy misphelper.jar to wrapper/.../lib
cp "${cwd}/out/production/misphelper/misphelper.jar" "${cwd}${wrapper}/WEB-INF/lib" cp -v "${cwd}/out/production/misphelper/misphelper.jar" "${cwd}${wrapper}/WEB-INF/lib"
# copy compiled code into the wrapper. # copy compiled code into the wrapper.
cp -r "${cwd}${out}" "${cwd}${wrapper}/WEB-INF/classes/com/olexyn/${a}" cp -vr "${cwd}${out}" "${cwd}${wrapper}/WEB-INF/classes/com/olexyn/${a}"
# compress .war # compress .war
cd "${cwd}${wrapper}" || exit cd "${cwd}${wrapper}" || exit
jar -cvf "../${n}.war" * jar -cvf "../${n}.war" *
echo "================"
echo "END MispClient WAR"
echo "================"
a='mirror' a='mirror'
@ -62,11 +78,15 @@ out="/out/production/${a}${pkg}"
wrapper="/${a}/war/wrapper" wrapper="/${a}/war/wrapper"
# copy misphelper.jar to wrapper/.../lib # copy misphelper.jar to wrapper/.../lib
cp "${cwd}/out/production/misphelper/misphelper.jar" "${cwd}${wrapper}/WEB-INF/lib" cp -v "${cwd}/out/production/misphelper/misphelper.jar" "${cwd}${wrapper}/WEB-INF/lib"
# copy compiled code into the wrapper. # copy compiled code into the wrapper.
cp -r "${cwd}${out}" "${cwd}${wrapper}/WEB-INF/classes/com/olexyn" cp -vr "${cwd}${out}" "${cwd}${wrapper}/WEB-INF/classes/com/olexyn"
# compress .war # compress .war
cd "${cwd}${wrapper}" || exit cd "${cwd}${wrapper}" || exit
jar -cvf "../${a}.war" * jar -cvf "../${a}.war" *
echo "================"
echo "END Mirror WAR"
echo "================"

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,9 +1,8 @@
package com.olexyn.misp.bridge; package com.olexyn.misp.bridge;
import com.olexyn.misp.helper.JsonHelper;
import com.olexyn.misp.helper.Ride; import com.olexyn.misp.helper.Ride;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.json.JSONException;
import org.json.JSONObject;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
@ -30,33 +29,21 @@ public class BridgeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
final String payload = IOUtils.toString(request.getReader());
final boolean isJson = JsonHelper.isJson(payload);
boolean hasID = false;
boolean hasRequest = false;
boolean hasData = false;
if (isJson) {
String payload = IOUtils.toString(request.getReader()); final Ride ridePayload = new Ride(payload);
hasID = ridePayload.getID() != null;
JSONObject obj = new JSONObject(); hasRequest = ridePayload.getRequest() != null;
try { hasData = ridePayload.getData() != null;
obj = new JSONObject(payload);
}catch (JSONException jsonE){
int br = 0;
Thread handleGetUserRequestThread = new Thread(() -> {
try {
handleGetRequest(request, response);
} catch (IOException | InterruptedException e) {e.printStackTrace(); }
});
handleGetUserRequestThread.setName("handleGetUserRequestThread");
handleGetUserRequestThread.start();
} }
Ride ridePayload = new Ride(payload);
boolean hasID = ridePayload.getID() != null;
boolean hasRequest = ridePayload.getRequest() != null;
boolean hasData = ridePayload.getData() != null;
if (isJson && hasID && hasRequest && hasData) {
if (hasID && hasRequest && hasData) {
Thread handleGetRideRequestDataThread = new Thread(() -> { Thread handleGetRideRequestDataThread = new Thread(() -> {
try { try {
handleGetRideRequestData(request, response); handleGetRideRequestData(request, response);
@ -64,8 +51,16 @@ public class BridgeServlet extends HttpServlet {
}); });
handleGetRideRequestDataThread.setName("handleGetRideRequestDataThread"); handleGetRideRequestDataThread.setName("handleGetRideRequestDataThread");
handleGetRideRequestDataThread.start(); handleGetRideRequestDataThread.start();
}
} else {
Thread handleGetRequestThread = new Thread(() -> {
try {
handleGetRequest(request, response);
} catch (IOException | InterruptedException e) {e.printStackTrace(); }
});
handleGetRequestThread.setName("handleGetRequestThread");
handleGetRequestThread.start();
}
} }
@ -124,7 +119,8 @@ public class BridgeServlet extends HttpServlet {
final PrintWriter writer = response.getWriter(); final PrintWriter writer = response.getWriter();
writer.write(ride.getData()); writer.write(ride.getData());
writer.flush(); writer.flush();
writer.close(); } writer.close();
}
/** /**
@ -181,7 +177,7 @@ public class BridgeServlet extends HttpServlet {
} }
// ID is final/threadsafe // ID is final/threadsafe
while(!(booked.containsKey(ride.getID()))){ while (!(booked.containsKey(ride.getID()))) {
} }

@ -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.

@ -19,8 +19,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/mispclient"; protected static final String APP_URL = "http://localhost:9090/mispclient";
public static final int AVAILABLE_RIDES_OVERHEAD_TRIGGER = 32; public static final int AVAILABLE_RIDES_OVERHEAD_TRIGGER = 2;
public static final int AVAILABLE_RIDES_OVERHEAD = 64; public static final int AVAILABLE_RIDES_OVERHEAD = 4;
public final Map<Long, Ride> available = new HashMap<>(); public final Map<Long, Ride> available = new HashMap<>();

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;
}
}

@ -32,7 +32,7 @@ public class Ride {
} }
Long _id; long _id;
try { try {
_id = obj.getLong("id"); _id = obj.getLong("id");
@ -55,6 +55,31 @@ public class Ride {
} }
public Ride(JSONObject obj){
long _id;
try {
_id = obj.getLong("id");
}catch (JSONException e){
_id = count++;
}
id = _id;
try{
request = obj.getString("request");
} catch (JSONException e){
request = null;
}
try{
data = obj.getString("data");
}catch (JSONException e){
data = null;
}
}
public void setRequest(String request) { public void setRequest(String request) {
this.request = request; this.request = request;
} }

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

@ -15,20 +15,24 @@ public class Main {
Runnable publicRunnable = new UserMock(mockSet); Runnable publicRunnable = new UserMock(mockSet);
Runnable bridgeRunable = new BridgeRunnable(mockSet); Runnable bridgeRunable = new BridgeRunnable(mockSet);
Runnable clientRunnable = new ClientRunnable(mockSet); Runnable adapterRunnable = new AdapterRunnable(mockSet);
//Runnable clientRunnable = new ClientRunnable(mockSet);
Runnable appRunnable = new AppMock(mockSet); Runnable appRunnable = new AppMock(mockSet);
Thread userThread = new Thread(publicRunnable); Thread userThread = new Thread(publicRunnable);
Thread bridgeThread = new Thread(bridgeRunable); Thread bridgeThread = new Thread(bridgeRunable);
Thread clientThread = new Thread(clientRunnable); Thread adapterThread = new Thread(adapterRunnable);
//Thread clientThread = new Thread(clientRunnable);
Thread appThread = new Thread(appRunnable); Thread appThread = new Thread(appRunnable);
userThread.setName("userThread"); userThread.setName("userThread");
userThread.start(); userThread.start();
bridgeThread.setName("bridgeThread"); bridgeThread.setName("bridgeThread");
bridgeThread.start(); bridgeThread.start();
clientThread.setName("clientThread"); adapterThread.setName("adapterThread");
clientThread.start(); adapterThread.start();
//clientThread.setName("clientThread");
//clientThread.start();
appThread.setName("appThread"); appThread.setName("appThread");
appThread.start(); appThread.start();
} }

@ -6,6 +6,7 @@ import com.olexyn.misp.mock.actor.UserMock;
public class MockSet { public class MockSet {
public UserMock userMock; public UserMock userMock;
public BridgeMock bridgeMock; public BridgeMock bridgeMock;
public AdapterMock adapterMock;
public ClientMock clientMock; public ClientMock clientMock;
public AppMock appMock; public AppMock appMock;
} }
Loading…
Cancel
Save