YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Election.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef YARP_OS_ELECTION_H
8#define YARP_OS_ELECTION_H
9
10#include <yarp/os/Log.h>
11
12#include <map>
13#include <mutex>
14#include <string>
15
16namespace yarp::os {
17
18template <class T>
20{
21public:
22 typedef T peer_type;
23 typedef std::map<T*, bool> map_type;
25 typedef typename map_type::iterator iterator;
26 typedef typename map_type::const_iterator const_iterator;
27
28 PeerRecord() = default;
29 PeerRecord(const PeerRecord& alt) = default;
30
31 void add(T* entity)
32 {
33 peerSet[entity] = true;
34 }
35
36 void remove(T* entity)
37 {
38 peerSet.erase(entity);
39 }
40
42 {
43 if (peerSet.begin() != peerSet.end()) {
44 return peerSet.begin()->first;
45 }
46 return nullptr;
47 }
48};
49
50
58template <class PR>
60{
61private:
62 typedef void* voidPtr;
63
64 std::mutex mutex;
65
66 typedef typename std::map<std::string, PR> map_type;
67 map_type nameMap;
68 long ct{0};
69
70 PR* getRecordRaw(const std::string& key, bool create = false)
71 {
72 typename map_type::iterator entry = nameMap.find(key);
73 if (entry == nameMap.end() && create) {
74 nameMap[key] = PR();
75 entry = nameMap.find(key);
76 }
77 if (entry == nameMap.end()) {
78 return nullptr;
79 }
80 return &(entry->second);
81 }
82
83public:
84 ElectionOf() = default;
85 virtual ~ElectionOf() = default;
86
87 PR* add(const std::string& key, typename PR::peer_type* entity)
88 {
89 mutex.lock();
90 ct++;
91 PR* rec = getRecordRaw(key, true);
92 yAssert(rec);
93 rec->add(entity);
94 mutex.unlock();
95 return rec;
96 }
97
98 void remove(const std::string& key, typename PR::peer_type* entity)
99 {
100 mutex.lock();
101 ct++;
102 PR* rec = getRecordRaw(key, false);
103 yAssert(rec);
104 rec->remove(entity);
105 mutex.unlock();
106 }
107
108 typename PR::peer_type* getElect(const std::string& key)
109 {
110 mutex.lock();
111 PR* rec = getRecordRaw(key, false);
112 mutex.unlock();
113 if (rec) {
114 return rec->getFirst();
115 }
116 return nullptr;
117 }
118
119 PR* getRecord(const std::string& key)
120 {
121 mutex.lock();
122 PR* rec = getRecordRaw(key, false);
123 mutex.unlock();
124 return rec;
125 }
126
128 {
129 return ct;
130 }
131
132 void lock()
133 {
134 mutex.lock();
135 }
136
137 void unlock()
138 {
139 mutex.unlock();
140 }
141};
142
143} // namespace yarp::os
144
145#endif // YARP_OS_ELECTION_H
#define yAssert(x)
Definition Log.h:388
A mini-server for performing network communication in the background.
Pick one of a set of peers to be "active".
Definition Election.h:60
virtual ~ElectionOf()=default
PR * add(const std::string &key, typename PR::peer_type *entity)
Definition Election.h:87
PR::peer_type * getElect(const std::string &key)
Definition Election.h:108
PR * getRecord(const std::string &key)
Definition Election.h:119
void remove(const std::string &key, typename PR::peer_type *entity)
Definition Election.h:98
void remove(T *entity)
Definition Election.h:36
PeerRecord(const PeerRecord &alt)=default
map_type::iterator iterator
Definition Election.h:25
void add(T *entity)
Definition Election.h:31
map_type::const_iterator const_iterator
Definition Election.h:26
std::map< T *, bool > map_type
Definition Election.h:23
An interface to the operating system, including Port based communication.