ctkDicomAppServer.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. // Qt includes
  16. #include <QHostAddress>
  17. // CTK includes
  18. #include "ctkDicomAppServer_p.h"
  19. #include "ctkDicomAppPlugin_p.h"
  20. #include "ctkAppSoapMessageProcessor.h"
  21. #include <ctkDicomAppHostingTypesHelper.h>
  22. #include <ctkDicomAppInterface.h>
  23. #include <ctkExchangeSoapMessageProcessor.h>
  24. #include <ctkServiceReference.h>
  25. // STD includes
  26. #include <stdexcept>
  27. //----------------------------------------------------------------------------
  28. ctkDicomAppServer::ctkDicomAppServer(int port, QString path)
  29. : AppInterfaceRegistered(false), Port(port), Path(path),
  30. AppInterfaceTracker(ctkDicomAppPlugin::getPluginContext(), this)
  31. {
  32. this->AppInterfaceTracker.open();
  33. connect(&this->Server, SIGNAL(incomingSoapMessage(QtSoapMessage,QtSoapMessage*)),
  34. this, SLOT(incomingSoapMessage(QtSoapMessage,QtSoapMessage*)));
  35. connect(&this->Server, SIGNAL(incomingWSDLMessage(QString,QString*)),
  36. this, SLOT(incomingWSDLMessage(QString,QString*)));
  37. if (!this->Server.listen(QHostAddress::LocalHost, this->Port))
  38. {
  39. qCritical() << "Listening to 127.0.0.1:" << port << " failed.";
  40. }
  41. }
  42. //----------------------------------------------------------------------------
  43. ctkDicomAppServer::~ctkDicomAppServer()
  44. {
  45. this->Server.close ();
  46. }
  47. //----------------------------------------------------------------------------
  48. void ctkDicomAppServer::incomingWSDLMessage(
  49. const QString& message, QString* reply)
  50. {
  51. if (message == "?wsdl")
  52. {
  53. QFile wsdlfile(":/dah/ApplicationService-20100825.wsdl");
  54. wsdlfile.open(QFile::ReadOnly | QFile::Text);
  55. if(wsdlfile.isOpen())
  56. {
  57. QTextStream textstream(&wsdlfile);
  58. *reply = textstream.readAll();
  59. QString actualURL="http://localhost:";
  60. //actualURL+=QString::number(Port)+"/ApplicationInterface"; // FIXME: has to be replaced by url provided by host
  61. actualURL+=QString::number(Port)+Path;
  62. reply->replace("REPLACE_WITH_ACTUAL_URL",actualURL);
  63. reply->replace("ApplicationService-20100825.xsd",actualURL+"?xsd=1");
  64. //reply->replace("<soap:body use=\"literal\"/>","<soap:body use=\"literal\"></soap:body>");
  65. }
  66. }
  67. else if (message == "?xsd=1")
  68. {
  69. QFile wsdlfile(":/dah/HostService-20100825.xsd");
  70. wsdlfile.open(QFile::ReadOnly | QFile::Text);
  71. if(wsdlfile.isOpen())
  72. {
  73. QTextStream textstream(&wsdlfile);
  74. *reply = textstream.readAll();
  75. }
  76. }
  77. }
  78. //----------------------------------------------------------------------------
  79. void ctkDicomAppServer::incomingSoapMessage(
  80. const QtSoapMessage& message,
  81. QtSoapMessage* reply)
  82. {
  83. QMutexLocker lock(&this->Mutex);
  84. this->Processors.process(message, reply);
  85. }
  86. //----------------------------------------------------------------------------
  87. ctkDicomAppInterface* ctkDicomAppServer::addingService(const ctkServiceReference& reference)
  88. {
  89. QMutexLocker lock(&this->Mutex);
  90. if (this->AppInterfaceRegistered)
  91. {
  92. //TODO maybe use ctkLogService
  93. qWarning() << "A ctkDicomAppInterface service has already been added";
  94. return 0;
  95. }
  96. this->AppInterfaceRegistered = true;
  97. ctkDicomAppInterface* appInterface = ctkDicomAppPlugin::getPluginContext()->getService<ctkDicomAppInterface>(reference);
  98. this->Processors.push_back(new ctkAppSoapMessageProcessor(appInterface));
  99. this->Processors.push_back(new ctkExchangeSoapMessageProcessor(appInterface));
  100. return appInterface;
  101. }
  102. //----------------------------------------------------------------------------
  103. void ctkDicomAppServer::modifiedService(const ctkServiceReference& reference, ctkDicomAppInterface* service)
  104. {
  105. Q_UNUSED(reference)
  106. Q_UNUSED(service)
  107. // do nothing
  108. }
  109. //----------------------------------------------------------------------------
  110. void ctkDicomAppServer::removedService(const ctkServiceReference& reference, ctkDicomAppInterface* service)
  111. {
  112. Q_UNUSED(reference)
  113. Q_UNUSED(service)
  114. QMutexLocker lock(&this->Mutex);
  115. this->AppInterfaceRegistered = false;
  116. this->Processors.clear();
  117. }