Added new C event code for handling the auth login events
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Tue, 6 Dec 2005 21:38:05 +0000 (21:38 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Tue, 6 Dec 2005 21:38:05 +0000 (21:38 +0000)
(just remembered that i need to read the events file from the settings server)

updated auth to use the new events code

added some events to the constants file

update srfsh to accept the new event

cleaned up some logging in the prefork and socket_bundle code

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

Open-ILS/src/c-apps/Makefile
Open-ILS/src/c-apps/oils_auth.c
Open-ILS/src/c-apps/oils_constants.h
Open-ILS/src/c-apps/oils_event.c [new file with mode: 0644]
Open-ILS/src/c-apps/oils_event.h [new file with mode: 0644]
OpenSRF/src/libstack/osrf_prefork.c
OpenSRF/src/srfsh/srfsh.c
OpenSRF/src/utils/socket_bundle.c

index 55996cf..a678500 100644 (file)
@@ -3,12 +3,13 @@ CFLAGS += -DOSRF_LOG_PARAMS
 
 all:   oils_auth.so
 
+oils_event.o:  oils_event.c oils_event.h
 oils_utils.o:  oils_utils.c oils_utils.h
 oils_auth.o:   oils_auth.c
 
 
-oils_auth.so:  oils_auth.o oils_utils.o
-       $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) oils_utils.o oils_auth.o -o $(TMPDIR)/oils_auth.so
+oils_auth.so:  oils_auth.o oils_utils.o oils_event.o
+       $(CC) -shared -W1 $(LDLIBS) $(LDFLAGS) oils_utils.o oils_event.o oils_auth.o -o $(TMPDIR)/oils_auth.so
 
 install:
        cp $(TMPDIR)/oils_auth.so $(LIBDIR)/
index 77f6a57..1ca1476 100644 (file)
@@ -4,6 +4,7 @@
 #include "opensrf/log.h"
 #include "oils_utils.h"
 #include "oils_constants.h"
+#include "oils_event.h"
 
 #define OILS_AUTH_CACHE_PRFX "oils_auth_"
 
@@ -94,13 +95,12 @@ int oilsAuthComplete( osrfMethodContext* ctx ) {
 
        if( uname && password ) {
 
-               jsonObject* response = jsonParseString("{\"ilsevent\":0}");
+               oilsEvent* response = NULL;
 
                /* grab the user object from storage */
                osrfLogDebug( "oilsAuth calling method %s with username %s", storageMethod, uname );
 
                osrfAppSession* session = osrfAppSessionClientInit( "open-ils.storage" ); /**/
-               //jsonObject* params = jsonNewObject(uname); /**/
                jsonObject* params = jsonParseString("[\"%s\"]", uname);
                int reqid = osrfAppSessionMakeRequest( session, params, storageMethod, 1, NULL );
                jsonObjectFree(params);
@@ -126,16 +126,11 @@ int oilsAuthComplete( osrfMethodContext* ctx ) {
                if(!userObj) { /* XXX needs to be a 'friendly' exception */
                        osrfMessageFree(omsg);
                        osrfAppSessionFree(session);
-                       jsonObjectSetKey(response, OILS_ILS_EVENT, 
-                                               jsonNewNumberObject(OILS_ILS_EVENT_AUTH_FAILED));
-                       osrfAppRespondComplete( ctx, response ); 
-                       jsonObjectFree(response);
+                       response = oilsNewEvent( OILS_EVENT_AUTH_FAILED );
+                       osrfAppRespondComplete( ctx, oilsEventToJSON(response) ); 
+                       oilsEventFree(response);
                        return 0;
 
-                       /*
-                       return osrfAppRequestRespondException( ctx->session, 
-                                       ctx->request, "User %s not found in the database", uname );
-                                       */
                }
 
                char* realPassword = oilsFMGetString( userObj, "passwd" ); /**/
@@ -168,20 +163,18 @@ int oilsAuthComplete( osrfMethodContext* ctx ) {
                        oilsFMSetString( userObj, "passwd", "" );
                        osrfCachePutObject( authKey, userObj, 28800 ); /* XXX config value */
                        osrfLogInternal("oilsAuthComplete(): Placed user object into cache");
-               //      response = jsonNewObject( authToken );
-                       jsonObjectSetKey( response, "authtoken", jsonNewObject(authToken) );    
+                       response = oilsNewEvent2( OILS_EVENT_SUCCESS, jsonNewObject(authToken) );
                        free(string); free(authToken); free(authKey);
 
                } else {
 
-                       jsonObjectSetKey(response, OILS_ILS_EVENT, 
-                                               jsonNewNumberObject(OILS_ILS_EVENT_AUTH_FAILED));
+                       response = oilsNewEvent( OILS_EVENT_AUTH_FAILED );
                        osrfLogInfo( "Login failed for for %s", uname );
                }
 
                osrfLogInternal("oilsAuthComplete responding to client");
-               osrfAppRespondComplete( ctx, response ); 
-               jsonObjectFree(response);
+               osrfAppRespondComplete( ctx, oilsEventToJSON(response) ); 
+               oilsEventFree(response);
                osrfMessageFree(omsg);
                osrfAppSessionFree(session);
 
index 1604bc1..a754bf5 100644 (file)
@@ -1,3 +1,3 @@
 
-#define OILS_ILS_EVENT "ilsevent"                      /* event object key */
-#define OILS_ILS_EVENT_AUTH_FAILED 1000
+#define OILS_EVENT_SUCCESS "SUCCESS"
+#define OILS_EVENT_AUTH_FAILED "LOGIN_FAILED"
diff --git a/Open-ILS/src/c-apps/oils_event.c b/Open-ILS/src/c-apps/oils_event.c
new file mode 100644 (file)
index 0000000..284ff23
--- /dev/null
@@ -0,0 +1,108 @@
+#include "oils_event.h"
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+
+osrfHash* __oilsEventEvents = NULL;
+
+oilsEvent* oilsNewEvent( char* event ) {
+       if(!event) return NULL;
+       if(!__oilsEventEvents) _oilsEventParseEvents();
+       oilsEvent* evt =  (oilsEvent*) safe_malloc(sizeof(oilsEvent));
+       evt->event = strdup(event);
+       evt->permloc = -1;
+       return evt;
+}
+
+oilsEvent* oilsNewEvent2( char* event, jsonObject* payload ) {
+       oilsEvent* evt = oilsNewEvent(event);
+       oilsEventSetPayload(evt, payload);
+       return evt;
+}
+
+oilsEvent* oilsNewEvent3( char* event, char* perm, int permloc ) {
+       oilsEvent* evt = oilsNewEvent(event);
+       oilsEventSetPermission( evt, perm, permloc );
+       return evt;
+}
+
+oilsEvent* oilsNewEvent4( char* event, char* perm, int permloc, jsonObject* payload ) {
+       oilsEvent* evt = oilsNewEvent3( event, perm, permloc );
+       if(evt) oilsEventSetPayload( evt, payload );
+       return evt;
+}
+
+void oilsEventSetPermission( oilsEvent* event, char* perm, int permloc ) {
+       if(!(event && perm)) return;
+       event->perm = strdup(perm);
+       event->permloc = permloc;
+}
+
+void oilsEventSetPayload( oilsEvent* event, jsonObject* payload ) {
+       if(!(event && payload)) return;
+       event->payload = jsonObjectClone(payload);
+}
+
+
+void oilsEventFree( oilsEvent* event ) {
+       if(!event) return;
+       free(event->perm);
+       if(event->json) jsonObjectFree(event->json);
+       else jsonObjectFree(event->payload);
+       free(event);
+}
+
+
+jsonObject* oilsEventToJSON( oilsEvent* event ) {
+       if(!event) return NULL;
+       char* code = osrfHashGet( __oilsEventEvents, event->event );
+
+       if(!code) {
+               osrfLogError( "No such event name: %s", event->event );
+               return NULL;
+       }
+
+       jsonObject* json = jsonNewObject(NULL);
+       jsonObjectSetKey( json, "ilsevent", jsonNewNumberObject(atoi(code)) );
+       jsonObjectSetKey( json, "textcode", jsonNewObject(event->event) );
+
+       if(event->perm) jsonObjectSetKey( json, "ilsperm", jsonNewObject(event->perm) );
+       if(event->permloc != -1) jsonObjectSetKey( json, "ilspermloc", jsonNewNumberObject(event->permloc) );
+       if(event->payload) jsonObjectSetKey( json, "payload", event->payload );
+       event->json = json;
+       return json;
+}
+
+
+void _oilsEventParseEvents() {
+       
+       char* xml = "/openils/var/data/ils_events.xml"; /* get me from the settings client */
+
+       xmlDocPtr doc = xmlParseFile(xml);
+       int success = 0;
+       __oilsEventEvents = osrfNewHash();
+
+       if( doc ) {
+               xmlNodePtr root = xmlDocGetRootElement(doc);
+               if( root ) {
+                       xmlNodePtr child = root->children;
+                       //osrfLogDebug("Node Name: %s", child->name);
+                       while( child ) {
+                               if( !strcmp((char*) child->name, "event") ) {
+                                       xmlChar* code = xmlGetProp( child, BAD_CAST "code");
+                                       xmlChar* textcode = xmlGetProp( child, BAD_CAST "textcode");
+                                       if( code && textcode ) {
+                                               osrfHashSet( __oilsEventEvents, code, textcode );
+                                               success = 1;
+                                               osrfLogDebug("Loading OILS Event: %s => %s", 
+                                                               textcode, osrfHashGet( __oilsEventEvents, textcode) );
+                                       }
+                               }
+                               child = child->next;
+                       }
+               }
+       }
+
+       if(!success) osrfLogError( " ! Unable to parse events file: %s", xml );
+}
+
+
diff --git a/Open-ILS/src/c-apps/oils_event.h b/Open-ILS/src/c-apps/oils_event.h
new file mode 100644 (file)
index 0000000..767207e
--- /dev/null
@@ -0,0 +1,50 @@
+#include "objson/object.h"
+#include "opensrf/utils.h"
+#include "opensrf/log.h"
+#include "opensrf/osrf_hash.h"
+
+
+/* OILS Event structure */
+struct _oilsEventStruct {
+       char* event;                    /* the event name */
+       char* perm;                             /* the permission error name */
+       int permloc;                    /* the permission location id */
+       jsonObject* payload;    /* the payload */
+       jsonObject* json;               /* the event as a jsonObject */
+};
+typedef struct _oilsEventStruct oilsEvent;
+
+
+/** Creates a new event.  User is responsible for freeing event with oilsEventFree */
+oilsEvent* oilsNewEvent( char* event );
+
+/** Creates a new event with payload.  
+ * User is responsible for freeing event with oilsEventFree */
+oilsEvent* oilsNewEvent2( char* event, jsonObject* payload );
+
+/** Creates a new event with permission and permission location.  
+ * User is responsible for freeing event with oilsEventFree */
+oilsEvent* oilsNewEvent3( char* event, char* perm, int permloc );
+
+/** Creates a new event with permission, permission location, and payload.  
+ * User is responsible for freeing event with oilsEventFree */
+oilsEvent* oilsNewEvent4( char* event, char* perm, int permloc, jsonObject* payload );
+
+/** Sets the permission info for the event */
+void oilsEventSetPermission( oilsEvent* event, char* perm, int permloc );
+
+/* Sets the payload for the event */
+void oilsEventSetPayload( oilsEvent* event, jsonObject* payload );
+
+/** Creates the JSON associated with an event.  The JSON should NOT be
+ * freed by the user.  It will be freed by oilsEventFree */
+jsonObject* oilsEventToJSON( oilsEvent* event );
+
+/* Parses the events file */
+void _oilsEventParseEvents();
+
+/* Frees an event object */
+void oilsEventFree( oilsEvent* event );
+
+
+
index 528c337..c467ae9 100644 (file)
@@ -109,7 +109,7 @@ void osrf_prefork_register_routers( char* appname ) {
 void prefork_child_init_hook(prefork_child* child) {
 
        if(!child) return;
-       osrfLogInfo("Child init hook for child %d", child->pid);
+       osrfLogDebug("Child init hook for child %d", child->pid);
        char* resc = va_list_to_string("%s_drone",child->appname);
 
        if(!osrf_system_bootstrap_client_resc( NULL, NULL, resc)) {
@@ -186,7 +186,7 @@ prefork_child*  launch_child( prefork_simple* forker ) {
                return NULL;
        }
 
-       osrfLogDebug( "Pipes: %d %d %d %d", data_fd[0], data_fd[1], status_fd[0], status_fd[1] );
+       osrfLogInternal( "Pipes: %d %d %d %d", data_fd[0], data_fd[1], status_fd[0], status_fd[1] );
        prefork_child* child = prefork_child_init( forker->max_requests, data_fd[0], 
                        data_fd[1], status_fd[0], status_fd[1] );
 
@@ -206,7 +206,7 @@ prefork_child*  launch_child( prefork_simple* forker ) {
                (forker->current_num_children)++;
                child->pid = pid;
 
-               osrfLogInfo( "Parent launched %d", pid );
+               osrfLogDebug( "Parent launched %d", pid );
                /* *no* child pipe FD's can be closed or the parent will re-use fd's that
                        the children are currently using */
                return child;
@@ -214,7 +214,7 @@ prefork_child*  launch_child( prefork_simple* forker ) {
 
        else { /* child */
 
-               osrfLogDebug("I am  new child with read_data_fd = %d and write_status_fd = %d",
+               osrfLogInternal("I am  new child with read_data_fd = %d and write_status_fd = %d",
                        child->read_data_fd, child->write_status_fd );
 
                child->pid = getpid();
@@ -295,18 +295,18 @@ void prefork_run(prefork_simple* forker) {
                        /* Look for an available child */
                        for( k = 0; k < forker->current_num_children; k++ ) {
 
-                               osrfLogDebug("Searching for available child. cur_child->pid = %d", cur_child->pid );
-                               osrfLogDebug("Current num children %d and loop %d", forker->current_num_children, k);
+                               osrfLogInternal("Searching for available child. cur_child->pid = %d", cur_child->pid );
+                               osrfLogInternal("Current num children %d and loop %d", forker->current_num_children, k);
                        
                                if( cur_child->available ) {
-                                       osrfLogDebug( "sending data to %d", cur_child->pid );
+                                       osrfLogDebug( "forker sending data to %d", cur_child->pid );
 
                                        message_prepare_xml( cur_msg );
                                        char* data = cur_msg->msg_xml;
                                        if( ! data || strlen(data) < 1 ) break;
 
                                        cur_child->available = 0;
-                                       osrfLogDebug( "Writing to child fd %d", cur_child->write_data_fd );
+                                       osrfLogInternal( "Writing to child fd %d", cur_child->write_data_fd );
 
                                        int written = 0;
                                        //fprintf(stderr, "Writing Data %f\n", get_timestamp_millis() );
@@ -337,8 +337,8 @@ void prefork_run(prefork_simple* forker) {
                                        char* data = cur_msg->msg_xml;
                                        if( ! data || strlen(data) < 1 ) break;
                                        new_child->available = 0;
-                                       osrfLogDebug( "sending data to %d", new_child->pid );
-                                       osrfLogDebug( "Writing to new child fd %d", new_child->write_data_fd );
+                                       osrfLogDebug( "Writing to new child fd %d : pid %d", 
+                                                       new_child->write_data_fd, new_child->pid );
                                        write( new_child->write_data_fd, data, strlen(data) + 1 );
                                        forker->first_child = new_child->next;
                                        honored = 1;
index 294c5d5..8909887 100644 (file)
@@ -279,7 +279,7 @@ int handle_login( char* words[]) {
 
                jsonObject* x = last_result->_result_content;
                if(x) {
-                       char* authtoken = jsonObjectGetString(jsonObjectGetKey(x, "authtoken"));
+                       char* authtoken = jsonObjectGetString(jsonObjectGetKey(x,"payload"));
                        if(authtoken) login_session = strdup(authtoken);
                        else login_session = NULL;
                }
index 70d94ba..4849fa2 100644 (file)
@@ -63,7 +63,7 @@ socket_node* _socket_add_node(socket_manager* mgr,
                int endpoint, int addr_type, int sock_fd, int parent_id ) {
 
        if(mgr == NULL) return NULL;
-       osrfLogDebug("Adding socket node with fd %d", sock_fd);
+       osrfLogInternal("Adding socket node with fd %d", sock_fd);
        socket_node* new_node = safe_malloc(sizeof(socket_node));
 
        new_node->endpoint      = endpoint;
@@ -159,7 +159,7 @@ int socket_open_unix_server(socket_manager* mgr, char* path) {
        //osrfLogDebug("Setting SO_REUSEADDR");
        //setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
        
-       osrfLogDebug("Setting TCP_NODELAY");
+       //osrfLogDebug("Setting TCP_NODELAY");
        setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i));
 
        _socket_add_node(mgr, SERVER_SOCKET, UNIX, sock_fd, 0);
@@ -209,7 +209,7 @@ int socket_open_tcp_client(socket_manager* mgr, int port, char* dest_addr) {
    }
 
        int i = 1;
-       osrfLogDebug("Setting TCP_NODELAY");
+       //osrfLogDebug("Setting TCP_NODELAY");
        setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i));
 
 
@@ -351,7 +351,6 @@ void socket_remove_node(socket_manager* mgr, int sock_fd) {
        if(head->sock_fd == sock_fd) {
                mgr->socket = head->next;
                free(head);
-               osrfLogDebug("removing first socket in list");
                return;
        }
 
@@ -385,10 +384,9 @@ void _socket_print_list(socket_manager* mgr) {
 
 /* sends the given data to the given socket */
 int socket_send(int sock_fd, const char* data) {
-       osrfLogDebug( "socket_bundle sending to %d data %s",
+       osrfLogInternal( "socket_bundle sending to %d data %s",
                sock_fd, data);
 
-       osrfLogDebug("%d : Sending data at %lf\n", getpid(), get_timestamp_millis());
        signal(SIGPIPE, SIG_IGN); /* in case a unix socket was closed */
        if( send( sock_fd, data, strlen(data), 0 ) < 0 ) {
                osrfLogWarning( "tcp_server_send(): Error sending data" );
@@ -455,7 +453,7 @@ int socket_wait(socket_manager* mgr, int timeout, int sock_fd) {
                }
        }
 
-       osrfLogDebug("%d active sockets after select()", retval);
+       osrfLogInternal("%d active sockets after select()", retval);
        return _socket_route_data_id(mgr, sock_fd);
 }
 
@@ -530,7 +528,7 @@ int _socket_route_data(
                        
                        if(last_failed_id != -1) {
                                /* in case it was not removed by our overlords */
-                               osrfLogDebug("Attempting to remove last_failed_id of %d", last_failed_id);
+                               osrfLogInternal("Attempting to remove last_failed_id of %d", last_failed_id);
                                socket_remove_node( mgr, last_failed_id );
                                last_failed_id = -1;
                                status = -1;
@@ -540,7 +538,7 @@ int _socket_route_data(
                        /* does this socket have data? */
                        if( FD_ISSET( sock_fd, read_set ) ) {
        
-                               osrfLogDebug("Socket %d active", sock_fd);
+                               osrfLogInternal("Socket %d active", sock_fd);
                                handled++;
                                FD_CLR(sock_fd, read_set);
        
@@ -554,7 +552,7 @@ int _socket_route_data(
                                        us...start over with the first socket */
                                if(status == -1)  {
                                        last_failed_id = sock_fd;
-                                       osrfLogDebug("Backtracking back to start of loop because "
+                                       osrfLogInternal("Backtracking back to start of loop because "
                                                        "of -1 return code from _socket_handle_client_data()");
                                }
                        }
@@ -623,12 +621,11 @@ int _socket_handle_client_data(socket_manager* mgr, socket_node* node) {
 
        memset(buf, 0, RBUFSIZE);
        set_fl(sock_fd, O_NONBLOCK);
-       osrfLogDebug("Gathering client data for %d", node->sock_fd);
 
-       osrfLogDebug("%d : Received data at %lf\n", getpid(), get_timestamp_millis());
+       osrfLogInternal("%d : Received data at %lf\n", getpid(), get_timestamp_millis());
 
        while( (read_bytes = recv(sock_fd, buf, RBUFSIZE-1, 0) ) > 0 ) {
-               osrfLogDebug("Socket %d Read %d bytes and data: %s", sock_fd, read_bytes, buf);
+               osrfLogInternal("Socket %d Read %d bytes and data: %s", sock_fd, read_bytes, buf);
                if(mgr->data_received)
                        mgr->data_received(mgr->blob, mgr, sock_fd, buf, node->parent_id);