From 7889d71284a92f7bfc4870761c3396ffccfd06b3 Mon Sep 17 00:00:00 2001 From: Syed M Shaaf Date: Fri, 3 May 2019 01:56:36 +0200 Subject: [PATCH] fix peek for destination --- .../simulator/SimulationControl.java | 60 +++++++++++-------- temp.json | 5 +- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/redhat/cajun/navy/responder/simulator/SimulationControl.java b/src/main/java/com/redhat/cajun/navy/responder/simulator/SimulationControl.java index 14ad471..4d6403f 100644 --- a/src/main/java/com/redhat/cajun/navy/responder/simulator/SimulationControl.java +++ b/src/main/java/com/redhat/cajun/navy/responder/simulator/SimulationControl.java @@ -60,7 +60,7 @@ public String getMessageType() { @Override public void start(Future startFuture) throws Exception { - responders = new HashSet<>(150); + responders = Collections.synchronizedSet(new HashSet<>(150)); waitQueue = new HashMap<>(150); @@ -143,6 +143,10 @@ else if (r.peek().isDestination()) sendMessage(r); r.nextLocation(); } + else{ + sendMessage(r); + r.nextLocation(); + } } @@ -188,7 +192,6 @@ private void addResponder(MissionCommand mc){ vertx.executeBlocking(fut->{ - if (mc.getMessageType().equals(MessageType.MissionPickedUpEvent.getMessageType()) || mc.getMessageType().equals(MessageType.MissionCompletedEvent.getMessageType())) { } @@ -230,6 +233,7 @@ private void addResponder(MissionCommand mc){ } + public void onMessage(Message message) { if (!message.headers().contains("action")) { @@ -245,39 +249,43 @@ public void onMessage(Message message) { break; case "RESPONDER_MSG": Responder r = Json.decodeValue(String.valueOf(message.body()), Responder.class); - List list = new ArrayList<>(); - if(responders.contains(r)){ - for(Responder temp: responders){ - if(temp.getResponderId().equals(r.getResponderId())) { - Responder.Status status = r.getStatus(); - r = temp; - r.setHuman(true); - r.setStatus(status); - if(status.equals(Responder.Status.PICKEDUP) || status.equals(Responder.Status.DROPPED)) { - r.setContinue(true); - sendMessage(r); - r.nextLocation(); - } - list.add(r); - break; - } - } - // remove previous version of responder in HashSet - responders.remove(r); - // add latest version of responder with setHuman=true - responders.add(r); - + synchronized (this){ + setResponderStatus(r); } message.reply("request processed"); break; - - default: message.fail(ErrorCodes.BAD_ACTION.ordinal(), "Bad action: " + action); } } + protected void setResponderStatus(Responder r){ + if(responders.contains(r)) { + for (Responder temp : responders) { + if (temp.getResponderId().equals(r.getResponderId())) { + Responder.Status status = r.getStatus(); + r = temp; + r.setHuman(true); + r.setStatus(status); + if (status.equals(Responder.Status.PICKEDUP) || status.equals(Responder.Status.DROPPED)) { + r.setContinue(true); + sendMessage(r); + r.nextLocation(); + } + break; + } + } + synchronized (this) { + // remove previous version of responder in HashSet + responders.remove(r); + // add latest version of responder with setHuman=true + responders.add(r); + } + } + + } + protected Responder getResponder(MissionCommand mc, MessageType messageType) throws UnWantedResponderEvent{ diff --git a/temp.json b/temp.json index f2c5d5c..6484678 100644 --- a/temp.json +++ b/temp.json @@ -1,4 +1 @@ -curl -X POST -H "Content-Type: application/json" -d @temp.json "http://localhost:8080/api/responders" -{"responderId":"144","missionId":"0f8a09d3-af11-4782-a78b-92248c1e466a","incidentId":"2184afdf-10ac-46ac-9cf8-3a3cb36d7659","status":"MOVING","human":true} -{"responderId":"144","missionId":"0f8a09d3-af11-4782-a78b-92248c1e466a","incidentId":"2184afdf-10ac-46ac-9cf8-3a3cb36d7659","status":"PICKEDUP","human":true} -{"responderId":"144","missionId":"0f8a09d3-af11-4782-a78b-92248c1e466a","incidentId":"2184afdf-10ac-46ac-9cf8-3a3cb36d7659","status":"DROPPED","human":true} \ No newline at end of file +{"responderId":"151","status":"DROPPED"} \ No newline at end of file