Line data Source code
1 : /**yaml 2 : * openapi: 3.0.0 3 : * info: 4 : * title: DynamiNATOR API 5 : * description: Web REST API to interact with the DynamiNATOR simulator. 6 : * version: 0.0.4 7 : * servers: 8 : * - url: http://localhost:9000 9 : * 10 : * components: 11 : * schemas: 12 : * StaticSimulation: 13 : * type: object 14 : * required: 15 : * - netPath 16 : * - tazPath 17 : * - demandPath 18 : * - dstPath 19 : * properties: 20 : * netPath: 21 : * description: Path of SUMO network file to use. 22 : * type: string 23 : * example: network/net.net.xml 24 : * tazPath: 25 : * description: Path of SUMO TAZ file to use. 26 : * type: string 27 : * example: network/taz.xml 28 : * demandPath: 29 : * description: Path of O-formatted OD matrix to use. 30 : * type: string 31 : * example: od/matrix.9.0.10.0.2.fma 32 : * type: 33 : * description: Type of static simulation to run. 34 : * type: string 35 : * enum: 36 : * - convex 37 : * - nonconvex 38 : * example: nonconvex 39 : * default: nonconvex 40 : * outEdgesPath: 41 : * description: Path to which edges output will be printed. 42 : * type: string 43 : * example: out/edgedata-static.xml 44 : * outRoutesPath: 45 : * description: Path to which routes output will be printed. 46 : * type: string 47 : * example: out/routes-static.xml 48 : * DynamicSimulation: 49 : * type: object 50 : * required: 51 : * - netPath 52 : * - tazPath 53 : * - demandPath 54 : * - begin 55 : * - end 56 : * properties: 57 : * netPath: 58 : * description: Path of SUMO network file to use. 59 : * type: string 60 : * example: network/net.net.xml 61 : * tazPath: 62 : * description: Path of SUMO TAZ file to use. 63 : * type: string 64 : * example: network/taz.xml 65 : * demandPath: 66 : * description: Path of O-formatted OD matrix to use. 67 : * type: string 68 : * example: od/matrix.9.0.10.0.2.fma 69 : * begin: 70 : * description: Beginning time of simulation (in seconds). 71 : * type: float 72 : * example: 0 73 : * end: 74 : * description: End time of simulation (in seconds). 75 : * type: float 76 : * example: 3600 77 : * step: 78 : * description: Time step of simulation (in seconds). Only relevant if creating netstate output. 79 : * type: float 80 : * example: 1 81 : * default: 1 82 : * netstatePath: 83 : * description: Path to which netstate output will be printed. 84 : * type: string 85 : * example: out/netstate.xml 86 : */ 87 : 88 : #include "Com/HTTPServer.hpp" 89 : 90 : #include <spdlog/spdlog.h> 91 : 92 : #include <exception> 93 : #include <functional> 94 : #include <future> 95 : #include <ios> 96 : #include <memory> 97 : #include <mutex> 98 : #include <nlohmann/json.hpp> 99 : #include <stdexcept> 100 : 101 : #include "GlobalState.hpp" 102 : 103 : using namespace std; 104 : using namespace std::placeholders; 105 : using namespace Com; 106 : using namespace httplib; 107 : 108 : using json = nlohmann::json; 109 : 110 0 : HTTPServer::HTTPServer(int port_): 111 0 : port(port_) { 112 0 : spdlog::info("Starting HTTP server"); 113 : 114 0 : if(!svr.set_mount_point("/", "/var/www/html")) { 115 0 : throw runtime_error("Failed to setup HTTP server mount point"); 116 : } 117 : 118 : // Ping 119 0 : svr.Get( 120 : "/hello", 121 0 : Server::Handler(bind(&HTTPServer::helloGet, this, _1, _2)) 122 0 : ); 123 : 124 : // /static/simulation 125 0 : svr.Post( 126 : R"(/static/simulation/([\w\-]+))", 127 0 : Server::Handler(bind(&HTTPServer::staticSimulationPost, this, _1, _2)) 128 0 : ); 129 : 130 0 : svr.Get( 131 : R"((/static/simulation/([\w\-]+))/join)", 132 0 : Server::Handler(bind(&HTTPServer::staticSimulationJoinGet, this, _1, _2)) 133 0 : ); 134 : 135 : // /dynamic/simulation 136 0 : svr.Post( 137 : R"(/dynamic/simulation/([\w\-]+))", 138 0 : Server::Handler(bind(&HTTPServer::dynamicSimulationPost, this, _1, _2)) 139 0 : ); 140 : 141 0 : spdlog::info("Started HTTP server"); 142 0 : } 143 : 144 0 : void HTTPServer::loop() { 145 0 : if(!svr.listen("0.0.0.0", 80)) { 146 0 : throw runtime_error("HTTP server listen() failed"); 147 : } 148 : 149 0 : spdlog::info("Closing HTTPserver"); 150 0 : }