diff --git a/Modules/CppRestSdk/include/mitkRESTManager.h b/Modules/CppRestSdk/include/mitkRESTManager.h index 1c1dca6635..c919a0bd89 100644 --- a/Modules/CppRestSdk/include/mitkRESTManager.h +++ b/Modules/CppRestSdk/include/mitkRESTManager.h @@ -1,30 +1,30 @@ #ifndef mitkRESTManager_h #define mitkRESTManager_h #include #include #include #include namespace mitk { class RESTManager : public IRESTManager { public: RESTManager(); ~RESTManager() override; //calls RESTClient void sendRequest(RequestType type) override; //calls RESTServer void receiveRequest(web::uri uri, IRESTObserver *observer) override; bool handle(web::uri) override; private: std::map m_ClientMap; // Map with port client pairs std::map m_ServerMap; // Map with port server pairs - std::map m_Observer; //Map with all observers + std::map, IRESTObserver*> m_Observer; //Map with all observers }; } // namespace mitk #endif // !mitkRESTManager_h diff --git a/Modules/CppRestSdk/src/mitkRESTManager.cpp b/Modules/CppRestSdk/src/mitkRESTManager.cpp index ab19dc623d..578b58305a 100644 --- a/Modules/CppRestSdk/src/mitkRESTManager.cpp +++ b/Modules/CppRestSdk/src/mitkRESTManager.cpp @@ -1,89 +1,93 @@ #include "mitkRESTManager.h" #include mitk::RESTManager::RESTManager() {} mitk::RESTManager::~RESTManager() {} -void mitk::RESTManager::sendRequest(RequestType type) +void mitk::RESTManager::sendRequest(RequestType type) { switch (type) { case get: - //Call get in mitkRESTClientMicroService + // Call get in mitkRESTClientMicroService break; case post: - //Call post in mitkRESTClientMicroService + // Call post in mitkRESTClientMicroService break; case put: - //Call put in mitkRESTClientMicroService + // Call put in mitkRESTClientMicroService break; } } void mitk::RESTManager::receiveRequest(web::uri uri, mitk::IRESTObserver *observer) { - //New instance of RESTServerMicroservice in m_ServerMap, key is port of the request + // New instance of RESTServerMicroservice in m_ServerMap, key is port of the request int port = uri.port(); - //Checking if port is free to add a new Server + // Checking if port is free to add a new Server if (m_ServerMap.count(port) == 0) { - //new observer has to be added - m_Observer[uri] = observer; + // new observer has to be added + std::pair key(uri.port(), uri.path()); + m_Observer[key] = observer; - //testing if entry has been added to observer map - utility::string_t uristringt = uri.to_string(); + // testing if entry has been added to observer map + utility::string_t uristringt = uri.path(); std::string uristring(uristringt.begin(), uristringt.end()); - MITK_INFO << uristring << " : Number of elements in map: " << m_Observer.count(uri); + MITK_INFO << uristring << " : Number of elements in map: " << m_Observer.count(key); - //creating server instance + // creating server instance RESTServerMicroService *server = new RESTServerMicroService(uri.authority()); - //add reference to server instance to map + // add reference to server instance to map m_ServerMap[port] = server; - //info output + // info output utility::string_t host = uri.authority().to_string(); std::string hoststring(host.begin(), host.end()); - MITK_INFO << "new server" << hoststring<<" at port" << port; + MITK_INFO << "new server" << hoststring << " at port" << port; } - //If there is already a server under this port + // If there is already a server under this port else { - //Same host, means new observer but not a new server instance + // Same host, means new observer but not a new server instance if (m_ServerMap[port]->GetUri() == uri.authority()) { - //new observer has to be added - m_Observer[uri] = observer; + // new observer has to be added + std::pair key(uri.port(), uri.path()); + m_Observer[key] = observer; // testing if entry has been added to map utility::string_t uristringt = uri.to_string(); std::string uristring(uristringt.begin(), uristringt.end()); - MITK_INFO << uristring << " : Number of elements in map: " << m_Observer.count(uri); + MITK_INFO << uristring << " : Number of elements in map: " << m_Observer.count(key); - //info output + // info output MITK_INFO << "started listening, no new server instance has been created"; } - //Error, since another server can't be added under this port + // Error, since another server can't be added under this port else { MITK_ERROR << "there is already another server listening under this port"; } } } -//TODO replace boolean as return value by actual data +// TODO replace boolean as return value by actual data bool mitk::RESTManager::handle(web::uri uri) { - if (m_Observer.count(uri) == 1) + // Checking if is there is a observer for the port and path + std::pair key(uri.port(), uri.path()); + if (m_Observer.count(key) != 0) { - //TODO replace by data - bool worked = m_Observer[uri]->notify(); - return worked; + // TODO replace by data + return m_Observer[key]->notify(); } + //No map under this port, delete Server Object to release port for other servers else { MITK_WARN << "No Observer can handle the data"; return false; } } diff --git a/Modules/CppRestSdk/src/mitkRESTServerMicroService.cpp b/Modules/CppRestSdk/src/mitkRESTServerMicroService.cpp index bed6ba9e17..7428b10c0c 100644 --- a/Modules/CppRestSdk/src/mitkRESTServerMicroService.cpp +++ b/Modules/CppRestSdk/src/mitkRESTServerMicroService.cpp @@ -1,68 +1,68 @@ #include "mitkRESTServerMicroService.h" #include #include #include mitk::RESTServerMicroService::RESTServerMicroService(web::uri uri) : m_Listener(uri) { m_Uri = uri; m_Listener.support(MitkRESTMethods::GET, std::bind(&RESTServerMicroService::HandleGet, this, std::placeholders::_1)); openListener(); } mitk::RESTServerMicroService::~RESTServerMicroService() { closeListener(); } pplx::task mitk::RESTServerMicroService::openListener() { return m_Listener.open(); } pplx::task mitk::RESTServerMicroService::closeListener() { return m_Listener.close(); } web::uri mitk::RESTServerMicroService::GetUri() { return m_Uri; } void mitk::RESTServerMicroService::HandleGet(MitkRequest request) { int port = m_Listener.uri().port(); //getting exact request uri has to be a parameter in handle function web::uri_builder build(m_Listener.uri()); build.append(request.absolute_uri()); utility::string_t uri = build.to_uri().to_string(); std::string uriString(uri.begin(), uri.end()); web::json::value content; //example content, has to be replace by getting actual modified data by handle content[L"key 1"] = web::json::value::string(U("this is a first test")); request.set_body(content); auto answer = request.extract_json().get(); MITK_INFO << "Test for Server at port " << port << " Exact request uri: " << uriString; //TODO replace worked by data object - bool worked; + bool worked = false; us::ModuleContext *context = us::GetModuleContext(); auto managerRef = context->GetServiceReference(); if (managerRef) { auto managerService = context->GetService(managerRef); if (managerService) { worked = managerService->handle(build.to_uri()); } } if (worked) { //TODO return modified data MITK_INFO << "Pipeline worked"; } request.reply(MitkRestStatusCodes::OK, answer); } \ No newline at end of file diff --git a/Modules/CppRestSdk/src/mitkRESTTest.cpp b/Modules/CppRestSdk/src/mitkRESTTest.cpp index e39e4c8b89..7117db74ee 100644 --- a/Modules/CppRestSdk/src/mitkRESTTest.cpp +++ b/Modules/CppRestSdk/src/mitkRESTTest.cpp @@ -1,30 +1,30 @@ #include "mitkRESTTest.h" #include #include #include mitk::RestTest::RestTest() {} mitk::RestTest::~RestTest() {} void mitk::RestTest::TestRESTServer() { us::ModuleContext *context = us::GetModuleContext(); auto managerRef = context->GetServiceReference(); if (managerRef) { auto managerService = context->GetService(managerRef); if (managerService) { - managerService->receiveRequest(L"http://localhost:8080/test/", this); + managerService->receiveRequest(L"http://localhost:8080/test", this); managerService->receiveRequest(L"http://localhost:8080/example", this); managerService->receiveRequest(L"http://localhost:8090", this); } } } //TODO get Data, return modified Data bool mitk::RestTest::notify() { return true; }