Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
cxWebServerWidget.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxWebServerWidget.h"
13 
14 #include <qhttpserver.h>
15 #include <qhttprequest.h>
16 #include <qhttpresponse.h>
17 #include "cxHttpRequestHandler.h"
18 #include "cxRemoteAPI.h"
19 
20 #include "cxLogger.h"
21 
22 namespace cx
23 {
24 
26  WebServerWidgetBase(services, parent)
27 {
28  this->setObjectName("WebServerWidget");
29  this->setWindowTitle("Web Server");
30  this->setWhatsThis(this->defaultWhatsThis());
31 
32  this->initServer();
33 }
34 
36 {
37  this->shutdownServer();
38 }
39 
40 void WebServerWidget::initServer()
41 {
42  mPort = 8085;
43  CX_LOG_INFO() << QString("Starting REST server on port %1...").arg(mPort);
44  mServer = new QHttpServer(this);
45  mAPI.reset(new RemoteAPI(mServices));
46  mRequestHandler.reset(new HttpRequestHandler(mAPI));
47  connect(mServer, SIGNAL(newRequest(QHttpRequest*, QHttpResponse*)),
48  mRequestHandler.get(), SLOT(handle_request(QHttpRequest*, QHttpResponse*)));
49 
50 }
51 
52 void WebServerWidget::shutdownServer()
53 {
54  mRequestHandler.reset();
55  mAPI.reset();
56 
57  mServer->close();
58  delete mServer;
59  mServer = nullptr;
60  //Various crash at this point: pointer being freed was not allocated, or seg. fault.
61 }
62 
64 {
65  mServer->listen(mPort);
66 }
67 
69 {
70  mServer->close();
71 }
72 
73 QString WebServerWidget::defaultWhatsThis() const
74 {
75  return "<html>"
76  "<h3>WebServer plugin.</h3>"
77  "<p>Used to run a WebServer from CustusX</p>"
78  "</html>";
79 }
80 
81 
82 
83 } /* namespace cx */
virtual void startServer()
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:40
#define CX_LOG_INFO
Definition: cxLogger.h:96
virtual void stopServer()
WebServerWidget(cx::VisServicesPtr services, QWidget *parent=nullptr)
Namespace for all CustusX production code.