/* * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a * aplicativos mainframe. Registro no INPI sob o nome G3270. * * Copyright (C) <2008> * * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela * Free Software Foundation. * * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para * obter mais detalhes. * * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin * St, Fifth Floor, Boston, MA 02110-1301 USA * * Este programa está nomeado como gobject.c e possui - linhas de código. * * Contatos: * * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) * */ #include "private.h" #include /*---[ Implement ]----------------------------------------------------------------------------------*/ 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() { static unsigned int id = 0; struct session * rc = g_new0(struct session,1); 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; }; struct session * session_find(const gchar *key) { struct session * rc = NULL; unsigned int id = 0; if(sscanf(key,"%p-%u",&rc,&id) != 2) { g_warning("Invalid session id: %s",key); return NULL; } if(!g_list_find(sessions,rc)) { return NULL; } if(rc->id != id) { g_warning("Unexpected session id: %s",key); return NULL; } rc->activity = time(0); return rc; }; void session_destroy(struct session *ses) { lib3270_session_free(ses->host); sessions = g_list_remove(sessions,ses); g_free(ses); } void session_check_for_timeout(void) { GList * it; for(it = g_list_first(sessions); it != NULL; it = g_list_next(it)) { struct session *ses = (struct session *) it->data; 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; } } }