Make *everything* including connection open, happen from inside the glib main loop
authorCarl Worth <cworth@cworth.org>
Thu, 10 Jan 2008 06:00:13 +0000 (22:00 -0800)
committerCarl Worth <cworth@cworth.org>
Thu, 10 Jan 2008 06:00:13 +0000 (22:00 -0800)
lm-echo.c

index 687ac3ce4b004a5e16151ec57f7ecbaced031460..22291d3e2643da3b83d8ac966357eb3f2d62cb75 100644 (file)
--- a/lm-echo.c
+++ b/lm-echo.c
 #endif
  
 typedef struct {
-    gchar      *name;
-    gchar      *passwd;
+    gchar              *server;
+    gchar              *name;
+    gchar              *passwd;
+    LmConnection       *connection;
+    GMainLoop          *main_loop;
+    int                         return_value;
 } UserInfo;
 
-typedef struct {
-    GMainLoop  *main_loop;
-} msg_data_t;
 static void
-authentication_cb (LmConnection *connection, gboolean result, gpointer ud)
+loudgame_quit (UserInfo *info, int return_value)
 {
-    g_print ("Auth: %d\n", result);
+    GError *error;
+
+    info->return_value = return_value;
+
+    if (! lm_connection_close (info->connection, &error))
+       g_print ("An error occurred during lm_connection_close: %s\n",
+                error->message);
+
+    lm_connection_unref (info->connection);
+
+    g_main_loop_quit (info->main_loop);
+}
  
-    if (result == TRUE) {
-       LmMessage *m;
-                 
-       m = lm_message_new_with_sub_type (NULL,
-                                         LM_MESSAGE_TYPE_PRESENCE,
-                                         LM_MESSAGE_SUB_TYPE_AVAILABLE);
-                 
-       lm_connection_send (connection, m, NULL);
-       lm_message_unref (m);
+static void
+authentication_cb (LmConnection *connection, gboolean result, gpointer closure)
+{
+    LmMessage *m;
+    UserInfo *info = closure;
+
+    if (! result) {
+       g_print ("Authentication for %s failed\n", info->name);
+       loudgame_quit (info, 1);
+       return;
     }
+                 
+    m = lm_message_new_with_sub_type (NULL,
+                                     LM_MESSAGE_TYPE_PRESENCE,
+                                     LM_MESSAGE_SUB_TYPE_AVAILABLE);
 
+    lm_connection_send (connection, m, NULL);
+    lm_message_unref (m);
 }
  
 static void
@@ -63,7 +81,8 @@ connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
 static void
 send_reply (LmConnection       *connection,
            const char          *peer,
-           const char          *message)
+           const char          *message,
+           UserInfo            *info)
 {
     LmMessage *reply;
     gboolean result;
@@ -77,7 +96,8 @@ send_reply (LmConnection      *connection,
     lm_message_unref (reply);
 
     if (! result) {
-       g_error ("lm_connection_send failed");
+       g_error ("lm_connection_send failed: error->message");
+       loudgame_quit (info, 1);
     }
 }
 
@@ -85,18 +105,17 @@ static void
 handle_command (LmConnection   *connection,
                const char      *peer,
                const char      *command,
-               gpointer         closure)
+               UserInfo        *info)
 {
-    msg_data_t *data = closure;
     char *error;
 
     if (strcmp (command, "quit") == 0) {
-       g_main_loop_quit (data->main_loop);
+       loudgame_quit (info, 0);
        return;
     }
 
     error = g_strdup_printf ("Unknown command: '%s'", command);
-    send_reply (connection, peer, error);
+    send_reply (connection, peer, error, info);
     free (error);
 }
  
@@ -106,6 +125,7 @@ handle_messages (LmMessageHandler *handler,
                  LmMessage        *m,
                  gpointer          closure)
 {
+    UserInfo *info = closure;
     LmMessageNode *body;
     const char *peer;
     const char *body_str;
@@ -117,61 +137,73 @@ handle_messages (LmMessageHandler *handler,
        body_str = lm_message_node_get_value (body);
 
        if (body_str && body_str[0] == '%')
-           handle_command (connection, peer, body_str + 1, closure);
+           handle_command (connection, peer, body_str + 1, info);
        else
-           send_reply (connection, peer, body_str);
+           send_reply (connection, peer, body_str, info);
     }
        
     return LM_HANDLER_RESULT_REMOVE_MESSAGE;
 }
-int
-main (int argc, char **argv)
+
+static gboolean
+make_connection (gpointer closure)
 {
-    LmConnection     *connection;
+    UserInfo        *info;
     LmMessageHandler *handler;
-    gboolean          result;
-    UserInfo          info;
     gchar            *jid;
-    msg_data_t       data;
     GError          *error;
 
-    if (argc != 4) {
-       g_print ("Usage: %s <server> <username> <password>\n", argv[0]);
-       return 1;
-    }
+    info = closure;
                                                                                 
-    connection = lm_connection_new (argv[1]);
+    info->connection = lm_connection_new (info->server);
 
-    jid = g_strdup_printf ("%s@%s", argv[2], argv[1]);
-    lm_connection_set_jid (connection, jid);
+    jid = g_strdup_printf ("%s@%s", info->name, info->server);
+    lm_connection_set_jid (info->connection, jid);
     g_free (jid);
 
-    data.main_loop = g_main_loop_new (NULL, FALSE);
-
-    handler = lm_message_handler_new (handle_messages, &data, NULL);
-    lm_connection_register_message_handler (connection, handler,
+    handler = lm_message_handler_new (handle_messages, info, NULL);
+    lm_connection_register_message_handler (info->connection,
+                                           handler,
                                            LM_MESSAGE_TYPE_MESSAGE,
                                            LM_HANDLER_PRIORITY_NORMAL);
                                                                                 
     lm_message_handler_unref (handler);
                                                                                 
+    if (! lm_connection_open (info->connection,
+                             (LmResultFunction) connection_open_cb,
+                             info, NULL, &error))
+    {
+       g_print ("Opening connection failed: %s\n", error->message);
+       loudgame_quit (info, 1);
+    }
+
+    /* Return false to not schedule another call. */
+    return 0;
+}
+
+int
+main (int argc, char **argv)
+{
+    UserInfo          info;
+
+    if (argc != 4) {
+       g_print ("Usage: %s <server> <username> <password>\n", argv[0]);
+       return 1;
+    }
+
+    info.server = argv[1];
     info.name = argv[2];
     info.passwd = argv[3];
-                                                                                
-    result = lm_connection_open (connection,
-                                (LmResultFunction) connection_open_cb,
-                                &info, NULL, NULL);
-    if (!result) {
-       g_print ("Opening connection failed: %d\n", result);
-       exit (1);
-    }
-                                                                                
-    g_main_loop_run (data.main_loop);
 
-    g_main_loop_unref (data.main_loop);
+    info.connection = NULL;
+
+    info.main_loop = g_main_loop_new (NULL, FALSE);
+
+    g_idle_add (make_connection, &info);
 
-    lm_connection_close (connection, &error);
-    lm_connection_unref (connection);
+    g_main_loop_run (info.main_loop);
+
+    g_main_loop_unref (info.main_loop);
                                                                                 
-    return 0;
+    return info.return_value;
 }