ctkExampleHostedAppMain.cpp 5.8 KB

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