Created shutdown init function to initiate and wait for shutdown

This commit is contained in:
James Booth
2012-09-11 22:55:59 +01:00
parent 1d44a8a345
commit 3a8229bf6a
5 changed files with 73 additions and 38 deletions

View File

@@ -495,8 +495,8 @@ static gboolean
_cmd_quit(const char * const inp, struct cmd_help_t help) _cmd_quit(const char * const inp, struct cmd_help_t help)
{ {
log_info("Profanity is shutting down."); log_info("Profanity is shutting down.");
jabber_disconnect(); profanity_shutdown_init();
return TRUE; return FALSE;
} }
static gboolean static gboolean

View File

@@ -31,6 +31,7 @@
#include "jabber.h" #include "jabber.h"
#include "log.h" #include "log.h"
#include "preferences.h" #include "preferences.h"
#include "profanity.h"
#include "ui.h" #include "ui.h"
#define PING_INTERVAL 120000 // 2 minutes #define PING_INTERVAL 120000 // 2 minutes
@@ -157,14 +158,24 @@ jabber_get_jid(void)
return xmpp_conn_get_jid(jabber_conn.conn); return xmpp_conn_get_jid(jabber_conn.conn);
} }
void gboolean
jabber_disconnect(void) jabber_disconnect(void)
{ {
// if connected, send end stream and wait for response
if (jabber_conn.conn_status == JABBER_CONNECTED) { if (jabber_conn.conn_status == JABBER_CONNECTED) {
log_info("Closing connection"); log_info("Closing connection");
// attempt closing the XML stream
xmpp_disconnect(jabber_conn.conn); xmpp_disconnect(jabber_conn.conn);
jabber_conn.conn_status = JABBER_DISCONNECTING;
return TRUE;
// if disconnected dont wait just shutdown
} else if (jabber_conn.conn_status == JABBER_DISCONNECTED) {
log_info("No connection open");
return FALSE;
// any other states, just shutdown
} else {
return FALSE;
} }
} }
@@ -172,7 +183,8 @@ void
jabber_process_events(void) jabber_process_events(void)
{ {
if (jabber_conn.conn_status == JABBER_CONNECTED if (jabber_conn.conn_status == JABBER_CONNECTED
|| jabber_conn.conn_status == JABBER_CONNECTING) || jabber_conn.conn_status == JABBER_CONNECTING
|| jabber_conn.conn_status == JABBER_DISCONNECTING)
xmpp_run_once(jabber_conn.ctx, 10); xmpp_run_once(jabber_conn.ctx, 10);
} }
@@ -364,41 +376,44 @@ _jabber_conn_handler(xmpp_conn_t * const conn,
jabber_conn.conn_status = JABBER_CONNECTED; jabber_conn.conn_status = JABBER_CONNECTED;
jabber_conn.presence = PRESENCE_ONLINE; jabber_conn.presence = PRESENCE_ONLINE;
} } else {
else {
if (jabber_conn.conn_status == JABBER_CONNECTED) { // received close stream response from server after disconnect
if (jabber_conn.conn_status == JABBER_DISCONNECTING) {
// free memory for connection object and context
xmpp_conn_release(jabber_conn.conn);
xmpp_ctx_free(jabber_conn.ctx);
// shutdown libstrophe
xmpp_shutdown();
jabber_conn.conn_status = JABBER_DISCONNECTED;
jabber_conn.presence = PRESENCE_OFFLINE;
profanity_shutdown();
// lost connection for unkown reason
} else if (jabber_conn.conn_status == JABBER_CONNECTED) {
cons_bad_show("Lost connection."); cons_bad_show("Lost connection.");
log_info("Lost connection"); log_info("Lost connection");
win_disconnected(); win_disconnected();
win_page_off();
log_info("disconnected");
xmpp_stop(ctx);
jabber_conn.conn_status = JABBER_DISCONNECTED;
jabber_conn.presence = PRESENCE_OFFLINE;
// login attempt failed
} else { } else {
cons_bad_show("Login failed."); cons_bad_show("Login failed.");
log_info("Login failed"); log_info("Login failed");
win_page_off();
log_info("disconnected");
xmpp_stop(ctx);
jabber_conn.conn_status = JABBER_DISCONNECTED;
jabber_conn.presence = PRESENCE_OFFLINE;
} }
win_page_off();
log_info("disconnected");
xmpp_stop(ctx);
jabber_conn.conn_status = JABBER_DISCONNECTED;
jabber_conn.presence = PRESENCE_OFFLINE;
} }
/* TO DO IF END STREAM
// free memory for connection object and context
xmpp_conn_release(jabber_conn.conn);
xmpp_ctx_free(jabber_conn.ctx);
// shutdown libstrophe
xmpp_shutdown();
jabber_conn.conn_status = JABBER_DISCONNECTED;
jabber_conn.presence = PRESENCE_OFFLINE;
gui_close();
chat_log_close();
prefs_close();
log_info("Shutdown complete");
log_close();
*/
} }
static int static int

View File

@@ -27,6 +27,7 @@ typedef enum {
JABBER_STARTED, JABBER_STARTED,
JABBER_CONNECTING, JABBER_CONNECTING,
JABBER_CONNECTED, JABBER_CONNECTED,
JABBER_DISCONNECTING,
JABBER_DISCONNECTED JABBER_DISCONNECTED
} jabber_conn_status_t; } jabber_conn_status_t;
@@ -44,7 +45,7 @@ jabber_conn_status_t jabber_connection_status(void);
jabber_presence_t jabber_presence_status(void); jabber_presence_t jabber_presence_status(void);
jabber_conn_status_t jabber_connect(const char * const user, jabber_conn_status_t jabber_connect(const char * const user,
const char * const passwd); const char * const passwd);
void jabber_disconnect(void); gboolean jabber_disconnect(void);
void jabber_roster_request(void); void jabber_roster_request(void);
void jabber_process_events(void); void jabber_process_events(void);
void jabber_send(const char * const msg, const char * const recipient); void jabber_send(const char * const msg, const char * const recipient);

View File

@@ -32,11 +32,11 @@
#include "history.h" #include "history.h"
#include "log.h" #include "log.h"
#include "preferences.h" #include "preferences.h"
#include "profanity.h"
#include "ui.h" #include "ui.h"
static log_level_t _get_log_level(char *log_level); static log_level_t _get_log_level(char *log_level);
gboolean _process_input(char *inp); gboolean _process_input(char *inp);
static void _profanity_shutdown(void);
static void _create_config_directory(); static void _create_config_directory();
void void
@@ -85,14 +85,32 @@ profanity_init(const int disable_tls, char *log_level)
cmd_init(); cmd_init();
log_info("Initialising contact list"); log_info("Initialising contact list");
contact_list_init(); contact_list_init();
atexit(_profanity_shutdown); atexit(profanity_shutdown_init);
} }
void void
_profanity_shutdown(void) profanity_shutdown_init(void)
{ {
log_info("Profanity is shutting down."); log_info("Profanity is shutting down.");
jabber_disconnect(); gboolean wait_response = jabber_disconnect();
if (wait_response) {
while (TRUE) {
jabber_process_events();
}
}
profanity_shutdown();
}
void
profanity_shutdown(void)
{
gui_close();
chat_log_close();
prefs_close();
log_info("Shutdown complete");
log_close();
} }
static log_level_t static log_level_t

View File

@@ -25,6 +25,7 @@
void profanity_init(const int disable_tls, char *log_level); void profanity_init(const int disable_tls, char *log_level);
void profanity_run(void); void profanity_run(void);
void profanity_shutdown_init(void);
void profanity_shutdown(void); void profanity_shutdown(void);
#endif #endif