Line data Source code
1 : #pragma once 2 : 3 : #include <future> 4 : #include <memory> 5 : #include <sstream> 6 : #include <stdexcept> 7 : #include <string> 8 : #include <thread> 9 : #include <unordered_map> 10 : #include <utility> 11 : 12 : #include "Static/Demand.hpp" 13 : #include "Static/SUMOAdapter.hpp" 14 : #include "Static/supply/Network.hpp" 15 : #include "utils/pipestream.hpp" 16 : #include "utils/synchronizer.hpp" 17 : 18 : struct GlobalState { 19 : typedef std::string ResourceID; 20 : 21 : class ResourceException: public std::runtime_error { 22 : const ResourceID id; 23 : 24 : public: 25 : ResourceException(const ResourceID &id, const std::string &msg); 26 : const ResourceID &getID() const { return id; } 27 : }; 28 : 29 : class ResourceDoesNotExistException: public ResourceException { 30 : public: 31 : ResourceDoesNotExistException(const ResourceID &id); 32 : }; 33 : 34 : class ResourceAlreadyExistsException: public ResourceException { 35 : public: 36 : ResourceAlreadyExistsException(const ResourceID &id); 37 : }; 38 : 39 0 : struct TaskReturn { 40 : int status; 41 : std::string content; 42 : std::string content_type = "text/plain"; 43 : }; 44 : 45 : // clang-format off 46 : class Streams: private utils::synchronizer< 47 : std::unordered_map< 48 : ResourceID, 49 : std::shared_ptr<utils::pipestream> 50 : > 51 : > { 52 : // clang-format on 53 : public: 54 : utils::pipestream &create(const ResourceID &id); 55 : utils::pipestream &get(const ResourceID &id); 56 : void erase(const ResourceID &id); 57 : }; 58 : 59 : static Streams streams; 60 : 61 : // clang-format off 62 : class Tasks: private utils::synchronizer< 63 : std::unordered_map< 64 : ResourceID, 65 : std::shared_ptr<std::shared_future<TaskReturn>> 66 : > 67 : > { 68 : // clang-format on 69 : public: 70 : std::shared_future<TaskReturn> &create( 71 : const ResourceID &id, 72 : const std::function<TaskReturn()> &f 73 : ); 74 : std::shared_future<TaskReturn> &get(const ResourceID &id); 75 : void erase(const ResourceID &id); 76 : }; 77 : 78 : static Tasks tasks; 79 : 80 : private: 81 : GlobalState(); 82 : };