16 #ifndef SICK_BUFFER_MONITOR 17 #define SICK_BUFFER_MONITOR 30 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
105 template < class SICK_MONITOR_CLASS, class SICK_MSG_CLASS >
107 _sick_monitor_instance(monitor_instance), _continue_grabbing(true), _monitor_thread_id(0) {
110 if (pthread_mutex_init(&_container_mutex,NULL) != 0) {
111 throw SickThreadException(
"SickBufferMonitor::SickBufferMonitor: pthread_mutex_init() failed!");
115 if (pthread_mutex_init(&_stream_mutex,NULL) != 0) {
116 throw SickThreadException(
"SickBufferMonitor::SickBufferMonitor: pthread_mutex_init() failed!");
125 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
143 std::cerr << sick_thread_exception.
what() << std::endl;
148 std::cerr <<
"SickBufferMonitor::SetDataStream: Unknown exception!" << std::endl;
158 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
179 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
182 bool acquired_message =
false;
197 acquired_message =
true;
207 std::cerr << sick_thread_exception.
what() << std::endl;
213 std::cerr <<
"SickBufferMonitor::CheckMessageContainer: Unknown exception!" << std::endl;
218 return acquired_message;
225 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
231 void *monitor_result = NULL;
240 throw SickThreadException(
"SickBufferMonitor::StopMonitor: pthread_join() failed!");
246 catch(SickThreadException &sick_thread_exception) {
247 std::cerr << sick_thread_exception.
what() << std::endl;
252 std::cerr <<
"SickBufferMonitor::StopMonitor: Unknown exception!" << std::endl;
261 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
266 throw SickThreadException(
"SickBufferMonitor::AcquireDataStream: pthread_mutex_lock() failed!");
274 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
279 throw SickThreadException(
"SickBufferMonitor::ReleaseDataStream: pthread_mutex_unlock() failed!");
287 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
292 throw SickThreadException(
"SickBufferMonitor::~SickBufferMonitor: pthread_mutex_destroy() failed!");
297 throw SickThreadException(
"SickBufferMonitor::~SickBufferMonitor: pthread_mutex_destroy() failed!");
305 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
310 throw SickThreadException(
"SickBufferMonitor::_acquireMessageContainer: pthread_mutex_lock() failed!");
318 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
323 throw SickThreadException(
"SickBufferMonitor::_releaseMessageContainer: pthread_mutex_unlock() failed!");
335 template<
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
340 int num_bytes_read = 0;
341 int total_num_bytes_read = 0;
342 int num_active_files = 0;
344 struct timeval timeout_val;
345 fd_set file_desc_set;
348 while ( total_num_bytes_read < num_bytes_to_read ) {
351 FD_ZERO(&file_desc_set);
352 FD_SET(_sick_fd,&file_desc_set);
355 memset(&timeout_val,0,
sizeof(timeout_val));
356 timeout_val.tv_usec = timeout_value;
359 num_active_files = select(getdtablesize(),&file_desc_set,0,0,(timeout_value > 0) ? &timeout_val : 0);
362 if (num_active_files > 0) {
370 if (FD_ISSET(_sick_fd,&file_desc_set)) {
373 num_bytes_read = read(_sick_fd,&dest_buffer[total_num_bytes_read],1);
376 if (num_bytes_read > 0) {
377 total_num_bytes_read += num_bytes_read;
381 throw SickIOException(
"SickBufferMonitor::_readBytes: read() failed!");
387 else if (num_active_files == 0) {
396 throw SickIOException(
"SickBufferMonitor::_readBytes: select() failed!");
408 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
412 SICK_MSG_CLASS curr_message;
415 SICK_MONITOR_CLASS *buffer_monitor = (SICK_MONITOR_CLASS *)thread_args;
423 curr_message.Clear();
426 buffer_monitor->AcquireDataStream();
428 if (!buffer_monitor->_continue_grabbing) {
429 buffer_monitor->ReleaseDataStream();
433 buffer_monitor->GetNextMessageFromDataStream(curr_message);
434 buffer_monitor->ReleaseDataStream();
437 buffer_monitor->_acquireMessageContainer();
438 buffer_monitor->_recv_msg_container = curr_message;
439 buffer_monitor->_releaseMessageContainer();
445 std::cerr << sick_io_exception.
what() << std::endl;
450 std::cerr << sick_thread_exception.
what() << std::endl;
455 std::cerr <<
"SickBufferMonitor::_bufferMonitorThread: Unknown exception!" << std::endl;
Contains some simple exception classes.