Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkUSActivator.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkUSActivator.h"
15 
16 mitk::USActivator::USActivator() : m_Context(nullptr)
17 {
18 }
19 
21 {
22 }
23 
24 void mitk::USActivator::Load(us::ModuleContext* context)
25 {
26  m_Context = context;
27 
28  // load us video devices from the harddisk
29  mitk::USDevicePersistence::Pointer devicePersistence = mitk::USDevicePersistence::New();
30  m_Devices = devicePersistence->RestoreLastDevices();
31 
32  // register all devices in microservice
33  for (std::vector<mitk::USDevice::Pointer>::iterator it = m_Devices.begin();
34  it != m_Devices.end(); ++it)
35  {
36  (*it)->Initialize();
37  }
38 
39  // to be notified about every register and unregister of an USDevice
40  context->AddServiceListener(this, &mitk::USActivator::OnServiceEvent,
41  "(" + us::ServiceConstants::OBJECTCLASS() + "=" + us_service_interface_iid<mitk::USDevice>() + ")");
42 }
43 
44 void mitk::USActivator::Unload(us::ModuleContext* context)
45 {
46  m_Context = context;
47 
48  // no notifiation of the following unregistering is wanted
49  context->RemoveServiceListener(this, &mitk::USActivator::OnServiceEvent);
50 
51  // store us video devices on the harddisk
52  mitk::USDevicePersistence::Pointer devicePersistence = mitk::USDevicePersistence::New();
53  devicePersistence->StoreCurrentDevices();
54 
55  // mark all devices as no longer needed (they will be unregistered in their destructor)
56  m_Devices.erase(m_Devices.begin(), m_Devices.end());
57 }
58 
60 {
61  if ( ! m_Context )
62  {
63  MITK_WARN("us::ModuleActivator")("USActivator")
64  << "OnServiceEvent listener called without having a module context in "
65  << "the activator. Cannot handle event.";
66  return;
67  }
68 
69  us::ServiceReference<mitk::USDevice> service = event.GetServiceReference();
71 
72  switch (event.GetType())
73  {
75  // hold new device in vector
76  m_Devices.push_back(device.GetPointer());
77  break;
79  {
80  // unregistered device does not need to be hold any longer
81  std::vector<mitk::USDevice::Pointer>::iterator it = find(m_Devices.begin(), m_Devices.end(), device.GetPointer());
82  if (it != m_Devices.end()) { m_Devices.erase(it); }
83  break;
84  }
85  default:
86  MITK_DEBUG("us::ModuleActivator")("USActivator")
87  << "Received uninteresting service event: " << event.GetType();
88  break;
89  }
90 }
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
static Pointer New()
void Load(us::ModuleContext *context) override
The mitk::USVideoDevice obejcts are loaded from hard disk and registered into micro service...
#define MITK_WARN
Definition: mitkLogMacros.h:19
void OnServiceEvent(const us::ServiceEvent event)
Listens to ServiceRegistry changes and updates the list of mitk::USDevice object accordingly.
void Unload(us::ModuleContext *context) override
Registered mitk::USVideoDevice objects are stored to hard disk an deregistered from micro service...
us::ModuleContext * m_Context
US_Core_EXPORT const std::string & OBJECTCLASS()
std::vector< USDevice::Pointer > m_Devices
Type GetType() const