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