ctkDicomAbstractApp.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. // CTK includes
  16. #include "ctkDicomAbstractApp.h"
  17. #include <ctkDicomHostInterface.h>
  18. #include <ctkDicomObjectLocatorCache.h>
  19. #include <ctkPluginContext.h>
  20. #include <ctkServiceTracker.h>
  21. #include <ctkDicomAppHostingTypesHelper.h>
  22. class ctkDicomAbstractAppPrivate
  23. {
  24. public:
  25. ctkDicomAbstractAppPrivate(ctkPluginContext* context);
  26. ctkServiceTracker<ctkDicomHostInterface*> HostTracker;
  27. ctkDicomAppHosting::State currentState;
  28. ctkDicomObjectLocatorCache ObjectLocatorCache;
  29. };
  30. //----------------------------------------------------------------------------
  31. // ctkDicomAbstractAppPrivate methods
  32. //----------------------------------------------------------------------------
  33. ctkDicomAbstractAppPrivate::ctkDicomAbstractAppPrivate(ctkPluginContext * context):HostTracker(context),currentState(ctkDicomAppHosting::IDLE)
  34. {
  35. //perhaps notStarted or some dummy state instead of IDLE?
  36. }
  37. //----------------------------------------------------------------------------
  38. // ctkDicomAbstractApp methods
  39. //----------------------------------------------------------------------------
  40. ctkDicomAbstractApp::ctkDicomAbstractApp(ctkPluginContext* context) : d_ptr(new ctkDicomAbstractAppPrivate(context))
  41. {
  42. d_ptr->HostTracker.open();
  43. }
  44. //----------------------------------------------------------------------------
  45. ctkDicomAbstractApp::~ctkDicomAbstractApp()
  46. {
  47. }
  48. //----------------------------------------------------------------------------
  49. bool ctkDicomAbstractApp::setState(ctkDicomAppHosting::State newState)
  50. {
  51. qDebug()<<"treating new state: "<< ctkDicomSoapState::toStringValue(newState);
  52. bool result = false;
  53. //received a new state,
  54. switch (newState){
  55. case ctkDicomAppHosting::IDLE:
  56. if (d_ptr->currentState == ctkDicomAppHosting::COMPLETED)
  57. {
  58. emit releaseResources();
  59. result = true;
  60. }
  61. break;
  62. case ctkDicomAppHosting::INPROGRESS:
  63. if (d_ptr->currentState == ctkDicomAppHosting::IDLE)
  64. {
  65. emit startProgress();
  66. result = true;
  67. }
  68. else if(d_ptr->currentState == ctkDicomAppHosting::SUSPENDED)
  69. {
  70. emit resumeProgress();
  71. result = true;
  72. }
  73. break;
  74. case ctkDicomAppHosting::COMPLETED:
  75. qDebug() << "Hosting system shouldn't send completed";
  76. break;
  77. case ctkDicomAppHosting::SUSPENDED:
  78. //suspend computation, release as much resource as possible with possible resuming of computation
  79. emit suspendProgress();
  80. result = true;
  81. break;
  82. case ctkDicomAppHosting::CANCELED:
  83. //stop and release everything.
  84. if (d_ptr->currentState == ctkDicomAppHosting::INPROGRESS
  85. || d_ptr->currentState == ctkDicomAppHosting::SUSPENDED)
  86. {
  87. //special state, a transitional state, so we notify straight away the new state.
  88. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::CANCELED);
  89. d_ptr->currentState = ctkDicomAppHosting::CANCELED;
  90. //releasing resources
  91. emit cancelProgress();
  92. result = true;
  93. }
  94. break;
  95. case ctkDicomAppHosting::EXIT:
  96. //check if current state is IDLE
  97. if (d_ptr->currentState == ctkDicomAppHosting::IDLE)
  98. {
  99. //maybe not useful:
  100. getHostInterface()->notifyStateChanged(ctkDicomAppHosting::EXIT);
  101. emit exitHostedApp();
  102. result = true;
  103. }
  104. break;
  105. default:
  106. //should never happen
  107. qDebug() << "unexisting state Code, do nothing";
  108. }
  109. if (!result)
  110. {
  111. qDebug()<<"illegal transition to: "<< static_cast<int>(newState) <<
  112. "Current state is:" << static_cast<int>(d_ptr->currentState);
  113. qDebug()<<"illegal transition to: "<< ctkDicomSoapState::toStringValue(newState) <<
  114. "Current state is:" << ctkDicomSoapState::toStringValue(d_ptr->currentState);
  115. }
  116. return result;
  117. }
  118. //----------------------------------------------------------------------------
  119. ctkDicomHostInterface* ctkDicomAbstractApp::getHostInterface() const
  120. {
  121. ctkDicomHostInterface* host = d_ptr->HostTracker.getService();
  122. if (!host) throw std::runtime_error("DICOM Host Interface not available");
  123. return host;
  124. }
  125. //----------------------------------------------------------------------------
  126. ctkDicomAppHosting::State ctkDicomAbstractApp::getState()
  127. {
  128. return d_ptr->currentState;
  129. }
  130. void ctkDicomAbstractApp::setInternalState(ctkDicomAppHosting::State state)
  131. {
  132. d_ptr->currentState = state;
  133. }
  134. //----------------------------------------------------------------------------
  135. QList<ctkDicomAppHosting::ObjectLocator> ctkDicomAbstractApp::getData(
  136. const QList<QUuid>& objectUUIDs,
  137. const QList<QString>& acceptableTransferSyntaxUIDs,
  138. bool includeBulkData)
  139. {
  140. Q_UNUSED(acceptableTransferSyntaxUIDs);
  141. Q_UNUSED(includeBulkData);
  142. return this->objectLocatorCache()->getData(objectUUIDs);
  143. }
  144. //----------------------------------------------------------------------------
  145. ctkDicomObjectLocatorCache* ctkDicomAbstractApp::objectLocatorCache()const
  146. {
  147. Q_D(const ctkDicomAbstractApp);
  148. return const_cast<ctkDicomObjectLocatorCache*>(&d->ObjectLocatorCache);
  149. }
  150. //----------------------------------------------------------------------------
  151. bool ctkDicomAbstractApp::publishData(const ctkDicomAppHosting::AvailableData& availableData, bool lastData)
  152. {
  153. if (!this->objectLocatorCache()->isCached(availableData))
  154. {
  155. return false;
  156. }
  157. bool success = this->getHostInterface()->notifyDataAvailable(availableData, lastData);
  158. if(!success)
  159. {
  160. return false;
  161. }
  162. return true;
  163. }