CernVM-FS  2.13.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 #include "util/logging.h"
12 #include "util/pointer.h"
13 #include "util/string.h"
14 
15 namespace {
16 
18 
19 } // namespace
20 
21 namespace notify {
22 
23 namespace msg {
24 
25 Activity::Activity() : version_(0), timestamp_(), repository_(), manifest_() { }
26 
28 
29 bool Activity::operator==(const Activity &other) const {
30  return (this->version_ == other.version_)
31  && (this->timestamp_ == other.timestamp_)
32  && (this->repository_ == other.repository_)
33  && (this->manifest_ == other.manifest_);
34 }
35 
36 void Activity::ToJSONString(std::string *s) {
37  assert(s);
38 
39  *s = "{ \"version\" : " + StringifyInt(version_) + ", \"timestamp\" : \""
40  + timestamp_ + "\", \"type\" : \"activity\", \"repository\" : \""
41  + repository_ + "\", \"manifest\" : \"" + Base64(manifest_) + "\"}";
42 }
43 
44 bool Activity::FromJSONString(const std::string &s) {
46  if (!m.IsValid()) {
47  LogCvmfs(kLogCvmfs, kLogError, "Could not create JSON document.");
48  return false;
49  }
50 
51  std::string message_type;
52  if (!GetFromJSON(m->root(), "type", &message_type)) {
53  LogCvmfs(kLogCvmfs, kLogError, "Could not read message type.");
54  return false;
55  }
56  if (message_type != "activity") {
57  LogCvmfs(kLogCvmfs, kLogError, "Invalid message type: %s.",
58  message_type.c_str());
59  return false;
60  }
61 
62  if (!GetFromJSON(m->root(), "version", &version_)) {
63  LogCvmfs(kLogCvmfs, kLogError, "Could not read version.");
64  return false;
65  }
66 
67  if (!GetFromJSON(m->root(), "timestamp", &timestamp_)) {
68  LogCvmfs(kLogCvmfs, kLogError, "Could not read timestamp.");
69  return false;
70  }
71  if (!GetFromJSON(m->root(), "repository", &repository_)) {
72  LogCvmfs(kLogCvmfs, kLogError, "Could not read repository.");
73  return false;
74  }
75  std::string manifest_b64;
76  if (!GetFromJSON(m->root(), "manifest", &manifest_b64)) {
77  LogCvmfs(kLogCvmfs, kLogError, "Could not read manifest.");
78  return false;
79  }
80  if (!Debase64(manifest_b64, &manifest_)) {
81  LogCvmfs(kLogCvmfs, kLogError, "Could not debase64 manifest.");
82  return false;
83  }
84 
85  return true;
86 }
87 
88 } // namespace msg
89 
90 } // namespace notify
std::string manifest_
Definition: messages.h:51
bool operator==(const Activity &other) const
Definition: messages.cc:29
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:598
static JsonDocument * Create(const std::string &text)
std::string timestamp_
Definition: messages.h:49
string StringifyInt(const int64_t value)
Definition: string.cc:77
manifest::Manifest * manifest_
Definition: repository.h:147
string Base64(const string &data)
Definition: string.cc:537
std::string repository_
Definition: messages.h:50
virtual bool FromJSONString(const std::string &s)
Definition: messages.cc:44
LogFacilities
virtual ~Activity()
Definition: messages.cc:27
static LogFacilities error
const JSON * root() const
Definition: json_document.h:25
virtual void ToJSONString(std::string *s)
Definition: messages.cc:36
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545