#pragma once /************************************************************************ MeOS - Orienteering Software Copyright (C) 2009-2023 Melin Software HB This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Melin Software HB - software@melin.nu - www.melin.nu Eksoppsvägen 16, SE-75646 UPPSALA, Sweden ************************************************************************/ /** Class for providing a MeOS REST service */ #include #include #include #include #include #include #include #include class InfoCompetition; class xmlbuffer; namespace restbed { class Service; class Session; }; class RestServer { public: enum class EntryPermissionClass { None, DirectEntry, Any }; enum class EntryPermissionType { None, InDbExistingClub, InDbAny, Any }; static vector> getPermissionsPersons(); static vector> getPermissionsClass(); private: struct EventRequest { EventRequest() : state(false) {} multimap parameters; string answer; vector image; atomic_bool state; //false - asked, true - answerd bool isCompleted() { return state; } }; EntryPermissionClass epClass = EntryPermissionClass::None; EntryPermissionType epType = EntryPermissionType::None; mutex lock; atomic_bool hasAnyRequest; shared_ptr service; shared_ptr restService; condition_variable waitForCompletion; deque> requests; void getData(oEvent &ref, const string &what, const multimap ¶m, string &answer); void lookup(oEvent &ref, const string &what, const multimap ¶m, string &answer); void newEntry(oEvent &ref, const multimap ¶m, string &answer); void compute(oEvent &ref); void startThread(int port); static void getSelection(const string ¶m, set &sel); void handleRequest(const shared_ptr &session); friend void method_handler(const shared_ptr< restbed::Session > session); shared_ptr addRequest(multimap ¶m); shared_ptr getRequest(); vector responseTimes; map> imageCache; RestServer(); static vector< shared_ptr > startedServers; RestServer(const RestServer &); RestServer & operator=(const RestServer &) const; void computeInternal(oEvent &ref, shared_ptr &rq); map > > listCache; string root; multimap rootMap; struct InfoServerContainer { //static int currentInstanceId; int getNextInstanceId(); const int instanceIncrementor; int thisInstanceId = -1; int nextInstanceId; shared_ptr cmpModel; shared_ptr lastData; set classes; set controls; InfoServerContainer(int s, int e) : nextInstanceId(s), instanceIncrementor(e) {} }; shared_ptr randGen; list isContainers; int getNewInstanceId(); xmlbuffer *getMOPXML(oEvent &oe, int id, int &nextId); void difference(oEvent &oe, int id, string &answer); public: ~RestServer(); void startService(int port); void stop(); static shared_ptr construct(); static void remove(shared_ptr server); static void computeRequested(oEvent &ref); void setEntryPermission(EntryPermissionClass epClass, EntryPermissionType epType); void setRootMap(const string &rootMap); const string &getRootMap() const { return root; } tuple getEntryPermission() const { return make_tuple(epClass, epType); } struct Statistics { int numRequests; int averageResponseTime; int maxResponseTime; }; void getStatistics(Statistics &s); };