~ sync on rideMap

pull/1/head
Ivan Olexyn 5 years ago
parent 2bd1ff77e4
commit bb7f22e4b3

@ -1,5 +1,6 @@
#### TODO #### TODO
* FIX errors when setting WAIT to low.
* In `ClientMock` put `GET (Ride)(Request)(Data)` on it's own thread. * In `ClientMock` put `GET (Ride)(Request)(Data)` on it's own thread.
* Currently it is on the `POST (Ride)` thread. * Currently it is on the `POST (Ride)` thread.
* It would be better to "decouple" it. * It would be better to "decouple" it.

@ -14,7 +14,7 @@ public class BridgeServlet extends HttpServlet {
protected static final String MISP_CLIENT_URL = "http://localhost:9090/mispclient/core"; protected static final String MISP_CLIENT_URL = "http://localhost:9090/mispclient/core";
public Map<Long, Ride> rideMap = new HashMap<>(); public final Map<Long, Ride> rideMap = new HashMap<>();
protected RideMapHelper mapHelper = new RideMapHelper(rideMap); protected RideMapHelper mapHelper = new RideMapHelper(rideMap);
// ####### // #######

@ -20,7 +20,7 @@ public class ClientServlet extends HttpServlet {
public static final int AVAILABLE_RIDES_OVERHEAD = 32; public static final int AVAILABLE_RIDES_OVERHEAD = 32;
public Map<Long, Ride> rideMap = new HashMap<>(); public final Map<Long, Ride> rideMap = new HashMap<>();
protected RideMapHelper mapHelper = new RideMapHelper(rideMap); protected RideMapHelper mapHelper = new RideMapHelper(rideMap);
public ClientServlet() { public ClientServlet() {

@ -24,7 +24,7 @@ public class AppMock extends ActorRunnable {
public void run() { public void run() {
while (true) { while (true) {
try { try {
Thread.sleep(Main.MOCK_SPEED); Thread.sleep(Main.REQUEST_SPEED);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();

@ -1,11 +1,8 @@
package actor; package actor;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection;
import core.*; import core.*;
import exchange.ExchangeMock; import exchange.ExchangeMock;
@ -23,7 +20,7 @@ public class UserMock extends ActorRunnable {
public void run() { public void run() {
while (true){ while (true){
try { try {
Thread.sleep(Main.MOCK_SPEED); Thread.sleep(Main.REQUEST_SPEED);
sendGetRequest(); sendGetRequest();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
e.printStackTrace(); e.printStackTrace();

@ -36,25 +36,24 @@ public class BridgeMock extends BridgeServlet {
JSONObject obj = new JSONObject(parsedRequest); JSONObject obj = new JSONObject(parsedRequest);
parsedRequest = obj.getString("request"); parsedRequest = obj.getString("request");
while (mapHelper.containsLess(1, State.AVAILABLE)) { Thread.sleep(Main.MOCK_SPEED); }
synchronized (rideMap) {
while (mapHelper.containsLess(1, State.AVAILABLE)) { Thread.sleep(Main.WAIT_SPEED); }
Ride ride = null; Ride ride = null;
for (int i = 0; ride ==null; i++){
while (ride == null) {
ride = mapHelper.pickAvailable(); ride = mapHelper.pickAvailable();
if (i!=0){ Thread.sleep(Main.WAIT_SPEED);
Thread.sleep(Main.MOCK_SPEED);}
} }
synchronized (ride) {
ride.setRequest(parsedRequest); ride.setRequest(parsedRequest);
ride.setState(State.BOOKED); ride.setState(State.BOOKED);
while (ride.getState() != State.LOADED) { while (ride.getState() != State.LOADED) {
ride.notify(); rideMap.notify();
Thread.sleep(Main.MOCK_SPEED); Thread.sleep(Main.WAIT_SPEED);
ride.wait(); rideMap.wait();
} }
rideMap.remove(ride.getID()); rideMap.remove(ride.getID());
@ -65,9 +64,10 @@ public class BridgeMock extends BridgeServlet {
writer.flush(); writer.flush();
writer.close(); writer.close();
ride.notify(); rideMap.notify();
} }
exchange.notify(); exchange.notify();
} }
} }
@ -92,9 +92,10 @@ public class BridgeMock extends BridgeServlet {
synchronized (exchange) { synchronized (exchange) {
String jsonPayload = IOUtils.toString(request.getReader()); String jsonPayload = IOUtils.toString(request.getReader());
Ride parsedRide = new Ride(jsonPayload); Ride parsedRide = new Ride(jsonPayload);
Ride thisRide = rideMap.get(parsedRide.getID());
synchronized (thisRide) {
synchronized (rideMap) {
Ride thisRide = rideMap.get(parsedRide.getID());
thisRide.setData(parsedRide.getData()); thisRide.setData(parsedRide.getData());
thisRide.setState(State.LOADED); thisRide.setState(State.LOADED);
@ -104,9 +105,14 @@ public class BridgeMock extends BridgeServlet {
writer.flush(); writer.flush();
writer.close(); writer.close();
thisRide.notify();
if (thisRide.getID() == 100000L) {
int br = 0;
}
rideMap.notify();
} }
exchange.notify(); exchange.notify();
//rideMap.remove(thisRide.getID());
} }
} }
@ -124,22 +130,23 @@ public class BridgeMock extends BridgeServlet {
synchronized (exchange) { synchronized (exchange) {
String jsonPayload = IOUtils.toString(request.getReader()); String jsonPayload = IOUtils.toString(request.getReader());
Ride ride = new Ride(jsonPayload);
synchronized (ride) {
synchronized (rideMap) {
Ride ride = new Ride(jsonPayload);
rideMap.put(ride.getID(), ride.setState(State.AVAILABLE)); rideMap.put(ride.getID(), ride.setState(State.AVAILABLE));
while (ride.getState() == State.AVAILABLE) { while (ride.getState() == State.AVAILABLE) {
ride.notify(); rideMap.notify();
Thread.sleep(Main.MOCK_SPEED); Thread.sleep(Main.WAIT_SPEED);
ride.wait(); rideMap.wait();
} }
exchange.response.setStatus(200); exchange.response.setStatus(200);
PrintWriter writer = response.getWriter(); PrintWriter writer = response.getWriter();
writer.write(ride.json()); writer.write(ride.json());
writer.flush(); writer.flush();
writer.close(); writer.close();
ride.notify(); rideMap.notify();
} }
exchange.notify(); exchange.notify();
} }

@ -5,7 +5,8 @@ import actor.UserMock;
public class Main { public class Main {
final public static int MOCK_SPEED = 0; final public static int WAIT_SPEED = 10;
final public static int REQUEST_SPEED = 1;

Loading…
Cancel
Save