zookeeper-cpp
ZooKeeper Client for C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
package_registry.cpp
1 #include "package_registry.hpp"
2 
3 #include <stdexcept>
4 
5 namespace zk::server
6 {
7 
9 {
10  std::weak_ptr<package_registry> owner;
11  std::string name;
12 
13  registration_info(std::shared_ptr<package_registry> owner, std::string name) :
14  owner(std::move(owner)),
15  name(std::move(name))
16  { }
17 
18  ~registration_info() noexcept
19  {
20  if (auto strong_owner = owner.lock())
21  strong_owner->unregister_server(*this);
22  }
23 };
24 
26  _lifetime(std::make_shared<int>())
27 { }
28 
29 package_registry::~package_registry() noexcept
30 {
31  std::unique_lock<std::mutex> ax(_protect);
32  _lifetime.reset();
33 }
34 
35 package_registry::registration package_registry::register_classpath_server(std::string version, classpath packages)
36 {
37  std::unique_lock<std::mutex> ax(_protect);
38 
39  auto ret = _registrations.insert({ std::move(version), std::move(packages) });
40  if (!ret.second)
41  throw std::invalid_argument(version + " is already registered");
42 
43  return std::make_shared<registration_info>(std::shared_ptr<package_registry>(_lifetime, this), ret.first->first);
44 }
45 
46 bool package_registry::unregister_server(const registration_info& reg)
47 {
48  std::unique_lock<std::mutex> ax(_protect);
49  return _registrations.erase(reg.name) > 0U;
50 }
51 
53 {
54  if (reg)
55  return unregister_server(*reg);
56  else
57  return false;
58 }
59 
60 package_registry::size_type package_registry::size() const
61 {
62  std::unique_lock<std::mutex> ax(_protect);
63  return _registrations.size();
64 }
65 
66 optional<classpath> package_registry::find_newest_classpath() const
67 {
68  std::unique_lock<std::mutex> ax(_protect);
69  if (_registrations.empty())
70  return nullopt;
71  else
72  return _registrations.rbegin()->second;
73 }
74 
75 }
Represents a collection of JARs or other Java entities that should be provided as the --classpath to ...
Definition: classpath.hpp:18
size_type size() const
How many registrations have been registered?
optional< classpath > find_newest_classpath() const
Get the classpath for running the newest registered server version.
registration register_classpath_server(std::string version, classpath packages)
Register a server that can be created via the specified Java classpath.
bool unregister_server(registration reg)
package_registry()
Create an empty registry.
Represents a version of the data.
Definition: types.hpp:158