Line data Source code
1 :
2 : /* Copyright (c) 2012, Stefan Eilemann <eile@equalizergraphics.com>
3 : *
4 : * This file is part of Collage <https://github.com/Eyescale/Collage>
5 : *
6 : * This library is free software; you can redistribute it and/or modify it under
7 : * the terms of the GNU Lesser General Public License version 2.1 as published
8 : * by the Free Software Foundation.
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 FITNESS
12 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 : * details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public License
16 : * along with this library; if not, write to the Free Software Foundation, Inc.,
17 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #include "zeroconf.h"
21 :
22 : #include <servus/servus.h>
23 : #include <map>
24 :
25 : namespace co
26 : {
27 22 : static const std::string empty_;
28 :
29 : namespace detail
30 : {
31 : typedef std::map< std::string, std::string > ValueMap;
32 : typedef std::map< std::string, ValueMap > InstanceMap;
33 : typedef ValueMap::const_iterator ValueMapCIter;
34 : typedef InstanceMap::const_iterator InstanceMapCIter;
35 :
36 1 : class Zeroconf
37 : {
38 : public:
39 1 : explicit Zeroconf( servus::Servus& service )
40 1 : : service_( service )
41 : {
42 1 : service_.getData( instanceMap_ );
43 1 : }
44 :
45 0 : void set( const std::string& key, const std::string& value )
46 : {
47 0 : service_.set( key, value );
48 0 : }
49 :
50 1 : Strings getInstances() const
51 : {
52 1 : Strings instances;
53 3 : for( InstanceMapCIter i = instanceMap_.begin();
54 2 : i != instanceMap_.end(); ++i )
55 : {
56 0 : instances.push_back( i->first );
57 : }
58 1 : return instances;
59 : }
60 :
61 0 : Strings getKeys( const std::string& instance ) const
62 : {
63 0 : Strings keys;
64 0 : InstanceMapCIter i = instanceMap_.find( instance );
65 0 : if( i == instanceMap_.end( ))
66 0 : return keys;
67 :
68 0 : const ValueMap& values = i->second;
69 0 : for( ValueMapCIter j = values.begin(); j != values.end(); ++j )
70 0 : keys.push_back( j->first );
71 0 : return keys;
72 : }
73 :
74 0 : bool containsKey( const std::string& instance,
75 : const std::string& key ) const
76 : {
77 0 : InstanceMapCIter i = instanceMap_.find( instance );
78 0 : if( i == instanceMap_.end( ))
79 0 : return false;
80 :
81 0 : const ValueMap& values = i->second;
82 0 : ValueMapCIter j = values.find( key );
83 0 : if( j == values.end( ))
84 0 : return false;
85 0 : return true;
86 : }
87 :
88 0 : const std::string& get( const std::string& instance,
89 : const std::string& key ) const
90 : {
91 0 : InstanceMapCIter i = instanceMap_.find( instance );
92 0 : if( i == instanceMap_.end( ))
93 0 : return empty_;
94 :
95 0 : const ValueMap& values = i->second;
96 0 : ValueMapCIter j = values.find( key );
97 0 : if( j == values.end( ))
98 0 : return empty_;
99 0 : return j->second;
100 : }
101 :
102 : private:
103 : servus::Servus& service_;
104 : InstanceMap instanceMap_; //!< copy of discovered data
105 : };
106 : }
107 :
108 1 : Zeroconf::Zeroconf( servus::Servus& service )
109 1 : : _impl( new detail::Zeroconf( service ))
110 1 : {}
111 :
112 0 : Zeroconf::Zeroconf( const Zeroconf& from )
113 0 : : _impl( new detail::Zeroconf( *from._impl ))
114 : {
115 0 : }
116 :
117 1 : Zeroconf::~Zeroconf()
118 : {
119 1 : delete _impl;
120 1 : }
121 :
122 0 : Zeroconf& Zeroconf::operator = ( const Zeroconf& rhs )
123 : {
124 0 : if( this != &rhs )
125 : {
126 0 : delete _impl;
127 0 : _impl = new detail::Zeroconf( *rhs._impl );
128 : }
129 0 : return *this;
130 : }
131 :
132 0 : void Zeroconf::set( const std::string& key, const std::string& value )
133 : {
134 0 : _impl->set( key, value );
135 0 : }
136 :
137 1 : Strings Zeroconf::getInstances() const
138 : {
139 1 : return _impl->getInstances();
140 : }
141 :
142 0 : Strings Zeroconf::getKeys( const std::string& instance ) const
143 : {
144 0 : return _impl->getKeys( instance );
145 : }
146 :
147 0 : bool Zeroconf::containsKey( const std::string& instance,
148 : const std::string& key ) const
149 : {
150 0 : return _impl->containsKey( instance, key );
151 : }
152 :
153 0 : const std::string& Zeroconf::get( const std::string& instance,
154 : const std::string& key ) const
155 : {
156 0 : return _impl->get( instance, key );
157 : }
158 :
159 66 : }
|