Commit 71cb271c authored by Nikola Hardi's avatar Nikola Hardi Committed by Nikola Hardi
Browse files

Introducing the TJAlienResultRewriter

This functionality is required in order to preserve backwards
compatibility with TAlien and how legacy code handles return
and error values.
parent c43a52bf
......@@ -31,6 +31,7 @@ set(SRCS
TJAlienMasterJobStatus.cxx
TJAlienPackage.cxx
TJAlienResult.cxx
TJAlienResultRewriter.cxx
TJAlienSAXHandler.cxx
TJAlienSystem.cxx)
string(REPLACE ".cxx" ".h" HDRS "${SRCS}")
......
......@@ -17,6 +17,7 @@
#pragma link C++ class TJAlienMasterJobStatus;
#pragma link C++ class TJAlienPackage;
#pragma link C++ class TJAlienResult;
#pragma link C++ class TJAlienResultRewriter;
#pragma link C++ class TJAlienSAXHandler;
#pragma link C++ class TJAlienSystem;
#endif
......@@ -18,6 +18,7 @@
#include "TJAlienJob.h"
#include "TJAlienJobStatus.h"
#include "TJAlienJobStatusList.h"
#include "TJAlienResultRewriter.h"
ClassImp(TJAlien)
......@@ -611,6 +612,7 @@ TGridResult *TJAlien::Command(const char *command, bool interactive, UInt_t stre
{
if (interactive) Info("Command", "Received only command");
result = (TJAlienResult*) RunJsonCommand(sCmd, 0);
TJAlienResultRewriter().Rewrite((std::string)*sCmd, result);
}
else
{
......@@ -627,6 +629,8 @@ TGridResult *TJAlien::Command(const char *command, bool interactive, UInt_t stre
TString *s = new TString(sCommand->GetString());
result = (TJAlienResult*) RunJsonCommand(s, options);
TJAlienResultRewriter().Rewrite((std::string)sCommand->GetString(), result);
if (interactive)
{
Stdout();
......
#include "TJAlienResultRewriter.h"
ClassImp(TJAlienResultRewriter);
using namespace std;
// Result codes definitions
// Map from command to pair of result codes (success, failure).
// A new key will be inserted into the result, this key being "__result__".
// The value of the "__result__" key is taken from this map, and it is a string.
// If the value string is empty (NULL or ""), then the "__result__" key is not
// inserted at all.
// Extend this map initializer when needed as new rewriting rules are discovered.
const map<string, pair<string, string>> TJAlienResultRewriter::rules = {
{"ls" , {"" , "0"}},
{"cd" , {"1", "0"}},
{"stat" , {"0", "1"}},
{"mkdir", {"1", "0"}},
};
void TJAlienResultRewriter::Rewrite(const string& command,
TJAlienResult *inResult)
{
auto cmdRule = rules.find(command);
if (cmdRule != rules.end()) {
ApplyRule(&(cmdRule->second), inResult);
}
}
void TJAlienResultRewriter::ApplyRule(const std::pair<string, string>* rule,
TJAlienResult* result)
{
const char *code = NULL;
if (IsSuccess(result) && rule->first != "") {
code = rule->first.c_str();
} else if(IsFailure(result) && rule->second != "") {
code = rule->second.c_str();
}
if(code != NULL) {
SetResultCode(result, code);
}
}
void TJAlienResultRewriter::SetResultCode(TJAlienResult *result, const char *target) {
TMap *m = ObtainFirstMap(result);
RemoveResultCode(m);
m->Add(new TObjString("__result__"), new TObjString(target));
}
TMap* TJAlienResultRewriter::ObtainFirstMap(TJAlienResult *result) {
TMap *m;
if(result->IsEmpty()) {
m = new TMap();
result->Add(m);
} else {
m = (TMap*)result->At(0);
}
return m;
}
void TJAlienResultRewriter::RemoveResultCode(TMap *m) {
TPair *p = (TPair*)m->FindObject("__result__");
if(p != NULL) {
auto *obj = m->Remove(new TObjString("__result__"));
delete obj;
}
}
bool TJAlienResultRewriter::IsSuccess(TJAlienResult* result)
{
auto key = TObjString("error");
if(result && result->GetMetaData(&key)) {
return false;
} else {
return true;
}
}
bool TJAlienResultRewriter::IsFailure(TJAlienResult* result)
{
return !(IsSuccess(result));
}
#ifndef ROOT_TJAlienResultRewriter
#define ROOT_TJAlienResultRewriter
#include "TJAlienResult.h"
#include <map>
#include <utility>
#include <string>
class TJAlienResultRewriter {
private:
static const std::map<std::string, std::pair<std::string, std::string>> rules;
TMap* ObtainFirstMap(TJAlienResult *result);
void SetResultCode(TJAlienResult *result, const char *target);
void RemoveResultCode(TMap *m);
public:
void Rewrite(const std::string& command, TJAlienResult* inResult);
void ApplyRule(const std::pair<std::string, std::string>* rule, TJAlienResult* result);
bool IsSuccess(TJAlienResult* result);
bool IsFailure(TJAlienResult* result);
ClassDef(TJAlienResultRewriter,0); // Insert return codes like in TAlien
};
#endif
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