Skip to content

Commit

Permalink
fix peek for destination
Browse files Browse the repository at this point in the history
  • Loading branch information
sshaaf committed May 2, 2019
1 parent 651c4cc commit 7889d71
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String getMessageType() {

@Override
public void start(Future<Void> startFuture) throws Exception {
responders = new HashSet<>(150);
responders = Collections.synchronizedSet(new HashSet<>(150));
waitQueue = new HashMap<>(150);


Expand Down Expand Up @@ -143,6 +143,10 @@ else if (r.peek().isDestination())
sendMessage(r);
r.nextLocation();
}
else{
sendMessage(r);
r.nextLocation();
}

}

Expand Down Expand Up @@ -188,7 +192,6 @@ private void addResponder(MissionCommand mc){

vertx.<String>executeBlocking(fut->{


if (mc.getMessageType().equals(MessageType.MissionPickedUpEvent.getMessageType()) ||
mc.getMessageType().equals(MessageType.MissionCompletedEvent.getMessageType())) {
}
Expand Down Expand Up @@ -230,6 +233,7 @@ private void addResponder(MissionCommand mc){

}


public void onMessage(Message<JsonObject> message) {

if (!message.headers().contains("action")) {
Expand All @@ -245,39 +249,43 @@ public void onMessage(Message<JsonObject> message) {
break;
case "RESPONDER_MSG":
Responder r = Json.decodeValue(String.valueOf(message.body()), Responder.class);
List<Responder> 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{

Expand Down
5 changes: 1 addition & 4 deletions temp.json
Original file line number Diff line number Diff line change
@@ -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}
{"responderId":"151","status":"DROPPED"}

0 comments on commit 7889d71

Please sign in to comment.