CernVM-FS  2.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
messages.cc
Go to the documentation of this file.
1 
5 #include "messages.h"
6 
7 #include <cassert>
8 
9 #include "json.h"
10 #include "json_document.h"
11 
12 #include "logging.h"
13 #include "util/pointer.h"
14 #include "util/string.h"
15 
16 namespace {
17 
19 
20 } // namespace
21 
22 namespace notify {
23 
24 namespace msg {
25 
26 Activity::Activity() : version_(0), timestamp_(), repository_(), manifest_() {}
27 
29 
30 bool Activity::operator==(const Activity& other) const {
31  return (this->version_ == other.version_) &&
32  (this->timestamp_ == other.timestamp_) &&
33  (this->repository_ == other.repository_) &&
34  (this->manifest_ == other.manifest_);
35 }
36 
37 void Activity::ToJSONString(std::string* s) {
38  assert(s);
39 
40  *s = "{ \"version\" : " + StringifyInt(version_) + ", \"timestamp\" : \"" +
41  timestamp_ + "\", \"type\" : \"activity\", \"repository\" : \"" +
42  repository_ + "\", \"manifest\" : \"" + Base64(manifest_) + "\"}";
43 }
44 
45 bool Activity::FromJSONString(const std::string& s) {
47  if (!m.IsValid()) {
48  LogCvmfs(kLogCvmfs, kLogError, "Could not create JSON document.");
49  return false;
50  }
51 
52  std::string message_type;
53  if (!GetFromJSON(m->root(), "type", &message_type)) {
54  LogCvmfs(kLogCvmfs, kLogError, "Could not read message type.");
55  return false;
56  }
57  if (message_type != "activity") {
58  LogCvmfs(kLogCvmfs, kLogError, "Invalid message type: %s.",
59  message_type.c_str());
60  return false;
61  }
62 
63  if (!GetFromJSON(m->root(), "version", &version_)) {
64  LogCvmfs(kLogCvmfs, kLogError, "Could not read version.");
65  return false;
66  }
67 
68  if (!GetFromJSON(m->root(), "timestamp", &timestamp_)) {
69  LogCvmfs(kLogCvmfs, kLogError, "Could not read timestamp.");
70  return false;
71  }
72  if (!GetFromJSON(m->root(), "repository", &repository_)) {
73  LogCvmfs(kLogCvmfs, kLogError, "Could not read repository.");
74  return false;
75  }
76  std::string manifest_b64;
77  if (!GetFromJSON(m->root(), "manifest", &manifest_b64)) {
78  LogCvmfs(kLogCvmfs, kLogError, "Could not read manifest.");
79  return false;
80  }
81  if (!Debase64(manifest_b64, &manifest_)) {
82  LogCvmfs(kLogCvmfs, kLogError, "Could not debase64 manifest.");
83  return false;
84  }
85 
86  return true;
87 }
88 
89 } // namespace msg
90 
91 } // namespace notify
#define LogCvmfs(source, mask,...)
Definition: logging.h:20
std::string manifest_
Definition: messages.h:49
bool operator==(const Activity &other) const
Definition: messages.cc:30
bool GetFromJSON(const JSON *object, const std::string &name, T *value)
const LogFacilities & kLogError
Definition: cmd_pub.cc:23
assert((mem||(size==0))&&"Out Of Memory")
bool Debase64(const string &data, string *decoded)
Definition: string.cc:534
static JsonDocument * Create(const std::string &text)
std::string timestamp_
Definition: messages.h:47
string StringifyInt(const int64_t value)
Definition: string.cc:78
manifest::Manifest * manifest_
Definition: repository.h:147
string Base64(const string &data)
Definition: string.cc:474
std::string repository_
Definition: messages.h:48
virtual bool FromJSONString(const std::string &s)
Definition: messages.cc:45
LogFacilities
virtual ~Activity()
Definition: messages.cc:28
static LogFacilities error
const JSON * root() const
Definition: json_document.h:25
virtual void ToJSONString(std::string *s)
Definition: messages.cc:37