implemented -f field1,field2,...
authorAleš Křenek <ljocha@ics.muni.cz>
Wed, 27 Feb 2008 12:51:03 +0000 (12:51 +0000)
committerAleš Křenek <ljocha@ics.muni.cz>
Wed, 27 Feb 2008 12:51:03 +0000 (12:51 +0000)
org.glite.lb.client/Makefile
org.glite.lb.client/src/notify.c
org.glite.lb.client/src/notify_supp.cpp [new file with mode: 0644]

index 9d52c2c..646910a 100644 (file)
@@ -220,8 +220,8 @@ ${THRPLUSLIB}: ${PLUSTHROBJS} ${THRLIB}
 logevent: logevent.o args.o
        ${LINK} -o $@ logevent.o args.o ${LIB} ${EXT_LIB} 
 
-notify: notify.o
-       ${LINK} -o $@ notify.o ${LIB} ${EXT_LIB} 
+notify: notify.o notify_supp.o
+       ${LINKXX} -o $@ notify.o notify_supp.o ${PLUSLIB} ${EXT_LIB} 
 
 ${TOOLS} ${EXAMPLES}: %: %.o
        ${LINK} -o $@ $< ${LIB} ${EXT_LIB} 
@@ -240,7 +240,7 @@ ${TOOLS}: ${LIB}
 ${FAKE_EXAMPLES:=.o}: %.o: %.cpp
        ${COMPILE} ${TEST_INC} -c $< -o $@
 
-${PLUSOBJS}: %.o: %.cpp
+notify_supp.o ${PLUSOBJS}: %.o: %.cpp
        ${CXXCOMPILE} -c $<
 
 ${PLUSTHROBJS}: %.thr.o: %.cpp
index 6d56466..9445a47 100644 (file)
@@ -10,6 +10,8 @@
 #include "glite/lb/context.h"
 #include "glite/lb/notification.h"
 
+#include "notify_supp.h"
+
 
 static char *me;
 
@@ -62,9 +64,10 @@ static void usage(char *cmd)
                fprintf(stderr,"\n'refresh' command usage: %s refresh notifid\n"
                        "    notifid     Notification ID.\n", me);
        if ( !cmd || !strcmp(cmd, "receive") )
-               fprintf(stderr,"\n'receive' command usage: %s receive [ { -s socket_fd | -a fake_addr } ] [-t timeout] [notifid]\n"
+               fprintf(stderr,"\n'receive' command usage: %s receive [ { -s socket_fd | -a fake_addr } ] [-t timeout] [-f field1,field2,...] [notifid]\n"
                        "    notifid     Notification ID (not used if -s specified).\n"
                        "    fake_addr   Fake the client address.\n"
+                       "    field1,field2,...  list of status fields to print (only owner by default)\n"
                        "    timeout     Timeout to receive operation in seconds.\n", me);
        if ( !cmd || !strcmp(cmd, "drop") )
                fprintf(stderr,"\n'drop' command usage: %s drop notifid\n"
@@ -78,6 +81,7 @@ int main(int argc,char **argv)
        time_t                          valid;
        char                       *errt, *errd;
        struct timeval          tout = {220, 0};
+       void            *fields = NULL;
 
        int     sock = -1;
        char    *fake_addr = NULL;
@@ -182,8 +186,9 @@ int main(int argc,char **argv)
                edg_wll_JobStat         stat;
                edg_wll_NotifId         nid = NULL;
                int                     c;
+               char    *field_arg = "owner",*err;
 
-               while ((c = getopt(argc-1,argv+1,"s:a:t:")) > 0) switch (c) {
+               while ((c = getopt(argc-1,argv+1,"s:a:t:f:")) > 0) switch (c) {
                        case 's':
                                if (fake_addr) { usage("receive"); return EX_USAGE; }
                                sock = atoi(optarg); break;
@@ -192,10 +197,16 @@ int main(int argc,char **argv)
                                fake_addr = optarg; break;
                        case 't':
                                tout.tv_sec = atoi(optarg); break;
+                       case 'f':
+                               field_arg = optarg; break;
                        default:
                                usage("receive"); return EX_USAGE;
                }
 
+               if ((err = parse_fields(field_arg,&fields))) {
+                       fprintf(stderr,"%s: invalid argument\n",err);
+                       return EX_USAGE;
+               }
 
                memset(&stat,0,sizeof stat);
 
@@ -223,7 +234,11 @@ int main(int argc,char **argv)
                                else goto receive_err;
                        }
                        
-                       printf("\nnotification ID: %s\n", edg_wll_NotifIdUnparse(recv_nid));
+                       print_fields(fields,recv_nid,&stat);
+
+/* original example */
+#if 0
+                       printf("\nnotification ID: %s\n", edg_wll_NotifIdUnparse(recv_nid)); 
                        
                        if (stat.state != EDG_WLL_JOB_UNDEF) {
                                char    *jobid_s;
@@ -236,6 +251,7 @@ int main(int argc,char **argv)
                                free(jobid_s);
                                stat.state = EDG_WLL_JOB_UNDEF;
                        }
+#endif
                        
                        if (recv_nid) {
                                edg_wll_NotifIdFree(recv_nid);
diff --git a/org.glite.lb.client/src/notify_supp.cpp b/org.glite.lb.client/src/notify_supp.cpp
new file mode 100644 (file)
index 0000000..1b7bd61
--- /dev/null
@@ -0,0 +1,67 @@
+#include <stdlib.h>
+#include <string.h>
+#include <iostream>
+#include <vector>
+
+#include "glite/lb/LoggingExceptions.h"
+#include "glite/lb/JobStatus.h"
+#include "glite/lb/notification.h"
+
+#include "notify_supp.h"
+
+using namespace glite::lb;
+
+char * parse_fields(const char *arg,void **out)
+{
+       char    *aux = strdup(arg),*p;
+       std::vector<JobStatus::Attr>    *fields = new std::vector<JobStatus::Attr>;
+
+       for (p = strtok(aux,","); p; p = strtok(NULL,",")) {
+               try { fields->push_back(JobStatus::attrByName(p)); }
+               catch (std::runtime_error &e) { delete fields; return p; };
+       }
+       
+       *out = (void *) fields;
+       return NULL;
+}
+
+std::string & escape(std::string &s)
+{
+       for (std::string::iterator p = s.begin(); p < s.end(); p++) switch (*p) {
+               case '\n':
+               case '\t':
+                       s.insert(p-s.begin(),"\\"); p++;
+                       break;
+               default: break;
+       }
+       return s;
+}
+
+typedef std::vector<std::pair<JobStatus::Attr,JobStatus::AttrType> > attrs_t;
+
+void print_fields(void **ff,const edg_wll_NotifId n,edg_wll_JobStat const *s)
+{
+       std::vector<JobStatus::Attr>    *fields = (std::vector<JobStatus::Attr> *) ff;
+       JobStatus       stat(*s,0);
+       attrs_t         attrs = stat.getAttrs();
+       attrs_t::iterator a;
+
+       std::cout << glite_jobid_unparse(s->jobId) << '\t' << stat.name() << '\t';
+
+       for (std::vector<JobStatus::Attr>::iterator f = fields->begin(); f != fields->end(); f++) {
+               for (a = attrs.begin(); a != attrs.end() && a->first != *f; a++);
+               if (a != attrs.end() ) switch (a->second) {
+                       case JobStatus::INT_T:
+                               std::cout << stat.getValInt(a->first) << '\t';
+                               break;
+                       case JobStatus::STRING_T: {
+                               std::string val = stat.getValString(a->first);
+                               std::cout << (val.empty() ? "(null)" : escape(val)) << '\t';
+                               } break;
+                       default:
+                               std::cout << "(unsupported)";
+                               break;
+               }
+       }
+       std::cout << std::endl;
+}