rgbd
client.cpp
Go to the documentation of this file.
1 #include "rgbd/client.h"
2 
3 #include <opencv2/core/mat.hpp>
4 
5 #include "rgbd/image.h"
6 #include "rgbd/ros/conversions.h"
7 #include "rgbd/utility.h"
8 
9 #include <ros/console.h>
10 #include <ros/duration.h>
11 #include <ros/rate.h>
12 
13 #include <std_msgs/String.h>
14 
15 
16 namespace rgbd {
17 
18 // ----------------------------------------------------------------------------------------
19 
20 Client::Client() : nh_(nullptr), client_impl_mode_(ClientImplMode::rgbd)
21 {
23 }
24 
25 // ----------------------------------------------------------------------------------------
26 
28 {
29  deinitialize();
30 }
31 
32 // ----------------------------------------------------------------------------------------
33 
34 bool Client::initialize(const std::string& server_name, float /*timeout*/)
35 {
36  if (initialized() && server_name_ == server_name)
37  return true;
38  else if (initialized())
39  deinitialize();
40 
41  nh_.reset(new ros::NodeHandle);
42  nh_->setCallbackQueue(&cb_queue_);
43 
44  sub_shm_hosts_ = nh_->subscribe<std_msgs::String>(server_name + "/hosts", 10, &Client::hostsCallback, this);
45 
47 
48  server_name_ = server_name;
49  return true;
50 }
51 
52 // ----------------------------------------------------------------------------------------
53 
55 {
56  if (!initialized())
57  return true;
58 
59  nh_->shutdown();
62 
67 
68  last_time_shm_server_online_ = ros::WallTime();
69  nh_.reset();
70 
71  return true;
72 }
73 
74 // ----------------------------------------------------------------------------------------
75 
77 {
80  return client_shm_.nextImage(image);
81  else // client_impl_mode == ClientImplMode::rgbd
82  return client_rgbd_.nextImage(image);
83 }
84 
85 // ----------------------------------------------------------------------------------------
86 
88 {
91  return client_shm_.nextImage();
92  else // client_impl_mode == ClientImplMode::rgbd
93  return client_rgbd_.nextImage();
94 }
95 
96 // ----------------------------------------------------------------------------------------
97 
98 void Client::hostsCallback(const std_msgs::StringConstPtr& msg)
99 {
100  if (msg->data != hostname_)
101  return;
102 
103  last_time_shm_server_online_ = ros::WallTime::now();
104  ROS_DEBUG_STREAM_THROTTLE_NAMED(5, "Client", "SHM server online on: " << hostname_);
105 }
106 
107 // ----------------------------------------------------------------------------------------
108 
109 void Client::subHostsThreadFunc(const float frequency)
110 {
111  ros::WallRate r(frequency);
112  ros::WallDuration d(3*r.expectedCycleTime().toSec()); // To prevent any issues by a very small delay
113  while(nh_->ok())
114  {
115  cb_queue_.callAvailable();
116  // Decide on implementation mode
117  if (ros::WallTime::now() > last_time_shm_server_online_ + d)
118  {
119  // Too much time has elapsed since last message, use rgbd
120  // Lock the entire switching procedure, but not the decision;
125  {
126  ROS_DEBUG_NAMED("Client", "Switching to ClientRGBD");
128  }
130  }
131  else
132  {
133  // Last message is recent enough to use shm
137  if (!client_shm_.initialized())
138  {
139  ROS_DEBUG_NAMED("Client", "Switching to ClientSHM");
140  // It might take multiple iterations before ClientSHM is initialized.
142  }
144  }
145  r.sleep();
146  }
147 }
148 
149 }
rgbd::ClientSHM::initialized
bool initialized()
Check if the client is initialized. nextImage shouldn't be called if client is not initialized.
Definition: client_shm.h:53
rgbd::ClientRGBD::nextImage
bool nextImage(Image &image)
Get a new Image. If no new image has been received since the last call, no image will be written and ...
Definition: client_rgbd.cpp:44
std::string
std::shared_ptr
rgbd::Client::hostname_
std::string hostname_
Definition: client.h:94
rgbd::Client::hostsCallback
void hostsCallback(const std_msgs::StringConstPtr &msg)
Definition: client.cpp:98
rgbd::Client::sub_hosts_thread_
std::thread sub_hosts_thread_
Definition: client.h:99
rgbd::Client::ClientImplMode::rgbd
@ rgbd
rgbd::get_hostname
std::string get_hostname()
Get the hostname of the current machine.
Definition: utility.cpp:13
std::lock_guard
rgbd::Client::switch_impl_mutex_
std::mutex switch_impl_mutex_
Definition: client.h:102
utility.h
std::unique_ptr::reset
T reset(T... args)
conversions.h
rgbd::ClientRGBD::initialized
bool initialized()
Check if the client is initialized. nextImage will not return an image if client is not initialized.
Definition: client_rgbd.h:55
rgbd::Client::~Client
virtual ~Client()
Destructor.
Definition: client.cpp:27
std::thread::joinable
T joinable(T... args)
rgbd::Image
Definition: image.h:43
rgbd::Client::initialized
bool initialized()
Check if the client is initialized. Checks if nh_ is exists. nextImage will not return an image if cl...
Definition: client.h:62
rgbd::ClientSHM::initialize
bool initialize(const std::string &server_name, float timeout=5.0)
Initialize shared memory client.
Definition: client_shm.cpp:33
std::thread
rgbd::Client::client_rgbd_
ClientRGBD client_rgbd_
Definition: client.h:86
rgbd::Client::deinitialize
bool deinitialize()
Calls deinitialize on implementation clients. Shutsdown both implementations and deletes nh_....
Definition: client.cpp:54
rgbd::ClientSHM::deinitialize
bool deinitialize()
Clears all the shared memory classes to nullptrs or empty classes. initialized will now return false.
Definition: client_shm.cpp:77
rgbd::Client::initialize
bool initialize(const std::string &server_name, float timeout=5.0)
Initialize the client.
Definition: client.cpp:34
rgbd
Definition: client.h:24
rgbd::ClientRGBD::initialize
bool initialize(const std::string &server_name)
Initialize the client.
Definition: client_rgbd.cpp:22
rgbd::Client::last_time_shm_server_online_
ros::WallTime last_time_shm_server_online_
Definition: client.h:97
rgbd::ClientRGBD::deinitialize
bool deinitialize()
Clears the subscriber. initialized will now return false.
Definition: client_rgbd.cpp:36
rgbd::Client::Client
Client()
Constructor.
Definition: client.cpp:20
image.h
rgbd::Client::server_name_
std::string server_name_
Definition: client.h:95
rgbd::Client::nextImage
ImagePtr nextImage()
Get a new Image. If no new image has been received since the last call, The ImagePtr will be a nullpt...
Definition: client.cpp:87
rgbd::ClientSHM::nextImage
bool nextImage(Image &image)
Get a new Image. If no new image has been received, the sequence nummer is still the same as the prev...
Definition: client_shm.cpp:89
rgbd::Client::cb_queue_
ros::CallbackQueue cb_queue_
Definition: client.h:91
rgbd::Client::sub_shm_hosts_
ros::Subscriber sub_shm_hosts_
Definition: client.h:92
rgbd::Client::ClientImplMode::shm
@ shm
rgbd::Client::ClientImplMode
ClientImplMode
Definition: client.h:81
rgbd::Client::client_impl_mode_
ClientImplMode client_impl_mode_
Definition: client.h:101
rgbd::Client::subHostsThreadFunc
void subHostsThreadFunc(const float frequency)
Definition: client.cpp:109
client.h
std::thread::join
T join(T... args)
rgbd::Client::nh_
std::unique_ptr< ros::NodeHandle > nh_
Definition: client.h:90
rgbd::Client::client_shm_
ClientSHM client_shm_
Definition: client.h:88