cleaning up the JSON code some
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 25 Aug 2005 15:55:13 +0000 (15:55 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 25 Aug 2005 15:55:13 +0000 (15:55 +0000)
added host config retrieval for the stack
did some more memory checking

git-svn-id: svn://svn.open-ils.org/ILS/trunk@1728 dcc99617-32d9-48b4-a31d-7c20da2025e4

OpenSRF/src/libstack/Makefile
OpenSRF/src/libstack/osrf_config.c
OpenSRF/src/libstack/osrf_system.c
OpenSRF/src/libstack/osrf_system.h
OpenSRF/src/libtransport/transport_session.c
OpenSRF/src/objson/object.c
OpenSRF/src/objson/object.h
OpenSRF/src/utils/Makefile

index 7f39955..20024a2 100644 (file)
@@ -1,15 +1,32 @@
-CC_OPTS                +=  -DASSUME_STATELESS
-EXE_LD_OPTS += -lxml2 -lopensrf_transport  -lopensrf_stack -lobjson -lc_utils
+CC_OPTS                +=  -DASSUME_STATELESS 
+LD_OPTS += -lxml2 -lopensrf_transport  -lopensrf_stack -lobjson -lc_utils
 
-SOURCES = osrf_message.c osrf_app_session.c osrf_stack.c osrf_system.c osrf_config.c
-TARGETS = osrf_message.o osrf_app_session.o osrf_stack.o osrf_system.o osrf_config.o
-HEADERS = osrf_message.h osrf_app_session.h osrf_stack.h osrf_system.h osrf_config.h
+SOURCES = osrf_message.c osrf_app_session.c osrf_stack.c osrf_system.c osrf_config.c osrf_settings.c
+TARGETS = osrf_message.o osrf_app_session.o osrf_stack.o osrf_system.o osrf_config.o osrf_settings.o
+HEADERS = osrf_message.h osrf_app_session.h osrf_stack.h osrf_system.h osrf_config.h osrf_settings.h
 
-all: msg libopensrf_stack.so 
+all: msg libopensrf_stack.so  test
 
 msg: 
        echo "-> $$(pwd)"       
 
+test:  test.o
+       echo $@
+       $(CC) $(LD_OPTS) test.o xml_utils.o -o $@
+
+test.o:        test.c xml_utils.o
+       echo $@
+       $(CC) -c $(CC_OPTS) test.c -o $@ 
+
+xml_utils.o:
+       echo $@
+       cp ../utils/xml_utils.h .
+       cp ../utils/xml_utils.c .
+       $(CC) -c $(CC_OPTS) xml_utils.c -o $@ 
+
+# -------------------------------------------------------------
+
+
 libopensrf_stack.so: $(TARGETS)
        echo $@
        $(CC) -shared -W1 $(TARGETS) -o libopensrf_stack.so
@@ -31,10 +48,13 @@ osrf_system.o:      osrf_system.c osrf_system.h
 osrf_config.o: osrf_config.c osrf_config.h
        echo $@; $(CC) -c $(CC_OPTS) osrf_config.c -o $@
 
+osrf_settings.o:       osrf_settings.c osrf_settings.h
+       echo $@; $(CC) -c $(CC_OPTS) osrf_settings.c -o $@
+
 install:
        echo installing libopensrf_stack.so
        cp $(HEADERS) $(INCLUDEDIR)/$(OPENSRF)
        cp $(TMPDIR)/libopensrf_stack.so $(LIBDIR)/
 
 clean:
-       /bin/rm -f *.o libopensrf_stack.so
+       /bin/rm -f *.o libopensrf_stack.so test
index a4953b1..84e90da 100644 (file)
@@ -1,18 +1,5 @@
 #include "osrf_config.h"
 
-//object* config_object = NULL;
-
-/*
-char* osrf_config_value(char* path) {
-}
-
-
-void _osrf_config_grab_config() {
-       if(config_object) return;
-}
-*/
-
-
 void config_reader_init( char* name, char* config_file ) {
 
        if( name == NULL || config_file == NULL || strlen(config_file) == 0 ) {
index 92ddb48..62bf673 100644 (file)
@@ -1,6 +1,7 @@
 #include "osrf_system.h"
 
 transport_client* global_client;
+char* system_config = NULL;
 
 transport_client* osrf_system_get_transport_client() {
        return global_client;
@@ -8,12 +9,15 @@ transport_client* osrf_system_get_transport_client() {
 
 int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
 
-       if( config_file == NULL )
+       if( !config_file || !contextnode )
                fatal_handler("No Config File Specified\n" );
 
+       free(system_config);
+       free(osrf_config_context);
+       system_config = strdup(config_file);
+       osrf_config_context = strdup(contextnode);
+
        config_reader_init( "opensrf.bootstrap", config_file ); 
-       
-       osrf_config_context = contextnode;
 
        char* log_file          = config_value( "opensrf.bootstrap", "//%s/logfile", contextnode );
        char* log_level = config_value( "opensrf.bootstrap", "//%s/loglevel", contextnode );
@@ -57,12 +61,20 @@ int osrf_system_bootstrap_client( char* config_file, char* contextnode ) {
        return 0;
 }
 
-int osrf_system_shutdown() {
-       config_reader_free();
-       log_free();
+int osrf_system_disconnect_client() {
        client_disconnect( global_client );
        client_free( global_client );
        global_client = NULL;
+       return 0;
+}
+
+int osrf_system_shutdown() {
+       config_reader_free();
+       osrf_system_disconnect_client();
+       free(system_config);
+       free(osrf_config_context);
+       osrf_settings_free_host_config(NULL);
+       log_free();
        return 1;
 }
 
index 70fe6f3..cf50989 100644 (file)
@@ -2,6 +2,7 @@
 #include "utils.h"
 #include "logging.h"
 #include "osrf_config.h"
+//#include "osrf_settings.h"
 
 #ifndef OSRF_SYSTEM_H
 #define OSRF_SYSTEM_H
@@ -15,6 +16,8 @@ char* osrf_config_context;
 int osrf_system_bootstrap_client( char* config_file, char* contextnode );
 transport_client* osrf_system_get_transport_client();
 
+/* disconnects and destroys the current client connection */
+int osrf_system_disconnect_client();
 int osrf_system_shutdown(); 
 
 
index 44f88a8..21dfea1 100644 (file)
@@ -53,30 +53,19 @@ transport_session* init_transport(  char* server,
        session->parser_ctxt = xmlCreatePushParserCtxt(SAXHandler, session, "", 0, NULL);
 
        /* initialize the transport_socket structure */
-       //session->sock_obj = (transport_socket*) safe_malloc( sizeof(transport_socket) );
        session->sock_mgr = (socket_manager*) safe_malloc( sizeof(socket_manager) );
 
-       //int serv_size = strlen( server );
-/*
-       session->sock_obj->server = server;
-       session->sock_obj->port = port;
-       session->sock_obj->data_received_callback = &grab_incoming;
-*/
-
        session->sock_mgr->data_received = &grab_incoming;
        session->sock_mgr->blob = session;
        
        session->port = port;
        session->server = strdup(server);
-       if(unix_path)
+       if(unix_path)   
                session->unix_path = strdup(unix_path);
-       session->unix_path = NULL;
+       else session->unix_path = NULL;
 
        session->sock_id = 0;
 
-       /* this will be handed back to us in callbacks */
-       //session->sock_obj->user_data = session; 
-
        return session;
 }
 
@@ -86,18 +75,17 @@ transport_session* init_transport(  char* server,
 int session_free( transport_session* session ) {
        if( ! session ) { return 0; }
 
-       //if( session->sock_obj ) 
-       //      free( session->sock_obj );
-
        if(session->sock_mgr)
                socket_manager_free(session->sock_mgr);
 
        if( session->state_machine ) free( session->state_machine );
        if( session->parser_ctxt) {
-               xmlCleanupCharEncodingHandlers();
                xmlFreeDoc( session->parser_ctxt->myDoc );
                xmlFreeParserCtxt(session->parser_ctxt);
        }
+
+       xmlCleanupCharEncodingHandlers();
+       xmlDictCleanup();
        xmlCleanupParser();
 
        buffer_free(session->body_buffer);
index cea3d87..e04d6ad 100644 (file)
@@ -667,11 +667,11 @@ object* object_find_path(object* obj, char* format, ...) {
        char* t = buf;
        char* tt = tokbuf;
 
+       /* copy the path before strtok_r destroys it */
        char* pathcopy = strdup(buf);
 
        /* grab the root of the path */
        token = strtok_r(t, "/", &tt);
-       t = NULL;
        if(!token) return NULL;
 
        /* special case where path starts with //  (start anywhere) */
@@ -683,6 +683,7 @@ object* object_find_path(object* obj, char* format, ...) {
 
        free(pathcopy);
 
+       t = NULL;
        do { 
                obj = obj->get_key(obj, token);
        } while( (token = strtok_r(NULL, "/", &tt)) && obj);
@@ -695,20 +696,22 @@ object* object_find_path(object* obj, char* format, ...) {
 object* _object_find_path_recurse(object* obj, char* root, char* path) {
 
        if(!obj || ! root) return NULL;
+
+       /* collect all of the potential objects */
        object* arr = __object_find_path_recurse(obj, root);
-       object* newarr = json_parse_string("[]");
 
+       /* container for fully matching objects */
+       object* newarr = json_parse_string("[]");
        int i;
 
        /* path is just /root or /root/ */
        if( strlen(root) + 2 >= strlen(path) ) {
                return arr;
+
        } else {
 
+               /* gather all of the sub-objects that match the full path */
                for( i = 0; i < arr->size; i++ ) {
-                       /*
-                       fprintf(stderr, "Searching root %s and path %s and size %ld\n", root, path + strlen(root) + 1, arr->size);
-                       */
                        object* a = arr->get_index(arr, i);
                        object* thing = object_find_path(a , path + strlen(root) + 1); 
                        if(thing) newarr->push(newarr, thing);
@@ -723,6 +726,10 @@ object* __object_find_path_recurse(object* obj, char* root) {
 
        object* arr = json_parse_string("[]");
        if(!obj) return arr;
+
+       int i;
+
+       /* if the current object has a node that matches, add it */
        object* o = obj->get_key(obj, root);
        if(o) arr->push(arr, object_clone(o));
 
@@ -730,15 +737,14 @@ object* __object_find_path_recurse(object* obj, char* root) {
        object* childarr;
        object_iterator* itr = new_iterator(obj);
 
+       /* recurse through the children and find all potential nodes */
        while( (tmp = itr->next(itr)) ) {
                childarr = __object_find_path_recurse(tmp->item, root);
                if(childarr && childarr->size > 0) {
-                       int i;
                        for( i = 0; i!= childarr->size; i++ ) {
                                arr->push(arr, object_clone(childarr->get_index(childarr, i)));
                        }
                }
-
                free_object(childarr);
        }
 
index 8209de1..bf8f4a5 100644 (file)
@@ -219,6 +219,9 @@ char* json_string_format(char* json);
 object* object_clone(object* o);
 
 
+/* ------------------------------------------------------------------------ */
+/* XPATH */
+
 /* provide an XPATH style path (e.g. /some/node/here) and this will 
        return the object at that location if one exists.  Naturally,  
        every element in the path must be a proper object ("hash" / {}).
@@ -229,9 +232,14 @@ object* object_clone(object* o);
 object* object_find_path(object* obj, char* format, ...);
 
 
-/* returns an object array of matches */
+/* Utility method. finds any object in the tree that matches the path.  
+       Use this for finding paths that start with '//' */
 object* _object_find_path_recurse(object* obj, char* root, char* path);
+
+/* returns a list of object whose key is 'root'.  These are used as
+       potential objects when doing a // search */
 object* __object_find_path_recurse(object* obj, char* root);
 
+/* ------------------------------------------------------------------------ */
 
 #endif
index 6964e61..2221ae2 100644 (file)
@@ -4,18 +4,16 @@
 # ------------------------------------------------------------------------
 
 
-LD_OPTS        += -lc_utils
-CC_OPTS -= -Wall
-
 UTIL_SOURCES = logging.c utils.c socket_bundle.c md5.c sha.c string_array.c
 UTIL_HEADERS = logging.h utils.h socket_bundle.h md5.h sha.h string_array.h
 UTIL_OBJECTS = logging.o utils.o socket_bundle.o md5.o sha.o string_array.o
 
-all:   msg libc_utils.so
+all:   msg libc_utils.so 
 
 msg:
        echo "-> $$(pwd)"       
 
+
 install: 
        echo installing libc_utils.so
        cp $(UTIL_HEADERS) $(INCLUDEDIR)/
@@ -51,8 +49,9 @@ string_array.o: string_array.c string_array.h
        @echo $@
        $(CC) -c $(CC_OPTS) string_array.c -o $@ 
 
+
 clean:
-       /bin/rm -f *.o socket_test libc_utils.so
+       /bin/rm -f *.o socket_test libc_utils.so test