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