Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkUSActivator.h"
19 
21 {
22 }
23 
25 {
26 }
27 
28 void mitk::USActivator::Load(us::ModuleContext* context)
29 {
30  m_Context = context;
31 
32  // load us video devices from the harddisk
34  m_Devices = devicePersistence->RestoreLastDevices();
35 
36  // register all devices in microservice
37  for (std::vector<mitk::USDevice::Pointer>::iterator it = m_Devices.begin();
38  it != m_Devices.end(); ++it)
39  {
40  (*it)->Initialize();
41  }
42 
43  // to be notified about every register and unregister of an USDevice
44  context->AddServiceListener(this, &mitk::USActivator::OnServiceEvent,
45  "(" + us::ServiceConstants::OBJECTCLASS() + "=" + us_service_interface_iid<mitk::USDevice>() + ")");
46 }
47 
48 void mitk::USActivator::Unload(us::ModuleContext* context)
49 {
50  m_Context = context;
51 
52  // no notifiation of the following unregistering is wanted
53  context->RemoveServiceListener(this, &mitk::USActivator::OnServiceEvent);
54 
55  // store us video devices on the harddisk
57  devicePersistence->StoreCurrentDevices();
58 
59  // mark all devices as no longer needed (they will be unregistered in their destructor)
60  m_Devices.erase(m_Devices.begin(), m_Devices.end());
61 }
62 
64 {
65  if ( ! m_Context )
66  {
67  MITK_WARN("us::ModuleActivator")("USActivator")
68  << "OnServiceEvent listener called without having a module context in "
69  << "the activator. Cannot handle event.";
70  return;
71  }
72 
73  us::ServiceReference<mitk::USDevice> service = event.GetServiceReference();
74  mitk::USDevice::Pointer device = m_Context->GetService(us::ServiceReference<mitk::USDevice>(service));
75 
76  switch (event.GetType())
77  {
79  // hold new device in vector
80  m_Devices.push_back(device.GetPointer());
81  break;
83  {
84  // unregistered device does not need to be hold any longer
85  std::vector<mitk::USDevice::Pointer>::iterator it = find(m_Devices.begin(), m_Devices.end(), device.GetPointer());
86  if (it != m_Devices.end()) { m_Devices.erase(it); }
87  break;
88  }
89  default:
90  MITK_DEBUG("us::ModuleActivator")("USActivator")
91  << "Received uninteresting service event: " << event.GetType();
92  break;
93  }
94 }
itk::SmartPointer< Self > Pointer
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
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:23
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...
Type GetType() const
US_Core_EXPORT const std::string & OBJECTCLASS()