[Opensrf-commits] r1922 - trunk/src/srfsh (scottmk)

svn at svn.open-ils.org svn at svn.open-ils.org
Sat Feb 6 15:53:37 EST 2010


Author: scottmk
Date: 2010-02-06 15:53:31 -0500 (Sat, 06 Feb 2010)
New Revision: 1922

Modified:
   trunk/src/srfsh/srfsh.c
Log:
1. For the "request" command: removed the calls to osrfAppSessionConnect()
and osrf_app_session_disconnect().  There's no point in opening and closing
a connection for a single request.

2. For the "math_bench" command: coerce the first command argument to a
positive value.  In the old code, a zero value led to a division-by-zero,
and a negative value led to a segfault.

3. For the "math_bench" command: accept an optional second parameter
to control whether and when we call osrf_app_session_disconnect().  It
has valid values of 0 (the default), 1, and 2; values out of range are
coerced to valid values.

4. In do_math(): initialize the array of floats by setting each float
to zero, instead of by using memset() to fill the array with
all-bits-zero.  The C Standard does not guarantee that all-bits-zero
represents zero for a float.

(This last change shouldn't make any difference anyway, because each float
in the array is overwritten before it is read.  Still, anything not worth
doing is not worth doing badly.)

M    src/srfsh/srfsh.c


Modified: trunk/src/srfsh/srfsh.c
===================================================================
--- trunk/src/srfsh/srfsh.c	2010-02-05 18:41:07 UTC (rev 1921)
+++ trunk/src/srfsh/srfsh.c	2010-02-06 20:53:31 UTC (rev 1922)
@@ -752,10 +752,6 @@
 			osrfLogToStderr();
 			params = jsonParse( OSRF_BUFFER_C_STR( buffer ) );
 			osrfRestoreLogType();
-			if( params == NULL) {
-				fprintf(stderr, "JSON error detected, not executing\n");
-				return 1;
-			}
 		}
 	} else {
 		if(!last_result || ! last_result->_result_content) { 
@@ -768,6 +764,12 @@
 		}
 	}
 
+	if(buffer->n_used > 0 && params == NULL) {
+		fprintf(stderr, "JSON error detected, not executing\n");
+		jsonObjectFree(params);
+		return 1;
+	}
+
 	int session_is_temporary;    // boolean
 	osrfAppSession* session = osrfHashGet( server_hash, server );
 	if( session ) {
@@ -777,17 +779,17 @@
 		session_is_temporary = 1;                     // just for this request
 	}
 
-	if(!osrfAppSessionConnect(session)) {
+	double start = get_timestamp_millis();
+
+	int req_id = osrfAppSessionSendRequest( session, params, method, 1 );
+	if( -1 == req_id ) {
 		fprintf(stderr, "Unable to communicate with service %s\n", server);
-		osrfLogWarning( OSRF_LOG_MARK,  "Unable to connect to remote service %s\n", server );
+		osrfLogWarning( OSRF_LOG_MARK,
+				"Unable to communicate with remote service %s\n", server );
 		osrfAppSessionFree( session );
 		jsonObjectFree(params);
 		return 1;
 	}
-
-	double start = get_timestamp_millis();
-
-	int req_id = osrfAppSessionSendRequest( session, params, method, 1 );
 	jsonObjectFree(params);
 
 	osrfMessage* omsg = osrfAppSessionRequestRecv( session, req_id, recv_timeout );
@@ -795,7 +797,6 @@
 	if(!omsg) 
 		printf("\nReceived no data from server\n");
 
-
 	signal(SIGPIPE, SIG_IGN);
 
 	FILE* less; 
@@ -899,7 +900,6 @@
 	if( osrf_app_session_request_complete( session, req_id ))
 		fputs("Request Completed Successfully\n", less);
 
-
 	fprintf(less, "Request Time in seconds: %.6f\n", end - start );
 	fputs("------------------------------------\n", less);
 
@@ -907,10 +907,8 @@
 
 	osrf_app_session_request_finish( session, req_id );
 
-	if( session_is_temporary ) {
-		osrf_app_session_disconnect( session );
+	if( session_is_temporary )
 		osrfAppSessionFree( session );
-	}
 
 	return 1;
 
@@ -1046,11 +1044,37 @@
 }
 */
 
+/**
+	@brief Execute the "math_bench" command.
+	@param cmd_array A list of command arguments.
+	@return 1 if successful, 0 if not.
 
+	The first command argument is required.  It is the number of iterations requested.  If
+	it is less than 1, it is coerced to 1.
+
+	The second command argument is optional, with allowed values of 0 (the default), 1, or 2.
+	It controls when and whether we call osrf_app_session_disconnect().  If this argument is
+	out of range, it is coerced to a value of 0 or 2.
+*/
 static int handle_math( const osrfStringArray* cmd_array ) {
 	const char* word = osrfStringArrayGetString( cmd_array, 1 );
-	if( word )
-		return do_math( atoi( word ), 0 );
+	if( word ) {
+		int count = atoi( word );
+		if( count < 1 )
+			count = 1;
+
+		int style = 0;
+		const char* style_arg = osrfStringArrayGetString( cmd_array, 2 );
+		if( style_arg ) {
+			style = atoi( style_arg );
+			if( style > 2 )
+				style = 2;
+			else if( style < 0 )
+				style = 0;
+		}
+
+		return do_math( count, style );
+	}
 	return 0;
 }
 
@@ -1067,8 +1091,12 @@
 	char* methods[] = { "add", "sub", "mult", "div" };
 	char* answers[] = { "3", "-1", "2", "0.5" };
 
+	// Initialize timings to zero.  This shouldn't make a difference, because
+	// we overwrite each timing anyway before reporting them.
 	float times[ count * 4 ];
-	memset(times, 0, sizeof(times));
+	int fi;
+	for( fi = 0; fi < count; ++fi )
+		times[ fi ] = 0.0;
 
 	int k;
 	for(k=0;k!=100;k++) {



More information about the opensrf-commits mailing list