stdlib, string.h for linux compile2
[gp-arc-client-c.git] / ext / arc_get.cpp
1 /*
2  *  get.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_get.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 "arc_libs.h"
22
23
24 get_result_t* arc_get(int argc, char **argv) {
25         
26         
27         get_result_t* result = (get_result_t*)malloc(sizeof(get_result_t));
28         if (!result) {
29                 return NULL;//TBD: throw sth?
30         }
31         result->message = NULL;
32         result->get = NULL;
33         
34         Arc::OptionParser options(istring("[job ...]"),
35                                                           istring("The arcget command is used for "
36                                                                           "retrieving the results from a job."),
37                                                           istring("Argument to -c has the format "
38                                                                           "Flavour:URL e.g.\n"
39                                                                           "ARC0:ldap://grid.tsl.uu.se:2135/"
40                                                                           "nordugrid-cluster-name=grid.tsl.uu.se,"
41                                                                           "Mds-Vo-name=local,o=grid"));
42         
43         
44         std::string joblist;
45         options.AddOption('j', "joblist",
46                                           istring("file containing a list of jobs"),
47                                           istring("filename"),
48                                           joblist);
49         
50         std::list<std::string> clusters;
51         options.AddOption('c', "cluster",
52                                           istring("explicitly select or reject a specific resource"),
53                                           istring("[-]name"),
54                                           clusters);
55         
56         std::list<std::string> status;
57         options.AddOption('s', "status",
58                                           istring("only select jobs whose status is statusstr"),
59                                           istring("statusstr"),
60                                           status);
61         
62         std::string downloaddir;
63         options.AddOption('D', "dir",
64                                           istring("download directory (the job directory will"
65                                                           " be created in this directory)"),
66                                           istring("dirname"),
67                                           downloaddir);
68         
69         bool keep = false;
70         options.AddOption('k', "keep",
71                                           istring("keep the files on the server (do not clean)"),
72                                           keep);
73         
74         int timeout = -1;
75         options.AddOption('t', "timeout", istring("timeout in seconds (default 20)"),
76                                           istring("seconds"), timeout);
77         
78         std::string conffile;
79         options.AddOption('z', "conffile",
80                                           istring("configuration file (default ~/.arc/client.conf)"),
81                                           istring("filename"), conffile);
82         
83         
84         
85         std::list<std::string> jobs = options.Parse(argc, argv);
86         
87
88                 
89                 
90         Arc::UserConfig usercfg(conffile, joblist);
91         if (!usercfg) {
92                 result -> message = new_str("Failed configuration initialization");
93                 result -> get = 1;
94                 return result;
95         }
96                 
97         if (timeout > 0)
98         usercfg.Timeout(timeout);
99                 
100         if (jobs.empty() && clusters.empty()) {
101                 result -> message = new_str("No jobs given");
102                 result -> get = 1;
103                 return result;
104         }
105         
106         if (!jobs.empty())
107                 usercfg.ClearSelectedServices();
108         
109         if (!clusters.empty()) {
110                 usercfg.ClearSelectedServices();
111                 usercfg.AddServices(clusters, Arc::COMPUTING);
112         }
113         
114         Arc::JobSupervisor jobmaster(usercfg, jobs);
115         if (!jobmaster.JobsFound()) {
116                 result -> message = new_str("No jobs");
117                 result -> get = 0;
118                 return result;
119         }
120         std::list<Arc::JobController*> jobcont = jobmaster.GetJobControllers();
121         
122         // If the user specified a joblist on the command line joblist equals
123         // usercfg.JobListFile(). If not use the default, ie. usercfg.JobListFile().
124         if (jobcont.empty()) {
125                 result -> message = new_str("No job controller plugins loaded");
126                 result -> get = 1;
127                 return result;
128         }
129         
130         for (std::list<Arc::JobController*>::iterator it = jobcont.begin();
131                  it != jobcont.end(); it++){
132                 if (!(*it)->Get(status, downloaddir, keep)){
133                         result -> get = 1;
134                 }
135         }
136         
137         return result;
138 }