Dripline-Cpp  v2.10.11
Dripline Implementation in C++
simple_service.cc
Go to the documentation of this file.
1 /*
2  * simple_service.cc
3  *
4  * Created on: Aug 23, 2018
5  * Author: N.S. Oblath
6  */
7 
8 #define DRIPLINE_EXAMPLES_API_EXPORTS
9 
10 #include "simple_service.hh"
11 
12 #include "dripline_exceptions.hh"
13 
14 #include "logger.hh"
15 #include "macros.hh"
16 #include "param.hh"
17 #include "signal_handler.hh"
18 
19 #include <chrono>
20 #include <thread>
21 
22 LOGGER( dlog, "simple_service" )
23 
24 namespace dripline
25 {
26 
27  simple_service::simple_service( const scarab::param_node& a_config, const scarab::authentication& a_auth ) :
28  scarab::cancelable(),
29  service( a_config, a_auth ),
30  f_return( dl_success().rc_value() )
31  {
32  }
33 
35  {
36  }
37 
39  {
40  auto t_cwrap = scarab::wrap_cancelable( *this );
41  scarab::signal_handler::add_cancelable( t_cwrap );
42 
43  try
44  {
45  run();
46  }
47  catch( std::exception& e )
48  {
49  LERROR( dlog, "Exception caught: " << e.what() );
50  LERROR( dlog, "Exiting service" );
51  f_return = dl_service_error().rc_value() / 100;
52  scarab::signal_handler::cancel_all( f_return );
53  }
54 
55  if( scarab::signal_handler::get_exited() )
56  {
57  f_return = scarab::signal_handler::get_return_code();
58  }
59 
60  return;
61  }
62 
64  {
65  if( a_request->parsed_specifier().empty() )
66  {
67  return a_request->reply( dl_service_error_invalid_specifier(), "No specifier provided" );
68  }
69 
70  std::string t_specifier = a_request->parsed_specifier().front();
71  a_request->parsed_specifier().pop_front();
72 
73  if( t_specifier == "echo" )
74  {
75  reply_ptr_t t_reply = a_request->reply( dl_success(), "Echoed payload" );
76  LDEBUG( dlog, "Echoing payload: \n" << a_request->payload() );
77  t_reply->set_payload( a_request->payload().clone() );
78  return t_reply;
79  }
80  else if( t_specifier == "error" )
81  {
82  throw std::runtime_error( "An error occurred in the endpoint! (Note: this is a test, this is only a test)" );
83  }
84  else
85  {
86  return a_request->reply( dl_service_error_invalid_specifier(), "Unknown specifier: " + t_specifier );
87  }
88  }
89 
90 
91 
92 } /* namespace dripline */
Primary unit of software that connects to a broker and typically provides an interface with an instru...
Definition: service.hh:85
virtual void run()
Definition: service.cc:142
virtual reply_ptr_t do_cmd_request(const request_ptr_t a_request)
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
static ::scarab::logger_type< ::scarab::spd_initializer_async_stdout_color_mt > dlog("simple_service", __FILE_NAME__, __LINE__)
virtual unsigned rc_value() const