[Swift-commit] cog r3981

swift at ci.uchicago.edu swift at ci.uchicago.edu
Wed Jun 25 17:20:32 CDT 2014


------------------------------------------------------------------------
r3981 | timgarmstrong | 2014-06-25 17:18:13 -0500 (Wed, 25 Jun 2014) | 1 line

Add ability to get string representing job.  Slightly simplify code by removing pointer indirection.
------------------------------------------------------------------------
Index: modules/provider-coaster-c-client/src/Job.h
===================================================================
--- modules/provider-coaster-c-client/src/Job.h	(revision 3980)
+++ modules/provider-coaster-c-client/src/Job.h	(working copy)
@@ -35,7 +35,7 @@
 		 * it work to just store them by value and have zero-length
 		 * be equivalent to NULL.  Are zero-length strings meaningful?
 		 */
-		std::vector<std::string*>* arguments;
+		std::vector<std::string*> arguments;
 		std::string* directory;
 		std::string* stdinLocation;
 		std::string* stdoutLocation;
@@ -55,7 +55,7 @@
 
 		JobStatus* status;
 		
-                /* Disable default copy constructor */
+		/* Disable default copy constructor */
 		Job(const Job&);
 		/* Disable default assignment */
 		Job& operator=(const Job&);
@@ -81,11 +81,11 @@
 		const std::string* getRemoteIdentity() const;
 		void setRemoteIdentity(const std::string& remoteId);
 
-		std::vector<std::string*>* getArguments();
+		const std::vector<std::string*>& getArguments();
 
-                /*
-                 * Add argument, taking ownership
-                 */
+		/*
+		 * Add argument, taking ownership
+		 */
 		void addArgument(std::string* arg);
 		void addArgument(const std::string& arg);
 		void addArgument(const char* arg);
@@ -135,6 +135,12 @@
 
 		const std::string* getStdout() const;
 		const std::string* getStderr() const;
+		
+		/*
+		 * Return a human-readable string representation
+		 * of the job.
+		 */
+		std::string toString() const;
 };
 
 }
Index: modules/provider-coaster-c-client/src/coasters.cpp
===================================================================
--- modules/provider-coaster-c-client/src/coasters.cpp	(revision 3980)
+++ modules/provider-coaster-c-client/src/coasters.cpp	(working copy)
@@ -25,6 +25,7 @@
 
 #include <cassert>
 #include <cstdlib>
+#include <cstring>
 #include <pthread.h>
 
 #include "CoasterClient.h"
@@ -38,6 +39,7 @@
 using std::free;
 
 using std::string;
+using std::memcpy;
 
 /*
   Struct just wraps the objects
@@ -265,6 +267,29 @@
 }
 
 coaster_rc
+coaster_job_to_string(const coaster_job *job, char **str, size_t *str_len)
+                                   COASTERS_THROWS_NOTHING {
+  if (job == NULL || str == NULL || str_len == NULL) {
+    return coaster_return_error(COASTER_ERROR_INVALID, "invalid argument");
+  }
+
+  try {
+    string jobStr = job->toString();
+
+    *str = (char*)malloc(jobStr.length() + 1);
+    COASTER_CHECK_MALLOC(*str);
+    memcpy(*str, jobStr.c_str(), jobStr.length() + 1);
+    *str_len = jobStr.length();
+    return COASTER_SUCCESS;
+  } catch (const CoasterError& err) {
+    return coaster_error_rc(err);
+  } catch (const std::exception& ex) {
+    return exception_rc(ex);
+  }
+
+}
+
+coaster_rc
 coaster_job_set_redirects(coaster_job *job,
       const char *stdin_loc, size_t stdin_loc_len,
       const char *stdout_loc, size_t stdout_loc_len,
Index: modules/provider-coaster-c-client/src/Job.cpp
===================================================================
--- modules/provider-coaster-c-client/src/Job.cpp	(revision 3980)
+++ modules/provider-coaster-c-client/src/Job.cpp	(working copy)
@@ -7,6 +7,7 @@
 using std::map;
 using std::pair;
 using std::string;
+using std::stringstream;
 using std::vector;
 
 static job_id_t seq = 0;
@@ -15,7 +16,6 @@
 	executable = pexecutable;
 	identity = seq++;
 
-	arguments = NULL;
 	directory = NULL;
 	stdinLocation = NULL;
 	stdoutLocation = NULL;
@@ -51,15 +51,12 @@
 	remoteIdentity = new string(remoteId);
 }
 
-vector<string*>* Job::getArguments() {
+const vector<string*>& Job::getArguments() {
 	return arguments;
 }
 
 void Job::addArgument(string* arg) {
-	if (arguments == NULL) {
-		arguments = new vector<string*>;
-	}
-	arguments->push_back(arg);
+	arguments.push_back(arg);
 }
 
 const string& Job::getExecutable() const {
@@ -250,6 +247,24 @@
 	return stderr;
 }
 
+/*
+ * Just include the executable and arguments for now
+ */
+string Job::toString() const {
+	stringstream ss;
+	ss << executable;
+	for (vector<string*>::const_iterator it = arguments.begin();
+	     it != arguments.end(); ++it) {
+		const string *arg = *it;
+		if (arg == NULL) {
+			ss << " NULL";	
+		} else {
+			ss << " " << *arg ;
+		}
+	}
+	return ss.str();
+}
+
 void Job::setStatus(JobStatus* newStatus) {
 	// Since the client can process a job status while another
 	// status is coming in, a status cannot be deleted when a new status comes in.
@@ -276,10 +291,7 @@
 	if (remoteIdentity != NULL) {
 		delete remoteIdentity; 
 	}
-	if (arguments != NULL) {
-		for (int i = 0; i < arguments->size(); i++) {
-			delete arguments->at(i);
-		}
-		delete arguments;
+	for (int i = 0; i < arguments.size(); i++) {
+		delete arguments.at(i);
 	}
 }
Index: modules/provider-coaster-c-client/src/coasters.h
===================================================================
--- modules/provider-coaster-c-client/src/coasters.h	(revision 3980)
+++ modules/provider-coaster-c-client/src/coasters.h	(working copy)
@@ -191,6 +191,14 @@
 coaster_job_free(coaster_job *job) COASTERS_THROWS_NOTHING;
 
 /*
+ * Create a human readable string describing job.
+ * str: output for dynamically allocated string, to be freed by caller
+ */
+coaster_rc
+coaster_job_to_string(const coaster_job *job, char **str, size_t *str_len)
+                                  COASTERS_THROWS_NOTHING;
+
+/*
  * Set input and output stream redirections.
  * If set to NULL, don't modify.
  */
Index: modules/provider-coaster-c-client/src/JobSubmitCommand.cpp
===================================================================
--- modules/provider-coaster-c-client/src/JobSubmitCommand.cpp	(revision 3980)
+++ modules/provider-coaster-c-client/src/JobSubmitCommand.cpp	(working copy)
@@ -53,11 +53,9 @@
 	add(ss, "stderr", job->getStderrLocation());
 
 
-	vector<string*>* arguments = job->getArguments();
-	if (arguments != NULL) {
-		for (vector<string*>::iterator i = arguments->begin(); i != arguments->end(); ++i) {
-			add(ss, "arg", *i);
-		}
+	const vector<string*>& arguments = job->getArguments();
+	for (vector<string*>::const_iterator i = arguments.begin(); i != arguments.end(); ++i) {
+		add(ss, "arg", *i);
 	}
 
 	map<string, string>* env = job->getEnv();



More information about the Swift-commit mailing list