16 #ifndef SICK_BUFFER_MONITOR 17 #define SICK_BUFFER_MONITOR 31 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
106 template < class SICK_MONITOR_CLASS, class SICK_MSG_CLASS >
108 _sick_monitor_instance(monitor_instance), _continue_grabbing(true), _monitor_thread_id(0) {
111 if (pthread_mutex_init(&_container_mutex,NULL) != 0) {
112 throw SickThreadException(
"SickBufferMonitor::SickBufferMonitor: pthread_mutex_init() failed!");
116 if (pthread_mutex_init(&_stream_mutex,NULL) != 0) {
117 throw SickThreadException(
"SickBufferMonitor::SickBufferMonitor: pthread_mutex_init() failed!");
126 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
144 std::cerr << sick_thread_exception.
what() << std::endl;
149 std::cerr <<
"SickBufferMonitor::SetDataStream: Unknown exception!" << std::endl;
159 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
180 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
183 bool acquired_message =
false;
198 acquired_message =
true;
208 std::cerr << sick_thread_exception.
what() << std::endl;
214 std::cerr <<
"SickBufferMonitor::CheckMessageContainer: Unknown exception!" << std::endl;
219 return acquired_message;
226 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
232 void *monitor_result = NULL;
241 throw SickThreadException(
"SickBufferMonitor::StopMonitor: pthread_join() failed!");
247 catch(SickThreadException &sick_thread_exception) {
248 std::cerr << sick_thread_exception.
what() << std::endl;
253 std::cerr <<
"SickBufferMonitor::StopMonitor: Unknown exception!" << std::endl;
262 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
267 throw SickThreadException(
"SickBufferMonitor::AcquireDataStream: pthread_mutex_lock() failed!");
275 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
280 throw SickThreadException(
"SickBufferMonitor::ReleaseDataStream: pthread_mutex_unlock() failed!");
288 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
293 throw SickThreadException(
"SickBufferMonitor::~SickBufferMonitor: pthread_mutex_destroy() failed!");
298 throw SickThreadException(
"SickBufferMonitor::~SickBufferMonitor: pthread_mutex_destroy() failed!");
306 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
311 throw SickThreadException(
"SickBufferMonitor::_acquireMessageContainer: pthread_mutex_lock() failed!");
319 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
324 throw SickThreadException(
"SickBufferMonitor::_releaseMessageContainer: pthread_mutex_unlock() failed!");
336 template<
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
341 int num_bytes_read = 0;
342 int total_num_bytes_read = 0;
343 int num_active_files = 0;
345 struct timeval timeout_val;
346 fd_set file_desc_set;
349 while ( total_num_bytes_read < num_bytes_to_read ) {
352 FD_ZERO(&file_desc_set);
353 FD_SET(_sick_fd,&file_desc_set);
356 memset(&timeout_val,0,
sizeof(timeout_val));
357 timeout_val.tv_usec = timeout_value;
360 num_active_files = select(getdtablesize(),&file_desc_set,0,0,(timeout_value > 0) ? &timeout_val : 0);
363 if (num_active_files > 0) {
371 if (FD_ISSET(_sick_fd,&file_desc_set)) {
374 num_bytes_read = read(_sick_fd,&dest_buffer[total_num_bytes_read],1);
377 if (num_bytes_read > 0) {
378 total_num_bytes_read += num_bytes_read;
382 throw SickIOException(
"SickBufferMonitor::_readBytes: read() failed!");
388 else if (num_active_files == 0) {
397 throw SickIOException(
"SickBufferMonitor::_readBytes: select() failed!");
409 template <
class SICK_MONITOR_CLASS,
class SICK_MSG_CLASS >
413 SICK_MSG_CLASS curr_message;
416 SICK_MONITOR_CLASS *buffer_monitor = (SICK_MONITOR_CLASS *)thread_args;
424 curr_message.Clear();
427 buffer_monitor->AcquireDataStream();
429 if (!buffer_monitor->_continue_grabbing) {
430 buffer_monitor->ReleaseDataStream();
434 buffer_monitor->GetNextMessageFromDataStream(curr_message);
435 buffer_monitor->ReleaseDataStream();
438 buffer_monitor->_acquireMessageContainer();
439 buffer_monitor->_recv_msg_container = curr_message;
440 buffer_monitor->_releaseMessageContainer();
446 std::cerr << sick_io_exception.
what() << std::endl;
451 std::cerr << sick_thread_exception.
what() << std::endl;
456 std::cerr <<
"SickBufferMonitor::_bufferMonitorThread: Unknown exception!" << std::endl;
Contains some simple exception classes.