telepathygabble/src/gabble.c
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /*
       
     2  * gabble.h - entry point and utility functions for telepathy-gabble
       
     3  * Copyright (C) 2005 Collabora Ltd.
       
     4  * Portions  and/or its subsidiary/subsidiaris. All rights reserved.
       
     5  *
       
     6  * This library is free software; you can redistribute it and/or
       
     7  * modify it under the terms of the GNU Lesser General Public
       
     8  * License as published by the Free Software Foundation; either
       
     9  * version 2.1 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This library is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  * Lesser General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Lesser General Public
       
    17  * License along with this library; if not, write to the Free Software
       
    18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    19  */
       
    20 
       
    21 #include "config.h"
       
    22 
       
    23 #include <dbus/dbus-glib.h>
       
    24 #include <stdlib.h>
       
    25 #include <string.h>
       
    26 #include <unistd.h>
       
    27 
       
    28 #ifdef HAVE_EXECINFO_H
       
    29 #include <execinfo.h>
       
    30 #endif /* HAVE_EXECINFO_H */
       
    31 
       
    32 #ifdef HAVE_SIGNAL_H
       
    33 #include <signal.h>
       
    34 #endif /* HAVE_SIGNAL_H */
       
    35 
       
    36 #include "debug.h"
       
    37 #include "gabble-connection-manager.h"
       
    38 #include "telepathy-errors.h"
       
    39 #include "telepathy-errors-enumtypes.h"
       
    40 
       
    41 GSource *timeout = NULL;
       
    42 GMainLoop *mainloop = NULL;
       
    43 GabbleConnectionManager *manager = NULL;
       
    44 gboolean connections_exist = FALSE;
       
    45 guint timeout_id;
       
    46 
       
    47 #define DIE_TIME 5000
       
    48 
       
    49 static gboolean
       
    50 kill_connection_manager (gpointer data)
       
    51 {
       
    52     g_debug ("%s: in kill_connection_manager", G_STRFUNC);
       
    53 #ifdef ENABLE_DEBUG
       
    54   if (!gabble_debug_flag_is_set (GABBLE_DEBUG_PERSIST) && !connections_exist)
       
    55 #else
       
    56   if (!connections_exist)
       
    57 #endif
       
    58     {
       
    59       g_debug ("no connections, and timed out");
       
    60       g_object_unref (manager);
       
    61       g_main_loop_quit (mainloop);
       
    62     }
       
    63 
       
    64   timeout_id = 0;
       
    65   g_debug ("%s: out kill_connection_manager", G_STRFUNC);
       
    66   return FALSE;
       
    67 }
       
    68 
       
    69 static void
       
    70 new_connection (GabbleConnectionManager *conn,
       
    71                 gchar *bus_name,
       
    72                 gchar *object_path,
       
    73                 gchar *proto)
       
    74 {
       
    75   connections_exist = TRUE;
       
    76 
       
    77   if (0 != timeout_id)
       
    78     {
       
    79       g_source_remove (timeout_id);
       
    80     }
       
    81 }
       
    82 
       
    83 static void
       
    84 no_more_connections (GabbleConnectionManager *conn)
       
    85 {
       
    86   connections_exist = FALSE;
       
    87   g_debug ("%s: in no more connections", G_STRFUNC);
       
    88   if (0 != timeout_id)
       
    89     {
       
    90       g_source_remove (timeout_id);
       
    91     }
       
    92 
       
    93   timeout_id = g_timeout_add (DIE_TIME, kill_connection_manager, NULL);
       
    94   g_debug ("%s: out no more connections", G_STRFUNC);
       
    95 }
       
    96 
       
    97 #ifdef ENABLE_BACKTRACE
       
    98 static void
       
    99 print_backtrace (void)
       
   100 {
       
   101 #if defined (HAVE_BACKTRACE) && defined (HAVE_BACKTRACE_SYMBOLS_FD)
       
   102   void *array[20];
       
   103   size_t size;
       
   104 
       
   105 #define MSG "\n########## Backtrace (version " VERSION ") ##########\n"
       
   106   write (STDERR_FILENO, MSG, strlen (MSG));
       
   107 #undef MSG
       
   108 
       
   109   size = backtrace (array, 20);
       
   110   backtrace_symbols_fd (array, size, STDERR_FILENO);
       
   111 #endif /* HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS_FD */
       
   112 }
       
   113 
       
   114 static void
       
   115 critical_handler (const gchar *log_domain,
       
   116                   GLogLevelFlags log_level,
       
   117                   const gchar *message,
       
   118                   gpointer user_data)
       
   119 {
       
   120   g_log_default_handler (log_domain, log_level, message, user_data);
       
   121   print_backtrace ();
       
   122 }
       
   123 
       
   124 #ifdef HAVE_SIGNAL
       
   125 static void
       
   126 segv_handler (int sig)
       
   127 {
       
   128 #define MSG "caught SIGSEGV\n"
       
   129   write (STDERR_FILENO, MSG, strlen (MSG));
       
   130 #undef MSG
       
   131 
       
   132   print_backtrace ();
       
   133   abort ();
       
   134 }
       
   135 #endif /* HAVE_SIGNAL */
       
   136 
       
   137 #endif /* ENABLE_BACKTRACE */
       
   138 
       
   139 static void
       
   140 add_signal_handlers (void)
       
   141 {
       
   142 #if defined(HAVE_SIGNAL) && defined(ENABLE_BACKTRACE)
       
   143   signal (SIGSEGV, segv_handler);
       
   144 #endif /* HAVE_SIGNAL && ENABLE_BACKTRACE */
       
   145 }
       
   146 
       
   147 
       
   148 main (int argc,
       
   149       char **argv)
       
   150 /* bsr      main (int argc,
       
   151       char **argv)*/
       
   152 {
       
   153   add_signal_handlers ();
       
   154   g_type_init();
       
   155 
       
   156   g_set_prgname("telepathy-gabble");
       
   157 
       
   158 /* bsr #ifdef ENABLE_DEBUG*/
       
   159   gabble_debug_set_flags_from_env ();
       
   160 //_dbus_setenv ("DBUS_VERBOSE","1");
       
   161 
       
   162   if (g_getenv ("GABBLE_PERSIST"))
       
   163     gabble_debug_set_flags (0xffff);
       
   164 /*#endif bsr */
       
   165 
       
   166     {
       
   167       GLogLevelFlags fatal_mask;
       
   168 
       
   169       fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
       
   170       fatal_mask |= G_LOG_LEVEL_CRITICAL;
       
   171       g_log_set_always_fatal (fatal_mask);
       
   172 
       
   173 #ifdef ENABLE_BACKTRACE
       
   174       g_log_set_handler ("GLib-GObject",
       
   175           G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR |
       
   176           G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
       
   177           critical_handler, NULL);
       
   178       g_log_set_handler ("GLib",
       
   179           G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR |
       
   180           G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
       
   181           critical_handler, NULL);
       
   182       g_log_set_handler (NULL,
       
   183           G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_ERROR |
       
   184           G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
       
   185           critical_handler, NULL);
       
   186 #endif /* ENABLE_BACKTRACE */
       
   187     }
       
   188 
       
   189   g_message("before gabble mainloop new \n");
       
   190   //getchar();
       
   191  /* exit(1); */
       
   192   mainloop = g_main_loop_new (NULL, FALSE);
       
   193 
       
   194   dbus_g_error_domain_register (TELEPATHY_ERRORS,
       
   195       "org.freedesktop.Telepathy.Error", TELEPATHY_TYPE_ERRORS);
       
   196 
       
   197   g_message("after dbus_g_error_domain_register \n");
       
   198   //getchar();
       
   199   
       
   200   manager = g_object_new (GABBLE_TYPE_CONNECTION_MANAGER, /*"name","GabbleConnectionManager",*/ NULL); /* bsr */
       
   201   g_assert (manager != NULL);
       
   202   g_message("after g_object_new \n");
       
   203   //getchar();
       
   204 
       
   205   g_signal_connect (manager, "new-connection",
       
   206       (GCallback) new_connection, NULL);
       
   207   g_message("after g_signal_connect \n");
       
   208   //getchar();
       
   209 
       
   210   g_signal_connect (manager, "no-more-connections",
       
   211       (GCallback) no_more_connections, NULL);
       
   212 
       
   213   g_message("calling gabble_connection_manager_register \n");
       
   214   _gabble_connection_manager_register (manager);
       
   215 
       
   216   g_message("started version " VERSION);
       
   217 
       
   218   timeout_id = g_timeout_add (DIE_TIME, kill_connection_manager, NULL);
       
   219 
       
   220   g_main_loop_run (mainloop);
       
   221   g_message("extign tg \n");
       
   222   return 0;
       
   223 }