ctkExampleHostedAppMain.cpp 5.0 KB

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