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