Finally really fixing the segfault.
[gp-arc-client-c.git] / ext / arc_stat.cpp
1 /*
2  *  arc_stat.cpp
3  *  arcclientc
4  *
5  *  Created by Tamas Jung on 10/25/10.
6  *
7  */
8
9 #include "arc_stat.h"
10
11 // -*- indent-tabs-mode: nil -*-
12
13 #include "logger.h"
14 #include <iostream>
15 #include <list>
16 #include <string>
17 #include <algorithm>
18 #include "arc_libs.h"
19
20 char*  arc_stat(int argc, char **argv) {
21         
22   char* result;
23         Arc::OptionParser options(istring("[job ...]"),
24                                                           istring("The arcstat command is used for "
25                                                                           "obtaining the status of jobs that have\n"
26                                                                           "been submitted to Grid enabled resources."),
27                                                           istring("Argument to -c has the format "
28                                                                           "Flavour:URL e.g.\n"
29                                                                           "ARC0:ldap://grid.tsl.uu.se:2135/"
30                                                                           "nordugrid-cluster-name=grid.tsl.uu.se,"
31                                                                           "Mds-Vo-name=local,o=grid"));
32         
33         
34         std::string joblist;
35         options.AddOption('j', "joblist",
36                                           istring("file containing a list of jobs"),
37                                           istring("filename"),
38                                           joblist);
39         
40         std::list<std::string> clusters;
41         options.AddOption('c', "cluster",
42                                           istring("explicitly select or reject a specific resource"),
43                                           istring("[-]name"),
44                                           clusters);
45         
46         
47         int timeout = -1;
48         options.AddOption('t', "timeout", istring("timeout in seconds (default 20)"),
49                                           istring("seconds"), timeout);
50         
51         std::string conffile;
52         options.AddOption('z', "conffile",
53                                           istring("configuration file (default ~/.arc/client.conf)"),
54                                           istring("filename"), conffile);
55                 
56         
57         std::list<std::string> jobs = options.Parse(argc, argv);
58         
59         
60                 
61         Arc::UserConfig usercfg(conffile, joblist);
62         if (!usercfg) {
63                 //result  -> message = new_str("Failed configuration initialization");
64                 logger.msg(Arc::ERROR, "Failed configuration initialization");
65                 return NULL;
66         }
67                 
68         if (timeout > 0)
69         usercfg.Timeout(timeout);
70         
71
72         
73         if (!jobs.empty())
74         usercfg.ClearSelectedServices();
75         
76         if (!clusters.empty()) {
77                 usercfg.ClearSelectedServices();
78                 usercfg.AddServices(clusters, Arc::COMPUTING);
79         }
80         
81         Arc::JobSupervisor jobmaster(usercfg, jobs);
82         if (!jobmaster.JobsFound()) {
83                 //result -> message = new_str("No jobs");
84     logger.msg(Arc::INFO, "No jobs");
85                 return NULL;
86         }
87         std::list<Arc::JobController*> jobcont = jobmaster.GetJobControllers();
88         
89         if (jobcont.empty()) {
90                 //result -> message = new_str("No job controller plugins loaded");
91     logger.msg(Arc::INFO, "No job controller plugins loaded");
92                 return NULL;
93         }
94         
95         if (jobcont.size() != 1){
96                 //result -> message = new_str("Too many jobs found");
97     logger.msg(Arc::ERROR, "Too many jobs found");
98                 return NULL;
99         } 
100         Arc::JobController* it = jobcont.front();
101         
102         it->GetJobInformation();
103         
104         std::list<Arc::Job> jobs_list = it->GetJobs();
105         
106         if (jobs_list.size() != 1) {
107                 //result -> message = new_str("Ambiguous job");
108     logger.msg(Arc::ERROR, "Ambiguous job");
109                 return NULL;
110         }
111         
112         Arc::Job& job = jobs_list.front();
113         
114         result = new_str(job.State().c_str());
115         
116         return result;
117         
118         
119 }