Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkIGTLMessageSource.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 "mitkIGTLMessageSource.h"
14 #include "mitkUIDGenerator.h"
15 
16 //Microservices
17 #include <usGetModuleContext.h>
18 #include <usModule.h>
19 #include <usServiceProperties.h>
20 #include <usModuleContext.h>
21 
23  "org.mitk.services.IGTLMessageSource";
25  US_INTERFACE_NAME + ".devicename";
27  US_INTERFACE_NAME + ".devicetype";
29  US_INTERFACE_NAME + ".id";
31  US_INTERFACE_NAME + ".isActive";
32 
34  : itk::ProcessObject(), m_Name("IGTLMessageSource (no defined type)"),
35  m_Type("NONE"), m_StreamingFPS(0)
36 {
37  m_StreamingFPSMutex = itk::FastMutexLock::New();
38 }
39 
41 {
42  //this->UnRegisterMicroservice();
43 }
44 
46 {
47  if (this->GetNumberOfIndexedOutputs() < 1)
48  {
49  MITK_WARN << "IGTLMessageSource contained no outputs. Returning nullptr.";
50  return nullptr;
51  }
52 
53  return static_cast<IGTLMessage*>(this->ProcessObject::GetPrimaryOutput());
54 }
55 
57  DataObjectPointerArraySizeType idx)
58 {
59  IGTLMessage* out =
60  dynamic_cast<IGTLMessage*>( this->ProcessObject::GetOutput(idx) );
61  if ( out == nullptr && this->ProcessObject::GetOutput(idx) != nullptr )
62  {
63  itkWarningMacro (<< "Unable to convert output number " << idx << " to type "
64  << typeid( IGTLMessage ).name () );
65  }
66  return out;
67 }
68 
70  const std::string& messageName)
71 {
72  DataObjectPointerArray outputs = this->GetOutputs();
73  for (DataObjectPointerArray::iterator it = outputs.begin();
74  it != outputs.end();
75  ++it)
76  {
77  if (messageName ==
78  (static_cast<IGTLMessage*>(it->GetPointer()))->GetName())
79  {
80  return static_cast<IGTLMessage*>(it->GetPointer());
81  }
82  }
83  return nullptr;
84 }
85 
86 itk::ProcessObject::DataObjectPointerArraySizeType
87 mitk::IGTLMessageSource::GetOutputIndex( std::string messageName )
88 {
89  DataObjectPointerArray outputs = this->GetOutputs();
90  for (DataObjectPointerArray::size_type i = 0; i < outputs.size(); ++i)
91  {
92  if (messageName ==
93  (static_cast<IGTLMessage*>(outputs.at(i).GetPointer()))->GetName())
94  {
95  return i;
96  }
97  }
98  throw std::invalid_argument("output name does not exist");
99 }
100 
102 {
103  // Get Context
104  us::ModuleContext* context = us::GetModuleContext();
105 
106  // Define ServiceProps
107  us::ServiceProperties props;
108  mitk::UIDGenerator uidGen =
109  mitk::UIDGenerator ("org.mitk.services.IGTLMessageSource.id_", 16);
110  props[ US_PROPKEY_ID ] = uidGen.GetUID();
111  props[ US_PROPKEY_DEVICENAME ] = m_Name;
112  props[ US_PROPKEY_DEVICETYPE ] = m_Type;
113  m_ServiceRegistration = context->RegisterService(this, props);
114 }
115 
117 {
118  if (m_ServiceRegistration != nullptr)
119  {
121  }
123 }
124 
126 {
127  us::Any referenceProperty =
128  this->m_ServiceRegistration.GetReference().GetProperty(US_PROPKEY_ID);
129  return referenceProperty.ToString();
130 }
131 
132 void mitk::IGTLMessageSource::GraftOutput(itk::DataObject *graft)
133 {
134  this->GraftNthOutput(0, graft);
135 }
136 
138  itk::DataObject *graft)
139 {
140  if ( idx >= this->GetNumberOfIndexedOutputs() )
141  {
142  itkExceptionMacro(<<"Requested to graft output " << idx << " but this filter"
143  "only has " << this->GetNumberOfIndexedOutputs() << " Outputs.");
144  }
145 
146  if ( !graft )
147  {
148  itkExceptionMacro(<<"Requested to graft output with a nullptr pointer object" );
149  }
150 
151  itk::DataObject* output = this->GetOutput(idx);
152  if ( !output )
153  {
154  itkExceptionMacro(<<"Requested to graft output that is a nullptr pointer" );
155  }
156  // Call Graft on IGTLMessage to copy member data
157  output->Graft( graft );
158 }
159 
160 itk::DataObject::Pointer mitk::IGTLMessageSource::MakeOutput ( DataObjectPointerArraySizeType /*idx*/ )
161 {
162  return IGTLMessage::New().GetPointer();
163 }
164 
165 itk::DataObject::Pointer mitk::IGTLMessageSource::MakeOutput( const DataObjectIdentifierType & name )
166 {
167  itkDebugMacro("MakeOutput(" << name << ")");
168  if( this->IsIndexedOutputName(name) )
169  {
170  return this->MakeOutput( this->MakeIndexFromOutputName(name) );
171  }
172  return static_cast<itk::DataObject *>(IGTLMessage::New().GetPointer());
173 }
174 
176 {
178  // add properties to p like this:
179  //p->SetProperty("MyFilter_MyParameter", mitk::PropertyDataType::New(m_MyParameter));
181 }
182 
183 void mitk::IGTLMessageSource::SetFPS(unsigned int fps)
184 {
185  this->m_StreamingFPSMutex->Lock();
186  this->m_StreamingFPS = fps;
187  this->m_StreamingFPSMutex->Unlock();
188 }
189 
190 
192 {
193  unsigned int fps = 0;
194  this->m_StreamingFPSMutex->Lock();
195  fps = this->m_StreamingFPS;
196  this->m_StreamingFPSMutex->Unlock();
197  return fps;
198 }
ServiceReference< I1 > GetReference(InterfaceType< I1 >) const
virtual void RegisterAsMicroservice()
Registers this object as a Microservice, making it available to every module and/or plugin...
static Pointer New()
virtual void UnRegisterMicroservice()
Registers this object as a Microservice, making it available to every module and/or plugin...
Generated unique IDs.
static const std::string US_PROPKEY_ISACTIVE
us::ServiceRegistration< Self > m_ServiceRegistration
itk::DataObject::Pointer MakeOutput(DataObjectPointerArraySizeType idx) override
virtual void GraftNthOutput(unsigned int idx, itk::DataObject *graft)
Graft the specified DataObject onto this ProcessObject&#39;s output.
std::string ToString() const
Definition: usAny.h:257
unsigned int GetFPS()
Gets the fps used for streaming this source.
IGTLMessage * GetOutput(void)
return the output (output with id 0) of the filter
#define MITK_WARN
Definition: mitkLogMacros.h:19
A wrapper for the OpenIGTLink message type.
static const std::string US_PROPKEY_DEVICETYPE
Definition: usAny.h:163
std::string GetMicroserviceID()
Returns the id that this device is registered with. The id will only be valid, if the IGTLMessageSour...
itk::SmartPointer< const Self > ConstPointer
virtual mitk::PropertyList::ConstPointer GetParameters() const
Get all filter parameters as a PropertyList.
itk::FastMutexLock::Pointer m_StreamingFPSMutex
static const std::string US_PROPKEY_ID
US_UNORDERED_MAP_TYPE< std::string, Any > ServiceProperties
static const std::string US_PROPKEY_DEVICENAME
static Pointer New()
virtual void GraftOutput(itk::DataObject *graft)
Graft the specified DataObject onto this ProcessObject&#39;s output.
static const std::string US_INTERFACE_NAME
These Constants are used in conjunction with Microservices.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
DataObjectPointerArraySizeType GetOutputIndex(std::string messageName)
return the index of the output with name messageName, -1 if no output with that name was found ...
void SetFPS(unsigned int fps)
Sets the fps used for streaming this source.