MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkUSActivator.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
13#include "mitkUSActivator.h"
15
16mitk::USActivator::USActivator() : m_Context(nullptr)
17{
18}
19
23
24void 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
44void 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
59void mitk::USActivator::OnServiceEvent(const us::ServiceEvent event)
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();
70 mitk::USDevice::Pointer device = m_Context->GetService(us::ServiceReference<mitk::USDevice>(service));
71
72 switch (event.GetType())
73 {
74 case us::ServiceEvent::REGISTERED:
75 // hold new device in vector
76 m_Devices.push_back(device.GetPointer());
77 break;
78 case us::ServiceEvent::UNREGISTERING:
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}
void OnServiceEvent(const us::ServiceEvent event)
Listens to ServiceRegistry changes and updates the list of mitk::USDevice object accordingly.
void Load(us::ModuleContext *context) override
The mitk::USVideoDevice obejcts are loaded from hard disk and registered into micro service.
void Unload(us::ModuleContext *context) override
Registered mitk::USVideoDevice objects are stored to hard disk an deregistered from micro service.