Dripline-Cpp  v2.4.2
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 ) :
28  scarab::cancelable(),
29  service( a_config, "simple" ),
30  f_return( dl_success().rc_value() )
31  {
32  }
33 
35  {
36  }
37 
39  {
40  scarab::signal_handler t_sig_hand;
41  t_sig_hand.add_cancelable( this );
42 
43  try
44  {
45  if( ! start() ) throw dripline_error() << "Unable to start service";
46 
47  if( ! listen() ) throw dripline_error() << "Unable to start listening";
48 
49  if( ! stop() ) throw dripline_error() << "Unable to stop service";
50  }
51  catch( std::exception& e )
52  {
53  LERROR( dlog, "Exception caught: " << e.what() );
54  f_return = dl_service_error().rc_value() / 100;
55  }
56 
57  if( scarab::signal_handler::get_exited() )
58  {
59  f_return = scarab::signal_handler::get_return_code();
60  }
61 
62  return;
63  }
64 
66  {
67  if( a_request->parsed_specifier().empty() )
68  {
69  return a_request->reply( dl_service_error_invalid_specifier(), "No specifier provided" );
70  }
71 
72  std::string t_specifier = a_request->parsed_specifier().front();
73  a_request->parsed_specifier().pop_front();
74 
75  if( t_specifier == "echo" )
76  {
77  reply_ptr_t t_reply = a_request->reply( dl_success(), "Echoed payload" );
78  LDEBUG( dlog, "Echoing payload: \n" << a_request->payload() );
79  t_reply->set_payload( a_request->payload().clone() );
80  return t_reply;
81  }
82  else if( t_specifier == "error" )
83  {
84  throw std::runtime_error( "An error occurred in the endpoint! (Note: this is a test, this is only a test)" );
85  }
86  else
87  {
88  return a_request->reply( dl_service_error_invalid_specifier(), "Unknown specifier: " + t_specifier );
89  }
90  }
91 
92 
93 
94 } /* namespace dripline */
virtual unsigned rc_value() const
std::shared_ptr< msg_request > request_ptr_t
Definition: dripline_fwd.hh:23
Dripline-specific errors.
static scarab::logger dlog("simple_service")
Consumer of Dripline messages on a particular queue.
Definition: service.hh:72
Definition: core.hh:17
static scarab::logger dlog("agent")
std::shared_ptr< msg_reply > reply_ptr_t
Definition: dripline_fwd.hh:24
virtual reply_ptr_t do_cmd_request(const request_ptr_t a_request)