Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkIGTLMessageQueue.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 "mitkIGTLMessageQueue.h"
18 #include <string>
19 #include "igtlMessageBase.h"
20 
22 {
23  this->m_Mutex->Lock();
25  m_SendQueue.clear();
26 
27  m_SendQueue.push_back(message);
28  this->m_Mutex->Unlock();
29 }
30 
32 {
33  this->m_Mutex->Lock();
34  if (this->m_BufferingType == IGTLMessageQueue::Infinit)
35  m_CommandQueue.clear();
36 
37  m_CommandQueue.push_back(message);
38  this->m_Mutex->Unlock();
39 }
40 
42 {
43  this->m_Mutex->Lock();
44 
45  std::stringstream infolog;
46 
47  infolog << "Received message of type ";
48 
49  if (dynamic_cast<igtl::TrackingDataMessage*>(msg.GetPointer()) != nullptr)
50  {
51  if (this->m_BufferingType == IGTLMessageQueue::Infinit)
52  m_TrackingDataQueue.clear();
53 
54  this->m_TrackingDataQueue.push_back(dynamic_cast<igtl::TrackingDataMessage*>(msg.GetPointer()));
55 
56  infolog << "TDATA";
57  }
58  else if (dynamic_cast<igtl::TransformMessage*>(msg.GetPointer()) != nullptr)
59  {
60  if (this->m_BufferingType == IGTLMessageQueue::Infinit)
61  m_TransformQueue.clear();
62 
63  this->m_TransformQueue.push_back(dynamic_cast<igtl::TransformMessage*>(msg.GetPointer()));
64 
65  infolog << "TRANSFORM";
66  }
67  else if (dynamic_cast<igtl::StringMessage*>(msg.GetPointer()) != nullptr)
68  {
69  if (this->m_BufferingType == IGTLMessageQueue::Infinit)
70  m_StringQueue.clear();
71 
72  this->m_StringQueue.push_back(dynamic_cast<igtl::StringMessage*>(msg.GetPointer()));
73 
74  infolog << "STRING";
75  }
76  else if (dynamic_cast<igtl::ImageMessage*>(msg.GetPointer()) != nullptr)
77  {
78  igtl::ImageMessage::Pointer imageMsg = dynamic_cast<igtl::ImageMessage*>(msg.GetPointer());
79  int* dim = new int[3];
80  imageMsg->GetDimensions(dim);
81  if (dim[2] > 1)
82  {
83  if (this->m_BufferingType == IGTLMessageQueue::Infinit)
84  m_Image3dQueue.clear();
85 
86  this->m_Image3dQueue.push_back(dynamic_cast<igtl::ImageMessage*>(msg.GetPointer()));
87 
88  infolog << "IMAGE3D";
89  }
90  else
91  {
92  if (this->m_BufferingType == IGTLMessageQueue::Infinit)
93  m_Image2dQueue.clear();
94 
95  this->m_Image2dQueue.push_back(dynamic_cast<igtl::ImageMessage*>(msg.GetPointer()));
96 
97  infolog << "IMAGE2D";
98  }
99  }
100  else
101  {
102  if (this->m_BufferingType == IGTLMessageQueue::Infinit)
103  m_MiscQueue.clear();
104 
105  this->m_MiscQueue.push_back(msg);
106 
107  infolog << "OTHER";
108  }
109 
110  m_Latest_Message = msg;
111 
112  MITK_INFO << infolog.str();
113 
114  this->m_Mutex->Unlock();
115 }
116 
118 {
119  igtl::MessageBase::Pointer ret = nullptr;
120  this->m_Mutex->Lock();
121  if (this->m_SendQueue.size() > 0)
122  {
123  ret = this->m_SendQueue.front();
124  this->m_SendQueue.pop_front();
125  }
126  this->m_Mutex->Unlock();
127  return ret;
128 }
129 
131 {
132  igtl::MessageBase::Pointer ret = nullptr;
133  this->m_Mutex->Lock();
134  if (this->m_MiscQueue.size() > 0)
135  {
136  ret = this->m_MiscQueue.front();
137  this->m_MiscQueue.pop_front();
138  }
139  this->m_Mutex->Unlock();
140  return ret;
141 }
142 
144 {
145  igtl::ImageMessage::Pointer ret = nullptr;
146  this->m_Mutex->Lock();
147  if (this->m_Image2dQueue.size() > 0)
148  {
149  ret = this->m_Image2dQueue.front();
150  this->m_Image2dQueue.pop_front();
151  }
152  this->m_Mutex->Unlock();
153  return ret;
154 }
155 
157 {
158  igtl::ImageMessage::Pointer ret = nullptr;
159  this->m_Mutex->Lock();
160  if (this->m_Image3dQueue.size() > 0)
161  {
162  ret = this->m_Image3dQueue.front();
163  this->m_Image3dQueue.pop_front();
164  }
165  this->m_Mutex->Unlock();
166  return ret;
167 }
168 
170 {
172  this->m_Mutex->Lock();
173  if (this->m_TrackingDataQueue.size() > 0)
174  {
175  ret = this->m_TrackingDataQueue.front();
176  this->m_TrackingDataQueue.pop_front();
177  }
178  this->m_Mutex->Unlock();
179  return ret;
180 }
181 
183 {
184  igtl::MessageBase::Pointer ret = nullptr;
185  this->m_Mutex->Lock();
186  if (this->m_CommandQueue.size() > 0)
187  {
188  ret = this->m_CommandQueue.front();
189  this->m_CommandQueue.pop_front();
190  }
191  this->m_Mutex->Unlock();
192  return ret;
193 }
194 
196 {
197  igtl::StringMessage::Pointer ret = nullptr;
198  this->m_Mutex->Lock();
199  if (this->m_StringQueue.size() > 0)
200  {
201  ret = this->m_StringQueue.front();
202  this->m_StringQueue.pop_front();
203  }
204  this->m_Mutex->Unlock();
205  return ret;
206 }
207 
209 {
210  igtl::TransformMessage::Pointer ret = nullptr;
211  this->m_Mutex->Lock();
212  if (this->m_TransformQueue.size() > 0)
213  {
214  ret = this->m_TransformQueue.front();
215  this->m_TransformQueue.pop_front();
216  }
217  this->m_Mutex->Unlock();
218  return ret;
219 }
220 
222 {
223  this->m_Mutex->Lock();
224  std::stringstream s;
225  if (this->m_Latest_Message != nullptr)
226  {
227  s << "Device Type: " << this->m_Latest_Message->GetDeviceType() << std::endl;
228  s << "Device Name: " << this->m_Latest_Message->GetDeviceName() << std::endl;
229  }
230  else
231  {
232  s << "No Msg";
233  }
234  this->m_Mutex->Unlock();
235  return s.str();
236 }
237 
239 {
240  this->m_Mutex->Lock();
241  std::stringstream s;
242  if (m_Latest_Message != nullptr)
243  {
244  s << this->m_Latest_Message->GetDeviceType();
245  }
246  else
247  {
248  s << "";
249  }
250  this->m_Mutex->Unlock();
251  return s.str();
252 }
253 
255 {
256  this->m_Mutex->Lock();
257  std::stringstream s;
258  if (m_Latest_Message != nullptr)
259  {
260  s << "Device Type: " << this->m_Latest_Message->GetDeviceType() << std::endl;
261  s << "Device Name: " << this->m_Latest_Message->GetDeviceName() << std::endl;
262  }
263  else
264  {
265  s << "No Msg";
266  }
267  this->m_Mutex->Unlock();
268  return s.str();
269 }
270 
272 {
273  this->m_Mutex->Lock();
274  std::stringstream s;
275  if (m_Latest_Message != nullptr)
276  {
277  s << this->m_Latest_Message->GetDeviceType();
278  }
279  else
280  {
281  s << "";
282  }
283  this->m_Mutex->Unlock();
284  return s.str();
285 }
286 
288 {
289  return (this->m_CommandQueue.size() + this->m_Image2dQueue.size() + this->m_Image3dQueue.size() + this->m_MiscQueue.size()
290  + this->m_StringQueue.size() + this->m_TrackingDataQueue.size() + this->m_TransformQueue.size());
291 }
292 
294 {
295  this->m_Mutex->Lock();
296  if (enable)
297  this->m_BufferingType = IGTLMessageQueue::BufferingType::Infinit;
298  else
299  this->m_BufferingType = IGTLMessageQueue::BufferingType::NoBuffering;
300  this->m_Mutex->Unlock();
301 }
302 
304 {
305  this->m_Mutex = itk::FastMutexLock::New();
306  this->m_BufferingType = IGTLMessageQueue::Infinit;
307 }
308 
310 {
311  this->m_Mutex->Unlock();
312 }
itk::SmartPointer< Self > Pointer
itk::FastMutexLock::Pointer m_Mutex
Mutex to take car of the queue.
#define MITK_INFO
Definition: mitkLogMacros.h:22
igtl::ImageMessage::Pointer PullImage2dMessage()
BufferingType m_BufferingType
defines the kind of buffering
void PushSendMessage(igtl::MessageBase::Pointer message)
void PushCommandMessage(igtl::MessageBase::Pointer message)
Adds the message to the queue.
void PushMessage(igtl::MessageBase::Pointer message)
Adds the message to the queue.
void EnableInfiniteBuffering(bool enable)
Sets infinite buffering on/off. Initiale value is enabled.
igtl::StringMessage::Pointer PullStringMessage()
std::string GetNextMsgInformationString()
Returns a string with information about the oldest message in the queue.
igtl::TransformMessage::Pointer PullTransformMessage()
igtl::MessageBase::Pointer PullCommandMessage()
std::deque< igtl::MessageBase::Pointer > m_SendQueue
std::string GetLatestMsgInformationString()
Returns a string with information about the oldest message in the queue.
std::string GetLatestMsgDeviceType()
Returns the device type of the oldest message in the queue.
igtl::TrackingDataMessage::Pointer PullTrackingMessage()
igtl::ImageMessage::Pointer PullImage3dMessage()
std::string GetNextMsgDeviceType()
Returns the device type of the oldest message in the queue.
int GetSize()
Get the number of messages in the queue.
igtl::MessageBase::Pointer PullSendMessage()
igtl::MessageBase::Pointer PullMiscMessage()
Returns and removes the oldest message from the queue.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.