Dripline-Cpp  v2.4.2
Dripline Implementation in C++
hub.cc
Go to the documentation of this file.
1 /*
2  * hub.cc
3  *
4  * Created on: Jan 7, 2016
5  * Author: nsoblath
6  */
7 
8 #define DRIPLINE_API_EXPORTS
9 
10 #include "hub.hh"
11 
12 #include "logger.hh"
13 
14 using std::string;
15 
16 namespace dripline
17 {
18 
19  LOGGER( dlog, "hub" );
20 
21 
22  hub::hub( const scarab::param_node& a_config, const string& a_queue_name, const std::string& a_broker_address, unsigned a_port, const std::string& a_auth_file, const bool a_make_connection) :
23  scarab::cancelable(),
24  service( a_config, a_queue_name, a_broker_address, a_port, a_auth_file, a_make_connection ),
25  f_run_handler(),
26  f_get_handlers(),
27  f_set_handlers(),
28  f_cmd_handlers()
29  {
30  }
31 
33  {
34  }
35 
36  void hub::set_run_handler( const handler_func_t& a_func )
37  {
38  f_run_handler = a_func;
39  LDEBUG( dlog, "Set RUN handler" );
40  return;
41  }
42 
43  void hub::register_get_handler( const std::string& a_key, const handler_func_t& a_func )
44  {
45  f_get_handlers[ a_key ] = a_func;
46  LDEBUG( dlog, "Set GET handler for <" << a_key << ">" );
47  return;
48  }
49 
50  void hub::register_set_handler( const std::string& a_key, const handler_func_t& a_func )
51  {
52  f_set_handlers[ a_key ] = a_func;
53  LDEBUG( dlog, "Set SET handler for <" << a_key << ">" );
54  return;
55  }
56 
57  void hub::register_cmd_handler( const std::string& a_key, const handler_func_t& a_func )
58  {
59  f_cmd_handlers[ a_key ] = a_func;
60  LDEBUG( dlog, "Set CMD handler for <" << a_key << ">" );
61  return;
62  }
63 
64  void hub::remove_get_handler( const std::string& a_key )
65  {
66  if( f_get_handlers.erase( a_key ) == 0 )
67  {
68  LWARN( dlog, "GET handler <" << a_key << "> was not present; nothing was removed" );
69  }
70  else
71  {
72  LDEBUG( dlog, "GET handler <" << a_key << "> was removed" );
73  }
74  return;
75  }
76 
77  void hub::remove_set_handler( const std::string& a_key )
78  {
79  if( f_set_handlers.erase( a_key ) == 0 )
80  {
81  LWARN( dlog, "SET handler <" << a_key << "> was not present; nothing was removed" );
82  }
83  else
84  {
85  LDEBUG( dlog, "SET handler <" << a_key << "> was removed" );
86  }
87  return;
88  }
89 
90  void hub::remove_cmd_handler( const std::string& a_key )
91  {
92  if( f_cmd_handlers.erase( a_key ) == 0 )
93  {
94  LWARN( dlog, "CMD handler <" << a_key << "> was not present; nothing was removed" );
95  }
96  else
97  {
98  LDEBUG( dlog, "CMD handler <" << a_key << "> was removed" );
99  }
100  return;
101  }
102 
104  {
105  return f_run_handler( a_request );
106  }
107 
109  {
110  std::string t_query_type = a_request->parsed_specifier().front();
111  a_request->parsed_specifier().pop_front();
112 
113  try
114  {
115  return f_get_handlers.at( t_query_type )( a_request );
116  }
117  catch( std::out_of_range& e )
118  {
119  LWARN( dlog, "GET query type <" << t_query_type << "> was not understood (" << e.what() << ")" );
120  return a_request->reply( dl_service_error_bad_payload(), "Unrecognized query type or no query type provided: <" + t_query_type + ">" );;
121  }
122  }
123 
125  {
126  std::string t_set_type = a_request->parsed_specifier().front();
127  a_request->parsed_specifier().pop_front();
128 
129  try
130  {
131  return f_set_handlers.at( t_set_type )( a_request );
132  }
133  catch( std::out_of_range& e )
134  {
135  LWARN( dlog, "SET request <" << t_set_type << "> not understood (" << e.what() << ")" );
136  return a_request->reply( dl_service_error_bad_payload(), "Unrecognized set request type or no set request type provided: <" + t_set_type + ">" );
137  }
138  }
139 
141  {
142  // get the instruction before checking the lockout key authentication because we need to have the exception for
143  // the unlock instruction that allows us to force the unlock.
144  std::string t_instruction = a_request->parsed_specifier().front();
145  a_request->parsed_specifier().pop_front();
146 
147  try
148  {
149  return f_cmd_handlers.at( t_instruction )( a_request );
150  }
151  catch( std::out_of_range& e )
152  {
153  LWARN( dlog, "CMD instruction <" << t_instruction << "> not understood (" << e.what() << ")" );
154  return a_request->reply( dl_service_error_bad_payload(), "Instruction <" + t_instruction + "> not understood" );;
155  }
156  }
157 
158 } /* namespace dripline */
handler_funcs_t f_get_handlers
Definition: hub.hh:108
std::function< reply_ptr_t(const dripline::request_ptr_t) > handler_func_t
Definition: hub.hh:73
void remove_get_handler(const std::string &a_key)
Removes a get request handler function.
Definition: hub.cc:64
void register_cmd_handler(const std::string &a_key, const handler_func_t &a_func)
Sets a command request handler function.
Definition: hub.cc:57
virtual ~hub()
Definition: hub.cc:32
hub(const scarab::param_node &a_config=scarab::param_node(), const std::string &a_queue_name="", const std::string &a_broker_address="", unsigned a_port=0, const std::string &a_auth_file="", const bool a_make_connection=true)
Definition: hub.cc:22
std::shared_ptr< msg_request > request_ptr_t
Definition: dripline_fwd.hh:23
void register_get_handler(const std::string &a_key, const handler_func_t &a_func)
Sets a get request handler function.
Definition: hub.cc:43
virtual reply_ptr_t do_cmd_request(const request_ptr_t a_request)
Definition: hub.cc:140
Consumer of Dripline messages on a particular queue.
Definition: service.hh:72
Definition: core.hh:17
handler_func_t f_run_handler
Definition: hub.hh:105
virtual reply_ptr_t do_set_request(const request_ptr_t a_request)
Definition: hub.cc:124
virtual reply_ptr_t do_run_request(const request_ptr_t a_request)
Definition: hub.cc:103
static scarab::logger dlog("agent")
handler_funcs_t f_set_handlers
Definition: hub.hh:109
void register_set_handler(const std::string &a_key, const handler_func_t &a_func)
Sets a set request handler function.
Definition: hub.cc:50
std::shared_ptr< msg_reply > reply_ptr_t
Definition: dripline_fwd.hh:24
void remove_cmd_handler(const std::string &a_key)
Removes a command request handler function.
Definition: hub.cc:90
void set_run_handler(const handler_func_t &a_func)
Sets the run request handler function.
Definition: hub.cc:36
virtual reply_ptr_t do_get_request(const request_ptr_t a_request)
Definition: hub.cc:108
handler_funcs_t f_cmd_handlers
Definition: hub.hh:110
void remove_set_handler(const std::string &a_key)
Removes a set request handler function.
Definition: hub.cc:77