Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
cxLogicManager.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 #include <cxLogicManager.h>
12 
13 #ifndef CX_WINDOWS
14 #include <sys/utsname.h>
15 #endif
16 
17 #include <QApplication>
18 #include <ctkPluginContext.h>
19 #include "cxLogger.h"
20 #include "cxVideoServiceProxy.h"
21 #include "cxStateService.h"
22 #include "cxGPUImageBuffer.h"
23 #include "cxSettings.h"
24 #include "cxSpaceProviderImpl.h"
25 #include "cxDataFactory.h"
26 #include "cxCoreServices.h"
27 #include "cxTypeConversions.h"
28 #include "cxSharedPointerChecker.h"
29 #include "cxPluginFramework.h"
30 #include "cxVideoServiceProxy.h"
31 #include "cxTrackingServiceProxy.h"
33 #include "cxStateServiceProxy.h"
34 #include "cxViewServiceProxy.h"
37 #include "cxReporter.h"
38 #include "cxProfile.h"
39 
40 namespace cx
41 {
42 // --------------------------------------------------------
43 LogicManager* LogicManager::mInstance = NULL;
44 // --------------------------------------------------------
45 
47 {
49 }
50 
52 {
53  LogicManager::getInstance()->basicSetup();
54 }
55 
57 {
58  LogicManager::getInstance()->initializeServices();
60 
61  // we might want to use this one, in order to shutdown within the main loop
62 // connect(qApp, &QApplication::aboutToQuit, LogicManager::getInstance(), &LogicManager::shutdown);
63 }
64 
66 {
67  //CX_LOG_DEBUG() << "Ubuntu 20.04 identifyed - skipping some shutdown procedures in LogicManager";
68  //CX_LOG_DEBUG() << "Skipping some shutdown procedures in LogicManager, because of CTK issues";
69  LogicManager::getInstance()->shutdownServicesLight();
70 
71  //Replacing these 3 lines with the above line seems to fix the test seg. faults on Ubuntu 20.04
72  //Now the same shutdown code is running on all platforms, and not only Ubuntu 20.04
73  //Old shutdown sequence cause seg. faults with new CTK - Qt combinations
74  //LogicManager::getInstance()->shutdownServices();
75  //delete mInstance;
76  //mInstance = NULL;
77 }
78 
80 {
81 #ifdef CX_WINDOWS
82  return false;
83 #else
84  struct utsname uname_pointer;
85  uname(&uname_pointer);
86  CX_LOG_DEBUG() << "System info: " << uname_pointer.sysname << ", " << uname_pointer.version << ", " << uname_pointer.release;
87 
88  QString systemVersion(uname_pointer.version);
89  //CX_LOG_DEBUG() << "System version: " << systemVersion;
90 
91  if(!systemVersion.contains("Ubuntu"))
92  return false;
93 
94  // 5.4 is the system kernel for Ubuntu 20.04, but for some reason later installations comes with 5.11 (the kernel for 21.04)
95  // In this case the uname_pointer.version string seems to contain 20.04
96  QString systemKernel(uname_pointer.release);
97  if(systemVersion.contains("20.04")) //For new installations of 20.04, with the "21.04 kernel"
98  return true;
99  else if (systemKernel.contains("5.4.")) //For old installations, with the original kernel
100  return true;
101  else
102  return false;
103 #endif
104 }
105 
106 void LogicManager::initializeServices()
107 {
108  CX_LOG_INFO() << " --- Initialize services for " << qApp->applicationName() << "...";
109 
110  this->basicSetup();
111 
112  mPluginFramework->loadState();
113 
114  if (mComponent)
115  mComponent->create();
116 
117  mShutdown = false;
118  CX_LOG_DEBUG() << " --- End initialize services.";
119 }
120 
121 void LogicManager::basicSetup()
122 {
125 
126  mPluginFramework = PluginFrameworkManager::create();
127  mPluginFramework->start();
128  mPluginFramework->setSearchPaths(QStringList());
129 
130  this->createLegacyStoredServices();
131 }
132 
133 void LogicManager::createLegacyStoredServices()
134 {
135  // services layer
136  ctkPluginContext* pc = this->getPluginContext();
137 
138  //mFileManagerService = FileManagerServiceProxy::create(pc);
139  mTrackingService = TrackingServiceProxy::create(pc);
140  mPatientModelService = PatientModelServiceProxy::create(pc);
141  mVideoService = VideoServiceProxy::create(pc);
142  mViewService = ViewServiceProxy::create(pc);
143  connect(mPluginFramework.get(), &PluginFrameworkManager::aboutToStop, mViewService.get(), &ViewService::aboutToStop);
144  mStateService = StateServiceProxy::create(pc);
145  mSessionStorageService = SessionStorageServiceProxy::create(pc);
146 
147  mSpaceProvider.reset(new cx::SpaceProviderImpl(mTrackingService, mPatientModelService));
148 }
149 
151 {
152  if (mComponent)
153  mComponent->destroy();
154 
155  mComponent = component;
156 
157  if (mComponent)
158  mComponent->create();
159 }
160 
162 {
163  QMetaObject::invokeMethod(this, "onRestartWithNewProfile",
164  Qt::QueuedConnection,
165  Q_ARG(QString, uid));
166 }
167 
168 void LogicManager::onRestartWithNewProfile(QString uid)
169 {
170  if (profile()->getUid()==uid)
171  return;
172  this->restartServicesWithProfile(uid);
173 }
174 
176 {
177  this->shutdownServices();
179  this->initializeServices();
180 }
181 
182 void LogicManager::shutdownServices()
183 {
184  if(mShutdown)
185  {
186  CX_LOG_ERROR() << "Trying to shutdown logicmanager when it already shutdown. Aborting shutdown, fix code.";
187  return;
188  }
189 
190  CX_LOG_INFO() << " --- Shutting down " << qApp->applicationName() << "...";
191  CX_LOG_DEBUG() << "Skipping some shutdown procedures in LogicManager, because of CTK issues";
192 
193  this->getPatientModelService()->autoSave();
194 
195  if (mComponent)
196  mComponent->destroy(); // this is the GUI - delete first
197 
198  mPluginFramework->stop();
199 
200  this->shutdownLegacyStoredServices();
201  mPluginFramework.reset();
205 
206  mShutdown = true;
207  CX_LOG_DEBUG() << " --- End shutdown services";
208 }
209 
210 void LogicManager::shutdownServicesLight()
211 {
212  if(mShutdown)
213  {
214  CX_LOG_ERROR() << "Trying to shutdown logicmanager when it already shutdown. Aborting shutdown, fix code.";
215  return;
216  }
217 
218  CX_LOG_INFO() << " --- Shutting down (Light) " << qApp->applicationName() << "...";
219 
220  this->getPatientModelService()->autoSave();
221 
222  if (mComponent)
223  mComponent->destroy(); // this is the GUI - delete first
224 
225  this->shutdownLegacyStoredServices();
226 
230 
231  mShutdown = true;
232  CX_LOG_DEBUG() << " --- End (Light) shutdown services";
233 }
234 
235 void LogicManager::shutdownLegacyStoredServices()
236 {
237  this->shutdownService(mSpaceProvider, "SpaceProvider"); // remove before patmodel and track
238  this->shutdownService(mStateService, "StateService");
239  this->shutdownService(mViewService, "ViewService");
240  this->shutdownService(mTrackingService, "TrackingService");
241  this->shutdownService(mPatientModelService, "PatientModelService");
242  this->shutdownService(mVideoService, "VideoService");
243  this->shutdownService(mSessionStorageService, "SessionStorageService");
244 }
245 
246 template<class T>
247 void LogicManager::shutdownService(boost::shared_ptr<T>& service, QString name)
248 {
249  requireUnique(service, name);
250  service.reset();
251 }
252 
254 {
255  return mPatientModelService;
256 }
258 {
259  return mTrackingService;
260 }
262 {
263  return mVideoService;
264 }
266 {
267  return mStateService;
268 }
270 {
271  return mSpaceProvider;
272 }
274 {
275  return mViewService;
276 }
278 {
279  return mSessionStorageService;
280 }
281 
282 //FileManagerServicePtr LogicManager::getFileManagerService()
283 //{
284 // return mFileManagerService;
285 //}
286 
288 {
289  return mPluginFramework;
290 }
292 {
293  return this->getPluginFramework()->getPluginContext();
294 }
295 
297 {
298  if (!mInstance)
299  {
300  mInstance = new LogicManager;
301  }
302  return mInstance;
303 }
304 
305 LogicManager::LogicManager()
306 {
307 }
308 
309 LogicManager::~LogicManager()
310 {
311 
312 }
313 
314 }
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:160
static bool isUbuntu2004()
boost::shared_ptr< class VideoService > VideoServicePtr
boost::shared_ptr< class ApplicationComponent > ApplicationComponentPtr
static void shutdown()
void restartServicesWithProfile(QString uid)
boost::shared_ptr< class StateService > StateServicePtr
ctkPluginContext * getPluginContext()
static void initializeBasic()
boost::shared_ptr< class TrackingService > TrackingServicePtr
TrackingServicePtr getTrackingService()
static void initialize()
Definition: cxProfile.cpp:176
static void shutdown()
shutdown service, destroy static object if none holds a reference.
Definition: cxReporter.cpp:73
#define CX_LOG_INFO
Definition: cxLogger.h:96
static void initialize()
Initialize logging, static object is guaranteed to exist at least until shutdown. ...
Definition: cxReporter.cpp:60
static void shutdown()
Definition: cxProfile.cpp:181
static SessionStorageServicePtr create(ctkPluginContext *pluginContext)
static ProfileManager * getInstance(QString defaultProfile=QString("Laboratory"))
returns the only instance of this class
Definition: cxProfile.cpp:167
boost::shared_ptr< class ViewService > ViewServicePtr
static VideoServicePtr create(ctkPluginContext *pluginContext)
void setActiveProfile(QString uid)
Definition: cxProfile.cpp:316
static TrackingServicePtr create(ctkPluginContext *pluginContext)
Control the custusx backend.
static PatientModelServicePtr create(ctkPluginContext *pluginContext)
SessionStorageServicePtr getSessionStorageService()
VideoServicePtr getVideoService()
#define CX_LOG_ERROR
Definition: cxLogger.h:99
static StateServicePtr create(ctkPluginContext *pluginContext)
static LogicManager * getInstance()
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
void restartWithNewProfile(QString uid)
LogicManager * logicManager()
#define CX_LOG_DEBUG
Definition: cxLogger.h:95
boost::shared_ptr< class PluginFrameworkManager > PluginFrameworkManagerPtr
ViewServicePtr getViewService()
static ViewServicePtr create(ctkPluginContext *pluginContext)
static PluginFrameworkManagerPtr create()
virtual void aboutToStop()=0
void setApplicationComponent(ApplicationComponentPtr component)
PluginFrameworkManagerPtr getPluginFramework()
void requireUnique(int use_count, QString objectName)
SpaceProviderPtr getSpaceProvider()
boost::shared_ptr< class SessionStorageService > SessionStorageServicePtr
static void initialize(ApplicationComponentPtr component=ApplicationComponentPtr())
PatientModelServicePtr getPatientModelService()
StateServicePtr getStateService()
Namespace for all CustusX production code.