Commit 3364585a authored by hengwen's avatar hengwen
Browse files

Add endpoint for checking status of specific simulation task

parent 261e17c7
......@@ -139,7 +139,7 @@ paths:
description: "success"
schema:
type: string
enum: [running, finished, not_found]
enum: [READY, RUNNING, FINISHED, FAILED]
/simulation/{id}/map-data/{sectorIdx}:
get:
tags:
......
package server.restful.view;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import server.restful.dao.SimulationDAO;
import server.restful.model.MapModel;
import server.restful.model.SimulationModel;
......@@ -11,7 +13,9 @@ import server.restful.service.MapService;
import server.restful.service.SimulationService;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/simulation")
......@@ -20,9 +24,9 @@ public class SimulationView {
public SimulationModel createSimulation(
@RequestParam("scenario_id") int scenarioID,
@RequestParam("num_sectors") int numSectors
){
) {
int simID = SimulationDAO.create(scenarioID, numSectors);
return new SimulationModel(){{
return new SimulationModel() {{
setId(simID);
setScenarioID(scenarioID);
setNumSectors(numSectors);
......@@ -30,30 +34,47 @@ public class SimulationView {
}};
}
@RequestMapping(value="", method= RequestMethod.GET)
public List<SimulationModel> getSimulation(){
@RequestMapping(value = "", method = RequestMethod.GET)
public List<SimulationModel> getSimulation() {
return SimulationDAO.getAll();
}
@PostMapping("/{id}/start")
public String startSimulation(@PathVariable("id") int simulationID){
ServiceRegistry registry = RegistryFactory.getZookeeperRegistry();
public String startSimulation(@PathVariable("id") int simulationID) {
ServiceRegistry registry = RegistryFactory.getDefaultRegistry();
try {
Thread thread = new Thread(
new SimulationService(simulationID, registry)
new SimulationService(simulationID, registry)
);
thread.start();
return "ok";
} catch (Exception e) {
e.printStackTrace();
}finally {
} finally {
registry.releaseAll();
}
return "simulation failed";
}
@GetMapping("/{id}/status")
public Map<String, SimulationService.SimulationStatus> getStatus(@PathVariable("id") int simulationID) {
if (SimulationService.getInstances()!= null && SimulationService.getInstances().containsKey(simulationID)) {
return new HashMap<String, SimulationService.SimulationStatus>() {{
put("status", SimulationService.getInstances().get(simulationID).getStatus());
}};
}
throw new ResponseStatusException(
HttpStatus.NOT_FOUND,
String.format(
"Simulation instance %d not started yet. Please start it using POST /simulation/%d",
simulationID, simulationID
)
);
}
@GetMapping("/{id}/result")
public List<VehicleModel> getResult(@PathVariable("id") int simulationID){
public List<VehicleModel> getResult(@PathVariable("id") int simulationID) {
return SimulationDAO.getSimulationResultByID(simulationID);
}
......@@ -61,7 +82,7 @@ public class SimulationView {
public MapModel getResult(
@PathVariable("id") int simulationID,
@PathVariable("sectorIdx") int sectorIdx
){
) {
SimulationModel simulationModel = SimulationDAO.getByID(simulationID);
SimulationService simulationService = new SimulationService(simulationID);
try {
......@@ -73,5 +94,7 @@ public class SimulationView {
e.printStackTrace();
}
return null;
};
}
;
}
......@@ -5,6 +5,8 @@ import org.junit.Test;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import server.restful.registry.RegistryFactory;
import server.restful.service.SimulationService;
public class SimulationViewTest extends AbstractViewTest{
......@@ -24,8 +26,25 @@ public class SimulationViewTest extends AbstractViewTest{
}
@Test
public void testStartSimulation() throws Exception{
// TODO mock simulation.start
// RequestBuilder builder = MockMvcRequestBuilders.post("/simulation/1/start");
public void testSimulationStatus() throws Exception{
// simulation 1 not exist
RequestBuilder builder = MockMvcRequestBuilders.get("/simulation/1/status");
MvcResult result = null;
result = mvc.perform(builder).andReturn();
Assert.assertEquals(404, result.getResponse().getStatus());
// simulation 2 should be ready after we initialized its simulation service instance
new SimulationService(2, RegistryFactory.getZookeeperRegistry());
builder = MockMvcRequestBuilders.get("/simulation/2/status");
result = mvc.perform(builder).andReturn();
Assert.assertEquals(200, result.getResponse().getStatus());
Assert.assertTrue(result.getResponse().getContentAsString().contains("READY"));
// simulation 2 should failed, since there are no available sub-simulators and rmi servers
SimulationService.getInstances().get(2).run();
builder = MockMvcRequestBuilders.get("/simulation/2/status");
result = mvc.perform(builder).andReturn();
Assert.assertEquals(200, result.getResponse().getStatus());
Assert.assertTrue(result.getResponse().getContentAsString().contains("FAILED"));
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment