m2etis  0.4
ChimeraResponsible.h
Go to the documentation of this file.
1 /*
2  Copyright (2016) Michael Baer, Daniel Bonrath, All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 
17 #ifndef __M2ETIS_WRAPPER_CHIMERA_CHIMERARESPONSIBLE_H__
18 #define __M2ETIS_WRAPPER_CHIMERA_CHIMERARESPONSIBLE_H__
19 
20 #include <cmath>
21 #include <string>
22 
24 
25 
26 extern "C" {
27  #include "chimera/key.h"
28 }
29 
30 // that's defined in key.c
31 extern "C" char * sha1_keygen(char * key, size_t digest_size, char * digest);
32 
33 
34 namespace m2etis {
35 namespace wrapper {
36 namespace chimera {
37 
39  public:
40  explicit ChimeraResponsible(const std::string & topic) : self_(), root_() {
41  char * got = sha1_keygen(const_cast<char*>(topic.c_str()), topic.size(), 0);
42  root_ = std::string(got);
43  free(got); // yes, I know. But it's created via malloc inside sha1_keygen.
44  }
45 
46  virtual ~ChimeraResponsible() {}
47 
48  void setSelf(const std::string & self) {
49  self_ = self;
50  }
51 
52  bool isResponsible() const {
53  return wrapper::chimera::ChimeraWrapperImpl::GetSingletonPtr()->isResponsibleFor(getRoot(), self_);
54  }
55 
56  // So, now calculate the SHA for the given topic_name
57  std::string getRoot() const {
58  return root_;
59  }
60 
61  private:
62  std::string self_;
63  std::string root_;
64  };
65 
66 } /* namespace chimera */
67 } /* namespace wrapper */
68 } /* namespace m2etis */
69 
70 #endif /* __M2ETIS_WRAPPER_CHIMERA_CHIMERARESPONSIBLE_H__ */
char * sha1_keygen(char *key, size_t digest_size, char *digest)