21 #ifndef __SSRC_WISP_UTILITY_SERVICE_MAIN_H
22 #define __SSRC_WISP_UTILITY_SERVICE_MAIN_H
32 template<
typename service_type,
typename options_type>
33 std::unique_ptr<service_type>
new_service(
const options_type & options) {
34 return std::make_unique<service_type>(options.connection,
39 template<
typename service_type,
typename options_type,
typename init_type>
40 std::unique_ptr<service_type>
new_service(
const options_type & options,
41 const init_type & initializer)
43 return std::make_unique<service_type>(initializer,
49 template<
typename service_type,
typename options_type,
bool has_initializer>
52 template<
typename service_type,
typename options_type>
55 std::unique_ptr<service_type>
operator()(
const options_type & options) {
56 return new_service<service_type>(options);
60 template<
typename service_type,
typename options_type>
63 std::unique_ptr<service_type>
operator()(
const options_type & options) {
64 return new_service<service_type>(options, options.initializer);
69 template<
typename CustomOptions>
72 std::cout << options.description << std::endl;
74 }
else if(options.wisp_version) {
75 std::cout <<
"Wisp version: " << WISP_VERSION << std::endl;
77 }
else if(options.arg_error) {
78 std::cerr <<
"Service option error: " << options.arg_error_msg
83 if(options.connection.empty()) {
84 std::cerr <<
"Service error: You must specify a connection.\n";
88 if(options.locale.size() > 0)
89 std::locale::global(std::locale(std::locale::classic(),
90 options.locale.c_str(),
96 template<
typename ProtocolProcessor,
97 template <
typename PP,
typename...>
class EH,
99 typename CustomOptionsProcessor,
100 bool has_initializer>
104 using NS_SSRC_WISP_UTILITY::ServiceOptions;
107 typedef std::unique_ptr<Options> options_ptr;
108 typedef std::unique_ptr<CustomOptionsProcessor> options_processor_ptr;
109 typedef Service<EH<ProtocolProcessor> > service_type;
115 std::string service_name;
118 options_processor_ptr options_processor(make_smart_ptr<options_processor_ptr>());
119 options_ptr options(make_smart_ptr<options_ptr>(argc, argv));
120 int exit_code = options_processor->process_options(*options);
122 if(exit_code != -1) {
126 typename service_factory::service_ptr service =
new_service(*options);
128 service_name = service->name();
129 service->
start(loop, options->call_timeout);
132 options_processor.reset();
135 }
catch(
const NS_SSRC_SPREAD::Error & spread_error) {
136 std::cerr <<
"Service error: " << service_name
137 <<
" : Caught ssrc::spread::Error\n";
138 spread_error.print();
140 }
catch(
const std::exception & std_error) {
141 std::cerr <<
"Service error: " << service_name <<
" : "
142 << std_error.what() << std::endl;
145 std::cerr <<
"Service error: " << service_name
146 <<
" : Caught unknown exception!\n";
153 #define WISP_EMPTY_INIT_CODE
155 #define WISP_CUSTOM_SERVICE_MAIN_WITH_INIT(service_type, handler_type, options_type, options_processor_type, has_initializer, init_code) \
156 int main(int argc, char *argv[]) { \
158 return NS_SSRC_WISP_UTILITY::service_main<service_type, handler_type, options_type, options_processor_type, has_initializer>(argc, argv); \
161 #define WISP_CUSTOM_SERVICE_MAIN(service_type, handler_type, options_type, options_processor_type, has_initializer) \
162 WISP_CUSTOM_SERVICE_MAIN_WITH_INIT(service_type, handler_type, options_type, options_processor_type, has_initializer, WISP_EMPTY_INIT_CODE)
164 #define WISP_SERVICE_MAIN_WITH_INIT(service_type, init_code) \
165 WISP_CUSTOM_SERVICE_MAIN_WITH_INIT(service_type, \
166 NS_SSRC_WISP_SERVICE::ServiceEventHandler, \
167 NS_SSRC_WISP_UTILITY::ServiceMainOptions, \
168 NS_SSRC_WISP_UTILITY::ServiceMainOptionsProcessor, \
171 #define WISP_SERVICE_MAIN(service_type) \
172 WISP_SERVICE_MAIN_WITH_INIT(service_type, WISP_EMPTY_INIT_CODE)