Changeset 2293

Show
Ignore:
Timestamp:
06/28/07 12:43:23
Author:
murrayc
Message:

Change the test case to demo a connect_and_wait() function.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tests/test-conic.c

    r2227 r2293  
    1515 
    1616static ConIcConnection *cnx = NULL; 
     17static gboolean attempting_connection = FALSE; 
     18static gboolean is_online = FALSE; 
    1719 
    1820static void 
     
    4345        case CON_IC_STATUS_CONNECTED: 
    4446                printf ("DEBUG: %s: Connected.\n", __FUNCTION__); 
     47                is_online = TRUE; 
     48                attempting_connection = FALSE; /* unblock */ 
    4549                break; 
    4650        case CON_IC_STATUS_DISCONNECTED: 
    4751                printf ("DEBUG: %s: Disconnected.\n", __FUNCTION__); 
     52                is_online = FALSE; 
     53                attempting_connection = FALSE; /* unblock */ 
    4854                break; 
    4955        case CON_IC_STATUS_DISCONNECTING: 
     
    5864} 
    5965 
    60 void on_button_clicked (gpointer user_data
     66gboolean connect_and_wait(
    6167{ 
     68        printf ("%s: Creating ConIcConnection.\n", __FUNCTION__); 
     69 
     70        cnx = con_ic_connection_new (); 
     71        if (!cnx) { 
     72                g_warning ("con_ic_connection_new failed."); 
     73        } 
     74 
     75        const int connection = g_signal_connect (cnx, "connection-event", 
     76                G_CALLBACK(on_connection_event), NULL); 
     77 
     78        /* This might be necessary to make the connection object  
     79         * actually emit the signal, though the documentation says  
     80         * that they should be sent even when this is not set,  
     81         * when we explicitly try to connect.  
     82         * The signal still does not seem to be emitted. 
     83         */ 
     84        g_object_set (cnx, "automatic-connection-events", TRUE, NULL); 
     85 
    6286        printf ("%s: Attempting to connect. Waiting for signal.\n", __FUNCTION__); 
    6387         
     88        attempting_connection = TRUE; 
    6489        if (!con_ic_connection_connect (cnx, CON_IC_CONNECT_FLAG_NONE)) { 
    6590                g_warning ("con_ic_connection_connect() failed."); 
    6691        } 
    6792 
     93        printf ("%s: Waiting for !attempting_connection.\n", __FUNCTION__); 
     94         
     95        /* When the signal has been handled,  
     96         * attempting_connection will be reset to FALSE. */ 
     97        while (attempting_connection) { 
     98                /* Iterate the main loop so that the signal can be called. */ 
     99                if (g_main_context_pending (NULL)) { 
     100                        g_main_context_iteration (NULL, FALSE); 
     101                } 
     102        } 
     103 
     104        printf ("%s: Finished waiting for !attempting_connection.\n", __FUNCTION__); 
     105 
     106        g_signal_handler_disconnect (cnx, connection); 
     107 
     108        return is_online; /* This was set by the signal handler. */ 
     109} 
     110 
     111void on_button_clicked (gpointer user_data) 
     112{ 
     113        /* This blocks the UI. That's what we want. */ 
     114        gboolean result = connect_and_wait(); 
     115        printf ("%s: connect_and_wait() returned %d\n", __FUNCTION__, result); 
    68116} 
    69117 
     
    102150        g_signal_connect (G_OBJECT (button), "clicked", 
    103151                      G_CALLBACK (on_button_clicked), NULL); 
    104     
    105  
    106         printf ("%s: Creating ConIcConnection.\n", __FUNCTION__); 
    107  
    108         cnx = con_ic_connection_new (); 
    109         if (!cnx) { 
    110                 g_warning ("con_ic_connection_new failed."); 
    111         } 
    112         g_signal_connect (cnx, "connection-event", 
    113                           G_CALLBACK(on_connection_event), NULL); 
    114  
    115         /* This might be necessary to make the connection object  
    116          * actually emit the signal, though the documentation says  
    117          * that they should be sent even when this is not set,  
    118          * when we explicitly try to connect.  
    119          * The signal still does not seem to be emitted. 
    120          */ 
    121         g_object_set (cnx, "automatic-connection-events", TRUE, NULL); 
    122152 
    123153