From 1fb5a3edc609d2ed8838da2ee8d31e3ff48ee5f6 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 17 Sep 2014 00:03:14 +0100 Subject: [PATCH] Use hash counts to calculate numbers of clients. --- src/context.c | 6 ------ src/mosquitto_broker.h | 2 -- src/persist.c | 3 --- src/read_handle_server.c | 6 +----- src/sys_tree.c | 17 +++++++++++------ 5 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/context.c b/src/context.c index 396cc138..037d0f97 100644 --- a/src/context.c +++ b/src/context.c @@ -199,12 +199,6 @@ void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *ctxt) ctxt->will = NULL; } ctxt->disconnect_t = time(NULL); -#ifdef WITH_SYS_TREE - db->connected_count--; - if(!ctxt->clean_session){ - db->disconnected_count++; - } -#endif _mosquitto_socket_close(db, ctxt); } diff --git a/src/mosquitto_broker.h b/src/mosquitto_broker.h index 2b067339..2a210ce4 100644 --- a/src/mosquitto_broker.h +++ b/src/mosquitto_broker.h @@ -221,8 +221,6 @@ struct mosquitto_db{ #ifdef WITH_SYS_TREE int subscription_count; int retained_count; - int connected_count; - int disconnected_count; #endif }; diff --git a/src/persist.c b/src/persist.c index 70fded58..c028a6b5 100644 --- a/src/persist.c +++ b/src/persist.c @@ -55,9 +55,6 @@ static struct mosquitto *_db_find_or_add_context(struct mosquitto_db *db, const return NULL; } -#ifdef WITH_SYS_TREE - db->disconnected_count++; -#endif context->clean_session = false; HASH_ADD_KEYPTR(hh_id, db->contexts_by_id, context->id, strlen(context->id), context); diff --git a/src/read_handle_server.c b/src/read_handle_server.c index a6864052..3cc68566 100644 --- a/src/read_handle_server.c +++ b/src/read_handle_server.c @@ -104,7 +104,6 @@ int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context) #ifdef WITH_SYS_TREE g_connection_count++; - db->connected_count++; #endif /* Don't accept multiple CONNECT commands. */ @@ -407,10 +406,7 @@ int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context) /* Found a matching client */ if(found_context->sock == INVALID_SOCKET){ /* Client is reconnecting after a disconnect */ - /* FIXME - does anything else need to be done here? */ -#ifdef WITH_SYS_TREE - db->disconnected_count--; -#endif + /* FIXME - does anything need to be done here? */ }else{ /* Client is already connected, disconnect old version. This is * done in mqtt3_context_cleanup() below. */ diff --git a/src/sys_tree.c b/src/sys_tree.c index 72ed5ed6..97848e35 100644 --- a/src/sys_tree.c +++ b/src/sys_tree.c @@ -48,8 +48,13 @@ static void _sys_update_clients(struct mosquitto_db *db, char *buf) static unsigned int disconnected_count = -1; static unsigned int connected_count = -1; - if(client_count != db->connected_count + db->disconnected_count){ - client_count = db->connected_count + db->disconnected_count; + unsigned int count_total, count_by_sock; + + count_total = HASH_CNT(hh_id, db->contexts_by_id); + count_by_sock = HASH_CNT(hh_sock, db->contexts_by_sock); + + if(client_count != count_total){ + client_count = count_total; snprintf(buf, BUFLEN, "%d", client_count); mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/total", 2, strlen(buf), buf, 1); @@ -60,14 +65,14 @@ static void _sys_update_clients(struct mosquitto_db *db, char *buf) } } - if(disconnected_count != db->disconnected_count){ - disconnected_count = db->disconnected_count; + if(disconnected_count != count_total-count_by_sock){ + disconnected_count = count_total-count_by_sock; snprintf(buf, BUFLEN, "%d", disconnected_count); mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/inactive", 2, strlen(buf), buf, 1); mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/disconnected", 2, strlen(buf), buf, 1); } - if(connected_count != db->connected_count){ - connected_count = db->connected_count; + if(connected_count != count_by_sock){ + connected_count = count_by_sock; snprintf(buf, BUFLEN, "%d", connected_count); mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/active", 2, strlen(buf), buf, 1); mqtt3_db_messages_easy_queue(db, NULL, "$SYS/broker/clients/connected", 2, strlen(buf), buf, 1);