Commit 5266a62f authored by U-BENTLEY\Aleksandr.Konstantin's avatar U-BENTLEY\Aleksandr.Konstantin
Browse files

Quick implementation for json infosys output. to be refactored and cleaned a bit.

parent 7c6cca62
Pipeline #914 passed with stage
in 33 minutes and 48 seconds
......@@ -265,14 +265,32 @@ Arc::MCC_Status ARexService::GetLogs(Arc::Message& inmsg,Arc::Message& outmsg,AR
}
Arc::MCC_Status ARexService::GetInfo(Arc::Message& inmsg,Arc::Message& outmsg,ARexGMConfig& config,std::string const& subpath) {
if(!subpath.empty()) return Arc::MCC_Status(Arc::UNKNOWN_SERVICE_ERROR);
int h = infodoc_.OpenDocument();
if(h == -1) return Arc::MCC_Status();
Arc::MessagePayload* payload = newFileRead(h);
if(!payload) { ::close(h); return Arc::MCC_Status(); };
outmsg.Payload(payload);
outmsg.Attributes()->set("HTTP:content-type","text/xml");
return Arc::MCC_Status(Arc::STATUS_OK);
if(subpath == "json") {
std::string fname = config_.ControlDir() + "/info.json";
int file = ::open(fname.c_str(),O_RDONLY);
if(file != -1) {
off_t range_start;
off_t range_end;
ExtractRange(inmsg, range_start, range_end);
Arc::MessagePayload* h = newFileRead(file,range_start,range_end);
if(h) {
outmsg.Payload(h);
outmsg.Attributes()->set("HTTP:content-type","application/json");
return Arc::MCC_Status(Arc::STATUS_OK);
} else {
::close(file);
}
};
} else if ((subpath == "xml") || subpath.empty()) {
int h = infodoc_.OpenDocument();
if(h == -1) return Arc::MCC_Status();
Arc::MessagePayload* payload = newFileRead(h);
if(!payload) { ::close(h); return Arc::MCC_Status(); };
outmsg.Payload(payload);
outmsg.Attributes()->set("HTTP:content-type","text/xml");
return Arc::MCC_Status(Arc::STATUS_OK);
}
return Arc::MCC_Status(Arc::UNKNOWN_SERVICE_ERROR);
}
Arc::MCC_Status ARexService::GetNew(Arc::Message& inmsg,Arc::Message& outmsg,ARexGMConfig& config,std::string const& subpath) {
......
......@@ -3,6 +3,7 @@
#endif
#include <sstream>
#include <set>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
......@@ -27,6 +28,91 @@
namespace ARex {
static void JSONStringOut(std::string const& str, std::ostream& file) {
file << "\"";
for(std::string::size_type p = 0; p < str.length(); ++p) {
if(str[p] == '\"') {
file << "\\\"";
} else if(str[p] == '\r') {
file << "\\r";
} else if(str[p] == '\n') {
file << "\\n";
} else if(str[p] == '\t') {
file << "\\t";
} else {
file << str[p];
}
}
file << "\"";
}
static bool isblank(std::string const& str) {
for(std::string::size_type pos = 0; pos < str.length(); ++ pos) {
if(!isspace(str[pos])) return false;
}
return true;
}
static bool isnumber(std::string const& str) {
char* e(NULL);
(void)strtold(str.c_str(), &e);
return (e != NULL) && ((*e) == '\0');
}
static void XMLtoJSON(Arc::XMLNode& xml, std::ostream& file);
static void XMLValuetoJSON(Arc::XMLNode& xml, std::ostream& file);
static void XMLValuetoJSON(Arc::XMLNodeList& xmls, std::ostream& file);
static void XMLValuetoJSON(Arc::XMLNode& xml, std::ostream& file) {
if(xml.Size() <= 0) {
std::string value = static_cast<std::string>(xml);
if(isblank(value)) {
JSONStringOut(value, file);
} else if((value == "true") || (value == "false") || isnumber(value))
file << value;
else
JSONStringOut(value, file);
} else {
std::set<std::string> names;
file << "{" << std::endl;
for(int n = 0; n < xml.Size(); ++n) {
std::string name = xml.Child(n).Name();
if(names.count(name) > 0) continue;
names.insert(name);
if(n != 0) file << "," << std::endl;
JSONStringOut(name, file);
file << " : ";
Arc::XMLNodeList xmls = xml.Path(name);
XMLValuetoJSON(xmls, file);
}
file << std::endl << "}";
}
}
static void XMLValuetoJSON(Arc::XMLNodeList& xmls, std::ostream& file) {
if(xmls.size() <= 0) return;
if(xmls.size() == 1) {
XMLValuetoJSON(xmls.front(), file);
} else {
file << "[" << std::endl;
Arc::XMLNodeList::iterator node = xmls.begin();
XMLValuetoJSON(*node, file);
for(++node; node != xmls.end(); ++node) {
file << "," << std::endl;
XMLValuetoJSON(*node, file);
}
file << std::endl << "]";
}
}
static void XMLtoJSON(Arc::XMLNode& xml, std::ostream& file) {
std::string name = xml.Name();
JSONStringOut(name, file);
file << " : ";
XMLValuetoJSON(xml, file);
}
void ARexService::InformationCollector(void) {
thread_count_.RegisterThread();
for(;;) {
......@@ -72,7 +158,13 @@ void ARexService::InformationCollector(void) {
Arc::stringto((std::string)all_jobs_count,all_jobs_count_);
all_jobs_count.Destroy(); // is not glue2 info
};
std::ostringstream json;
json << "{" << std::endl;
XMLtoJSON(root, json);
json << std::endl << "}" << std::endl;
infodoc_.Release();
std::string jsonFile(config_.ControlDir()+G_DIR_SEPARATOR_S+"info.json");
(void)Arc::FileCreate(jsonFile, json.str());
} else {
logger_.msg(Arc::ERROR,"Informational document is empty");
};
......@@ -143,9 +235,10 @@ bool ARexService::RegistrationCollector(Arc::XMLNode &doc) {
}
logger.msg(Arc::VERBOSE, "Registered static information: \n doc: %s",(std::string)doc);
infodoc_.Release();
} else
} else {
logger.msg(Arc::VERBOSE, "Information Registered without static attributes: \n doc: %s",(std::string)doc);
return true;
}
return true;
//
// TODO: filter information here.
//Arc::XMLNode regdoc("<Service/>");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment