Commit c8718b5e authored by Volodymyr Yurchenko's avatar Volodymyr Yurchenko
Browse files

JAL-70 #comment Run command, set metadata and readbuffer in one place

parent 56d8ee3e
......@@ -80,9 +80,10 @@ typedef char int8_t;
class TJAlien : public TGrid {
private:
static TString fPwd; // working directory
TString fHome; // home directory with alien:// prefix
std::string tmpdir; // tmp directory
static TString fPwd; // working directory
TString fHome; // home directory with alien:// prefix
std::string tmpdir; // tmp directory
std::string readBuffer; // stdout & stderr of the last command
Bool_t WriteTokenFile();
void Connect();
......
......@@ -64,6 +64,7 @@ private:
static size_t WriteCallback(void *contents, size_t size, size_t nmemb);
void clearFlags();
TJAlienCredentials creds;
static std::string readBuffer;
public:
TJAlienConnectionManager();
......@@ -74,6 +75,7 @@ public:
void MakeWebsocketConnection(TJAlienCredentialsObject creds, string host, int WSPort);
void ForceRestart();
TJAlienResult *RunJsonCommand(TString *command, TList *options);
TJAlienResult *RunJsonCommand(TString *command, TList *options, std::map<std::string, TString> *metadata, std::string *readBuffer);
// Parse the result from Json structure
TJAlienResult *GetCommandResult(json_object *json_response, bool expand_find = false);
......@@ -83,7 +85,6 @@ public:
virtual Bool_t IsConnected() const { return connection_flag; }
void GetHostAndPort(TString &fHost, Int_t &fPort);
static std::string readBuffer;
ClassDef(TJAlienConnectionManager, 0)
};
......
......@@ -26,6 +26,7 @@ TJAlien::TJAlien (const char* gridUrl, const char* uId, const char* passwd,
fGrid = "alien";
gGrid = this;
readBuffer = "";
if (getenv("TMPDIR") != nullptr)
tmpdir = getenv("TMPDIR");
......@@ -48,6 +49,7 @@ void TJAlien::Connect()
{
// Failed to connect
// TJAlienConnectionManager will produce an error message
gGrid = nullptr;
return;
}
else if (connection_mode == 1)
......@@ -76,8 +78,8 @@ TJAlien::~TJAlien()
//______________________________________________________________________________
void TJAlien::Stderr()
{
if (connection.readBuffer != nullptr) {
json_object * jobj_res = json_tokener_parse(connection.readBuffer.c_str());
if (readBuffer != nullptr) {
json_object * jobj_res = json_tokener_parse(readBuffer.c_str());
// get metadata
json_object *js_results;
json_object_object_get_ex(jobj_res, "metadata", &js_results);
......@@ -94,8 +96,8 @@ void TJAlien::Stderr()
//______________________________________________________________________________
void TJAlien::Stdout()
{
if (connection.readBuffer != nullptr) {
json_object * jobj_res = json_tokener_parse(connection.readBuffer.c_str());
if (readBuffer != nullptr) {
json_object * jobj_res = json_tokener_parse(readBuffer.c_str());
//Info("Stdout", "%s", TJAlien::readBuffer.c_str());
// get data from results
json_object *js_results;
......@@ -116,6 +118,7 @@ void TJAlien::Stdout()
}
}
}
}
//______________________________________________________________________________
......@@ -127,8 +130,8 @@ unsigned int TJAlien::ReadTags(int column, std::map<std::string, std::string> &t
The return value is the number of tags found.
*/
UNUSED(column);
if (connection.readBuffer != nullptr) {
json_object * jobj_res = json_tokener_parse(connection.readBuffer.c_str());
if (readBuffer != nullptr) {
json_object * jobj_res = json_tokener_parse(readBuffer.c_str());
// get data from results
json_object *js_results;
json_object_object_get_ex(jobj_res, "results", &js_results);
......@@ -150,6 +153,7 @@ unsigned int TJAlien::ReadTags(int column, std::map<std::string, std::string> &t
}
return tags.size();
}
//______________________________________________________________________________
......@@ -173,13 +177,12 @@ TGridResult *TJAlien::Command(const char *command, bool interactive, UInt_t stre
TString *sCmd = new TString(command);
TObjArray *tokens = sCmd->Tokenize(" ");
TJAlienResult *result;
connection.readBuffer = "";
std::map<std::string, TString> metadata;
if (tokens->GetEntries() == 1)
{
if (interactive) Info("Command", "Received only command");
result = (TJAlienResult*) connection.RunJsonCommand(sCmd, 0);
result = (TJAlienResult*) connection.RunJsonCommand(sCmd, 0, &metadata, &readBuffer);
if (result)
TJAlienResultRewriter().Rewrite((std::string)*sCmd, result);
}
......@@ -230,7 +233,7 @@ TGridResult *TJAlien::Command(const char *command, bool interactive, UInt_t stre
}
TString *s = new TString(sCommand->GetString());
result = (TJAlienResult*) connection.RunJsonCommand(s, options);
result = (TJAlienResult*) connection.RunJsonCommand(s, options, &metadata, &readBuffer);
if (result)
TJAlienResultRewriter().Rewrite(sCommand->GetString().Data(), result);
......@@ -247,22 +250,12 @@ TGridResult *TJAlien::Command(const char *command, bool interactive, UInt_t stre
delete tokens;
delete sCmd;
if (result)
{
// Extract the username, current directory, host and port
// immediately after each command, since it could change
TObjString p("user");
TObjString *sUserMetadata = result->GetMetaData(&p);
if (sUserMetadata) {
fUser = sUserMetadata->GetString();
}
TObjString k("currentdir");
TObjString *sPwdMetadata = result->GetMetaData(&k);
if (sPwdMetadata) {
fPwd = result->GetMetaData(&k)->GetString();
}
connection.GetHostAndPort(fHost, fPort);
}
fHost = metadata["fHost"];
fPort = metadata["fPort"].Atoi();
if (result) {
fUser = metadata["fUser"];
fPwd = metadata["fPwd"];
}
return result;
}
......
......@@ -452,6 +452,12 @@ json_object *TJAlienConnectionManager::CreateJsonCommand(TString *command, TList
//______________________________________________________________________________
TJAlienResult *TJAlienConnectionManager::RunJsonCommand(TString *command, TList *opt)
{
return RunJsonCommand(command, opt, nullptr, nullptr);
}
//______________________________________________________________________________
TJAlienResult *TJAlienConnectionManager::RunJsonCommand(TString *command, TList *opt, std::map<std::string, TString> *metadata, std::string *readBuffer)
{
json_object *jsonCommand = CreateJsonCommand(command, opt);
if (jsonCommand == nullptr)
......@@ -463,7 +469,7 @@ TJAlienResult *TJAlienConnectionManager::RunJsonCommand(TString *command, TList
if (gDebug > 1)
Info("TJAlienConnectionManager", "JSON command to be done: %s", json_object_to_json_string(jsonCommand) );
readBuffer = "";
this->readBuffer = "";
if (!connection_flag)
{
Error("TJAlienConnectionManager", "Connection is broken!");
......@@ -486,10 +492,31 @@ TJAlienResult *TJAlienConnectionManager::RunJsonCommand(TString *command, TList
receive_flag = 0;
json_object_put(jsonCommand);
json_object * jobj_res = json_tokener_parse(readBuffer.c_str());
json_object * jobj_res = json_tokener_parse(this->readBuffer.c_str());
// get response
TJAlienResult *GridResult = this->GetCommandResult(jobj_res, *command == "find");
if (readBuffer)
(*readBuffer) = this->readBuffer;
if (metadata) {
// Extract the username, current directory, host and port
// immediately after each command, since it could change
(*metadata)["fHost"] = fWSHost;
(*metadata)["fPort"] = fWSPort;
if (GridResult) {
TObjString u("user");
TObjString* sUserMetadata = GridResult->GetMetaData(&u);
if (sUserMetadata) {
(*metadata)["fUser"] = sUserMetadata->GetString();
}
TObjString c("currentdir");
TObjString* sPwdMetadata = GridResult->GetMetaData(&c);
if (sPwdMetadata) {
(*metadata)["fPwd"] = sPwdMetadata->GetString();
}
}
}
return GridResult;
}
......
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