18#include <mitkUIDGenerator.h>
21#include <usGetModuleContext.h>
23#include <usServiceProperties.h>
24#include <usModuleContext.h>
27#include <itkCommand.h>
35 this->SetName(
"IGTLDeviceSource (no defined type)");
40 if (m_IGTLDevice.IsNotNull())
44 this->StopCommunication();
50 this->RemoveObservers();
51 m_IGTLDevice =
nullptr;
57 if (m_IGTLDevice.IsNull())
63 igtl::MessageBase::Pointer msgIn =
dynamic_cast<igtl::MessageBase*
>(m_IGTLDevice->GetNextImage2dMessage().GetPointer());
64 if (msgIn.IsNotNull())
69 msgOut->SetName(msgIn->GetDeviceName());
79 if (this->m_IGTLDevice.IsNotNull())
81 this->m_IGTLDevice->RemoveObserver(m_IncomingMessageObserverTag);
82 this->m_IGTLDevice->RemoveObserver(m_IncomingCommandObserverTag);
83 this->m_IGTLDevice->RemoveObserver(m_LostConnectionObserverTag);
89 MITK_DEBUG <<
"Setting IGTLDevice to " << igtlDevice;
90 if (this->m_IGTLDevice.GetPointer() != igtlDevice)
93 if (this->m_IGTLDevice.IsNotNull())
96 this->RemoveObservers();
99 this->m_IGTLDevice = igtlDevice;
100 this->CreateOutputs();
101 std::stringstream name;
102 name <<
"OIGTL Device Source ( " << igtlDevice->GetName() <<
" )";
103 this->SetName(name.str());
106 typedef itk::SimpleMemberCommand<IGTLDeviceSource> DeviceSrcCommand;
108 DeviceSrcCommand::Pointer msgReceivedCommand = DeviceSrcCommand::New();
110 this->m_IncomingMessageObserverTag =
111 this->m_IGTLDevice->AddObserver(mitk::MessageReceivedEvent(), msgReceivedCommand);
113 DeviceSrcCommand::Pointer cmdReceivedCommand = DeviceSrcCommand::New();
115 this->m_IncomingCommandObserverTag =
116 this->m_IGTLDevice->AddObserver(mitk::CommandReceivedEvent(), cmdReceivedCommand);
118 DeviceSrcCommand::Pointer connectionLostCommand = DeviceSrcCommand::New();
120 this->m_LostConnectionObserverTag =
121 this->m_IGTLDevice->AddObserver(mitk::LostConnectionEvent(), connectionLostCommand);
128 if (this->GetNumberOfOutputs() > 0)
130 for (
int numOP = this->GetNumberOfOutputs() - 1; numOP >= 0; numOP--)
131 this->RemoveOutput(numOP);
136 if (m_IGTLDevice.IsNull())
139 this->SetNumberOfIndexedOutputs(1);
140 if (this->GetOutput(0) ==
nullptr)
142 DataObjectPointer newOutput = this->MakeOutput(0);
143 this->SetNthOutput(0, newOutput);
150 if (m_IGTLDevice.IsNull())
152 throw std::invalid_argument(
"mitk::IGTLDeviceSource: "
153 "No OpenIGTLink device set");
155 if (this->IsConnected())
161 m_IGTLDevice->OpenConnection();
163 catch (mitk::Exception &e)
165 throw std::runtime_error(std::string(
"mitk::IGTLDeviceSource: Could not open"
166 "connection to OpenIGTLink device. Error: ") + e.GetDescription());
172 if (m_IGTLDevice.IsNull())
173 throw std::invalid_argument(
"mitk::IGTLDeviceSource: "
174 "No OpenIGTLink device set");
177 if (m_IGTLDevice->StartCommunication() ==
false)
178 throw std::runtime_error(
"mitk::IGTLDeviceSource: "
179 "Could not start communication");
184 if (m_IGTLDevice.IsNull())
185 throw std::invalid_argument(
"mitk::IGTLDeviceSource: "
186 "No OpenIGTLink device set");
187 if (m_IGTLDevice->CloseConnection() ==
false)
188 throw std::runtime_error(
"mitk::IGTLDeviceSource: Could not close connection"
189 " to OpenIGTLink device");
194 if (m_IGTLDevice.IsNull())
195 throw std::invalid_argument(
"mitk::IGTLDeviceSource: "
196 "No OpenIGTLink device set");
197 if (m_IGTLDevice->StopCommunication() ==
false)
198 throw std::runtime_error(
"mitk::IGTLDeviceSource: "
199 "Could not stop communicating");
205 Superclass::UpdateOutputInformation();
212 this->RemoveInput(idx);
217 this->ProcessObject::SetNthInput(idx,
const_cast<IGTLMessage*
>(msg));
224 if (m_IGTLDevice.IsNull())
233 if (m_IGTLDevice.IsNull())
242 us::ModuleContext* context = us::GetModuleContext();
245 us::ServiceProperties props;
246 mitk::UIDGenerator uidGen =
247 mitk::UIDGenerator(
"org.mitk.services.IGTLDeviceSource.id_");
248 props[US_PROPKEY_ID] = uidGen.GetUID();
249 props[US_PROPKEY_DEVICENAME] = this->GetName();
250 props[US_PROPKEY_IGTLDEVICENAME] = m_Name;
251 props[US_PROPKEY_DEVICETYPE] = m_Type;
252 m_ServiceRegistration = context->RegisterService(
this, props);
254 MITK_INFO <<
"Registered new DeviceSource as microservice: " << uidGen.GetUID();
271 if (this->GetNumberOfInputs() < 1)
274 return static_cast<const IGTLMessage*
>(this->ProcessObject::GetInput(0));
280 if (this->GetNumberOfInputs() < 1)
283 return static_cast<const IGTLMessage*
>(this->ProcessObject::GetInput(idx));
289 const DataObjectPointerArray& inputs =
const_cast<Self*
>(
this)->GetInputs();
290 for (DataObjectPointerArray::const_iterator it = inputs.begin();
291 it != inputs.end(); ++it)
292 if (std::string(msgName) ==
293 (
static_cast<IGTLMessage*
>(it->GetPointer()))->GetName())
294 return static_cast<IGTLMessage*
>(it->GetPointer());
298itk::ProcessObject::DataObjectPointerArraySizeType
301 DataObjectPointerArray outputs = this->GetInputs();
302 for (DataObjectPointerArray::size_type i = 0; i < outputs.size(); ++i)
304 (
static_cast<IGTLMessage*
>(outputs.at(i).GetPointer()))->GetName())
306 throw std::invalid_argument(
"output name does not exist");
virtual void OnIncomingCommand()
This method is called when the IGTL device hold by this class receives a new command.
void RegisterAsMicroservice() override
Registers this object as a Microservice, making it available to every module and/or plugin....
const IGTLMessage * GetInput(void) const
Get the input of this filter.
~IGTLDeviceSource() override
DataObjectPointerArraySizeType GetInputIndex(std::string msgName)
return the index of the input with name msgName, throw std::invalid_argument exception if that name w...
void Disconnect()
Closes the connection to the OpenIGTLink device.
void Connect()
Establishes a connection to the OpenIGTLink device. If there is already a connection the method does ...
static const std::string US_PROPKEY_IGTLDEVICENAME
These Constants are used in conjunction with Microservices.
void UpdateOutputInformation() override
Used for pipeline update.
virtual bool IsCommunicating()
returns true if communication is in progress
virtual bool IsConnected()
returns true if a connection to the OpenIGTLink device is established
void CreateOutputs()
Create the necessary outputs for the m_IGTLDevice.
void StartCommunication()
starts the communication of the device. This needs to be called before Update() or GetOutput()->Updat...
virtual void RemoveObservers()
Removes all observers that listen to the igtl device.
virtual void OnIncomingMessage()
This method is called when the IGTL device hold by this class receives a new message.
void GenerateData() override
filter execute method
void StopCommunication()
stops the communication of the device.
virtual void SetIGTLDevice(mitk::IGTLDevice *td)
sets the OpenIGTLink device that will be used as a data source
virtual void SetInput(unsigned int idx, const IGTLMessage *msg)
Set input with id idx of this filter.
virtual void OnLostConnection()
This method is called when the IGTL device lost the connection to the other side.
Interface for all OpenIGTLink Devices.
OpenIGTLink message source.
static const std::string US_INTERFACE_NAME
These Constants are used in conjunction with Microservices.
A wrapper for the OpenIGTLink message type.
void SetMessage(igtl::MessageBase::Pointer msg)
Sets the OpenIGTLink message.