connectionMgr/src/pingThread.c File Reference

#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include "connectionMgrLog.h"
#include "ecdc_err.h"
#include "connectScreenData.h"
#include "connectionMgr.h"
#include "pingThread.h"
#include "system.h"

Go to the source code of this file.

Functions

static gpointer pingThread (gpointer arg)
gboolean pingThread_start (ping_thread_parms *ping_parms)
gboolean pingThread_stop ()
gboolean pingThread_wait (int timeout_sec)
gboolean pingThread_stopped (void)

Variables

static volatile GThread * g_ping_thread = NULL
static volatile gboolean g_ping_abort = FALSE


Function Documentation

static gpointer pingThread ( gpointer  arg  )  [static]

Definition at line 46 of file pingThread.c.

00047 {
00048 #ifdef ECDC_LIBRARY
00049     pecd_connection_t pecd_connection;
00050 #endif
00051     gboolean connected = FALSE;
00052 
00053     ping_thread_parms *parms = (ping_thread_parms*)arg;
00054 #ifdef ECDC_LIBRARY
00055     gchar *redirectUrl = connect_data_get_ecd_url();
00056 #else
00057     gchar *redirectUrl = NULL;
00058 #endif
00059     gchar *errmsg;
00060 
00061     int   i;
00062     int   rc;
00063     int   argc;
00064     char* argv[10];
00065 
00066 
00067     CN_LOGPRINTF("entry");
00068 
00069     gdk_threads_enter();
00070     gtk_label_set_text(GTK_LABEL(parms->pingStatus), "ping-started");
00071     gdk_threads_leave();
00072 
00073     parms->err = connect_enable_network(parms->settings);
00074     if (   g_ping_abort == FALSE
00075         && parms->err == ECD_NO_ERROR)
00076     {
00077         if (parms->pcshare == NULL)
00078         {
00079 #ifdef ECDC_LIBRARY
00080             // connect to iDS
00081             pecd_connection = ecd_connection_create(redirectUrl, 
00082                                                     DUMMY_EUI64, 
00083                                                     DUMMY_PRODUCT_ID);
00084             if (pecd_connection)
00085             {
00086                 if (ecd_ping(pecd_connection))
00087                 {
00088                     connected = TRUE;
00089                 }
00090                 else
00091                 {
00092                     connect_disable_network();
00093                 }
00094                 parms->err = ecd_errno(pecd_connection);
00095                 ecd_connection_destroy(pecd_connection);
00096             }
00097 #else
00098                         connected = TRUE;
00099 #endif
00100         }
00101         else
00102         {
00103             // connect to PC share
00104             argc = 0;
00105             argv[argc++] = COMMAND_MOUNT_SAMBA;
00106             argv[argc++] = "mount";
00107             argv[argc++] = parms->pcshare->pcname ? parms->pcshare->pcname    : "";
00108             argv[argc++] = parms->pcshare->sharename ? parms->pcshare->sharename : "";
00109             argv[argc++] = parms->pcshare->workgroup ? parms->pcshare->workgroup : "";
00110             if (    parms->pcshare->username
00111                 &&  parms->pcshare->username[0] != '\0' )
00112             {
00113                 argv[argc++] = parms->pcshare->username;
00114                 argv[argc++] = parms->pcshare->password;
00115             }
00116             argv[argc] = NULL;
00117             g_assert( argc < (sizeof(argv)/sizeof(argv[0])) );
00118 
00119             CN_LOGPRINTF("execute command [%s]", argv[0]);
00120             for (i = 0 ; i < argc ; i++)
00121             {
00122                 CN_LOGPRINTF("    argv[%d] = [%s]", i, argv[i]);
00123             }
00124             rc = fork_exec(argc, argv);
00125             CN_LOGPRINTF("fork_exec [%s] returned [%d]", argv[0], rc);
00126             if (rc == 0)
00127             {
00128                 connected = TRUE;
00129             }
00130             else
00131             {
00132                 CN_ERRORPRINTF("Execute [%s] failed - "
00133                                "fork_exec returns [%d]", 
00134                                argv[0], rc);
00135                 connect_disable_network();
00136                 parms->err = ECD_NO_CONNECTION;
00137             }
00138         }
00139     }
00140 
00141     g_free(redirectUrl);
00142 
00143     // tell GTK thread we are done ...
00144     gdk_threads_enter();
00145     {
00146         g_ping_thread = NULL;
00147         if (gtk_main_level() == 0)
00148         {
00149             CN_WARNPRINTF("    -- oops, gtk_main has quit!");
00150         }
00151         else
00152         {
00153             if (connected)
00154             {
00155                 gtk_label_set_text(GTK_LABEL(parms->pingStatus), 
00156                                    "ping-done-ok");
00157             }
00158             else
00159             {
00160                 if (g_ping_abort)
00161                 {
00162                     errmsg = "ping-done-abort";
00163                 }
00164                 else
00165                 {
00166                     if (  ((parms->settings->connection == wireless_t) 
00167                             && (parms->err == ECD_WIRELESS_ERROR))
00168                        || ((parms->settings->connection == wired_t)
00169                             && (parms->err == ECD_WIRED_ERROR))
00170                        || ((parms->settings->connection == dialup_t)
00171                             && (parms->err == ECD_DIALUP_ERROR))  )
00172                     {
00173                         errmsg = "ping-network-error";
00174                     }
00175                     else
00176                     {
00177                         if (parms->pcshare)
00178                         {
00179                             errmsg = "ping-pcshare-error";
00180                         }
00181                         else
00182                         {
00183                             errmsg = "ping-done-error";
00184                         }
00185                     }
00186                 }
00187                 gtk_label_set_text(GTK_LABEL(parms->pingStatus), errmsg);
00188             }
00189         }
00190     }
00191     gdk_threads_leave();
00192     
00193     // ... and quit
00194     CN_LOGPRINTF("exit, connected [%d]", connected);
00195     return (gpointer)connected;
00196 }

Here is the call graph for this function:

gboolean pingThread_start ( ping_thread_parms ping_parms  ) 

Definition at line 199 of file pingThread.c.

00200 {
00201     GThread* thread = NULL;
00202     GError*  error = NULL;
00203 
00204     CN_LOGPRINTF("entry");
00205     if (g_ping_thread)
00206     {
00207         CN_ERRORPRINTF("ping thread already running");
00208         return FALSE;
00209     }
00210 
00211     // create the ping thread
00212     g_ping_abort = FALSE;
00213     thread = g_thread_create(pingThread, ping_parms, FALSE, &error);
00214     if (error != NULL)
00215     {
00216         CN_ERRORPRINTF("Failed to create ping thread - %s [%d]", 
00217                        error->message, error->code);
00218         g_free(error);
00219         error = NULL;
00220     }
00221     else
00222     {
00223         g_ping_thread = thread;
00224     }
00225 
00226     return TRUE;
00227 }

Here is the call graph for this function:

gboolean pingThread_stop ( void   ) 

Definition at line 230 of file pingThread.c.

00231 {
00232     CN_LOGPRINTF("entry");
00233 
00234     if (g_ping_thread)
00235     {
00236         // stop the ping thread
00237         g_ping_abort = TRUE;
00238 
00239         // stop connect action, when running
00240         connect_enable_network_abort();
00241 
00242         // stop any process spawned by pingThread
00243         pid_t pid = get_forkexec_child_pid();
00244         if (pid > 0)
00245         {
00246             kill(pid, SIGTERM);
00247         }
00248 
00249         return TRUE;
00250     }
00251     else
00252     {
00253         CN_ERRORPRINTF("ping thread not running");
00254         return FALSE;
00255     }
00256 }

Here is the call graph for this function:

gboolean pingThread_stopped ( void   ) 

Definition at line 282 of file pingThread.c.

00283 {
00284     CN_LOGPRINTF("entry");
00285 
00286     if (g_ping_thread)
00287     {
00288         return FALSE;
00289     }
00290     else
00291     {
00292         return TRUE;
00293     }
00294 }

gboolean pingThread_wait ( int  timeout_sec  ) 

Definition at line 259 of file pingThread.c.

00260 {
00261     int max_ticks = 2 * timeout_sec;
00262 
00263     CN_LOGPRINTF("entry");
00264     
00265     if (g_ping_thread)
00266     {
00267         // sorry don't like busy-wait, 
00268         // but g_thread_join has no timeout option
00269         while (g_ping_thread  &&  --max_ticks >= 0)
00270         {
00271             g_usleep(500*1000L);
00272         }
00273         return TRUE;
00274     }
00275     else
00276     {
00277         CN_ERRORPRINTF("ping thread not running");
00278         return FALSE;
00279     }
00280 }


Variable Documentation

volatile gboolean g_ping_abort = FALSE [static]

Definition at line 43 of file pingThread.c.

volatile GThread* g_ping_thread = NULL [static]

Definition at line 42 of file pingThread.c.


Generated on Sun Dec 14 17:16:43 2008 by  doxygen 1.5.6