Cafer_core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
component.hpp
Go to the documentation of this file.
1 //| This file is a part of the CAFER framework developped within
2 //| the DREAM project (http://www.robotsthatdream.eu/).
3 //| Copyright 2015, ISIR / Universite Pierre et Marie Curie (UPMC)
4 //| Main contributor(s):
5 //| * Stephane Doncieux, stephane.doncieux@isir.upmc.fr
6 //|
7 //|
8 //| This experiment allows to generate neural networks for simple
9 //| navigation tasks (obstacle avoidance and maze navigation).
10 //|
11 //| This software is governed by the CeCILL license under French law
12 //| and abiding by the rules of distribution of free software. You
13 //| can use, modify and/ or redistribute the software under the terms
14 //| of the CeCILL license as circulated by CEA, CNRS and INRIA at the
15 //| following URL "http://www.cecill.info".
16 //|
17 //| As a counterpart to the access to the source code and rights to
18 //| copy, modify and redistribute granted by the license, users are
19 //| provided only with a limited warranty and the software's author,
20 //| the holder of the economic rights, and the successive licensors
21 //| have only limited liability.
22 //|
23 //| In this respect, the user's attention is drawn to the risks
24 //| associated with loading, using, modifying and/or developing or
25 //| reproducing the software by the user in light of its specific
26 //| status of free software, that may mean that it is complicated to
27 //| manipulate, and that also therefore means that it is reserved for
28 //| developers and experienced professionals having in-depth computer
29 //| knowledge. Users are therefore encouraged to load and test the
30 //| software's suitability as regards their requirements in conditions
31 //| enabling the security of their systems and/or data to be ensured
32 //| and, more generally, to use and operate it in the same conditions
33 //| as regards security.
34 //|
35 //| The fact that you are presently reading this means that you have
36 //| had knowledge of the CeCILL license and that you accept its terms.
37 
38 #ifndef _SFERES_CAFER_HPP
39 #define _SFERES_CAFER_HPP
40 
41 #include <unordered_map>
42 #include <functional>
43 
44 #include "cafer_core/Management.h"
45 #include "cafer_core/GetID.h"
46 #include "cafer_core/aliases.hpp"
47 
48 #include <ros/ros.h>
49 #include <ros/spinner.h>
50 #include <ros/callback_queue.h>
51 
52 
53 namespace cafer_core {
54 
55  void init(int argc, char **argv, std::string node_name);
56  // std::string get_node_group(std::string namespace_base, std::string launch_file, double frequency);
57  // void release_node_group(std::string namespace_base, std::string gr_namespace);
58  // void kill_node_group(std::string namespace_base, std::string gr_namespace);
59  // void kill_all_allocated_node_groups(void);
60 
61  typedef enum {
62  CHG_FREQ = 0,
68  } MgmtType;
69 
75  public:
76  std::string ns;
77  long int id;
78  std::string type;
79  };
80 
85  bool operator==(ClientDescriptor const&, ClientDescriptor const&);
86 
88  std::size_t operator()(const ClientDescriptor& cd) const
89  {
90 
91  std::ostringstream oss;
92  oss << cd.ns << "_" << cd.id;
93  std::hash<std::string> string_hash;
94 
95  return string_hash(oss.str());
96  }
97  };
98 
99  using MapWatchDog_t=std::unordered_map<ClientDescriptor, ros::Time, ClientDescriptorHasher>;
100  using CreatedNodes_t=std::unordered_map<std::string, std::vector<ClientDescriptor> >;
101 
102 
110  class Component {
111 
112  public:
113 
121  Component(std::string mgmt_topic, std::string _type, double freq = 10, bool new_nodehandle = false);
122 
123 
125  {
126  //disconnect_from_ros();
127  }
128 
129 
134  virtual void update() = 0;
135 
140  virtual void client_connect_to_ros() = 0;
141 
146  virtual void client_disconnect_from_ros() = 0;
147 
152  virtual void init() = 0;
153 
158  virtual bool is_initialized()
159  { return _is_init; }
160 
161 
168  bool get_terminate(void) const
169  { return terminate; }
170 
175  std::string get_type(void) const
176  { return type; }
177 
178 
183  long int get_id(void) const
184  { return id; }
185 
190  bool is_connected_to_ros() const
191  { return _is_connected_to_ros; }
192 
196  void connect_to_ros(void)
197  {
198  // connection to management topic is done in the constructor only.
200  _is_connected_to_ros = true;
201  }
202 
207  {
209  management_s.reset();
210  watchdog.reset();
211  _is_connected_to_ros = false;
212  sleep();
213  }
214 
218  void shutdown(void)
219  {
221  ros::shutdown();
222  }
223 
231  std::string call_launch_file(std::string launch_file, std::string namespace_base,
232  std::string management_topic = "");
233 
234 
238  void sleep(void)
239  {rate->sleep();}
240 
244  void spin(void);
245 
253  unsigned int how_many_client_from_type(std::string _type, bool up_only = true);
254 
255 
262  void get_connected_client_with_type(std::string _type, std::vector<ClientDescriptor>& vcd, bool up_only = true);
263 
270  bool is_it_recent_enough(ros::Time t);
271 
278  bool is_client_up(std::string ns, long int id)
279  {
280  return is_it_recent_enough(get_watchdog(ns, id));
281  }
282 
283 
289  void wait_for_client(std::string ns, long int id);
290 
294  void wait_for_init(void);
295 
300  std::string get_namespace(void) const
301  {return my_ros_nh->getNamespace();}
302 
308  {return created_nodes;}
309 
315  std::vector<ClientDescriptor>& get_created_nodes(std::string created_ns)
316  {return created_nodes[created_ns];}
317 
321  void kill_created_nodes(void);
322 
327  void management_cb(const cafer_core::Management& mgmt);
328 
332  void ask_new_ack();
333 
337  void ack_creation();
338 
345  void update_watchdog(std::string ns, long int id, std::string _type);
346 
353  ros::Time get_watchdog(std::string ns, long int id);
354 
359  void watchdog_cb(const ros::TimerEvent& event);
360 
366  void send_complete_node_death(std::string ns, long int id);
367 
373  void send_local_node_death(std::string ns, long int id);
374 
375  public:
383 
384 
390  protected :
391  int id;
392  std::string type;
394  std::string creator_ns;
395  std::string created_ns;
396  std::string _mgmt_topic;
397  bool terminate;
407  bool _is_init = false;
408  bool _is_connected_to_ros = false;
409 
410  };
411 
412 }
413 
414 #endif
std::vector< ClientDescriptor > & get_created_nodes(std::string created_ns)
Gets the created_nodes.
Definition: component.hpp:315
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
Definition: component.hpp:376
std::string get_type(void) const
Get type of the client.
Definition: component.hpp:175
std::string _mgmt_topic
Definition: component.hpp:396
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
Definition: component.hpp:385
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
Definition: component.hpp:402
bool is_connected_to_ros() const
this method inform if the subcriber, publisher or server are up or not.
Definition: component.hpp:190
void shutdown(void)
Shutting down the component and the corresponding node.
Definition: component.hpp:218
bool operator==(ClientDescriptor const &, ClientDescriptor const &)
operator == for comparison between client.
std::size_t operator()(const ClientDescriptor &cd) const
Definition: component.hpp:88
ask for acknowledgement to all nodes that are handled by this Component
Definition: component.hpp:67
void disconnect_from_ros(void)
Disconnect the client from ROS, i.e. destroy subscribers and publishers.
Definition: component.hpp:206
changement of the ROS frequency
Definition: component.hpp:62
std::string creator_ns
Definition: component.hpp:394
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
Definition: component.hpp:65
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...
Definition: component.hpp:278
long int get_id(void) const
Accessor to the client id (unique)
Definition: component.hpp:183
void wait_for_init(void)
Waits for the initialization from the client specific side.
std::unordered_map< ClientDescriptor, ros::Time, ClientDescriptorHasher > MapWatchDog_t
Definition: component.hpp:99
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...
Definition: component.hpp:66
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
Definition: aliases.hpp:15
shared_ptr< Publisher > management_p
Definition: component.hpp:387
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
Definition: component.hpp:168
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.
Definition: component.hpp:196
void sleep(void)
Sleep: should be called by the user code once all the things that have to be done during one iteratio...
Definition: component.hpp:238
shared_ptr< ros::CallbackQueue > my_ros_queue
Definition: component.hpp:382
shared_ptr< ros::Rate > rate
Definition: component.hpp:400
Abstract class for the Cafer client Main functionnalities:
Definition: component.hpp:110
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
Definition: component.hpp:381
ask for the death of the component. It is local as a node may have several components and what is ask...
Definition: component.hpp:63
class ClientDescriptor. To describe a client by his namspace (ns), an id and a type ...
Definition: component.hpp:74
std::unordered_map< std::string, std::vector< ClientDescriptor > > CreatedNodes_t
Definition: component.hpp:100
CreatedNodes_t created_nodes
Definition: component.hpp:379
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 ...
virtual bool is_initialized()
is_initialized
Definition: component.hpp:158
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.
Definition: component.hpp:300
void init(int argc, char **argv, std::string node_name)
CreatedNodes_t & get_created_nodes(void)
Gets the created_nodes.
Definition: component.hpp:307
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() ...
Definition: component.hpp:64
std::string created_ns
Definition: component.hpp:395
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...