diff --git a/Modules/CppRestSdk/include/mitkIRESTManager.h b/Modules/CppRestSdk/include/mitkIRESTManager.h index cbf371e942..331bc6aeed 100644 --- a/Modules/CppRestSdk/include/mitkIRESTManager.h +++ b/Modules/CppRestSdk/include/mitkIRESTManager.h @@ -1,38 +1,37 @@ #ifndef mitkIRESTManager_h #define mitkIRESTManager_h #include "cpprest/uri.h" #include "cpprest/json.h" - #include #include #include - namespace mitk { class IRESTManager { public: virtual ~IRESTManager(); enum RequestType { get, post, put }; - virtual void SendRequest(RequestType type) = 0; + virtual pplx::task SendRequest(web::uri uri, RequestType type = get, web::json::value = NULL) = 0; virtual void ReceiveRequest(web::uri uri, IRESTObserver *observer) = 0; virtual web::json::value Handle(web::uri, web::json::value) = 0; - virtual void HandleDeleteObserver(IRESTObserver *observer) = 0; + //virtual void HandleDeleteObserver(IRESTObserver *observer) = 0; + virtual void HandleDeleteObserver(IRESTObserver *observer, web::uri = L"") = 0; }; } // namespace mitk MITK_DECLARE_SERVICE_INTERFACE(mitk::IRESTManager, "org.mitk.IRESTManager") #endif diff --git a/Modules/CppRestSdk/include/mitkRESTClientMicroService.h b/Modules/CppRestSdk/include/mitkRESTClientMicroService.h index 26e8fc9d08..500dc80359 100644 --- a/Modules/CppRestSdk/include/mitkRESTClientMicroService.h +++ b/Modules/CppRestSdk/include/mitkRESTClientMicroService.h @@ -1,21 +1,39 @@ #ifndef mitkRESTClientMicroService_h #define mitkRESTClientMicroService_h +#include "cpprest/asyncrt_utils.h" +#include "cpprest/containerstream.h" +//#include "cpprest/filestream.h" #include "cpprest/http_client.h" +#include "cpprest/json.h" +#include "cpprest/producerconsumerstream.h" +#include "cpprest/uri.h" +#include "MitkCppRestSdkExports.h" +#include +#include +#include +#include typedef web::http::client::http_client MitkClient; +typedef web::http::http_request MitkRequest; +typedef web::http::http_response MitkResponse; +typedef web::http::methods MitkRESTMethods; +typedef web::http::uri_builder MitkUriBuilder; +typedef web::http::status_codes MitkRestStatusCodes; +typedef web::json::json_exception MitkJsonException; namespace mitk { class RESTClientMicroService { public: - RESTClientMicroService(utility::string_t url); + RESTClientMicroService(); ~RESTClientMicroService(); - void TestFunctionClient(); - + pplx::task Get(web::uri); + pplx::task PUT(web::uri, web::json::value content); + pplx::task POST(web::uri, web::json::value content); }; } // namespace mitk #endif // !mitkRESTClientMicroService_h diff --git a/Modules/CppRestSdk/include/mitkRESTManager.h b/Modules/CppRestSdk/include/mitkRESTManager.h index 48873c819a..4aa1201487 100644 --- a/Modules/CppRestSdk/include/mitkRESTManager.h +++ b/Modules/CppRestSdk/include/mitkRESTManager.h @@ -1,40 +1,41 @@ #ifndef mitkRESTManager_h #define mitkRESTManager_h #include #include #include #include #include #include #include namespace mitk { class MITKCPPRESTSDK_EXPORT RESTManager : public QObject, public IRESTManager { Q_OBJECT public: RESTManager(); ~RESTManager() override; // calls RESTClient - void SendRequest(RequestType type) override; + pplx::task SendRequest(web::uri uri, RequestType type = get, web::json::value = NULL) override; // calls RESTServer void ReceiveRequest(web::uri uri, IRESTObserver *observer) override; web::json::value Handle(web::uri, web::json::value) override; - virtual void HandleDeleteObserver(IRESTObserver *observer) override; + //virtual void HandleDeleteObserver(IRESTObserver *observer) override; + virtual void HandleDeleteObserver(IRESTObserver *observer, 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_ServerThreadMap; // Map with threads for servers std::map, IRESTObserver *> m_Observer; // Map with all observers }; } // namespace mitk #endif // !mitkRESTManager_h diff --git a/Modules/CppRestSdk/src/mitkRESTClientMicroService.cpp b/Modules/CppRestSdk/src/mitkRESTClientMicroService.cpp index fdddfb318d..e179efa84a 100644 --- a/Modules/CppRestSdk/src/mitkRESTClientMicroService.cpp +++ b/Modules/CppRestSdk/src/mitkRESTClientMicroService.cpp @@ -1,16 +1,86 @@ #include "mitkRESTClientMicroService.h" +#include "mitkRESTUtil.h" #include -mitk::RESTClientMicroService::RESTClientMicroService(utility::string_t url) +mitk::RESTClientMicroService::RESTClientMicroService() {} + +mitk::RESTClientMicroService::~RESTClientMicroService() {} + +pplx::task mitk::RESTClientMicroService::Get(web::uri uri) { + MitkClient *client = new MitkClient(uri); + + MITK_INFO << "Calling GET with " << utility::conversions::to_utf8string(uri.path()) << " on client " + << mitk::RESTUtil::convertToUtf8(uri.authority().to_string()); + + MitkRequest getRequest(MitkRESTMethods::GET); + // getRequest.set_request_uri(uri); + return client->request(getRequest).then([=](MitkResponse response) { + auto status = response.status_code(); + MITK_INFO << " status: " << status; + + if (status != web::http::status_codes::OK) + { + mitkThrow() << "response was not OK"; + } + return response.extract_json().get(); + }); + // answer.wait(); + // return answer.get(); } -mitk::RESTClientMicroService::~RESTClientMicroService() +pplx::task mitk::RESTClientMicroService::PUT(web::uri uri, web::json::value content) { + MitkClient *client = new MitkClient(uri); + MITK_INFO << "Calling PUT with " << utility::conversions::to_utf8string(uri.path()) << " on client " + << mitk::RESTUtil::convertToUtf8(uri.authority().to_string()); + + MitkRequest putRequest(MitkRESTMethods::PUT); + // putRequest.set_request_uri(uri); + putRequest.set_body(content); + + return client->request(putRequest).then([=](MitkResponse response) { + auto status = response.status_code(); + MITK_INFO << " status: " << status; + + if (status != web::http::status_codes::OK) + { + mitkThrow() << "response was not OK"; + } + + return response.extract_json().get(); + }); } -void mitk::RESTClientMicroService::TestFunctionClient() +pplx::task mitk::RESTClientMicroService::POST(web::uri uri, web::json::value content) { - MITK_INFO << "Test for Client"; -} \ No newline at end of file + MitkClient *client = new MitkClient(uri); + MITK_INFO << "Calling POST with " << utility::conversions::to_utf8string(uri.path()) << " on client " + << mitk::RESTUtil::convertToUtf8(uri.authority().to_string()); + + MitkRequest postRequest(MitkRESTMethods::POST); + if (content != NULL) + { + postRequest.set_body(content); + } + + return client->request(postRequest).then([=](MitkResponse response) { + try + { + auto status = response.status_code(); + MITK_INFO << " status: " << status; + + if (status != web::http::status_codes::Created) + { + mitkThrow() << "response was not OK"; + } + + return response.extract_json().get(); + } + catch (...) + { + MITK_WARN << "Exception in Client Class"; + } + }); +} diff --git a/Modules/CppRestSdk/src/mitkRESTManager.cpp b/Modules/CppRestSdk/src/mitkRESTManager.cpp index 9521f14d35..e6729089cb 100644 --- a/Modules/CppRestSdk/src/mitkRESTManager.cpp +++ b/Modules/CppRestSdk/src/mitkRESTManager.cpp @@ -1,159 +1,234 @@ #include "mitkRESTManager.h" #include mitk::RESTManager::RESTManager() {} mitk::RESTManager::~RESTManager() {} -void mitk::RESTManager::SendRequest(RequestType type) +pplx::task mitk::RESTManager::SendRequest(web::uri uri, RequestType type, web::json::value content) { + pplx::task answer; + auto client = new RESTClientMicroService(); switch (type) { case get: - // Call get in mitkRESTClientMicroService + { + answer = client->Get(uri); break; + } case post: - // Call post in mitkRESTClientMicroService + { + if (content == NULL) + { + MITK_WARN << "Content for put is empty, this will create an empty ressource"; + } + answer = client->POST(uri, content); + break; + } break; case put: - // Call put in mitkRESTClientMicroService + { + if (content == NULL) + { + MITK_WARN << "Content for put is empty, this will empty the ressource"; + } + answer = client->PUT(uri,content); break; + } } + + return answer; } void mitk::RESTManager::ReceiveRequest(web::uri uri, mitk::IRESTObserver *observer) { // 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 if (m_ServerMap.count(port) == 0) { // 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.path(); std::string uristring(uristringt.begin(), uristringt.end()); MITK_INFO <<"[" <moveToThread(m_ServerThreadMap[port]); connect(m_ServerThreadMap[port], &QThread::finished, server, &QObject::deleteLater); //starting Server m_ServerThreadMap[port]->start(); QMetaObject::invokeMethod(server, "OpenListener"); utility::string_t host = uri.authority().to_string(); std::string hoststring(host.begin(), host.end()); MITK_INFO << "new server" << hoststring << " at port" << port; } // If there is already a server under this port else { // Same host, means new observer but not a new server instance if (m_ServerMap[port]->GetUri() == uri.authority()) { // new observer has to be added std::pair key(uri.port(), uri.path()); //only add a new observer if there isn't already an observer for this uri if (m_Observer.count(key) == 0) { m_Observer[key] = observer; // testing if entry has been added to map utility::string_t uristringt = uri.path(); std::string uristring(uristringt.begin(), uristringt.end()); MITK_INFO << "[" << uri.port() << ", " << uristring<< "] : Number of elements in map: " << m_Observer.count(key); // info output MITK_INFO << "started listening, no new server instance has been created"; } else { MITK_ERROR << "Threre is already a observer handeling this data"; } } // Error, since another server can't be added under this port else { MITK_ERROR << "There is already another server listening under this port"; } } } web::json::value mitk::RESTManager::Handle(web::uri uri, web::json::value data) { // 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) { MITK_INFO << "Manager: Data send to observer"; return m_Observer[key]->Notify(data); } //No map under this port, delete Server Object to release port for other servers else { MITK_WARN << "No Observer can handle the data"; return NULL; } } -void mitk::RESTManager::HandleDeleteObserver(mitk::IRESTObserver *observer) +//void mitk::RESTManager::HandleDeleteObserver(mitk::IRESTObserver *observer) +//{ +// for (auto it = m_Observer.begin(); it != m_Observer.end();) +// { +// mitk::IRESTObserver *obsMap = it->second; +// //Check weather observer is at this place in map +// if (obsMap == observer) +// { +// // if yes +// // 1. store port in a temporary variable +// int port = it->first.first; +// utility::string_t path = it->first.second; +// std::pair key(port, path); +// MITK_INFO << "Number of elements at key [ " << port << ", " << std::string(key.second.begin(), key.second.end()) +// << "]: " << m_Observer.count(key); +// // 2. delete map entry +// it = m_Observer.erase(it); +// MITK_INFO << "Number of elements at key [ " << port << ", " << std::string(key.second.begin(), key.second.end()) +// << "]: " << m_Observer.count(key); +// // 3. check, if there is another observer under this port in observer map (with bool flag) +// bool noObserverForPort = true; +// for (auto o : m_Observer) +// { +// if (o.first.first == port) +// { +// //there still exists an observer for this port +// noObserverForPort = false; +// } +// } +// if (noObserverForPort) +// { +// // there isn't an observer at this port, delete m_ServerMap entry for this port +// //close listener +// QMetaObject::invokeMethod(m_ServerMap[port], "CloseListener"); +// //end thread +// m_ServerThreadMap[port]->quit(); +// m_ServerThreadMap[port]->wait(); +// //delete server from map +// m_ServerMap.erase(port); +// } +// } +// else +// { +// ++it; +// } +// } +//} + +void mitk::RESTManager::HandleDeleteObserver(IRESTObserver *observer, web::uri uri=L"") { for (auto it = m_Observer.begin(); it != m_Observer.end();) { mitk::IRESTObserver *obsMap = it->second; - //Check weather observer is at this place in map + // Check wether observer is at this place in map if (obsMap == observer) { - // if yes - // 1. store port in a temporary variable - int port = it->first.first; - utility::string_t path = it->first.second; - std::pair key(port, path); - MITK_INFO << "Number of elements at key [ " << port << ", " << std::string(key.second.begin(), key.second.end()) - << "]: " << m_Observer.count(key); - // 2. delete map entry - it = m_Observer.erase(it); - MITK_INFO << "Number of elements at key [ " << port << ", " << std::string(key.second.begin(), key.second.end()) - << "]: " << m_Observer.count(key); - // 3. check, if there is another observer under this port in observer map (with bool flag) - bool noObserverForPort = true; - for (auto o : m_Observer) + //Check wether it is the right uri to be deleted + if (uri==L""||it->first.second == uri.path()) { - if (o.first.first == port) + // if yes + // 1. store port in a temporary variable + int port = it->first.first; + utility::string_t path = it->first.second; + std::pair key(port, path); + MITK_INFO << "Number of elements at key [ " << port << ", " << std::string(key.second.begin(), key.second.end()) + << "]: " << m_Observer.count(key); + // 2. delete map entry + it = m_Observer.erase(it); + MITK_INFO << "Number of elements at key [ " << port << ", " << std::string(key.second.begin(), key.second.end()) + << "]: " << m_Observer.count(key); + // 3. check, if there is another observer under this port in observer map (with bool flag) + bool noObserverForPort = true; + for (auto o : m_Observer) { - //there still exists an observer for this port - noObserverForPort = false; + if (o.first.first == port) + { + // there still exists an observer for this port + noObserverForPort = false; + } } - } - if (noObserverForPort) + if (noObserverForPort) + { + // there isn't an observer at this port, delete m_ServerMap entry for this port + // close listener + QMetaObject::invokeMethod(m_ServerMap[port], "CloseListener"); + // end thread + m_ServerThreadMap[port]->quit(); + m_ServerThreadMap[port]->wait(); + // delete server from map + m_ServerMap.erase(port); + } + } + else { - // there isn't an observer at this port, delete m_ServerMap entry for this port - //close listener - QMetaObject::invokeMethod(m_ServerMap[port], "CloseListener"); - //end thread - m_ServerThreadMap[port]->quit(); - m_ServerThreadMap[port]->wait(); - //delete server from map - m_ServerMap.erase(port); - } + ++it; + } } else { ++it; } } } diff --git a/Plugins/PluginList.cmake b/Plugins/PluginList.cmake index d8426ee60b..a6919629a4 100644 --- a/Plugins/PluginList.cmake +++ b/Plugins/PluginList.cmake @@ -1,116 +1,117 @@ # Plug-ins must be ordered according to their dependencies set(MITK_PLUGINS org.blueberry.core.runtime:ON org.blueberry.core.expressions:OFF org.blueberry.core.commands:OFF org.blueberry.core.jobs:OFF org.blueberry.ui.qt:OFF org.blueberry.ui.qt.help:ON org.blueberry.ui.qt.log:ON org.blueberry.ui.qt.objectinspector:OFF #org.blueberry.test:ON #org.blueberry.uitest:ON #Testing/org.blueberry.core.runtime.tests:ON #Testing/org.blueberry.osgi.tests:ON org.mitk.core.services:ON org.mitk.gui.common:ON org.mitk.planarfigure:ON org.mitk.core.ext:OFF org.mitk.core.jobs:OFF org.mitk.gui.qt.application:ON org.mitk.gui.qt.coreapplication:OFF org.mitk.gui.qt.ext:OFF org.mitk.gui.qt.extapplication:OFF org.mitk.gui.qt.common:ON org.mitk.gui.qt.stdmultiwidgeteditor:ON org.mitk.gui.qt.common.legacy:OFF org.mitk.gui.qt.cmdlinemodules:OFF org.mitk.gui.qt.diffusionimagingapp:OFF org.mitk.gui.qt.datamanager:ON org.mitk.gui.qt.datamanagerlight:OFF org.mitk.gui.qt.datastorageviewertest:OFF org.mitk.gui.qt.properties:ON org.mitk.gui.qt.basicimageprocessing:OFF org.mitk.gui.qt.dicom:OFF org.mitk.gui.qt.dicominspector:OFF org.mitk.gui.qt.diffusionimaging:OFF org.mitk.gui.qt.diffusionimaging.connectomics:OFF org.mitk.gui.qt.diffusionimaging.denoising:OFF org.mitk.gui.qt.diffusionimaging.fiberfox:OFF org.mitk.gui.qt.diffusionimaging.fiberprocessing:OFF org.mitk.gui.qt.diffusionimaging.ivim:OFF org.mitk.gui.qt.diffusionimaging.odfpeaks:OFF org.mitk.gui.qt.diffusionimaging.partialvolume:OFF org.mitk.gui.qt.diffusionimaging.preprocessing:OFF org.mitk.gui.qt.diffusionimaging.reconstruction:OFF org.mitk.gui.qt.diffusionimaging.registration:OFF org.mitk.gui.qt.diffusionimaging.tbss:OFF org.mitk.gui.qt.diffusionimaging.tractography:OFF org.mitk.gui.qt.diffusionimaging.python:OFF org.mitk.gui.qt.dosevisualization:OFF org.mitk.gui.qt.geometrytools:OFF org.mitk.gui.qt.igtexamples:OFF org.mitk.gui.qt.igttracking:OFF org.mitk.gui.qt.lasercontrol:OFF org.mitk.gui.qt.openigtlink:OFF org.mitk.gui.qt.imagecropper:OFF org.mitk.gui.qt.imagenavigator:ON org.mitk.gui.qt.viewnavigator:OFF org.mitk.gui.qt.materialeditor:OFF org.mitk.gui.qt.measurementtoolbox:OFF org.mitk.gui.qt.moviemaker:OFF org.mitk.gui.qt.pointsetinteraction:OFF org.mitk.gui.qt.pointsetinteractionmultispectrum:OFF org.mitk.gui.qt.python:OFF org.mitk.gui.qt.remeshing:OFF org.mitk.gui.qt.segmentation:OFF org.mitk.gui.qt.segmentation.rework:OFF org.mitk.gui.qt.aicpregistration:OFF org.mitk.gui.qt.renderwindowmanager:OFF org.mitk.gui.qt.toftutorial:OFF org.mitk.gui.qt.tofutil:OFF org.mitk.gui.qt.tubegraph:OFF org.mitk.gui.qt.ugvisualization:OFF org.mitk.gui.qt.photoacoustics.pausviewer:OFF org.mitk.gui.qt.photoacoustics.pausmotioncompensation:OFF org.mitk.gui.qt.photoacoustics.imageprocessing:OFF org.mitk.gui.qt.photoacoustics.simulation:OFF org.mitk.gui.qt.photoacoustics.spectralunmixing:OFF org.mitk.gui.qt.ultrasound:OFF org.mitk.gui.qt.volumevisualization:OFF org.mitk.gui.qt.eventrecorder:OFF org.mitk.gui.qt.xnat:OFF org.mitk.gui.qt.igt.app.echotrack:OFF org.mitk.gui.qt.spectrocamrecorder:OFF org.mitk.gui.qt.classificationsegmentation:OFF org.mitk.gui.qt.overlaymanager:OFF org.mitk.gui.qt.igt.app.hummelprotocolmeasurements:OFF org.mitk.gui.qt.multilabelsegmentation:OFF org.mitk.matchpoint.core.helper:OFF org.mitk.gui.qt.matchpoint.algorithm.browser:OFF org.mitk.gui.qt.matchpoint.algorithm.control:OFF org.mitk.gui.qt.matchpoint.algorithm.batch:OFF org.mitk.gui.qt.matchpoint.mapper:OFF org.mitk.gui.qt.matchpoint.framereg:OFF org.mitk.gui.qt.matchpoint.visualizer:OFF org.mitk.gui.qt.matchpoint.evaluator:OFF org.mitk.gui.qt.matchpoint.manipulator:OFF org.mitk.gui.qt.preprocessing.resampling:OFF org.mitk.gui.qt.radiomics:OFF org.mitk.gui.qt.cest:OFF org.mitk.gui.qt.fit.demo:OFF org.mitk.gui.qt.fit.inspector:OFF org.mitk.gui.qt.fit.genericfitting:OFF org.mitk.gui.qt.pharmacokinetics.mri:OFF org.mitk.gui.qt.pharmacokinetics.pet:OFF org.mitk.gui.qt.pharmacokinetics.simulation:OFF org.mitk.gui.qt.pharmacokinetics.curvedescriptor:OFF org.mitk.gui.qt.pharmacokinetics.concentration.mri:OFF org.mitk.gui.qt.thread:ON + org.mitk.gui.qt.client:ON ) diff --git a/Plugins/org.mitk.gui.qt.client/CMakeLists.txt b/Plugins/org.mitk.gui.qt.client/CMakeLists.txt new file mode 100644 index 0000000000..f5afc5a21e --- /dev/null +++ b/Plugins/org.mitk.gui.qt.client/CMakeLists.txt @@ -0,0 +1,7 @@ +project(org_mitk_gui_qt_client) + +mitk_create_plugin( + EXPORT_DIRECTIVE THREAD_EXPORT + EXPORTED_INCLUDE_SUFFIXES src + MODULE_DEPENDS PRIVATE MitkQtWidgets MitkCppRestSdk +) diff --git a/Plugins/org.mitk.gui.qt.client/files.cmake b/Plugins/org.mitk.gui.qt.client/files.cmake new file mode 100644 index 0000000000..70a828e79f --- /dev/null +++ b/Plugins/org.mitk.gui.qt.client/files.cmake @@ -0,0 +1,21 @@ +set(CPP_FILES + src/internal/PluginActivator.cpp + src/internal/QmitkClientView.cpp +) + +set(UI_FILES + src/internal/QmitkClientView.ui +) + +set(MOC_H_FILES + src/internal/PluginActivator.h + src/internal/QmitkClientView.h +) + +set(CACHED_RESOURCE_FILES + resources/ClientIcon.png + plugin.xml +) + +set(QRC_FILES +) diff --git a/Plugins/org.mitk.gui.qt.client/manifest_headers.cmake b/Plugins/org.mitk.gui.qt.client/manifest_headers.cmake new file mode 100644 index 0000000000..2b3732209f --- /dev/null +++ b/Plugins/org.mitk.gui.qt.client/manifest_headers.cmake @@ -0,0 +1,5 @@ +set(Plugin-Name "Client Plugin") +set(Plugin-Version "1.0") +set(Plugin-Vendor "CAMIC") +set(Plugin-ContactAddress "https://mitk.org") +set(Require-Plugin org.mitk.gui.qt.common) diff --git a/Plugins/org.mitk.gui.qt.client/plugin.xml b/Plugins/org.mitk.gui.qt.client/plugin.xml new file mode 100644 index 0000000000..8a7740c52f --- /dev/null +++ b/Plugins/org.mitk.gui.qt.client/plugin.xml @@ -0,0 +1,10 @@ + + + + + + diff --git a/Plugins/org.mitk.gui.qt.client/resources/ClientIcon.png b/Plugins/org.mitk.gui.qt.client/resources/ClientIcon.png new file mode 100644 index 0000000000..67039a6cfb Binary files /dev/null and b/Plugins/org.mitk.gui.qt.client/resources/ClientIcon.png differ diff --git a/Plugins/org.mitk.gui.qt.client/src/internal/PluginActivator.cpp b/Plugins/org.mitk.gui.qt.client/src/internal/PluginActivator.cpp new file mode 100644 index 0000000000..500358de6c --- /dev/null +++ b/Plugins/org.mitk.gui.qt.client/src/internal/PluginActivator.cpp @@ -0,0 +1,27 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "PluginActivator.h" +#include "QmitkClientView.h" + +void PluginActivator::start(ctkPluginContext* context) +{ + BERRY_REGISTER_EXTENSION_CLASS(QmitkClientView, context) +} + +void PluginActivator::stop(ctkPluginContext*) +{ +} diff --git a/Plugins/org.mitk.gui.qt.client/src/internal/PluginActivator.h b/Plugins/org.mitk.gui.qt.client/src/internal/PluginActivator.h new file mode 100644 index 0000000000..4a8cacfbb5 --- /dev/null +++ b/Plugins/org.mitk.gui.qt.client/src/internal/PluginActivator.h @@ -0,0 +1,33 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#ifndef PluginActivator_h +#define PluginActivator_h + +#include + +class PluginActivator : public QObject, public ctkPluginActivator +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org_mitk_gui_qt_exampleplugin") + Q_INTERFACES(ctkPluginActivator) + +public: + void start(ctkPluginContext* context); + void stop(ctkPluginContext* context); +}; + +#endif diff --git a/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.cpp b/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.cpp new file mode 100644 index 0000000000..85440f77e4 --- /dev/null +++ b/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.cpp @@ -0,0 +1,153 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "QmitkClientView.h" + +#include + +#include "cpprest/json.h" +#include +#include +#include +#include +#include +#include +#include + +const std::string QmitkClientView::VIEW_ID = "org.mitk.views.clientview"; + +QmitkClientView::QmitkClientView() : m_Ui(new Ui::QmitkClientView) +{ + mitk::ForceLinkage(); +} + +QmitkClientView::~QmitkClientView() +{ + delete m_Ui; +} + +void QmitkClientView::CreateQtPartControl(QWidget *parent) +{ + m_Ui->setupUi(parent); + + connect(m_Ui->getPushButton, &QPushButton::clicked, this, &QmitkClientView::OnGetButtonClicked); + connect(m_Ui->putPushButton, &QPushButton::clicked, this, &QmitkClientView::OnPutButtonClicked); + connect(m_Ui->postPushButton, &QPushButton::clicked, this, &QmitkClientView::OnPostButtonClicked); + connect(this, &QmitkClientView::UpdateProgressBar, this, &QmitkClientView::OnUpdateProgressBar); + connect(this, SIGNAL(UpdateLabel(QString)), this, SLOT(OnUpdateLabel(QString))); + m_Ui->progressBar->setValue(0); +} + +void QmitkClientView::OnUpdateProgressBar() +{ + m_Ui->progressBar->setValue(m_Ui->progressBar->value() + 5); +} + +void QmitkClientView::OnUpdateLabel(QString text) +{ + m_Ui->responseLabel->setText(text); +} + +void QmitkClientView::SetFocus() {} + +void QmitkClientView::OnGetButtonClicked() +{ + m_Ui->progressBar->setValue(0); + m_Ui->getPushButton->setDisabled(true); + us::ModuleContext *context = us::ModuleRegistry::GetModule(1)->GetModuleContext(); + + auto managerRef = context->GetServiceReference(); + if (managerRef) + { + auto managerService = context->GetService(managerRef); + if (managerService) + { + std::vector < pplx::task> tasks; + for (int i = 0; i < 20; i++) + { + pplx::task singleTask = + managerService->SendRequest(L"https://jsonplaceholder.typicode.com/photos") + .then([=](web::json::value result) + { + emit UpdateProgressBar(); + }); + tasks.push_back(singleTask); + } + auto joinTask = pplx::when_all(begin(tasks), end(tasks)); + joinTask.then([=]() { emit UpdateLabel("All tasks finished"); + }); + m_Ui->responseLabel->setText("Waiting for change"); + } + } + m_Ui->getPushButton->setEnabled(true); +} + +void QmitkClientView::OnPutButtonClicked() +{ + m_Ui->putPushButton->setDisabled(true); + us::ModuleContext *context = us::ModuleRegistry::GetModule(1)->GetModuleContext(); + + auto managerRef = context->GetServiceReference(); + if (managerRef) + { + auto managerService = context->GetService(managerRef); + if (managerService) + { + web::json::value data; + data[L"userId"] = web::json::value(1); + data[L"id"] = web::json::value(1); + data[L"title"] = web::json::value(U("this is a changed title")); + data[L"body"] = web::json::value(U("and the body is changed as well")); + managerService->SendRequest(L"https://jsonplaceholder.typicode.com/posts/1", mitk::IRESTManager::RequestType::put, data) + .then([=](web::json::value result) + { + utility::string_t stringT = result.to_string(); + std::string stringStd(stringT.begin(), stringT.end()); + QString stringQ = QString::fromStdString(stringStd); + emit UpdateLabel(stringQ); + }); + } + } + m_Ui->putPushButton->setEnabled(true); +} + +void QmitkClientView::OnPostButtonClicked() +{ + m_Ui->postPushButton->setDisabled(true); + us::ModuleContext *context = us::ModuleRegistry::GetModule(1)->GetModuleContext(); + + auto managerRef = context->GetServiceReference(); + if (managerRef) + { + auto managerService = context->GetService(managerRef); + if (managerService) + { + web::json::value data; + data[L"userId"] = web::json::value(1); + data[L"title"] = web::json::value(U("this is a new title")); + data[L"body"] = web::json::value(U("this is a new body")); + managerService + ->SendRequest(L"https://jsonplaceholder.typicode.com/posts", mitk::IRESTManager::RequestType::post, data) + .then([=](web::json::value result) { + utility::string_t stringT = result.to_string(); + std::string stringStd(stringT.begin(), stringT.end()); + QString stringQ = QString::fromStdString(stringStd); + emit UpdateLabel(stringQ); + }); + } + } + m_Ui->postPushButton->setEnabled(true); +} diff --git a/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.h b/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.h similarity index 60% copy from Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.h copy to Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.h index afbfd701c0..09d9d60b62 100644 --- a/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.h +++ b/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.h @@ -1,57 +1,62 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ -#ifndef QmitkThreadView_h -#define QmitkThreadView_h +#ifndef QmitkClientView_h +#define QmitkClientView_h #include -#include - - +#include "cpprest/uri.h" +#include +#include +#include namespace Ui { - class QmitkThreadView; + class QmitkClientView; } namespace mitk { class RESTManager; } -class QmitkThreadView : public QmitkAbstractView, public mitk::IRESTObserver +class QmitkClientView : public QmitkAbstractView { Q_OBJECT public: static const std::string VIEW_ID; - QmitkThreadView(); - ~QmitkThreadView() override; + QmitkClientView(); + ~QmitkClientView() override; void CreateQtPartControl(QWidget *parent) override; - web::json::value Notify(web::json::value data) override; +signals: + void UpdateProgressBar(); + void UpdateLabel(QString); private slots: - void OnStartButtonClicked(); - void OnStopButtonClicked(); + void OnGetButtonClicked(); + void OnPutButtonClicked(); + void OnPostButtonClicked(); + void OnUpdateProgressBar(); + void OnUpdateLabel(QString text); private: void SetFocus() override; - - Ui::QmitkThreadView *m_Ui; + Ui::QmitkClientView *m_Ui; }; #endif diff --git a/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.ui b/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.ui similarity index 56% copy from Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.ui copy to Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.ui index 90430d834d..7b3fa5cdfb 100644 --- a/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.ui +++ b/Plugins/org.mitk.gui.qt.client/src/internal/QmitkClientView.ui @@ -1,65 +1,79 @@ - QmitkThreadView - + QmitkClientView + 0 0 - 220 - 160 + 253 + 206 - Thread View + Client View - + + + + - Start Listening + GET - - - Process selected image + + + PUT + + + + - Stop Listening + POST + + + + + + + 24 - + - + This label changes after request true Qt::Vertical QSizePolicy::Expanding 20 220 diff --git a/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.cpp b/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.cpp index 6775892af1..9018601208 100644 --- a/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.cpp +++ b/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.cpp @@ -1,95 +1,148 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "QmitkThreadView.h" #include #include #include #include #include #include #include #include const std::string QmitkThreadView::VIEW_ID = "org.mitk.views.threadview"; QmitkThreadView::QmitkThreadView() : m_Ui(new Ui::QmitkThreadView) { mitk::ForceLinkage(); } web::json::value QmitkThreadView::Notify(web::json::value data) { MITK_INFO << "Observer: Data in observer"; return data.at(U("key 1")); } QmitkThreadView::~QmitkThreadView() { delete m_Ui; } void QmitkThreadView::CreateQtPartControl(QWidget* parent) { m_Ui->setupUi(parent); - m_Ui->stopPushButton->setDisabled(true); - connect(m_Ui->startPushButton, &QPushButton::clicked, this, &QmitkThreadView::OnStartButtonClicked); - connect(m_Ui->stopPushButton, &QPushButton::clicked, this, &QmitkThreadView::OnStopButtonClicked); + connect(m_Ui->stopAllPushButton, &QPushButton::clicked, this, &QmitkThreadView::OnStopAllButtonClicked); + + connect(m_Ui->testListenCheckBox, &QCheckBox::clicked, this, &QmitkThreadView::OnTestListenCheckBoxClicked); + connect(m_Ui->exampleListenCheckBox, &QCheckBox::clicked, this, &QmitkThreadView::OnExampleListenCheckBoxClicked); + connect(m_Ui->port8090CheckBox, &QCheckBox::clicked, this, &QmitkThreadView::OnPort8090ListenCheckBoxClicked); + + connect(m_Ui->stopAllPushButton, &QPushButton::clicked, this, &QmitkThreadView::OnStopAllButtonClicked); } void QmitkThreadView::SetFocus() { - m_Ui->startPushButton->setFocus(); } -void QmitkThreadView::OnStartButtonClicked() + +void QmitkThreadView::StartListening(web::uri uri) { - us::ModuleContext *context = us::ModuleRegistry::GetModule(1)->GetModuleContext(); + us::ModuleContext *context = us::ModuleRegistry::GetModule(1)->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:8090", this); - managerService->ReceiveRequest(L"http://localhost:8080/example", this); + managerService->ReceiveRequest(uri, this); } } - m_Ui->stopPushButton->setEnabled(true); } -void QmitkThreadView::OnStopButtonClicked() +void QmitkThreadView::StopListening(web::uri uri) +{ + us::ModuleContext *context = us::ModuleRegistry::GetModule(1)->GetModuleContext(); + + auto managerRef = context->GetServiceReference(); + if (managerRef) + { + auto managerService = context->GetService(managerRef); + if (managerService) + { + managerService->HandleDeleteObserver(this,uri); + } + } +} + +void QmitkThreadView::OnStopAllButtonClicked() { us::ModuleContext *context = us::ModuleRegistry::GetModule(1)->GetModuleContext(); auto managerRef = context->GetServiceReference(); if (managerRef) { auto managerService = context->GetService(managerRef); if (managerService) { managerService->HandleDeleteObserver(this); } } - m_Ui->stopPushButton->setDisabled(true); - m_Ui->startPushButton->setEnabled(true); + m_Ui->testListenCheckBox->setChecked(false); + m_Ui->exampleListenCheckBox->setChecked(false); + m_Ui->port8090CheckBox->setChecked(false); +} + +void QmitkThreadView::OnTestListenCheckBoxClicked() +{ + if (m_Ui->testListenCheckBox->isChecked()) + { + StartListening(L"http://localhost:8080/test"); + } + else + { + StopListening(L"http://localhost:8080/test"); + } +} + +void QmitkThreadView::OnExampleListenCheckBoxClicked() +{ + if (m_Ui->exampleListenCheckBox->isChecked()) + { + StartListening(L"http://localhost:8080/example"); + } + else + { + StopListening(L"http://localhost:8080/example"); + } +} + +void QmitkThreadView::OnPort8090ListenCheckBoxClicked() +{ + if (m_Ui->port8090CheckBox->isChecked()) + { + StartListening(L"http://localhost:8090"); + } + else + { + StopListening(L"http://localhost:8090"); + } } diff --git a/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.h b/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.h index afbfd701c0..ea5f94c263 100644 --- a/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.h +++ b/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.h @@ -1,57 +1,60 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef QmitkThreadView_h #define QmitkThreadView_h #include #include - +#include "cpprest/uri.h" namespace Ui { class QmitkThreadView; } namespace mitk { class RESTManager; } class QmitkThreadView : public QmitkAbstractView, public mitk::IRESTObserver { Q_OBJECT public: static const std::string VIEW_ID; QmitkThreadView(); ~QmitkThreadView() override; void CreateQtPartControl(QWidget *parent) override; web::json::value Notify(web::json::value data) override; private slots: - void OnStartButtonClicked(); - void OnStopButtonClicked(); + void OnStopAllButtonClicked(); + void OnTestListenCheckBoxClicked(); + void OnExampleListenCheckBoxClicked(); + void OnPort8090ListenCheckBoxClicked(); private: void SetFocus() override; - + void StartListening(web::uri); + void StopListening(web::uri); Ui::QmitkThreadView *m_Ui; }; #endif diff --git a/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.ui b/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.ui index 90430d834d..9d77b87bec 100644 --- a/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.ui +++ b/Plugins/org.mitk.gui.qt.thread/src/internal/QmitkThreadView.ui @@ -1,65 +1,76 @@ QmitkThreadView 0 0 - 220 - 160 + 253 + 206 Thread View - + - Start Listening + http://localhost:8080/test - - - Process selected image + + + http://localhost:8080/example + + + + + + http://localhost:8090 + + + + + - Stop Listening + Stop Listening all - + true Qt::Vertical QSizePolicy::Expanding 20 220