casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Registrar.h
Go to the documentation of this file.
1 //# Registrar.h: maintain registry of services
2 //# Copyright (C) 2017
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 #ifndef CASATOOLS_PROC_REGISTRAR_H
27 #define CASATOOLS_PROC_REGISTRAR_H
28 #include <set>
29 #include <list>
30 #include <mutex>
31 #include <string>
32 #include <algorithm>
33 
34 namespace casatools {
36  class ServiceId {
37  public:
38  ServiceId( std::string id_p, std::string uri_p, std::list<std::string> types_p, unsigned int priority_p=0 ) : id_(id_p), uri_(uri_p), types_(types_p), priority_(priority_p) { }
39  ServiceId( const ServiceId &other ) : id_(other.id_), uri_(other.uri_), types_(other.types_), priority_(other.priority_) { }
40  ~ServiceId( ) { }
41 
42  std::string id( ) const { return id_; }
43  std::string uri( ) const { return uri_; }
44  std::list<std::string> &types( ) { return types_; }
45  const std::list<std::string> &types( ) const { return types_; }
46  int priority( ) const { return priority_; }
47 
48  operator std::string( ) const { return id_; }
49 
50  bool operator<( const ServiceId &other ) const { return id_ < other.id_; }
51  bool operator>( const ServiceId &other ) const { return id_ > other.id_; }
52  bool operator==( const ServiceId &other ) const { return id_ == other.id_; }
53  bool operator!=( const ServiceId &other ) const { return id_ != other.id_; }
54  bool operator<=( const ServiceId &other ) const { return id_ <= other.id_; }
55  bool operator>=( const ServiceId &other ) const { return id_ >= other.id_; }
56 
57  bool operator<( const std::string &id_o ) const { return id_ < id_o; }
58  bool operator>( const std::string &id_o ) const { return id_ > id_o; }
59  bool operator==( const std::string &id_o ) const { return id_ == id_o; }
60  bool operator!=( const std::string &id_o ) const { return id_ != id_o; }
61  bool operator<=( const std::string &id_o ) const { return id_ <= id_o; }
62  bool operator>=( const std::string &id_o ) const { return id_ >= id_o; }
63 
64  private:
65  std::string id_;
66  std::string uri_;
67  std::list<std::string> types_;
68  int priority_;
69  };
70 
71  class Registrar {
72  public:
73 
74  Registrar( );
75 
76  virtual ~Registrar( );
77 
78  // get map of registrations
79  std::list<ServiceId> services( ) {
80  std::lock_guard<std::mutex> guard(service_list_mutex);
81  return service_list;
82  }
83 
84  // get list of service types
85  std::list<std::string> types( ) {
86  std::lock_guard<std::mutex> guard(service_list_mutex);
87  std::set<std::string> result_set;
88  std::for_each( service_list.begin(), service_list.end( ),
89  [&] (const ServiceId &sid) {
90  result_set.insert(sid.types( ).begin( ), sid.types( ).end( ) ); } );
91 
92  return std::list<std::string>(result_set.begin( ),result_set.end( ));
93  }
94 
95  // returns true if a registration for 'id' was found
96  bool remove( std::string id );
97 
98  // returns assigned identifier (likely based upon the proposed_id)
99  ServiceId add( const ServiceId &proposed );
100 
101  std::string uri( ) {
102  std::lock_guard<std::mutex> guard(uri_mutex);
103  return uri_;
104  }
105 
106  private:
107  std::mutex service_list_mutex;
108  std::list<ServiceId> service_list;
109  std::mutex uri_mutex;
110  std::string uri_;
111 #ifdef USE_GRPC
112  void *grpc_state;
113 #endif
114  };
115 
116 }
117 
118 #endif
ServiceId add(const ServiceId &proposed)
returns assigned identifier (likely based upon the proposed_id)
bool operator>=(const std::string &id_o) const
Definition: Registrar.h:62
const std::list< std::string > & types() const
Definition: Registrar.h:45
bool operator<=(const ServiceId &other) const
Definition: Registrar.h:54
bool operator!=(const std::string &id_o) const
Definition: Registrar.h:60
std::string id_
Definition: Registrar.h:65
bool operator>(const std::string &id_o) const
Definition: Registrar.h:58
std::list< std::string > types_
Definition: Registrar.h:67
bool operator>(const ServiceId &other) const
Definition: Registrar.h:51
namespace for CASAtools classes within &quot;CASA code&quot;
Definition: Registrar.h:36
bool operator>=(const ServiceId &other) const
Definition: Registrar.h:55
bool operator==(const ServiceId &other) const
Definition: Registrar.h:52
bool operator<(const ServiceId &other) const
Definition: Registrar.h:50
std::list< std::string > types()
get list of service types
Definition: Registrar.h:85
std::string id() const
Definition: Registrar.h:42
bool operator==(const std::string &id_o) const
Definition: Registrar.h:59
bool operator<(const std::string &id_o) const
Definition: Registrar.h:57
ServiceId(const ServiceId &other)
Definition: Registrar.h:39
std::string uri()
Definition: Registrar.h:101
bool operator!=(const ServiceId &other) const
Definition: Registrar.h:53
std::list< ServiceId > service_list
Definition: Registrar.h:108
ServiceId(std::string id_p, std::string uri_p, std::list< std::string > types_p, unsigned int priority_p=0)
Definition: Registrar.h:38
std::list< std::string > & types()
Definition: Registrar.h:44
std::mutex uri_mutex
Definition: Registrar.h:109
std::string uri_
Definition: Registrar.h:66
int priority() const
Definition: Registrar.h:46
bool operator<=(const std::string &id_o) const
Definition: Registrar.h:61
std::list< ServiceId > services()
get map of registrations
Definition: Registrar.h:79
std::string uri_
Definition: Registrar.h:110
std::string uri() const
Definition: Registrar.h:43
std::mutex service_list_mutex
Definition: Registrar.h:107