NorMIT-nav  22.09
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 }
cx::SessionStorageServiceProxy::create
static SessionStorageServicePtr create(ctkPluginContext *pluginContext)
Definition: cxSessionStorageServiceProxy.cpp:19
cx::logicManager
LogicManager * logicManager()
Definition: cxLogicManager.cpp:46
cxLogger.h
cx::LogicManager::setApplicationComponent
void setApplicationComponent(ApplicationComponentPtr component)
Definition: cxLogicManager.cpp:150
cx::VideoServicePtr
boost::shared_ptr< class VideoService > VideoServicePtr
Definition: cxLogicManager.h:26
cx::PatientModelServiceProxy::create
static PatientModelServicePtr create(ctkPluginContext *pluginContext)
Definition: cxPatientModelServiceProxy.cpp:24
cxSharedPointerChecker.h
cx::Reporter::shutdown
static void shutdown()
shutdown service, destroy static object if none holds a reference.
Definition: cxReporter.cpp:73
cx::ViewServiceProxy::create
static ViewServicePtr create(ctkPluginContext *pluginContext)
Definition: cxViewServiceProxy.cpp:23
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::ViewService::aboutToStop
virtual void aboutToStop()=0
cx::LogicManager::getSessionStorageService
SessionStorageServicePtr getSessionStorageService()
Definition: cxLogicManager.cpp:277
cx::LogicManager::initialize
static void initialize(ApplicationComponentPtr component=ApplicationComponentPtr())
Definition: cxLogicManager.cpp:56
cx::LogicManager::restartServicesWithProfile
void restartServicesWithProfile(QString uid)
Definition: cxLogicManager.cpp:175
CX_LOG_INFO
#define CX_LOG_INFO
Definition: cxLogger.h:96
cx::GPUImageBufferRepository::shutdown
static void shutdown()
Definition: cxGPUImageBuffer.cpp:561
cxDataFactory.h
cxReporter.h
cxStateServiceProxy.h
cx::LogicManager
Control the custusx backend.
Definition: cxLogicManager.h:60
cx::LogicManager::isUbuntu2004
static bool isUbuntu2004()
Definition: cxLogicManager.cpp:79
cx::SessionStorageServicePtr
boost::shared_ptr< class SessionStorageService > SessionStorageServicePtr
Definition: cxLogicManager.h:30
cxProfile.h
cxPatientModelServiceProxy.h
cxSessionStorageServiceProxy.h
cxSpaceProviderImpl.h
cx::TrackingServicePtr
boost::shared_ptr< class TrackingService > TrackingServicePtr
Definition: cxToolFilterWidget.h:27
cx::PluginFrameworkManager::create
static PluginFrameworkManagerPtr create()
Definition: cxPluginFramework.h:53
cx::PatientModelServicePtr
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
Definition: cxLogicManager.h:25
cxGPUImageBuffer.h
cx::ViewServicePtr
boost::shared_ptr< class ViewService > ViewServicePtr
Definition: cxLogicManager.h:28
cxTypeConversions.h
cx::LogicManager::getViewService
ViewServicePtr getViewService()
Definition: cxLogicManager.cpp:273
cx::LogicManager::getPluginContext
ctkPluginContext * getPluginContext()
Definition: cxLogicManager.cpp:291
CX_LOG_ERROR
#define CX_LOG_ERROR
Definition: cxLogger.h:99
cx::LogicManager::restartWithNewProfile
void restartWithNewProfile(QString uid)
Definition: cxLogicManager.cpp:161
cx::PluginFrameworkManager::aboutToStop
void aboutToStop()
cxSettings.h
cx::VideoServiceProxy::create
static VideoServicePtr create(ctkPluginContext *pluginContext)
Definition: cxVideoServiceProxy.cpp:24
cx::SpaceProviderImpl
Definition: cxSpaceProviderImpl.h:29
cx::LogicManager::getPluginFramework
PluginFrameworkManagerPtr getPluginFramework()
Definition: cxLogicManager.cpp:287
cx::profile
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:160
cx::ProfileManager::setActiveProfile
void setActiveProfile(QString uid)
Definition: cxProfile.cpp:308
cx::ApplicationComponentPtr
boost::shared_ptr< class ApplicationComponent > ApplicationComponentPtr
Definition: cxLogicManager.h:41
cxStateService.h
cxLogicManager.h
cx::LogicManager::getSpaceProvider
SpaceProviderPtr getSpaceProvider()
Definition: cxLogicManager.cpp:269
cx::requireUnique
void requireUnique(int use_count, QString objectName)
Definition: cxSharedPointerChecker.cpp:18
cx::TrackingServiceProxy::create
static TrackingServicePtr create(ctkPluginContext *pluginContext)
Definition: cxTrackingServiceProxy.cpp:22
cxCoreServices.h
CX_LOG_DEBUG
#define CX_LOG_DEBUG
Definition: cxLogger.h:95
cxTrackingServiceProxy.h
cx::ProfileManager::getInstance
static ProfileManager * getInstance(QString defaultProfile=QString("Laboratory"))
returns the only instance of this class
Definition: cxProfile.cpp:167
cx::LogicManager::getTrackingService
TrackingServicePtr getTrackingService()
Definition: cxLogicManager.cpp:257
cxPluginFramework.h
cxViewServiceProxy.h
cx::LogicManager::getVideoService
VideoServicePtr getVideoService()
Definition: cxLogicManager.cpp:261
cxVideoServiceProxy.h
cx::LogicManager::getInstance
static LogicManager * getInstance()
Definition: cxLogicManager.cpp:296
cx::LogicManager::shutdown
static void shutdown()
Definition: cxLogicManager.cpp:65
cxFileManagerServiceProxy.h
cx::LogicManager::initializeBasic
static void initializeBasic()
Definition: cxLogicManager.cpp:51
cx::ProfileManager::initialize
static void initialize()
Definition: cxProfile.cpp:176
cx::StateServiceProxy::create
static StateServicePtr create(ctkPluginContext *pluginContext)
Definition: cxStateServiceProxy.cpp:19
cx::PluginFrameworkManagerPtr
boost::shared_ptr< class PluginFrameworkManager > PluginFrameworkManagerPtr
Definition: cxLogicManager.h:39
cx::ProfileManager::shutdown
static void shutdown()
Definition: cxProfile.cpp:181
cx::SpaceProviderPtr
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
Definition: cxLogicManager.h:23
cx::LogicManager::getPatientModelService
PatientModelServicePtr getPatientModelService()
Definition: cxLogicManager.cpp:253
cx::Reporter::initialize
static void initialize()
Initialize logging, static object is guaranteed to exist at least until shutdown.
Definition: cxReporter.cpp:60
cx::LogicManager::getStateService
StateServicePtr getStateService()
Definition: cxLogicManager.cpp:265
cx::StateServicePtr
boost::shared_ptr< class StateService > StateServicePtr
Definition: cxLogicManager.h:27