Dripline-Cpp  v2.10.11
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 scarab::authentication& a_auth, const bool a_make_connection) :
23  scarab::cancelable(),
24  service( a_config, a_auth, a_make_connection ),
25  f_run_handler(),
26  f_get_handlers(),
27  f_set_handlers(),
28  f_cmd_handlers()
29  {}
30 
31  hub& hub::operator=( hub&& a_orig )
32  {
33  cancelable::operator=( std::move(a_orig) );
34  service::operator=( std::move(a_orig) );
35 
36  f_run_handler = std::move( a_orig.f_run_handler );
37  f_get_handlers = std::move( a_orig.f_get_handlers );
38  f_set_handlers = std::move( a_orig.f_set_handlers );
39  f_cmd_handlers = std::move( a_orig.f_cmd_handlers );
40 
41  return *this;
42  }
43 
44  void hub::set_run_handler( const handler_func_t& a_func )
45  {
46  f_run_handler = a_func;
47  LDEBUG( dlog, "Set RUN handler" );
48  return;
49  }
50 
51  void hub::register_get_handler( const std::string& a_key, const handler_func_t& a_func )
52  {
53  f_get_handlers[ a_key ] = a_func;
54  LDEBUG( dlog, "Set GET handler for <" << a_key << ">" );
55  return;
56  }
57 
58  void hub::register_set_handler( const std::string& a_key, const handler_func_t& a_func )
59  {
60  f_set_handlers[ a_key ] = a_func;
61  LDEBUG( dlog, "Set SET handler for <" << a_key << ">" );
62  return;
63  }
64 
65  void hub::register_cmd_handler( const std::string& a_key, const handler_func_t& a_func )
66  {
67  f_cmd_handlers[ a_key ] = a_func;
68  LDEBUG( dlog, "Set CMD handler for <" << a_key << ">" );
69  return;
70  }
71 
72  void hub::remove_get_handler( const std::string& a_key )
73  {
74  if( f_get_handlers.erase( a_key ) == 0 )
75  {
76  LWARN( dlog, "GET handler <" << a_key << "> was not present; nothing was removed" );
77  }
78  else
79  {
80  LDEBUG( dlog, "GET handler <" << a_key << "> was removed" );
81  }
82  return;
83  }
84 
85  void hub::remove_set_handler( const std::string& a_key )
86  {
87  if( f_set_handlers.erase( a_key ) == 0 )
88  {
89  LWARN( dlog, "SET handler <" << a_key << "> was not present; nothing was removed" );
90  }
91  else
92  {
93  LDEBUG( dlog, "SET handler <" << a_key << "> was removed" );
94  }
95  return;
96  }
97 
98  void hub::remove_cmd_handler( const std::string& a_key )
99  {
100  if( f_cmd_handlers.erase( a_key ) == 0 )
101  {
102  LWARN( dlog, "CMD handler <" << a_key << "> was not present; nothing was removed" );
103  }
104  else
105  {
106  LDEBUG( dlog, "CMD handler <" << a_key << "> was removed" );
107  }
108  return;
109  }
110 
112  {
113  return f_run_handler( a_request );
114  }
115 
117  {
118  std::string t_query_type = a_request->parsed_specifier().front();
119  a_request->parsed_specifier().pop_front();
120 
121  try
122  {
123  return f_get_handlers.at( t_query_type )( a_request );
124  }
125  catch( std::out_of_range& e )
126  {
127  LWARN( dlog, "GET query type <" << t_query_type << "> was not understood (" << e.what() << ")" );
128  return a_request->reply( dl_service_error_bad_payload(), "Unrecognized query type or no query type provided: <" + t_query_type + ">" );;
129  }
130  }
131 
133  {
134  std::string t_set_type = a_request->parsed_specifier().front();
135  a_request->parsed_specifier().pop_front();
136 
137  try
138  {
139  return f_set_handlers.at( t_set_type )( a_request );
140  }
141  catch( std::out_of_range& e )
142  {
143  LWARN( dlog, "SET request <" << t_set_type << "> not understood (" << e.what() << ")" );
144  return a_request->reply( dl_service_error_bad_payload(), "Unrecognized set request type or no set request type provided: <" + t_set_type + ">" );
145  }
146  }
147 
149  {
150  // get the instruction before checking the lockout key authentication because we need to have the exception for
151  // the unlock instruction that allows us to force the unlock.
152  std::string t_instruction = a_request->parsed_specifier().front();
153  a_request->parsed_specifier().pop_front();
154 
155  try
156  {
157  return f_cmd_handlers.at( t_instruction )( a_request );
158  }
159  catch( std::out_of_range& e )
160  {
161  LWARN( dlog, "CMD instruction <" << t_instruction << "> not understood (" << e.what() << ")" );
162  return a_request->reply( dl_service_error_bad_payload(), "Instruction <" + t_instruction + "> not understood" );;
163  }
164  }
165 
166 } /* namespace dripline */
Service class aimed at adding a Dripline API to an existing codebase.
Definition: hub.hh:71
void register_cmd_handler(const std::string &a_key, const handler_func_t &a_func)
Sets a command request handler function.
Definition: hub.cc:65
void remove_set_handler(const std::string &a_key)
Removes a set request handler function.
Definition: hub.cc:85
virtual reply_ptr_t do_cmd_request(const request_ptr_t a_request)
Definition: hub.cc:148
void register_set_handler(const std::string &a_key, const handler_func_t &a_func)
Sets a set request handler function.
Definition: hub.cc:58
handler_funcs_t f_set_handlers
Definition: hub.hh:138
void remove_cmd_handler(const std::string &a_key)
Removes a command request handler function.
Definition: hub.cc:98
void register_get_handler(const std::string &a_key, const handler_func_t &a_func)
Sets a get request handler function.
Definition: hub.cc:51
std::function< reply_ptr_t(const dripline::request_ptr_t) > handler_func_t
Definition: hub.hh:73
void set_run_handler(const handler_func_t &a_func)
Sets the run request handler function.
Definition: hub.cc:44
virtual reply_ptr_t do_get_request(const request_ptr_t a_request)
Definition: hub.cc:116
handler_funcs_t f_cmd_handlers
Definition: hub.hh:139
handler_funcs_t f_get_handlers
Definition: hub.hh:137
void remove_get_handler(const std::string &a_key)
Removes a get request handler function.
Definition: hub.cc:72
virtual reply_ptr_t do_run_request(const request_ptr_t a_request)
Definition: hub.cc:111
handler_func_t f_run_handler
Definition: hub.hh:134
virtual reply_ptr_t do_set_request(const request_ptr_t a_request)
Definition: hub.cc:132
hub & operator=(const hub &)=delete
hub(const scarab::param_node &a_config, const scarab::authentication &a_auth, const bool a_make_connection=true)
Definition: hub.cc:22
Primary unit of software that connects to a broker and typically provides an interface with an instru...
Definition: service.hh:85
service & operator=(const service &)=delete
std::shared_ptr< msg_reply > reply_ptr_t
Definition: dripline_fwd.hh:24
static ::scarab::logger_type< ::scarab::spd_initializer_async_stdout_color_mt > dlog("agent", __FILE_NAME__, __LINE__)
std::shared_ptr< msg_request > request_ptr_t
Definition: dripline_fwd.hh:23
Definition: agent.hh:18