From aab652bdfd98e8260abc2207003950b5806c2de5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Voc=C5=AF?= Date: Fri, 19 Dec 2008 17:04:23 +0000 Subject: [PATCH] c++ api work --- org.glite.lb.doc/examples/cons_example1.cpp | 120 +++++++++++++++++++++++++ org.glite.lb.doc/examples/cons_example2.cpp | 134 ++++++++++++++++++++++++++++ org.glite.lb.doc/examples/cons_example3.cpp | 123 +++++++++++++++++++++++++ org.glite.lb.doc/examples/util.C | 90 +++++++++++++++++-- org.glite.lb.doc/src/consumer_api.tex | 84 ++++++++--------- 5 files changed, 498 insertions(+), 53 deletions(-) create mode 100644 org.glite.lb.doc/examples/cons_example1.cpp create mode 100644 org.glite.lb.doc/examples/cons_example2.cpp create mode 100644 org.glite.lb.doc/examples/cons_example3.cpp diff --git a/org.glite.lb.doc/examples/cons_example1.cpp b/org.glite.lb.doc/examples/cons_example1.cpp new file mode 100644 index 0000000..92f895d --- /dev/null +++ b/org.glite.lb.doc/examples/cons_example1.cpp @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include +#include + +/*headers*/ +#include "glite/jobid/cjobid.h" +#include "glite/lb/events.h" +#include "glite/lb/consumer.h" +/*end headers*/ + + +static struct option opts[] = { + {"help", 0, NULL, 'h'}, + {"server", 1, NULL, 's'}, + {"port", 1, NULL, 'p'}, + {"jobid", 1, NULL, 'j'}, + {"user", 1, NULL, 'u'}, +}; + +static void usage(char *me) +{ + fprintf(stderr, "usage: %s [option]\n" + "\t-h, --help Shows this screen.\n" + "\t-s, --server Server address.\n" + "\t-p, --port Query server port.\n" + "\t-j, --jobid ID of requested job.\n" + "\t-u, --user User DN.\n" + , me); +} + + +int main(int argc, char *argv[]) +{ + char *server, *jobid_s, *user; + int opt, err = 0; + edg_wlc_JobId jobid = NULL; + long i; + int port = 0; + + server = jobid_s = user = NULL; + while ( (opt = getopt_long(argc, argv, "hs:p:j:u:c:n:v:", opts, NULL)) != EOF) + switch (opt) { + case 'h': usage(argv[0]); return 0; + case 's': server = strdup(optarg); break; + case 'p': port = atoi(optarg); break; + case 'j': jobid_s = strdup(optarg); break; + case 'u': user = strdup(optarg); break; + case '?': usage(argv[0]); return 1; + } + + if ( !jobid_s ) { fprintf(stderr, "JobId not given\n"); return 1; } + //if ( !server ) { fprintf(stderr, "LB proxy socket not given\n"); return 1; } + + /*variables*/ + edg_wll_Context ctx; + edg_wll_QueryRec jc[4]; + edg_wll_JobStat *statesOut = NULL; + edg_wlc_JobId *jobsOut = NULL; + /*end variables*/ + + if ( (errno = edg_wlc_JobIdParse(jobid_s, &jobid)) ) { perror(jobid_s); return 1; } + + /*context*/ + edg_wll_InitContext(&ctx); + + edg_wll_SetParam(ctx, EDG_WLL_PARAM_QUERY_SERVER, server); + if (port) edg_wll_SetParam(ctx, EDG_WLL_PARAM_QUERY_SERVER_PORT, port); + /*end context*/ + + /*queryrec*/ + jc[0].attr = EDG_WLL_QUERY_ATTR_OWNER; + jc[0].op = EDG_WLL_QUERY_OP_EQUAL; + jc[0].value.c = NULL; + jc[1].attr = EDG_WLL_QUERY_ATTR_STATUS; + jc[1].op = EDG_WLL_QUERY_OP_EQUAL; + jc[1].value.i = EDG_WLL_JOB_RUNNING; + jc[2].attr = EDG_WLL_QUERY_ATTR_DESTINATION; + jc[2].op = EDG_WLL_QUERY_OP_EQUAL; + jc[2].value.c = "XYZ"; + jc[3].attr = EDG_WLL_QUERY_ATTR_UNDEF; + /*end queryrec*/ + + /*query*/ + err = edg_wll_QueryJobs(ctx, jc, 0, &jobsOut, &statesOut); //* \label{l:queryjobs} + if ( err == E2BIG ) { + fprintf(stderr,"Warning: only limited result returned!\n"); + return 0; + } else if (err) { + char *et,*ed; + + edg_wll_Error(ctx,&et,&ed); + fprintf(stderr,"%s: edg_wll_QueryJobs(): %s (%s)\n",argv[0],et,ed); + + free(et); free(ed); + } + /*end query*/ + + /*printstates*/ + for (i = 0; statesOut[i].state; i++ ) { + printf("jobId : %s\n", edg_wlc_JobIdUnparse(statesOut[i].jobId)); + printf("state : %s\n\n", edg_wll_StatToString(statesOut[i].state)); + } + /*end printstates*/ + + if ( jobsOut ) { + for (i=0; jobsOut[i]; i++) edg_wlc_JobIdFree(jobsOut[i]); + free(jobsOut); + } + if ( statesOut ) { + for (i=0; statesOut[i].state; i++) edg_wll_FreeStatus(&statesOut[i]); + free(statesOut); + } + + edg_wll_FreeContext(ctx); + + return err; +} diff --git a/org.glite.lb.doc/examples/cons_example2.cpp b/org.glite.lb.doc/examples/cons_example2.cpp new file mode 100644 index 0000000..905942c --- /dev/null +++ b/org.glite.lb.doc/examples/cons_example2.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include + +/*headers*/ +#include "glite/jobid/cjobid.h" +#include "glite/lb/events.h" +#include "glite/lb/consumer.h" +/*end headers*/ + + +static struct option opts[] = { + {"help", 0, NULL, 'h'}, + {"server", 1, NULL, 's'}, + {"port", 1, NULL, 'p'}, + {"jobid", 1, NULL, 'j'}, + {"user", 1, NULL, 'u'}, +}; + +static void usage(char *me) +{ + fprintf(stderr, "usage: %s [option]\n" + "\t-h, --help Shows this screen.\n" + "\t-s, --server LB server.\n" + "\t-p, --port LB server port.\n" + "\t-j, --jobid ID of requested job.\n" + "\t-u, --user User DN.\n" + , me); +} + + +int main(int argc, char *argv[]) +{ + char *server, *jobid_s, *user; + edg_wlc_JobId jobid = NULL; + int opt, err = 0; + long i; + int port = 0; + + server = jobid_s = user = NULL; + while ( (opt = getopt_long(argc, argv, "hs:p:j:u:c:n:v:", opts, NULL)) != EOF) + switch (opt) { + case 'h': usage(argv[0]); return 0; + case 's': server = strdup(optarg); break; + case 'p': port = atoi(optarg); break; + case 'j': jobid_s = strdup(optarg); break; + case 'u': user = strdup(optarg); break; + case '?': usage(argv[0]); return 1; + } + + if ( !jobid_s ) { fprintf(stderr, "JobId not given\n"); return 1; } + + /*variables*/ + edg_wll_Context ctx; + edg_wll_QueryRec *jc[4]; + edg_wll_JobStat *statesOut = NULL; + edg_wlc_JobId *jobsOut = NULL; + /*end variables*/ + + if ( (errno = edg_wlc_JobIdParse(jobid_s, &jobid)) ) { perror(jobid_s); return 1; } + + /*context*/ + edg_wll_InitContext(&ctx); + + edg_wll_SetParam(ctx, EDG_WLL_PARAM_QUERY_SERVER, server); + if (port) edg_wll_SetParam(ctx, EDG_WLL_PARAM_QUERY_SERVER_PORT, port); + /*end context*/ + + /*queryrec*/ + jc[0] = (edg_wll_QueryRec *) malloc(2*sizeof(edg_wll_QueryRec)); + jc[0][0].attr = EDG_WLL_QUERY_ATTR_OWNER; + jc[0][0].op = EDG_WLL_QUERY_OP_EQUAL; + jc[0][0].value.c = NULL; + jc[0][1].attr = EDG_WLL_QUERY_ATTR_UNDEF; + + jc[1] = (edg_wll_QueryRec *) malloc(2*sizeof(edg_wll_QueryRec)); + jc[1][0].attr = EDG_WLL_QUERY_ATTR_STATUS; + jc[1][0].op = EDG_WLL_QUERY_OP_EQUAL; + jc[1][0].value.i = EDG_WLL_JOB_RUNNING; + jc[1][1].attr = EDG_WLL_QUERY_ATTR_UNDEF; + + jc[2] = (edg_wll_QueryRec *) malloc(3*sizeof(edg_wll_QueryRec)); + jc[2][0].attr = EDG_WLL_QUERY_ATTR_DESTINATION; + jc[2][0].op = EDG_WLL_QUERY_OP_EQUAL; + jc[2][0].value.c = "XXX"; + jc[2][1].attr = EDG_WLL_QUERY_ATTR_DESTINATION; + jc[2][1].op = EDG_WLL_QUERY_OP_EQUAL; + jc[2][1].value.c = "YYY"; + jc[2][2].attr = EDG_WLL_QUERY_ATTR_UNDEF; + + jc[3] = NULL; + /*end queryrec*/ + + /*query*/ + err = edg_wll_QueryJobsExt(ctx, (const edg_wll_QueryRec **)jc, + 0, &jobsOut, &statesOut); + /*end query*/ + + if ( err == E2BIG ) { + fprintf(stderr,"Warning: only limited result returned!\n"); + return 0; + } else if (err) { + char *et,*ed; + + edg_wll_Error(ctx,&et,&ed); + fprintf(stderr,"%s: edg_wll_QueryJobs(): %s (%s)\n",argv[0],et,ed); + + free(et); free(ed); + } + + /*printstates*/ + for (i = 0; statesOut[i].state; i++ ) { + printf("jobId : %s\n", edg_wlc_JobIdUnparse(statesOut[i].jobId)); + printf("state : %s\n\n", edg_wll_StatToString(statesOut[i].state)); + } + /*end printstates*/ + + if ( jobsOut ) { + for (i=0; jobsOut[i]; i++) edg_wlc_JobIdFree(jobsOut[i]); + free(jobsOut); + } + if ( statesOut ) { + for (i=0; statesOut[i].state; i++) edg_wll_FreeStatus(&statesOut[i]); + free(statesOut); + } + free(jc[0]); free(jc[1]); free(jc[2]); + + edg_wll_FreeContext(ctx); + + return err; +} diff --git a/org.glite.lb.doc/examples/cons_example3.cpp b/org.glite.lb.doc/examples/cons_example3.cpp new file mode 100644 index 0000000..7ac860e --- /dev/null +++ b/org.glite.lb.doc/examples/cons_example3.cpp @@ -0,0 +1,123 @@ +extern "C" { +#include +#include +} + +#include +#include + +/*headers*/ +#include "glite/jobid/JobId.h" +#include "glite/lb/Event.h" +#include "glite/lb/ServerConnection.h" +/*end headers*/ + +using namespace std; + +static struct option opts[] = { + {"help", 0, NULL, 'h'}, + {"server", 1, NULL, 's'}, + {"port", 1, NULL, 'p'}, + {"jobid", 1, NULL, 'j'}, + {"user", 1, NULL, 'u'}, +}; + +static void usage(char *me) +{ + cerr << "usage: %s [option]\n" + << "\t-h, --help Shows this screen.\n" + << "\t-s, --server LB server.\n" + << "\t-p, --port LB server port.\n" + << "\t-j, --jobid ID of requested job.\n" + << "\t-u, --user User DN.\n" + << me << endl; +} + + +int main(int argc, char *argv[]) +{ + string server_s, jobid_s, user; + int opt, err = 0; + glite_jobid_t jobid = NULL; + long i; + int port = 0; + + + server = jobid_s = NULL; + while ( (opt = getopt_long(argc, argv, "hs:p:j:u:c:n:v:", opts, NULL)) != EOF) + switch (opt) { + case 'h': usage(argv[0]); return 0; + case 's': server_s = optarg; break; + case 'p': port = atoi(optarg); break; + case 'j': jobid_s = optarg; break; + case 'u': user = optarg; break; + case '?': usage(argv[0]); return 1; + } + + if ( jobid_s.empty() ) { + cerr << "JobId not given\n"; return 1; + } + + /*variables*/ + ServerConnection server; + Event *eventsOut; + vector jc; + vector ec; + /*end variables*/ + + if ( (errno = edg_wlc_JobIdParse(jobid_s, &jobid)) ) { perror(jobid_s); return 1; } + + /*context*/ + edg_wll_InitContext(&ctx); + + edg_wll_SetParam(ctx, EDG_WLL_PARAM_QUERY_SERVER, server); + if (port) edg_wll_SetParam(ctx, EDG_WLL_PARAM_QUERY_SERVER_PORT, port); + /*end context*/ + + /*queryrec*/ + jc[0].attr = EDG_WLL_QUERY_ATTR_USERTAG; + jc[0].op = EDG_WLL_QUERY_OP_EQUAL; + jc[0].attr_id.tag = "color"; + jc[0].value.c = "red"; + jc[1].attr = EDG_WLL_QUERY_ATTR_UNDEF; + ec[0].attr = EDG_WLL_QUERY_ATTR_USERTAG; + ec[0].op = EDG_WLL_QUERY_OP_EQUAL; + ec[0].attr_id.tag = "color"; + ec[0].value.c = "green"; + ec[1].attr = EDG_WLL_QUERY_ATTR_UNDEF; + /*end queryrec*/ + + /*query*/ + err = edg_wll_QueryEvents(ctx, jc, ec, &eventsOut); + /*end query*/ + + if ( err == E2BIG ) { + fprintf(stderr,"Warning: only limited result returned!\n"); + return 0; + } else if (err) { + char *et,*ed; + + edg_wll_Error(ctx,&et,&ed); + fprintf(stderr,"%s: edg_wll_QueryEvents(): %s (%s)\n",argv[0],et,ed); + + free(et); free(ed); + } + + if ( err == ENOENT ) return err; + + /*printevents*/ + for (i = 0; eventsOut && (eventsOut[i].type); i++ ) { + //printf("jobId : %s\n", edg_wlc_JobIdUnparse(eventsOut[i].jobId)); + printf("event : %s\n\n", edg_wll_EventToString(eventsOut[i].type)); + } + /*end printevents*/ + + if ( eventsOut ) { + for (i=0; &(eventsOut[i]); i++) edg_wll_FreeEvent(&(eventsOut[i])); + free(eventsOut); + } + + edg_wll_FreeContext(ctx); + + return err; +} diff --git a/org.glite.lb.doc/examples/util.C b/org.glite.lb.doc/examples/util.C index 90de938..d6b5831 100644 --- a/org.glite.lb.doc/examples/util.C +++ b/org.glite.lb.doc/examples/util.C @@ -11,17 +11,17 @@ void dumpEvent(Event *event) { // list of attribute names and types - typedef std::vector> AttrListType; + typedef vector> AttrListType; cout << "Event name: " << event->name() << endl; - AttrListType attr_list = event->getAttrs(); + AttrListType attr_list = event->getAttrs(); //* \label{l:getattrs} for(AttrListType::iterator i = attr_list.begin(); i != attr_list.end(); i++) { - Event::Attr attr = attr_list[i].first(); + Event::Attr attr = attr_list[i].first; - cout << Event::getAttrName(attr) << " = "; - switch(attr_list[i].second()) { + cout << Event::getAttrName(attr) << " = "; + switch(attr_list[i].second) { case Event::INT_T: case Event::PORT_T: case Event::LOGSRC_T: @@ -60,6 +60,84 @@ dumpEvent(Event *event) /*status*/ void dumpState(JobStatus *status) { - + typedef vector> AttrListType; + + cout << "Job status: " << status->name << endl; + + AttrListType attr_list = status->getAttrs(); //* \label{l:jgetattrs} + for(AttrListType::iterator i = attr_list.begin(); + i != attr_list.end(); + i++) { + JobStatus::Attr attr = attr_list[i].first; + cout << JobStatus::getAttrName(attr) << " = "; + switch(attr_list[i].second) { + + case INT_T: + cout << status->getValInt(attr) << endl; + break; + + case STRING_T: + cout << status->getValInt(attr) << endl; + break; + + case TIMEVAL_T: + cout << status->getValTime(attr).tv_sec << endl; + break; + + case BOOL_T: + cout << status->getValBool(attr).tv_sec << endl; + break; + + case JOBID_T: + cout << status->getValJobid(attr).toString() << endl; + break; + + case INTLIST_T: + vector list = status->getValIntList(attr); + for(vector::iterator i = list.begin(); + i != list.end(); + i++) { + cout << list[i] << " "; + } + cout << endl; + break; + + case STRLIST_T: + vector list = status->getValStringList(attr); + for(vector::iterator i = list.begin(); + i != list.end(); + i++) { + cout << list[i] << " "; + } + cout << endl; + break; + + case TAGLIST_T: /**< List of user tags. */ + vector> list = status->getValTagList(attr); + for(vector>::iterator i = list.begin(); + i != list.end(); + i++) { + cout << list[i].first << "=" << list[i].second << " "; + } + cout << endl; + break; + + case STSLIST_T: /**< List of states. */ + vector list = status->getValJobStatusList(attr); + for(vector::iterator i = list.begin(); + i != list.end(); + i++) { + // recursion + dumpState(&list[i]); + } + cout << endl; + break; + + default: + cout << "attribute type not supported" << endl; + break; + + } + } } /*end status*/ diff --git a/org.glite.lb.doc/src/consumer_api.tex b/org.glite.lb.doc/src/consumer_api.tex index e99838d..2ab20a7 100644 --- a/org.glite.lb.doc/src/consumer_api.tex +++ b/org.glite.lb.doc/src/consumer_api.tex @@ -333,85 +333,75 @@ glite/lb/ServerConnection.h & Core of the C++ \LB API, defines \verb'ServerConnection' class for performing the queries. \\ glite/lb/Job.h & Defines \verb'Job' class with methods for job specific queries. \\ - \end{tabularx} +\caption{Consumer C++ API header files} +\label{t:ccppheaders} \end{table} \subsubsection{QueryRecord} + \subsubsection{Event} The objects of class \verb'glite::lb::Event' are returned by the \LB event queries. The \verb'Event' class introduces symbolic names for event -type (enumeration \verb'Event::Type'), event attributes (enumeration -\verb'Event::Attr') and their types (enumeration +type (enum \verb'Event::Type'), event attributes (enum +\verb'Event::Attr') and their types (enum \verb'Event::AttrType'), feature not available through the C API, as well as (read only) access to the attribute values. Using these methods you can: \begin{itemize} -\item get the string name for the symbolic event type: -\begin{lstlisting} - static const std::string getEventName(Type type); -\end{lstlisting} \item get the event type (both symbolic and string): \begin{lstlisting} - const std::string & name(void) const; -\end{lstlisting} -\item get the list of attribute types and values: -\begin{lstlisting} - const std::vector > & getAttrs(void) const; -\end{lstlisting} -\item get string representation of attribute names: -\begin{lstlisting} - static const std::string & getAttrName(Attr name); -\end{lstlisting} -\item get value of given attribute: -\begin{lstlisting} - int getValInt(Attr name) const; - float getValFloat(Attr name) const; - double getValDouble(Attr name) const; - std::string getValString(Attr name) const; - struct timeval getValTime(Attr name) const; - const glite::jobid::JobId getValJobId(Attr name) const; + Event event; + + // we suppose event gets somehow filled in + cout << "Event type: " << event.type << endl; + + cout << "Event name:" << endl; + // these two lines should print the same string + cout << Event::getEventName(event.type) << endl; + cout << event.name() << endl; \end{lstlisting} +\item get the list of attribute types and values (see +line~\ref{l:getattrs} of the example), +\item get string representation of attribute names, +\item get value of given attribute. \end{itemize} - -Following example demonstrates this by printing the event attributes: +The following example demonstrates this by printing event name and attributes: \lstinputlisting[title={\bf File:}\lstname,numbers=left,linerange=event-end\ event]{util.C} \subsubsection{JobStatus} The \verb'glite::lb::JobStatus' serves as a result type of job status queries in the same way the \verb'glite::lb::Event' serves in event queries. The \verb'JobStatus' class provides symbolic names for job -states (enumeration \verb'JobStatus::Code'), state attributes -(enumeration \verb'JobStatus::Attr') and their types (enumeration +states (enum \verb'JobStatus::Code'), state attributes +(enum \verb'JobStatus::Attr') and their types (enum \verb'JobStatus::AttrType'), and read only access to the attribute values. Using the \verb'JobStatus' interface you can: \begin{itemize} \item get the string name for the symbolic job state: \begin{lstlisting} - static const std::string &getStateName(Code state); -\end{lstlisting} -\item get the job state name (both symbolic and string): -\begin{lstlisting} - const std::string & name(void) const; -\end{lstlisting} -\item get the list of job state attributes and types: -\begin{lstlisting} - const std::vector >& getAttrs(void) const; + JobStatus status; + + // we suppose status gets somehow filled in + cout << "Job state: " << status.type << endl; + + cout << "State name: " << endl; + // these two lines should print the same string + cout << JobStatus::getStateName(status.type) << endl; + cout << status.name() << endl; \end{lstlisting} +\item get the job state name (both symbolic and string), +\item get the list of job state attributes and types, \item convert the attribute names from symbolic to string form and -vice versa: -\begin{lstlisting} - static const std::string& getAttrName(Attr name); - static Attr attrByName(std::string const &); -\end{lstlisting} -\item get value of given attribute: -\begin{lstlisting} -\end{lstlisting} +vice versa, +\item get value of given attribute. \end{itemize} - +The following example demostrates this by printing job status (name +and attributes): \lstinputlisting[title={\bf File:}\lstname,numbers=left,linerange=status-end\ status]{util.C} \subsubsection{ServerConnection}\label{s:ServerConnection} + \subsubsection{Job} -- 1.8.2.3