35#include "BESServiceRegistry.h"
37#include "BESInternalError.h"
46static std::once_flag d_euc_init_once;
48BESServiceRegistry::BESServiceRegistry() {}
50BESServiceRegistry::~BESServiceRegistry() {}
60 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
62 map<string,map<string,service_cmd> >::iterator i = _services.find( name ) ;
63 if( i == _services.end() )
65 map<string,service_cmd> cmds ;
66 _services[name] = cmds ;
70 string err = (string)
"The service " + name
71 +
" has already been registered" ;
93 const string &cmd_descript,
94 const string &format )
96 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
98 map<string,map<string,service_cmd> >::iterator si ;
99 si = _services.find( service ) ;
100 if( si != _services.end() )
103 ci = (*si).second.find( cmd ) ;
104 if( ci != (*si).second.end() )
106 string err = (string)
"Attempting to add command "
107 + (*ci).first +
" to the service "
108 + service +
", command alrady exists" ;
112 sc._description = cmd_descript ;
113 sc._formats[format] = format ;
114 (*si).second[cmd] = sc ;
118 string err = (string)
"Attempting to add commands to the service "
119 + service +
" that has not yet been registered" ;
135 const string &format )
137 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
139 map<string,map<string,service_cmd> >::iterator si ;
140 si = _services.find( service ) ;
141 if( si != _services.end() )
144 if( ci != (*si).second.end() )
147 fi = (*ci).second._formats.find( format ) ;
148 if( fi == (*ci).second._formats.end() )
150 (*ci).second._formats[format] = format ;
154 string err = (string)
"Attempting to add format "
155 + format +
" to command " + cmd
156 +
" for service " + service
157 +
" where the format has already been registered" ;
163 string err = (string)
"Attempting to add a format " + format
164 +
" to command " + cmd +
" for service " + service
165 +
" where the command has not been registered" ;
171 string err = (string)
"Attempting to add a format " + format
172 +
" to command " + cmd +
" for a service " + service
173 +
" that has not been registered" ;
189 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
191 map<string,map<string,service_cmd> >::iterator i ;
192 i = _services.find( service ) ;
193 if( i != _services.end() )
196 _services.erase( i ) ;
201 map<string,map<string,string> >::iterator hi = _handles.begin() ;
202 map<string,map<string,string> >::iterator he = _handles.end() ;
203 for( ; hi != he; hi++ )
206 if( hsi != (*hi).second.end() )
208 (*hi).second.erase( hsi ) ;
231 const string &format )
233 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
236 map<string,map<string,service_cmd> >::iterator si ;
237 si = _services.find( service ) ;
238 if( si != _services.end() )
243 if( ci != (*si).second.end() )
245 if( !format.empty() )
248 fi = (*ci).second._formats.find( format ) ;
249 if( fi != (*ci).second._formats.end() )
281 const string &service )
283 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
285 map<string,map<string,service_cmd> >::iterator si ;
286 si = _services.find( service ) ;
287 if( si == _services.end() )
289 string err = (string)
"Registering a handler to handle service "
290 + service +
" that has not yet been registered" ;
294 map<string,map<string,string> >::iterator hi = _handles.find( handler ) ;
295 if( hi == _handles.end() )
297 map<string,string> services ;
298 services[service] = service ;
299 _handles[handler] = services ;
304 if( ci == (*hi).second.end() )
306 (*hi).second[service] = service ;
321 const string &service )
323 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
325 bool handled = false ;
326 map<string,map<string,string> >::iterator hi = _handles.find( handler ) ;
327 if( hi != _handles.end() )
330 if( si != (*hi).second.end() )
348 list<string> &services )
350 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
352 map<string,map<string,string> >::iterator hi = _handles.find( handler ) ;
353 if( hi != _handles.end() )
357 for( ; si != se; si++ )
359 services.push_back( (*si).second ) ;
375 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
377 auto si = _services.begin() ;
378 auto se = _services.end() ;
379 for( ; si != se; si++ )
381 map<string, string, std::less<>> props ;
382 props[
"name"] = (*si).first ;
383 info.begin_tag(
"serviceDescription", &props ) ;
384 auto ci = (*si).second.begin() ;
385 auto ce = (*si).second.end() ;
386 for( ; ci != ce; ci++ )
388 map<string, string, std::less<>> cprops ;
389 cprops[
"name"] = (*ci).first ;
390 info.begin_tag(
"command", &cprops ) ;
391 info.add_tag(
"description", (*ci).second._description ) ;
392 auto fi = (*ci).second._formats.begin() ;
393 auto fe = (*ci).second._formats.end() ;
394 for( ; fi != fe; fi++ )
396 map<string, string, std::less<>> fprops ;
397 fprops[
"name"] = (*fi).first ;
398 info.add_tag(
"format",
"", &fprops ) ;
400 info.end_tag(
"command" ) ;
402 info.end_tag(
"serviceDescription" ) ;
416 std::lock_guard<std::recursive_mutex> lock_me(d_cache_lock_mutex);
418 strm << BESIndent::LMarg <<
"BESServiceRegistry::dump - ("
419 << (
void *)
this <<
")" << endl ;
420 BESIndent::Indent() ;
421 strm << BESIndent::LMarg <<
"registered services" << endl ;
422 BESIndent::Indent() ;
423 map<string,map<string,service_cmd> >::const_iterator si ;
424 si = _services.begin() ;
425 map<string,map<string,service_cmd> >::const_iterator se ;
426 se = _services.end() ;
427 for( ; si != se; si++ )
429 strm << BESIndent::LMarg << (*si).first << endl ;
430 BESIndent::Indent() ;
433 for( ; ci != ce; ci++ )
435 strm << BESIndent::LMarg << (*ci).first << endl ;
436 BESIndent::Indent() ;
437 strm << BESIndent::LMarg <<
"description: "
438 << (*ci).second._description << endl ;
439 strm << BESIndent::LMarg <<
"formats:" << endl ;
440 BESIndent::Indent() ;
442 fi = (*ci).second._formats.begin() ;
444 fe = (*ci).second._formats.end() ;
445 for( ; fi != fe; fi++ )
447 strm << BESIndent::LMarg << (*fi).first << endl ;
449 BESIndent::UnIndent() ;
450 BESIndent::UnIndent() ;
452 BESIndent::UnIndent() ;
454 BESIndent::UnIndent() ;
455 strm << BESIndent::LMarg <<
"services provided by handler" << endl ;
456 BESIndent::Indent() ;
457 map<string,map<string,string> >::const_iterator hi = _handles.begin() ;
458 map<string,map<string,string> >::const_iterator he = _handles.end() ;
459 for( ; hi != he; hi++ )
461 strm << BESIndent::LMarg << (*hi).first ;
464 bool isfirst = true ;
465 for( ; hsi != hse; hsi++ )
467 if( !isfirst ) strm <<
", " ;
469 strm << (*hsi).first ;
474 BESIndent::UnIndent() ;
475 BESIndent::UnIndent() ;
479BESServiceRegistry::TheRegistry()
481 std::call_once(d_euc_init_once,BESServiceRegistry::initialize_instance);
485void BESServiceRegistry::initialize_instance() {
486 d_instance =
new BESServiceRegistry;
488 atexit(delete_instance);
492void BESServiceRegistry::delete_instance() {
informational response object
exception thrown if internal error encountered
The service registry allows modules to register services with the BES that they provide.
virtual bool does_handle_service(const std::string &handler, const std::string &service)
Asks if the specified handler can handle the specified service.
virtual void services_handled(const std::string &handler, std::list< std::string > &services)
returns the list of servies provided by the handler in question
virtual bool service_available(const std::string &name, const std::string &cmd="", const std::string &format="")
Determines if a service and, optionally, a command and a return format, is available.
virtual void add_service(const std::string &name)
Add a service to the BES.
virtual void add_to_service(const std::string &service, const std::string &cmd, const std::string &cmd_descript, const std::string &format)
This function allows callers to add to a service that already exists.
virtual void show_services(BESInfo &info)
fills in the response object for the <showService /> request
virtual void handles_service(const std::string &handler, const std::string &service)
The specified handler can handle the specified service.
virtual void add_format(const std::string &service, const std::string &cmd, const std::string &format)
add a format response to a command of a service
virtual void dump(std::ostream &strm) const
dumps information about this object
virtual void remove_service(const std::string &name)
remove a service from the BES