CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swissknife_lease_json.cc
Go to the documentation of this file.
1 
6 
7 #include "json.h"
8 #include "json_document.h"
9 #include "util/logging.h"
10 #include "util/pointer.h"
11 
12 //TODO(@vvolkl): refactor
14  std::string *session_token, uint64_t *current_revision, std::string &current_root_hash) {
15  if (buffer.data.size() == 0 || session_token == NULL) {
16  return kLeaseReplyFailure;
17  }
18 
20  if (!reply.IsValid() || !reply->IsValid()) {
21  return kLeaseReplyFailure;
22  }
23 
24  const JSON *result =
25  JsonDocument::SearchInObject(reply->root(), "status", JSON_STRING);
26  if (result != NULL) {
27  const std::string status = result->string_value;
28  if (status == "ok") {
29  LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok");
30  const JSON *token = JsonDocument::SearchInObject(
31  reply->root(), "session_token", JSON_STRING);
32  if (token != NULL) {
33  const JSON *rev = JsonDocument::SearchInObject(reply->root(), "revision", JSON_INT); //TODO FIXME: make the json lib uint64 aware
34  if(rev!=NULL) { *current_revision = (uint64_t) rev->int_value; }
35  const JSON *hash = JsonDocument::SearchInObject(reply->root(), "root_hash", JSON_STRING);
36  if(hash!=NULL) { current_root_hash = hash->string_value; }
37  LogCvmfs(kLogCvmfs, kLogDebug, "Session token: %s",
38  token->string_value);
39  *session_token = token->string_value;
40  return kLeaseReplySuccess;
41  }
42  } else if (status == "path_busy") {
43  const JSON *time_remaining = JsonDocument::SearchInObject(
44  reply->root(), "time_remaining", JSON_STRING);
45  if (time_remaining != NULL) {
46  LogCvmfs(kLogCvmfs, kLogStdout, "Path busy. Time remaining = %s",
47  time_remaining->string_value);
48  return kLeaseReplyBusy;
49  }
50  } else if (status == "error") {
51  const JSON *reason =
52  JsonDocument::SearchInObject(reply->root(), "reason", JSON_STRING);
53  if (reason != NULL) {
54  LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", reason->string_value);
55  }
56  } else {
57  LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s",
58  status.c_str());
59  }
60  }
61 
62  return kLeaseReplyFailure;
63 }
65  std::string *session_token) {
66  if (buffer.data.size() == 0 || session_token == NULL) {
67  return kLeaseReplyFailure;
68  }
69 
71  if (!reply.IsValid() || !reply->IsValid()) {
72  return kLeaseReplyFailure;
73  }
74 
75  const JSON *result = JsonDocument::SearchInObject(reply->root(), "status",
76  JSON_STRING);
77  if (result != NULL) {
78  const std::string status = result->string_value;
79  if (status == "ok") {
80  LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok");
81  const JSON *token = JsonDocument::SearchInObject(
82  reply->root(), "session_token", JSON_STRING);
83  if (token != NULL) {
84  LogCvmfs(kLogCvmfs, kLogDebug, "Session token: %s",
85  token->string_value);
86  *session_token = token->string_value;
87  return kLeaseReplySuccess;
88  }
89  } else if (status == "path_busy") {
90  const JSON *time_remaining = JsonDocument::SearchInObject(
91  reply->root(), "time_remaining", JSON_INT);
92  if (time_remaining != NULL) {
93  LogCvmfs(kLogCvmfs, kLogStdout, "Path busy. Time remaining = %d s",
94  time_remaining->int_value);
95  return kLeaseReplyBusy;
96  }
97  } else if (status == "error") {
98  const JSON *reason = JsonDocument::SearchInObject(reply->root(), "reason",
99  JSON_STRING);
100  if (reason != NULL) {
101  LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", reason->string_value);
102  }
103  } else {
104  LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s",
105  status.c_str());
106  }
107  }
108 
109  return kLeaseReplyFailure;
110 }
111 
112 
114  if (buffer.data.size() == 0) {
115  return kLeaseReplyFailure;
116  }
117 
119  if (!reply.IsValid() || !reply->IsValid()) {
120  return kLeaseReplyFailure;
121  }
122 
123  const JSON *result = JsonDocument::SearchInObject(reply->root(), "status",
124  JSON_STRING);
125  if (result != NULL) {
126  const std::string status = result->string_value;
127  if (status == "ok") {
128  LogCvmfs(kLogCvmfs, kLogStdout, "Gateway reply: ok");
129  return kLeaseReplySuccess;
130  } else if (status == "invalid_token") {
131  LogCvmfs(kLogCvmfs, kLogStdout, "Error: invalid session token");
132  } else if (status == "error") {
133  const JSON *reason = JsonDocument::SearchInObject(reply->root(), "reason",
134  JSON_STRING);
135  if (reason != NULL) {
136  LogCvmfs(kLogCvmfs, kLogStdout, "Error: %s", reason->string_value);
137  }
138  } else {
139  LogCvmfs(kLogCvmfs, kLogStdout, "Unknown reply. Status: %s",
140  status.c_str());
141  }
142  }
143 
144  return kLeaseReplyFailure;
145 }
LeaseReply ParseAcquireReplyWithRevision(const CurlBuffer &buffer, std::string *session_token, uint64_t *current_revision, std::string &current_root_hash)
static JSON * SearchInObject(const JSON *json_object, const std::string &name, const json_type type)
bool IsValid() const
Definition: json_document.h:26
static JsonDocument * Create(const std::string &text)
std::string data
static LeaseReply ParseDropReply(const CurlBuffer &buffer, int llvl)
struct json_value JSON
Definition: helper_allow.cc:11
static LeaseReply ParseAcquireReply(const CurlBuffer &buffer, std::string *session_token, int llvl)
const JSON * root() const
Definition: json_document.h:25
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545