38 #ifndef _SFERES_CAFER_HPP
39 #define _SFERES_CAFER_HPP
41 #include <unordered_map>
44 #include "cafer_core/Management.h"
45 #include "cafer_core/GetID.h"
49 #include <ros/spinner.h>
50 #include <ros/callback_queue.h>
53 namespace cafer_core {
55 void init(
int argc,
char **argv, std::string node_name);
91 std::ostringstream oss;
92 oss << cd.
ns <<
"_" << cd.
id;
93 std::hash<std::string> string_hash;
95 return string_hash(oss.str());
99 using MapWatchDog_t=std::unordered_map<ClientDescriptor, ros::Time, ClientDescriptorHasher>;
100 using CreatedNodes_t=std::unordered_map<std::string, std::vector<ClientDescriptor> >;
121 Component(std::string mgmt_topic, std::string _type,
double freq = 10,
bool new_nodehandle =
false);
134 virtual void update() = 0;
152 virtual void init() = 0;
231 std::string
call_launch_file(std::string launch_file, std::string namespace_base,
232 std::string management_topic =
"");
std::vector< ClientDescriptor > & get_created_nodes(std::string created_ns)
Gets the created_nodes.
std::string call_launch_file(std::string launch_file, std::string namespace_base, std::string management_topic="")
Call a launch file. Corresponding nodes are to be launched in a namespace namespace_base_XX where XX ...
MapWatchDog_t map_watchdog
std::string get_type(void) const
Get type of the client.
bool is_it_recent_enough(ros::Time t)
Check if a given time is "recent" or not with respect to the node client frequency.
shared_ptr< Subscriber > management_s
void ack_creation()
Creation of an acknowledgement to respond to an ask for acknowledgment.
Component(std::string mgmt_topic, std::string _type, double freq=10, bool new_nodehandle=false)
constructor of Component
shared_ptr< ros::Timer > watchdog
bool is_connected_to_ros() const
this method inform if the subcriber, publisher or server are up or not.
void shutdown(void)
Shutting down the component and the corresponding node.
bool operator==(ClientDescriptor const &, ClientDescriptor const &)
operator == for comparison between client.
std::size_t operator()(const ClientDescriptor &cd) const
ask for acknowledgement to all nodes that are handled by this Component
void disconnect_from_ros(void)
Disconnect the client from ROS, i.e. destroy subscribers and publishers.
changement of the ROS frequency
void spin(void)
encapsulation of ros::spinOnce()
ros::Time get_watchdog(std::string ns, long int id)
Get the time of the last watchdog message for a particular client.
virtual void init()=0
init the purpose of this method is to make whatever initialization required.
void send_local_node_death(std::string ns, long int id)
send a request for client to disconect him from ros
watchdog message, to tell that the component is still alive
bool is_client_up(std::string ns, long int id)
Check whether a client is up or not (relies on the watchdog functionnality, works only for nodes that...
long int get_id(void) const
Accessor to the client id (unique)
void wait_for_init(void)
Waits for the initialization from the client specific side.
std::unordered_map< ClientDescriptor, ros::Time, ClientDescriptorHasher > MapWatchDog_t
void ask_new_ack()
ask for acknowledgement to all nodes that are handled by this Component
void wait_for_client(std::string ns, long int id)
Waits until the client is up (it needs to be on the same management topic)
message to tell to a component that has required the creation of a node that the creation is complete...
void send_complete_node_death(std::string ns, long int id)
send a request of death to a specific client
boost::shared_ptr< T > shared_ptr
shared_ptr< Publisher > management_p
void kill_created_nodes(void)
kill all nodes created by this component
bool get_terminate(void) const
get_terminate return true if the client is down and false otherwise
virtual void client_connect_to_ros()=0
connect_to_ros all the definition of subscriber and publisher must be done in this method...
void connect_to_ros(void)
Connect the client to ROD, i.e. intantiate subscribers, publishers and services.
void sleep(void)
Sleep: should be called by the user code once all the things that have to be done during one iteratio...
shared_ptr< ros::CallbackQueue > my_ros_queue
shared_ptr< ros::Rate > rate
Abstract class for the Cafer client Main functionnalities:
void get_connected_client_with_type(std::string _type, std::vector< ClientDescriptor > &vcd, bool up_only=true)
Get the ClientDescriptors of all clients connected to the same management topic and of a certain type...
shared_ptr< NodeHandle > my_ros_nh
ask for the death of the component. It is local as a node may have several components and what is ask...
class ClientDescriptor. To describe a client by his namspace (ns), an id and a type ...
std::unordered_map< std::string, std::vector< ClientDescriptor > > CreatedNodes_t
CreatedNodes_t created_nodes
void management_cb(const cafer_core::Management &mgmt)
Management callback: what to do when a management message is received.
unsigned int how_many_client_from_type(std::string _type, bool up_only=true)
Check the number of client nodes that have been observed up to now (watchdog) and return their number...
virtual void update()=0
update Calls the code to take into account what has been received through the subscribers (typically ...
bool _is_connected_to_ros
virtual bool is_initialized()
is_initialized
virtual void client_disconnect_from_ros()=0
disconnect_from_ros the purpose of this methode is to free the memory of all subscribers and publishe...
std::string get_namespace(void) const
Gets node namespace.
void init(int argc, char **argv, std::string node_name)
CreatedNodes_t & get_created_nodes(void)
Gets the created_nodes.
void watchdog_cb(const ros::TimerEvent &event)
Send a watchdog message telling that the client is still alive...
ask for the whole node death (not just the component). It results in a ros:shutdown() ...
void update_watchdog(std::string ns, long int id, std::string _type)
What to do when a watchdog message is received: update the corresponding time in the watchdog map...