Bug fix.
[OpenSRF.git] / src / router / osrf_router.c
index 19f1ebe..b048317 100644 (file)
@@ -40,12 +40,14 @@ struct osrfRouterStruct {
        char* resource;       /**< Router's resource name for the Jabber logon. */
        char* password;       /**< Router's password for the Jabber logon. */
        int port;             /**< Jabber's port number. */
-       sig_atomic_t stop;    /**< To be set by signal handler to interrupt main loop. */
+       volatile sig_atomic_t stop; /**< To be set by signal handler to interrupt main loop. */
 
        /** Array of client domains that we allow to send requests through us. */
        osrfStringArray* trustedClients;
        /** Array of server domains that we allow to register, etc. with us. */
        osrfStringArray* trustedServers;
+       /** List of osrfMessages to be returned from osrfMessageDeserialize() */
+       osrfList* message_list;
 
        transport_client* connection;
 };
@@ -167,6 +169,7 @@ osrfRouter* osrfNewRouter(
        router->classes = osrfNewHash();
        osrfHashSetCallback(router->classes, &osrfRouterClassFree);
        router->class_itr = osrfNewHashIterator( router->classes );
+       router->message_list = NULL;   // We'll allocate one later
 
        // Prepare to connect to Jabber, as a non-component, over TCP (not UNIX domain).
        router->connection = client_init( domain, port, NULL, 0 );
@@ -342,6 +345,12 @@ static void osrfRouterClassHandleIncoming( osrfRouter* router, const char* class
 
                                        // A previous message bounced.  Try to send a clone of it to a
                                        // different node of the same class.
+                                       
+                                       // First make a local copy of the class name.  If the class gets
+                                       // deleted, the classname parameter becomes invalid.
+                                       char classname_copy[ strlen( classname ) + 1 ];
+                                       strcpy( classname_copy, classname );
+
                                        transport_message* bouncedMessage = osrfRouterClassHandleBounce(
                                                        router, classname, class, msg );
                                        /* handle bounced message */
@@ -349,7 +358,12 @@ static void osrfRouterClassHandleIncoming( osrfRouter* router, const char* class
                                                /* we have no one to send the requested message to */
                                                message_free( msg );
                                                osrfLogClearXid();
-                                               continue;
+                                               
+                                               // See if the class still exists
+                                               if( osrfHashGet( router->classes, classname_copy ) )
+                                                       continue;   // It does; keep going
+                                               else
+                                                       break;      // It doesn't; don't try to read from it any more
                                        }
                                        osrfRouterClassHandleMessage( router, class, bouncedMessage );
                                        message_free( bouncedMessage );
@@ -684,6 +698,7 @@ void osrfRouterFree( osrfRouter* router ) {
 
        osrfStringArrayFree( router->trustedClients );
        osrfStringArrayFree( router->trustedServers );
+       osrfListFree( router->message_list );
 
        client_free( router->connection );
        free(router);
@@ -772,22 +787,15 @@ static int _osrfRouterFillFDSet( osrfRouter* router, fd_set* set ) {
 */
 static void osrfRouterHandleAppRequest( osrfRouter* router, const transport_message* msg ) {
 
-       int T = 32;
-       osrfMessage* arr[T];
-       
-       // Initialize pointer array to all NULLs
-       int i;
-       for( i = 0; i < T; ++i )
-               arr[ i ] = NULL;
-
-       // Translate the JSON into an array of pointers to osrfMessage
-       int num_msgs = osrf_message_deserialize( msg->body, arr, T );
+       // Translate the JSON into a list of osrfMessages
+       router->message_list = osrfMessageDeserialize( msg->body, router->message_list );
        osrfMessage* omsg = NULL;
 
        // Process each osrfMessage
-       for( i = 0; i < num_msgs; i++ ) {
+       int i;
+       for( i = 0; i < router->message_list->size; ++i ) {
 
-               omsg = arr[i];
+               omsg = osrfListGetIndex( router->message_list, i );
                if( omsg ) {
 
                        switch( omsg->m_type ) {