From 4d87d9fa8789efa503a638404282f55bb4bb1303 Mon Sep 17 00:00:00 2001 From: PerryWerneck Date: Sun, 5 Mar 2017 14:52:06 -0300 Subject: [PATCH] Implementando configuração dos timers de auto limpeza. --- src/gobject.c | 28 ++++++++++++++++++++++++++++ src/iocallback.c | 1 + src/private.h | 9 ++++++--- src/service.h | 6 +++--- src/session.c | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 82 insertions(+), 10 deletions(-) diff --git a/src/gobject.c b/src/gobject.c index 7558773..1535b32 100644 --- a/src/gobject.c +++ b/src/gobject.c @@ -320,3 +320,31 @@ void pw3270_dbus_get_screen_length(PW3270Dbus *object, const gchar *id, DBusGMet dbus_g_method_return(context,lib3270_get_length(ses->host)); } + +void pw3270_dbus_set_timeout(PW3270Dbus *object, const gchar *id, int timeout, DBusGMethodInvocation *context) { + + struct session * ses = session_find(id); + + if(!ses) { + pw3270_dbus_return_error(context,ENOENT); + return; + } + + ses->maxidle = (time_t) timeout; + + dbus_g_method_return(context,0); +} + +void pw3270_dbus_set_autoclose(PW3270Dbus *object, const gchar *id, int timeout, DBusGMethodInvocation *context) { + + struct session * ses = session_find(id); + + if(!ses) { + pw3270_dbus_return_error(context,ENOENT); + return; + } + + ses->autoclose = (time_t) timeout; + + dbus_g_method_return(context,0); +} diff --git a/src/iocallback.c b/src/iocallback.c index eb45daf..a126d55 100644 --- a/src/iocallback.c +++ b/src/iocallback.c @@ -254,6 +254,7 @@ static void beep(H3270 *session) { } void register_3270_io_handlers(void) { + static const struct lib3270_callbacks hdl = { sizeof(struct lib3270_callbacks), diff --git a/src/private.h b/src/private.h index 4f84b61..4677ebe 100644 --- a/src/private.h +++ b/src/private.h @@ -41,9 +41,12 @@ #include struct session { - unsigned int id; ///< @brief Identificador da sessão. - time_t activity; ///< @brief Timestamp da última atividade dessa sessão. - H3270 * host; ///< @brief Sessão TN3270. + unsigned int id; ///< @brief Identificador da sessão. + time_t activity; ///< @brief Timestamp da última atividade dessa sessão. + time_t timeout; ///< @brief Após quantos segundos eu encerro a sessao? + time_t maxidle; ///< @brief Tempo máximo que a sessão pode ficar IDLE + time_t autoclose; ///< @brief Destroi a sessão quantos segundos após a desconexão? + H3270 * host; ///< @brief Sessão TN3270. }; G_GNUC_INTERNAL GMainLoop * main_loop; diff --git a/src/service.h b/src/service.h index 30ac243..f6e24be 100644 --- a/src/service.h +++ b/src/service.h @@ -83,18 +83,18 @@ void pw3270_dbus_get_screen_height(PW3270Dbus *object, const gchar *id, DBusGMethodInvocation *context); void pw3270_dbus_get_screen_length(PW3270Dbus *object, const gchar *id, DBusGMethodInvocation *context); + void pw3270_dbus_set_timeout(PW3270Dbus *object, const gchar *id, int timeout, DBusGMethodInvocation *context); + void pw3270_dbus_set_autoclose(PW3270Dbus *object, const gchar *id, int timeout, DBusGMethodInvocation *context); + /* void pw3270_dbus_quit(PW3270Dbus *object, DBusGMethodInvocation *context); - void pw3270_dbus_connect(PW3270Dbus *object, const gchar *uri, DBusGMethodInvocation *context); void pw3270_dbus_get_ur_l(PW3270Dbus *object, DBusGMethodInvocation *context); void pw3270_dbus_set_ur_l(PW3270Dbus *object, const gchar *uri, DBusGMethodInvocation *context); - void pw3270_dbus_disconnect(PW3270Dbus *object, DBusGMethodInvocation *context); void pw3270_dbus_get_message_id(PW3270Dbus *object, DBusGMethodInvocation *context); void pw3270_dbus_get_connection_state(PW3270Dbus *object, DBusGMethodInvocation *context); void pw3270_dbus_get_secure_state(PW3270Dbus *object, DBusGMethodInvocation *context); - void pw3270_dbus_get_screen_contents(PW3270Dbus *object, DBusGMethodInvocation *context); H3270 * pw3270_dbus_get_session_handle(PW3270Dbus *object); GError * pw3270_dbus_get_error_from_errno(int code); diff --git a/src/session.c b/src/session.c index f7b383c..9189a24 100644 --- a/src/session.c +++ b/src/session.c @@ -31,8 +31,40 @@ /*---[ Implement ]----------------------------------------------------------------------------------*/ -static GList * sessions = NULL; ///< @brief Lista de sessões ativas. -static time_t maxidle = 300; +static GList * sessions = NULL; ///< @brief Lista de sessões ativas. + +struct session *find_by_h3270(H3270 *host) { + + GList * it; + + for(it = g_list_first(sessions); it != NULL; it = g_list_next(it)) { + + if( ((struct session *) it->data)->host == host) { + return (struct session *) it->data; + } + + } + + return NULL; +} + + +void on_connect(H3270 *hSession, int state, void *none) { + + struct session *ses = find_by_h3270(hSession); + + if(!ses) { + return; + } + + ses->activity = time(0); + ses->timeout = state ? ses->maxidle : ses->autoclose; + +#ifdef DEBUG + g_message("Session %p-%u is %s (timeout=%u)",ses,ses->id,state ? "connected" : "disconnected",(unsigned int) ses->timeout); +#endif // DEBUG + +} struct session * session_new() { @@ -42,10 +74,15 @@ struct session * session_new() { rc->id = ++id; rc->activity = time(0); + rc->timeout = 600; + rc->maxidle = 600; + rc->autoclose = 60; rc->host = lib3270_session_new(""); sessions = g_list_append(sessions,rc); + lib3270_register_schange(rc->host, LIB3270_STATE_CONNECT, (void (*)(H3270 *, int, void *)) on_connect, rc); + return rc; }; @@ -84,15 +121,18 @@ void session_destroy(struct session *ses) { void session_check_for_timeout(void) { - time_t timeout = time(NULL) - maxidle; GList * it; for(it = g_list_first(sessions); it != NULL; it = g_list_next(it)) { struct session *ses = (struct session *) it->data; - if( ses->activity < timeout) { + if( ses->timeout && (ses->activity + ses->timeout) < time(NULL) ) { +#ifdef DEBUG + g_message("Closing IDLE session %p-%u (idle=%u)",ses,ses->id,(unsigned int) (time(NULL) - ses->activity)); +#else g_message("Closing IDLE session %p-%u",ses,ses->id); +#endif // DEBUG session_destroy(ses); break; } -- libgit2 0.21.2