ctkExampleHostedAppMain.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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. #include <ctkPluginFrameworkFactory.h>
  16. #include <ctkPluginFramework.h>
  17. #include <ctkPluginException.h>
  18. // for testing purposes use:
  19. // --hostURL http://localhost:8081/host --applicationURL http://localhost:8082/app dicomapp
  20. // replace "//$" with nothing as soon as ctkDicomAppServer/ctkDicomHostService exist
  21. #include <ctkDicomAppServer.h>
  22. //$#include <ctkDicomHostService.h>
  23. #include <QApplication>
  24. #include <QString>
  25. #include <QStringList>
  26. #include <QDirIterator>
  27. #include <QWidget>
  28. #include <QFileInfo>
  29. void print_usage()
  30. {
  31. qCritical() << "Usage:";
  32. qCritical() << " " << QFileInfo(qApp->arguments().at(0)).fileName() << " --hostURL url1 --applicationURL url2 <plugin-name>";
  33. }
  34. int main(int argv, char** argc)
  35. {
  36. QApplication app(argv, argc);
  37. qApp->setOrganizationName("CTK");
  38. qApp->setOrganizationDomain("commontk.org");
  39. qApp->setApplicationName("ctkExampleHost");
  40. // parse the command line
  41. if(qApp->arguments().size() < 5)
  42. {
  43. qCritical() << "Wrong number of command line arguments.";
  44. print_usage();
  45. exit(1);
  46. }
  47. if(qApp->arguments().at(1) != "--hostURL")
  48. {
  49. qCritical() << "First argument must be --hostURL.";
  50. print_usage();
  51. exit(1);
  52. }
  53. QString hostURL = qApp->arguments().at(2);
  54. if(qApp->arguments().at(3) != "--applicationURL")
  55. {
  56. qCritical() << "First argument must be --applicationURL.";
  57. print_usage();
  58. exit(1);
  59. }
  60. QString appURL = qApp->arguments().at(4);
  61. // setup the plugin framework
  62. ctkPluginFrameworkFactory fwFactory;
  63. ctkPluginFramework* framework = fwFactory.getFramework();
  64. try {
  65. framework->init();
  66. }
  67. catch (const ctkPluginException& exc)
  68. {
  69. qCritical() << "Failed to initialize the plug-in framework:" << exc;
  70. exit(2);
  71. }
  72. #ifdef CMAKE_INTDIR
  73. QString pluginPath = qApp->applicationDirPath() + "/../plugins/" CMAKE_INTDIR "/";
  74. #else
  75. QString pluginPath = qApp->applicationDirPath() + "/plugins/";
  76. #endif
  77. qApp->addLibraryPath(pluginPath);
  78. // construct the name of the plugin with the business logic
  79. // (thus the actual logic of the hosted app)
  80. QString pluginName;
  81. if(qApp->arguments().size()>5)
  82. {
  83. pluginName = qApp->arguments().at(5);
  84. }
  85. else
  86. {
  87. pluginName = "org_commontk_dicomhosting_app_";
  88. pluginName += QFileInfo(qApp->arguments().at(0)).fileName();
  89. }
  90. // try to find the plugin and install all plugins available in
  91. // pluginPath (but do not start them)
  92. QStringList libFilter;
  93. libFilter << "*.dll" << "*.so" << "*.dylib";
  94. QDirIterator dirIter(pluginPath, libFilter, QDir::Files);
  95. bool pluginFound = false;
  96. QString pluginFileLocation;
  97. while(dirIter.hasNext())
  98. {
  99. try
  100. {
  101. QString fileLocation = dirIter.next();
  102. if (fileLocation.contains("org_commontk_dicom_wg23"))
  103. {
  104. ctkPlugin* plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(fileLocation));
  105. plugin->start(ctkPlugin::START_TRANSIENT);
  106. }
  107. if (fileLocation.contains(pluginName))
  108. {
  109. pluginFound = true;
  110. pluginFileLocation = fileLocation;
  111. }
  112. }
  113. catch (const ctkPluginException& e)
  114. {
  115. qCritical() << e.what();
  116. }
  117. }
  118. // if we did not find the business logic: abort
  119. if(!pluginFound)
  120. {
  121. qCritical() << "Could not find plugin.";
  122. qCritical() << " Plugin name: " << pluginName;
  123. qCritical() << " Plugin path: " << pluginPath;
  124. exit(3);
  125. }
  126. // start the plugin with the business logic
  127. try
  128. {
  129. ctkPlugin* plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(pluginFileLocation));
  130. plugin->start(ctkPlugin::START_TRANSIENT);
  131. }
  132. catch (const ctkPluginException& e)
  133. {
  134. qCritical() << e.what();
  135. }
  136. // setup the communication infrastructure: DicomAppServer and DicomHostService
  137. ctkDicomAppServer * appServer = new ctkDicomAppServer(QUrl(appURL).port()); // accesses the app-plugin via getService("ctkDicomAppInterface");
  138. //$ ctkDicomHostInterface * hostInterface = new ctkDicomHostService(QUrl(hostURL).port());
  139. //$ framework->getPluginContext()->registerService(QStringList("ctkDicomHostInterface"), hostInterface);
  140. framework->start();
  141. QWidget placeholder;
  142. placeholder.show();
  143. return app.exec();
  144. }