Skip to content

Commit

Permalink
Merge pull request #7 from btison/fix-responders
Browse files Browse the repository at this point in the history
Refactored handling of reponders
  • Loading branch information
sshaaf authored May 5, 2019
2 parents a421e39 + de46f3b commit cea0b9b
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 83 deletions.
5 changes: 3 additions & 2 deletions etc/app-config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ incident.service.path.create=/incidents

responder.service.host=localhost
responder.service.port=8080
responder.service.path.reset=/responders/init
responder.service.path=/responder
responder.service.path.reset=/responders/reset
responder.service.path.clear=/responders/clear
responder.service.path.create=/responders


mission.service.host=localhost
Expand Down
9 changes: 6 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@
<artifactId>vertx-web-client</artifactId>
<version>3.7.0</version>
</dependency>



<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.26</version>
</dependency>

<!-- Testing -->

Expand Down
23 changes: 2 additions & 21 deletions src/main/java/com/redhat/cajun/navy/datagenerate/Disaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public Victim generateVictim(){
return v;
}

public List<Responder> generateResponder(long number){
public List<Responder> generateResponders(int number) {
List<Responder> responders = new ArrayList<>();
for(int i=0; i<number; i++){
responders.add(generateResponder());
}
return responders;
}

public Responder generateResponder(){
public Responder generateResponder() {
Responder responder = new Responder();
Double point = points.getInternalPoint();
responder.setName(fullNames.getNextFullName());
Expand All @@ -61,23 +61,4 @@ public List<Victim> generateVictims(int number){
victims.add(generateVictim());
return victims;
}

public Responder generateResponder(int id) {
Responder responder = new Responder();
Double point = points.getInternalPoint();
responder.setId(Integer.toString(id));
responder.setLatitude(point.getY());
responder.setLongitude(point.getX());
responder.setEnrolled(true);
return responder;
}

public List<Responder> generateResponders(int number) {
List<Responder> responders = new ArrayList<>();
for (int i = 1; i <= number; i++) {
responders.add(generateResponder(i));
}
return responders;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void start(Future<Void> future) {
router.get("/g/incidents").handler(this::generateIncidents);
router.get("/g/responders").handler(this::generateResponders);
router.get("/c/incidents").handler(this::clearIncidents);
router.get("/c/responders").handler(this::clearResponders);
router.get("/c/missions").handler(this::clearMissions);

router.get("/g/incidents/lastrun").handler(this::lastRunIncidents);
Expand Down Expand Up @@ -122,31 +123,42 @@ private void clearIncidents(RoutingContext routingContext){

}

private void clearResponders(RoutingContext routingContext){
if(!isDryRun) {
boolean clearResponders = Boolean.valueOf(routingContext.request().getParam("clearResponders"));
// Reset incidents
if (clearResponders) {
DeliveryOptions options = new DeliveryOptions().addHeader("action", "clear-responders");
vertx.eventBus().send("rest-client-queue", new JsonObject(), options);
}
}
JsonObject response = new JsonObject()
.put("response", "Clear request sent to Responder Service, check logs for more details..")
.put("isDryRun", isDryRun);

routingContext.response()
.putHeader(CONTENT_TYPE, "application/json; charset=utf-8")
.end(response.encodePrettily());

}

private void generateResponders(RoutingContext routingContext) {
int responders = toInteger(routingContext.request().getParam("responders")).orElse(25);
boolean clearResponders = Boolean.valueOf(routingContext.request().getParam("clearResponders"));

// Reset responders
List<Responder> responderList = disaster.generateResponders(responders);
boolean resetResponders = Boolean.valueOf(routingContext.request().getParam("resetResponders"));

respondersForLastRun = new ArrayList<>();

if(!isDryRun){
if(clearResponders) {
if (resetResponders) {
DeliveryOptions options = new DeliveryOptions().addHeader("action", "reset-responders");
vertx.eventBus().send("rest-client-queue", new JsonObject().put("responders", new JsonArray(Json.encode(responderList))), options);
}
else {
Observable<Responder> ob = Observable.from(disaster.generateResponder(new Long(responders)));
ob.subscribe(
item -> {
sendMessage(item);
respondersForLastRun.add(item);
log.debug("message sent for "+item);
},
error -> error.printStackTrace(),
() -> log.info("Incidents generated"));
vertx.eventBus().send("rest-client-queue", new JsonObject(), options);
}

List<Responder> responderList = disaster.generateResponders(responders);
respondersForLastRun.addAll(responderList);

DeliveryOptions options = new DeliveryOptions().addHeader("action", "create-responders");
vertx.eventBus().send("rest-client-queue", new JsonObject().put("responders", new JsonArray(Json.encode(responderList))), options);
}

JsonObject response = new JsonObject()
Expand All @@ -168,22 +180,19 @@ private void generateIncidents(RoutingContext rc) {

victimCount = 0;

try {

Observable<Victim> ob = Observable.from(disaster.generateVictims(numVictims));
ob.subscribe(
item -> {victimCount++;
Observable<Victim> ob = Observable.from(disaster.generateVictims(numVictims));
ob.subscribe(
item -> {
victimCount++;
if(!isDryRun)
sendMessage(item, victimCount, waitTime);
victims.add(item);
},
error -> error.printStackTrace(),
() -> log.info("Incidents generated"));
},
error -> log.error("Exception while generating incidents", error),
() -> log.info("Incidents generated"));


}catch(NumberFormatException nfe){
nfe.printStackTrace();
}

JsonObject response = new JsonObject()
.put("response", "If enabled, requests are sent to incident service, check logs for more details on the requests or endpoint [/g/incidents/lastrun]")
Expand All @@ -194,15 +203,6 @@ private void generateIncidents(RoutingContext rc) {
.end(response.encodePrettily());
}




private void sendMessage(Responder r){
DeliveryOptions options = new DeliveryOptions().addHeader("action", "more-responders");
vertx.eventBus().send("rest-client-queue", new JsonObject(Json.encode(r)), options);
}


private void sendMessage(Victim victim, int victimCount, int delay) {
int waitTime = victimCount * delay;
// Cannot schedule a timer with delay < 1 ms, so if waitTime is 0, set it to 1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class RestClientVerticle extends AbstractVerticle {

private String responderServiceResetPath;

private String responderServiceClearPath;

private String missionServiceHost;

private int missionServicePort;
Expand All @@ -43,7 +45,7 @@ public class RestClientVerticle extends AbstractVerticle {

private String incidentPriorityServiceResetPath;

private String responderServicePath = null;
private String responderServiceCreatePath = null;

@Override
public void start(Future<Void> startFuture) throws Exception {
Expand All @@ -56,7 +58,8 @@ public void start(Future<Void> startFuture) throws Exception {
responderServiceHost = config().getString("responder.service.host");
responderServicePort = config().getInteger("responder.service.port");
responderServiceResetPath = config().getString("responder.service.path.reset");
responderServicePath = config().getString("responder.service.path");
responderServiceClearPath = config().getString("responder.service.path.clear");
responderServiceCreatePath = config().getString("responder.service.path.create");
missionServiceHost = config().getString("mission.service.host");
missionServicePort = config().getInteger("mission.service.port");
missionServiceResetPath = config().getString("mission.service.path.reset");
Expand Down Expand Up @@ -87,11 +90,15 @@ private void onMessage(Message<JsonObject> message) {
break;

case "reset-responders":
resetResponders(message);
resetResponders();
break;

case "clear-responders":
clearResponders();
break;

case "more-responders":
createResponder(message);
case "create-responders":
createResponders(message);
break;

case "reset-missions":
Expand All @@ -107,20 +114,29 @@ private void onMessage(Message<JsonObject> message) {
}
}

private void resetResponders(Message<JsonObject> message) {
private void resetResponders() {
client.post(responderServicePort, responderServiceHost, responderServiceResetPath)
.putHeader("Content-Type", "application/json")
.sendBuffer(message.body().getJsonArray("responders").toBuffer(), ar -> {
.send(ar -> {
if (ar.succeeded()) {
HttpResponse<Buffer> response = ar.result();
log.info("Init responders: HTTP response status " + response.statusCode());
log.info("Reset responders: HTTP response status " + response.statusCode());
} else {
log.error("Init responders failed.", ar.cause());
log.error("Clear responders failed.", ar.cause());
}
});
}


private void clearResponders() {
client.post(responderServicePort, responderServiceHost, responderServiceClearPath)
.send(ar -> {
if (ar.succeeded()) {
HttpResponse<Buffer> response = ar.result();
log.info("Clear responders: HTTP response status " + response.statusCode());
} else {
log.error("Clear responders failed.", ar.cause());
}
});
}

private void createIncident(Message<JsonObject> message) {
client.post(incidentServicePort, incidentServiceHost, incidentServiceCreatePath)
Expand All @@ -134,20 +150,20 @@ private void createIncident(Message<JsonObject> message) {
});
}

private void createResponder(Message<JsonObject> message) {
private void createResponders(Message<JsonObject> message) {

client.post(responderServicePort, responderServiceHost, responderServicePath)
.sendJsonObject(message.body(), ar -> {
client.post(responderServicePort, responderServiceHost, responderServiceCreatePath)
.putHeader("Content-Type", "application/json")
.sendBuffer(message.body().getJsonArray("responders").toBuffer(), ar -> {
if (ar.succeeded()) {
HttpResponse<Buffer> response = ar.result();
log.info("Create responder for" + message.body() + ": HTTP response status " + response.statusCode());
log.info("Create responders: HTTP response status " + response.statusCode());
}
else log.error("Create incident for" + message.body().getString("victimName") + " failed.", ar.cause());
else log.error("Create Responders failed.", ar.cause());

});
}


private void resetIncidents() {
client.post(incidentServicePort, incidentServiceHost, incidentServiceResetPath)
.send(ar -> {
Expand Down
32 changes: 28 additions & 4 deletions src/main/resources/webroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ <h3 id="_using_the_greeting_service">Incidents and Responders</h3>
<label for="responderCount">Number of Responders:</label>
<input type="text" class="form-control" id="responderCount"
placeholder="25">
<label for="responderClear">Clear previous responders:</label>
<input type="checkbox" class="form-control" id="responderClear">
<label for="responderReset">Reset previous responders:</label>
<input type="checkbox" class="form-control" id="responderReset">
</p>
</div>
</div>
Expand All @@ -111,6 +111,21 @@ <h3 id="_using_the_greeting_service">Incidents and Responders</h3>
</div>
</div>

<!-- Clear responders form -->
<div class="card" style="margin-bottom: 20px;">
<div class="card-header">
Clear responders
</div>
<div class="card-body">
<form>
<div>
<button id="clearResponders" type="submit"
class="btn btn-success">Clear responders</button>
</div>
</form>
</div>
</div>

<!-- Clear previous missions form -->
<div class="card" style="margin-bottom: 20px;">
<div class="card-header">
Expand Down Expand Up @@ -152,8 +167,8 @@ <h3 id="_using_the_greeting_service">Incidents and Responders</h3>
e.preventDefault();

var responderCount = $("#responderCount").val() || "25";
var responderClear = $("#responderClear").prop("checked");
var endpoint = `/g/responders?responders=${responderCount}&clearResponders=${responderClear}`;
var responderReset = $("#responderReset").prop("checked");
var endpoint = `/g/responders?responders=${responderCount}&resetResponders=${responderReset}`;
$.getJSON(endpoint, function (resp) {
$("#result").text(JSON.stringify(resp));
});
Expand All @@ -176,6 +191,15 @@ <h3 id="_using_the_greeting_service">Incidents and Responders</h3>
$("#result").text(JSON.stringify(resp));
});
});

$("#clearResponders").click(function (e) {
e.preventDefault();

var endpoint = '/c/responders?clearResponders=true';
$.getJSON(endpoint, function (resp) {
$("#result").text(JSON.stringify(resp));
});
});
});
</script>

Expand Down

0 comments on commit cea0b9b

Please sign in to comment.