header

Using the C++ Client API

There are two different C++ client APIs available for the AMGA metadata service. One is through the md_api which provides several api functions, the other is directly through the MDClient class which also serves as a backend to the md_api.

The MDClient class offers an interface which allows to issue AMGA commands directly but does not understand the semantics of the commands and thus does not parse the responses of the server into suitable structures, while this is done by the md_api. However, the control on the connection to the server is much better in the case of the MDClient class, for example it allows you to abort a query easily. It may also happen that some commands are not available in the md_api yet.

In any case, both ways to access the metadata service from C++ depend on an existing and accessible mdclient.config file being either in the current working directory or in the home directory as ~/.mdclient.config. See Configuration of the C++ and Java command line clients for explanations how to set up the client configuration.

The following is an example of a program using the md_api to

#include "client/md_api.h"
#include <iostream>

int main (int argc, char *argv[])
{
  std::cout << "Listing attributes of /test\";
  std::list< std::string > attrList;
  std::list< std::string > types;
  if( (res=listAttr("/test", attrList, types)) == 0){
    std::cout << "  Result:" << std::endl;
    std::list< std::string >::iterator I=attrList.begin();
    while(I != attrList.end())
      std::cout << "  >" << (*I++) << "<" << std::endl;
  } else {
    std::cout << "  Error: " << res << std::endl;
  }

  std::cout << "Getting gen and events attributes of /test/*\n";
  AttributeList attributeList(2);
  std::list< std::string > attributes;
  attributes.push_back("gen");
  attributes.push_back("events");
  if( (res=getAttr("/test/*", attributes, attributeList)) == 0){
    std::cout << "  Result:" << std::endl;
    while(!attributeList.lastRow()){
      std::vector< std::string > attrs;
      std::string filename;
      attributeList.getRow(filename, attrs);
      std::cout << "File: >" << filename << "<" << std::endl;
      for(size_t i=0; i< attrs.size(); i++)
        std::cout << "  >" <<  attrs[i] << "<" << std::endl;
      std::cout << std::endl;
    }
  } else {
    std::cout << "  Error: " << res << std::endl;
  }

  return 0;
}

A full overview of the available API functions is given at the following url.

http://project-arda-dev.web.cern.ch/project-arda-dev/metadata/md__api_8cc.html

#include <MDClient.h>
#include <iostream>

int main (int argc, char *argv[])
{ 
  int res;

  MDClient client;
  //  client.setDebug(true);

  if(client.connectToServer()){
    std::cout << client.getError() << std::endl;
    return 5;
  }
  
  std::string command="pwd";
  if( ( res=client.execute(command)) ){
      std::cout << "  ERROR: execute failed" 
                << "   (" << res << "): " 
                << client.getError() << std::endl;
      return res;
  }
  
  while(!client.eot()) {
    std::string row;
    if(res=client.fetchRow(row)){
     std::cout << "Error fetching: " << res << std::endl;
        return res;
    }
    std::cout << row << std::endl;
  }

  return 0;
}

All capabilities of the MDClient like cancellation of requests or the catching of CTRL_C are explained in the reference at http://project-arda-dev.web.cern.ch/project-arda-dev/metadata/classMDClient.html a short(!) example of how to make use of them is the mdclient.cc program itself.


Generated on Mon Apr 16 13:59:18 2012 for AMGA by  doxygen 1.4.7