Decouple hub and user manager more cleanly.

This commit is contained in:
Jan Vidar Krey 2012-09-27 10:43:53 +02:00
parent 4fcf3ffc83
commit 845aefc941
12 changed files with 153 additions and 170 deletions

View File

@ -19,7 +19,8 @@ EXO_TEST(setup, {
hub = hub_malloc_zero(sizeof(struct hub_info)); hub = hub_malloc_zero(sizeof(struct hub_info));
cbase = command_initialize(hub); cbase = command_initialize(hub);
hub->commands = cbase; hub->commands = cbase;
return cbase && hub && uman_init(hub) == 0; hub->users = uman_init();
return cbase && hub && hub->users;
}); });
static int test_handler(struct command_base* cbase, struct hub_user* user, struct hub_command* hcmd) static int test_handler(struct command_base* cbase, struct hub_user* user, struct hub_command* hcmd)
@ -127,7 +128,7 @@ EXO_TEST(command_number_3, { return verify("!test3 -12", cmd_status_ok); });
EXO_TEST(command_user_1, { return verify("!test4 tester", cmd_status_arg_nick); }); EXO_TEST(command_user_1, { return verify("!test4 tester", cmd_status_arg_nick); });
EXO_TEST(command_user_2, { return verify("!test5 3AGHMAASJA2RFNM22AA6753V7B7DYEPNTIWHBAY", cmd_status_arg_cid); }); EXO_TEST(command_user_2, { return verify("!test5 3AGHMAASJA2RFNM22AA6753V7B7DYEPNTIWHBAY", cmd_status_arg_cid); });
EXO_TEST(command_user_3, { return uman_add(hub, &user) == 0; }); EXO_TEST(command_user_3, { return uman_add(hub->users, &user) == 0; });
EXO_TEST(command_user_4, { return verify("!test4 tester", cmd_status_ok); }); EXO_TEST(command_user_4, { return verify("!test4 tester", cmd_status_ok); });
EXO_TEST(command_user_5, { return verify("!test5 3AGHMAASJA2RFNM22AA6753V7B7DYEPNTIWHBAY", cmd_status_ok); }); EXO_TEST(command_user_5, { return verify("!test5 3AGHMAASJA2RFNM22AA6753V7B7DYEPNTIWHBAY", cmd_status_ok); });
@ -225,7 +226,7 @@ EXO_TEST(command_argument_cred_6, {
#undef SETUP_COMMAND #undef SETUP_COMMAND
EXO_TEST(command_user_destroy, { return uman_remove(hub, &user) == 0; }); EXO_TEST(command_user_destroy, { return uman_remove(hub->users, &user) == 0; });
EXO_TEST(command_destroy, { EXO_TEST(command_destroy, {
@ -241,3 +242,10 @@ EXO_TEST(command_destroy, {
DEL_TEST(c_test7); DEL_TEST(c_test7);
return 1; return 1;
}); });
EXO_TEST(cleanup, {
uman_shutdown(hub->users);
command_shutdown(hub->commands);
hub_free(hub);
return 1;
});

View File

@ -2,32 +2,19 @@
#define MAX_USERS 64 #define MAX_USERS 64
static struct hub_info um_hub; static struct hub_user_manager* uman = 0;
static struct hub_user um_user[MAX_USERS]; static struct hub_user um_user[MAX_USERS];
// static struct net_connection um_cons[MAX_USERS];
EXO_TEST(um_test_setup, {
int i = 0;
memset(&um_hub, 0, sizeof(um_hub));
for (i = 0; i < MAX_USERS; i++)
{
/* memset(&um_cons[i], 0, sizeof(struct net_connection));
um_cons[i].sd = -1;
memset(&um_user[i], 0, sizeof(struct hub_user));
um_user[i].id.sid = i+1;
um_user[i].connection = &um_cons[i];*/
}
return 1;
});
EXO_TEST(um_init_1, { EXO_TEST(um_init_1, {
return uman_init(0) != 0; sid_t s;
}); uman = uman_init();
EXO_TEST(um_init_2, { for (s = 0; s < MAX_USERS; s++)
return uman_init(&um_hub) == 0; {
memset(&um_user[s], 0, sizeof(struct hub_user));
um_user[s].id.sid = s;
}
return !!uman;
}); });
EXO_TEST(um_shutdown_1, { EXO_TEST(um_shutdown_1, {
@ -35,32 +22,29 @@ EXO_TEST(um_shutdown_1, {
}); });
EXO_TEST(um_shutdown_2, { EXO_TEST(um_shutdown_2, {
return uman_shutdown(&um_hub) == 0; return uman_shutdown(uman) == 0;
}); });
EXO_TEST(um_shutdown_3, { EXO_TEST(um_init_2, {
return uman_shutdown(&um_hub) == -1; uman = uman_init();
}); return !!uman;
EXO_TEST(um_init_3, {
return uman_init(&um_hub) == 0;
}); });
EXO_TEST(um_add_1, { EXO_TEST(um_add_1, {
return uman_add(&um_hub, &um_user[0]) == 0; return uman_add(uman, &um_user[0]) == 0;
}); });
EXO_TEST(um_size_1, { EXO_TEST(um_size_1, {
return hub_get_user_count(&um_hub) == 1; return uman->count == 1;
}); });
EXO_TEST(um_remove_1, { EXO_TEST(um_remove_1, {
return uman_remove(&um_hub, &um_user[0]) == 0; return uman_remove(uman, &um_user[0]) == 0;
}); });
EXO_TEST(um_size_2, { EXO_TEST(um_size_2, {
return hub_get_user_count(&um_hub) == 0; return uman->count == 0;
}); });
@ -68,25 +52,21 @@ EXO_TEST(um_add_2, {
int i; int i;
for (i = 0; i < MAX_USERS; i++) for (i = 0; i < MAX_USERS; i++)
{ {
if (uman_add(&um_hub, &um_user[i]) != 0) if (uman_add(uman, &um_user[i]) != 0)
return 0; return 0;
} }
return 1; return 1;
}); });
EXO_TEST(um_size_3, { EXO_TEST(um_size_3, {
return hub_get_user_count(&um_hub) == MAX_USERS; return uman->count == MAX_USERS;
});
EXO_TEST(um_add_3, {
return uman_add(&um_hub, &um_user[5]) != 0;
}); });
EXO_TEST(um_remove_2, { EXO_TEST(um_remove_2, {
int i; int i;
for (i = 0; i < MAX_USERS; i++) for (i = 0; i < MAX_USERS; i++)
{ {
if (uman_remove(&um_hub, &um_user[i]) != 0) if (uman_remove(uman, &um_user[i]) != 0)
return 0; return 0;
} }
return 1; return 1;
@ -105,5 +85,5 @@ EXO_TEST(um_remove_2, {
/* Last test */ /* Last test */
EXO_TEST(um_shutdown_4, { EXO_TEST(um_shutdown_4, {
return uman_shutdown(&um_hub) == 0; return uman_shutdown(uman) == 0;
}); });

View File

@ -117,7 +117,7 @@ static enum command_parse_status command_extract_arguments(struct hub_info* hub,
case 'u': case 'u':
data = hub_malloc(sizeof(*data)); data = hub_malloc(sizeof(*data));
data->type = type_user; data->type = type_user;
data->data.user = uman_get_user_by_nick(hub, token); data->data.user = uman_get_user_by_nick(hub->users, token);
if (!data->data.user) if (!data->data.user)
{ {
hub_free(data); hub_free(data);
@ -129,7 +129,7 @@ static enum command_parse_status command_extract_arguments(struct hub_info* hub,
case 'i': case 'i':
data = hub_malloc(sizeof(*data)); data = hub_malloc(sizeof(*data));
data->type = type_user; data->type = type_user;
data->data.user = uman_get_user_by_cid(hub, token); data->data.user = uman_get_user_by_cid(hub->users, token);
if (!data->data.user) if (!data->data.user)
{ {
hub_free(data); hub_free(data);

View File

@ -421,7 +421,7 @@ static int command_whoip(struct command_base* cbase, struct hub_user* user, stru
struct hub_user* u; struct hub_user* u;
int ret = 0; int ret = 0;
ret = uman_get_user_by_addr(cbase->hub, users, arg->data.range); ret = uman_get_user_by_addr(cbase->hub->users, users, arg->data.range);
if (!ret) if (!ret)
{ {
list_clear(users, &null_free); list_clear(users, &null_free);

View File

@ -110,21 +110,21 @@ int hub_handle_message(struct hub_info* hub, struct hub_user* u, const char* lin
case ADC_CMD_DRES: case ADC_CMD_DRES:
cmd->priority = -1; cmd->priority = -1;
if (plugin_handle_search_result(hub, u, uman_get_user_by_sid(hub, cmd->target), cmd->cache) == st_deny) if (plugin_handle_search_result(hub, u, uman_get_user_by_sid(hub->users, cmd->target), cmd->cache) == st_deny)
break; break;
/* CHECK_FLOOD(search, 0); */ /* CHECK_FLOOD(search, 0); */
ROUTE_MSG; ROUTE_MSG;
case ADC_CMD_DRCM: case ADC_CMD_DRCM:
cmd->priority = -1; cmd->priority = -1;
if (plugin_handle_revconnect(hub, u, uman_get_user_by_sid(hub, cmd->target)) == st_deny) if (plugin_handle_revconnect(hub, u, uman_get_user_by_sid(hub->users, cmd->target)) == st_deny)
break; break;
CHECK_FLOOD(connect, 1); CHECK_FLOOD(connect, 1);
ROUTE_MSG; ROUTE_MSG;
case ADC_CMD_DCTM: case ADC_CMD_DCTM:
cmd->priority = -1; cmd->priority = -1;
if (plugin_handle_connect(hub, u, uman_get_user_by_sid(hub, cmd->target)) == st_deny) if (plugin_handle_connect(hub, u, uman_get_user_by_sid(hub->users, cmd->target)) == st_deny)
break; break;
CHECK_FLOOD(connect, 1); CHECK_FLOOD(connect, 1);
ROUTE_MSG; ROUTE_MSG;
@ -340,7 +340,7 @@ int hub_handle_chat_message(struct hub_info* hub, struct hub_user* u, struct adc
} }
else if (private_msg) else if (private_msg)
{ {
struct hub_user* target = uman_get_user_by_sid(hub, cmd->target); struct hub_user* target = uman_get_user_by_sid(hub->users, cmd->target);
if (target) if (target)
status = plugin_handle_private_message(hub, u, target, message_decoded, 0); status = plugin_handle_private_message(hub, u, target, message_decoded, 0);
else else
@ -381,7 +381,7 @@ void hub_send_sid(struct hub_info* hub, struct hub_user* u)
if (user_is_connecting(u)) if (user_is_connecting(u))
{ {
command = adc_msg_construct(ADC_CMD_ISID, 10); command = adc_msg_construct(ADC_CMD_ISID, 10);
sid = uman_get_free_sid(hub, u); sid = uman_get_free_sid(hub->users, u);
adc_msg_add_argument(command, (const char*) sid_to_string(sid)); adc_msg_add_argument(command, (const char*) sid_to_string(sid));
route_to_user(hub, u, command); route_to_user(hub, u, command);
adc_msg_free(command); adc_msg_free(command);
@ -520,11 +520,11 @@ static int check_duplicate_logins_ok(struct hub_info* hub, struct hub_user* user
struct hub_user* lookup1; struct hub_user* lookup1;
struct hub_user* lookup2; struct hub_user* lookup2;
lookup1 = uman_get_user_by_nick(hub, user->id.nick); lookup1 = uman_get_user_by_nick(hub->users, user->id.nick);
if (lookup1) if (lookup1)
return status_msg_inf_error_nick_taken; return status_msg_inf_error_nick_taken;
lookup2 = uman_get_user_by_cid(hub, user->id.cid); lookup2 = uman_get_user_by_cid(hub->users, user->id.cid);
if (lookup2) if (lookup2)
return status_msg_inf_error_cid_taken; return status_msg_inf_error_cid_taken;
@ -568,8 +568,8 @@ static void hub_event_dispatcher(void* callback_data, struct event_data* message
case UHUB_EVENT_USER_QUIT: case UHUB_EVENT_USER_QUIT:
{ {
uman_remove(hub, user); uman_remove(hub->users, user);
uman_send_quit_message(hub, user); uman_send_quit_message(hub, hub->users, user);
on_logout_user(hub, user); on_logout_user(hub, user);
hub_schedule_destroy_user(hub, user); hub_schedule_destroy_user(hub, user);
break; break;
@ -586,7 +586,7 @@ static void hub_event_dispatcher(void* callback_data, struct event_data* message
struct hub_user* u = (struct hub_user*) list_get_first(hub->users->list); struct hub_user* u = (struct hub_user*) list_get_first(hub->users->list);
while (u) while (u)
{ {
uman_remove(hub, u); uman_remove(hub->users, u);
user_destroy(u); user_destroy(u);
u = (struct hub_user*) list_get_first(hub->users->list); u = (struct hub_user*) list_get_first(hub->users->list);
} }
@ -781,7 +781,8 @@ struct hub_info* hub_start_service(struct hub_config* config)
hub->config = config; hub->config = config;
hub->users = NULL; hub->users = NULL;
if (uman_init(hub) == -1) hub->users = uman_init();
if (!hub->users)
{ {
net_con_close(hub->server); net_con_close(hub->server);
hub_free(hub); hub_free(hub);
@ -791,7 +792,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
if (event_queue_initialize(&hub->queue, hub_event_dispatcher, (void*) hub) == -1) if (event_queue_initialize(&hub->queue, hub_event_dispatcher, (void*) hub) == -1)
{ {
net_con_close(hub->server); net_con_close(hub->server);
uman_shutdown(hub); uman_shutdown(hub->users);
hub_free(hub); hub_free(hub);
return 0; return 0;
} }
@ -803,7 +804,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
net_con_close(hub->server); net_con_close(hub->server);
hub_free(hub->recvbuf); hub_free(hub->recvbuf);
hub_free(hub->sendbuf); hub_free(hub->sendbuf);
uman_shutdown(hub); uman_shutdown(hub->users);
hub_free(hub); hub_free(hub);
return 0; return 0;
} }
@ -832,7 +833,7 @@ void hub_shutdown_service(struct hub_info* hub)
event_queue_shutdown(hub->queue); event_queue_shutdown(hub->queue);
net_con_close(hub->server); net_con_close(hub->server);
server_alt_port_stop(hub); server_alt_port_stop(hub);
uman_shutdown(hub); uman_shutdown(hub->users);
hub->status = hub_status_stopped; hub->status = hub_status_stopped;
hub_free(hub->sendbuf); hub_free(hub->sendbuf);
hub_free(hub->recvbuf); hub_free(hub->recvbuf);

View File

@ -24,12 +24,12 @@
void on_login_success(struct hub_info* hub, struct hub_user* u) void on_login_success(struct hub_info* hub, struct hub_user* u)
{ {
/* Send user list of all existing users */ /* Send user list of all existing users */
if (!uman_send_user_list(hub, u)) if (!uman_send_user_list(hub, hub->users, u))
return; return;
/* Mark as being in the normal state, and add user to the user list */ /* Mark as being in the normal state, and add user to the user list */
user_set_state(u, state_normal); user_set_state(u, state_normal);
uman_add(hub, u); uman_add(hub->users, u);
/* Announce new user to all connected users */ /* Announce new user to all connected users */
if (user_is_logged_in(u)) if (user_is_logged_in(u))

View File

@ -325,8 +325,8 @@ static int check_nick(struct hub_info* hub, struct hub_user* user, struct adc_me
static int check_logged_in(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd) static int check_logged_in(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd)
{ {
struct hub_user* lookup1 = uman_get_user_by_nick(hub, user->id.nick); struct hub_user* lookup1 = uman_get_user_by_nick(hub->users, user->id.nick);
struct hub_user* lookup2 = uman_get_user_by_cid(hub, user->id.cid); struct hub_user* lookup2 = uman_get_user_by_cid(hub->users, user->id.cid);
if (lookup1 == user) if (lookup1 == user)
{ {

View File

@ -21,9 +21,6 @@
#include "hubio.h" #include "hubio.h"
#include "probe.h" #include "probe.h"
/* FIXME: This should not be needed! */
extern struct hub_info* g_hub;
int handle_net_read(struct hub_user* user) int handle_net_read(struct hub_user* user)
{ {
static char buf[MAX_RECV_BUF]; static char buf[MAX_RECV_BUF];
@ -71,9 +68,9 @@ int handle_net_read(struct hub_user* user)
} }
else else
{ {
if (((pos - start) > 0) && g_hub->config->max_recv_buffer > (pos - start)) if (((pos - start) > 0) && user->hub->config->max_recv_buffer > (pos - start))
{ {
if (hub_handle_message(g_hub, user, start, (pos - start)) == -1) if (hub_handle_message(user->hub, user, start, (pos - start)) == -1)
{ {
return quit_protocol_error; return quit_protocol_error;
} }
@ -88,7 +85,7 @@ int handle_net_read(struct hub_user* user)
if (lastPos || remaining) if (lastPos || remaining)
{ {
if (remaining < (size_t) g_hub->config->max_recv_buffer) if (remaining < (size_t) user->hub->config->max_recv_buffer)
{ {
hub_recvq_set(q, lastPos ? lastPos : buf, remaining); hub_recvq_set(q, lastPos ? lastPos : buf, remaining);
} }
@ -144,7 +141,7 @@ void net_event(struct net_connection* con, int event, void *arg)
{ {
if (user_is_connecting(user)) if (user_is_connecting(user))
{ {
hub_disconnect_user(g_hub, user, quit_timeout); hub_disconnect_user(user->hub, user, quit_timeout);
} }
return; return;
} }
@ -154,7 +151,7 @@ void net_event(struct net_connection* con, int event, void *arg)
flag_close = handle_net_read(user); flag_close = handle_net_read(user);
if (flag_close) if (flag_close)
{ {
hub_disconnect_user(g_hub, user, flag_close); hub_disconnect_user(user->hub, user, flag_close);
return; return;
} }
} }
@ -164,7 +161,7 @@ void net_event(struct net_connection* con, int event, void *arg)
flag_close = handle_net_write(user); flag_close = handle_net_write(user);
if (flag_close) if (flag_close)
{ {
hub_disconnect_user(g_hub, user, flag_close); hub_disconnect_user(user->hub, user, flag_close);
return; return;
} }
} }

View File

@ -30,7 +30,7 @@ int route_message(struct hub_info* hub, struct hub_user* u, struct adc_message*
break; break;
case 'D': case 'D':
target = uman_get_user_by_sid(hub, msg->target); target = uman_get_user_by_sid(hub->users, msg->target);
if (target) if (target)
{ {
route_to_user(hub, target, msg); route_to_user(hub, target, msg);
@ -38,7 +38,7 @@ int route_message(struct hub_info* hub, struct hub_user* u, struct adc_message*
break; break;
case 'E': case 'E':
target = uman_get_user_by_sid(hub, msg->target); target = uman_get_user_by_sid(hub->users, msg->target);
if (target) if (target)
{ {
route_to_user(hub, target, msg); route_to_user(hub, target, msg);

View File

@ -60,6 +60,8 @@ struct hub_user* user_create(struct hub_info* hub, struct net_connection* con, s
flood_control_reset(&user->flood_search); flood_control_reset(&user->flood_search);
flood_control_reset(&user->flood_update); flood_control_reset(&user->flood_update);
flood_control_reset(&user->flood_extras); flood_control_reset(&user->flood_extras);
user->hub = hub;
return user; return user;
} }

View File

@ -38,50 +38,48 @@ static void clear_user_list_callback(void* ptr)
} }
} }
void uman_update_stats(struct hub_info* hub) #ifdef STATS_SUPPORT
void uman_update_stats(struct hub_user_manager* users)
{ {
const int factor = TIMEOUT_STATS; const int factor = TIMEOUT_STATS;
struct net_statistics* total; struct net_statistics* total;
struct net_statistics* intermediate; struct net_statistics* intermediate;
net_stats_get(&intermediate, &total); net_stats_get(&intermediate, &total);
hub->stats.net_tx = (intermediate->tx / factor); users->stats.net_tx = (intermediate->tx / factor);
hub->stats.net_rx = (intermediate->rx / factor); users->stats.net_rx = (intermediate->rx / factor);
hub->stats.net_tx_peak = MAX(hub->stats.net_tx, hub->stats.net_tx_peak); users->stats.net_tx_peak = MAX(users->stats.net_tx, users->stats.net_tx_peak);
hub->stats.net_rx_peak = MAX(hub->stats.net_rx, hub->stats.net_rx_peak); users->stats.net_rx_peak = MAX(users->stats.net_rx, users->stats.net_rx_peak);
hub->stats.net_tx_total = total->tx; users->stats.net_tx_total = total->tx;
hub->stats.net_rx_total = total->rx; users->stats.net_rx_total = total->rx;
net_stats_reset(); net_stats_reset();
} }
void uman_print_stats(struct hub_info* hub) void uman_print_stats(struct hub_user_manager* users)
{ {
LOG_INFO("Statistics users=" PRINTF_SIZE_T " (peak_users=" PRINTF_SIZE_T "), net_tx=%d KB/s, net_rx=%d KB/s (peak_tx=%d KB/s, peak_rx=%d KB/s)", LOG_INFO("Statistics users=" PRINTF_SIZE_T " (peak_users=" PRINTF_SIZE_T "), net_tx=%d KB/s, net_rx=%d KB/s (peak_tx=%d KB/s, peak_rx=%d KB/s)",
hub->users->count, users->users->count,
hub->users->count_peak, users->users->count_peak,
(int) hub->stats.net_tx / 1024, (int) users->stats.net_tx / 1024,
(int) hub->stats.net_rx / 1024, (int) users->stats.net_rx / 1024,
(int) hub->stats.net_tx_peak / 1024, (int) users->stats.net_tx_peak / 1024,
(int) hub->stats.net_rx_peak / 1024); (int) users->stats.net_rx_peak / 1024);
} }
static void timer_statistics(struct timeout_evt* t) static void timer_statistics(struct timeout_evt* t)
{ {
struct hub_info* hub = (struct hub_info*) t->ptr; struct hub_user_manager* users = (struct hub_user_manager*) t->ptr;
uman_update_stats(hub); uman_update_stats(users);
timeout_queue_reschedule(net_backend_get_timeout_queue(), hub->users->timeout, TIMEOUT_STATS); timeout_queue_reschedule(net_backend_get_timeout_queue(), users->timeout, TIMEOUT_STATS);
} }
#endif // STATS_SUPPORT
int uman_init(struct hub_info* hub) struct hub_user_manager* uman_init()
{ {
struct hub_user_manager* users = NULL; struct hub_user_manager* users = (struct hub_user_manager*) hub_malloc_zero(sizeof(struct hub_user_manager));
if (!hub)
return -1;
users = (struct hub_user_manager*) hub_malloc_zero(sizeof(struct hub_user_manager));
if (!users) if (!users)
return -1; return NULL;
users->list = list_create(); users->list = list_create();
users->sids = sid_pool_create(net_get_max_sockets()); users->sids = sid_pool_create(net_get_max_sockets());
@ -90,143 +88,136 @@ int uman_init(struct hub_info* hub)
{ {
list_destroy(users->list); list_destroy(users->list);
hub_free(users); hub_free(users);
return -1; return NULL;
} }
#ifdef STATS_SUPPORT
if (net_backend_get_timeout_queue()) if (net_backend_get_timeout_queue())
{ {
users->timeout = hub_malloc_zero(sizeof(struct timeout_evt)); users->timeout = hub_malloc_zero(sizeof(struct timeout_evt));
timeout_evt_initialize(users->timeout, timer_statistics, hub); timeout_evt_initialize(users->timeout, timer_statistics, hub);
timeout_queue_insert(net_backend_get_timeout_queue(), users->timeout, TIMEOUT_STATS); timeout_queue_insert(net_backend_get_timeout_queue(), users->timeout, TIMEOUT_STATS);
} }
#endif // STATS_SUPPORT
hub->users = users; return users;
return 0;
} }
int uman_shutdown(struct hub_info* hub) int uman_shutdown(struct hub_user_manager* users)
{ {
if (!hub || !hub->users) if (!users)
return -1; return -1;
#ifdef STATS_SUPPORT
if (net_backend_get_timeout_queue()) if (net_backend_get_timeout_queue())
{ {
timeout_queue_remove(net_backend_get_timeout_queue(), hub->users->timeout); timeout_queue_remove(net_backend_get_timeout_queue(), users->timeout);
hub_free(hub->users->timeout); hub_free(users->timeout);
} }
#endif
if (hub->users->list) if (users->list)
{ {
list_clear(hub->users->list, &clear_user_list_callback); list_clear(users->list, &clear_user_list_callback);
list_destroy(hub->users->list); list_destroy(users->list);
} }
sid_pool_destroy(hub->users->sids); sid_pool_destroy(users->sids);
hub_free(hub->users);
hub->users = 0;
hub_free(users);
return 0; return 0;
} }
int uman_add(struct hub_info* hub, struct hub_user* user) int uman_add(struct hub_user_manager* users, struct hub_user* user)
{ {
if (!hub || !user) if (!users || !user)
return -1; return -1;
if (user->hub) list_append(users->list, user);
return -1; users->count++;
users->count_peak = MAX(users->count, users->count_peak);
list_append(hub->users->list, user); users->shared_size += user->limits.shared_size;
hub->users->count++; users->shared_files += user->limits.shared_files;
hub->users->count_peak = MAX(hub->users->count, hub->users->count_peak);
hub->users->shared_size += user->limits.shared_size;
hub->users->shared_files += user->limits.shared_files;
user->hub = hub;
return 0; return 0;
} }
int uman_remove(struct hub_info* hub, struct hub_user* user) int uman_remove(struct hub_user_manager* users, struct hub_user* user)
{ {
if (!hub || !user) if (!users || !user)
return -1; return -1;
list_remove(hub->users->list, user); list_remove(users->list, user);
if (hub->users->count > 0) if (users->count > 0)
{ {
hub->users->count--; users->count--;
} }
else else
{ {
uhub_assert(!"negative count!"); uhub_assert(!"negative count!");
} }
hub->users->shared_size -= user->limits.shared_size; users->shared_size -= user->limits.shared_size;
hub->users->shared_files -= user->limits.shared_files; users->shared_files -= user->limits.shared_files;
user->hub = 0;
return 0; return 0;
} }
struct hub_user* uman_get_user_by_sid(struct hub_info* hub, sid_t sid) struct hub_user* uman_get_user_by_sid(struct hub_user_manager* users, sid_t sid)
{ {
return sid_lookup(hub->users->sids, sid); return sid_lookup(users->sids, sid);
} }
struct hub_user* uman_get_user_by_cid(struct hub_info* hub, const char* cid) struct hub_user* uman_get_user_by_cid(struct hub_user_manager* users, const char* cid)
{ {
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list); /* iterate users - only on incoming INF msg */ struct hub_user* user = (struct hub_user*) list_get_first(users->list); /* iterate users - only on incoming INF msg */
while (user) while (user)
{ {
if (strcmp(user->id.cid, cid) == 0) if (strcmp(user->id.cid, cid) == 0)
return user; return user;
user = (struct hub_user*) list_get_next(hub->users->list); user = (struct hub_user*) list_get_next(users->list);
} }
return NULL; return NULL;
} }
struct hub_user* uman_get_user_by_nick(struct hub_info* hub, const char* nick) struct hub_user* uman_get_user_by_nick(struct hub_user_manager* users, const char* nick)
{ {
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list); /* iterate users - only on incoming INF msg */ struct hub_user* user = (struct hub_user*) list_get_first(users->list); /* iterate users - only on incoming INF msg */
while (user) while (user)
{ {
if (strcmp(user->id.nick, nick) == 0) if (strcmp(user->id.nick, nick) == 0)
return user; return user;
user = (struct hub_user*) list_get_next(hub->users->list); user = (struct hub_user*) list_get_next(users->list);
} }
return NULL; return NULL;
} }
size_t uman_get_user_by_addr(struct hub_info* hub, struct linked_list* users, struct ip_range* range) size_t uman_get_user_by_addr(struct hub_user_manager* users, struct linked_list* target, struct ip_range* range)
{ {
size_t num = 0; size_t num = 0;
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list); /* iterate users - only on incoming INF msg */ struct hub_user* user = (struct hub_user*) list_get_first(users->list); /* iterate users - only on incoming INF msg */
while (user) while (user)
{ {
if (ip_in_range(&user->id.addr, range)) if (ip_in_range(&user->id.addr, range))
{ {
list_append(users, user); list_append(target, user);
num++; num++;
} }
user = (struct hub_user*) list_get_next(hub->users->list); user = (struct hub_user*) list_get_next(users->list);
} }
return num; return num;
} }
int uman_send_user_list(struct hub_info* hub, struct hub_user* target) int uman_send_user_list(struct hub_info* hub, struct hub_user_manager* users, struct hub_user* target)
{ {
int ret = 1; int ret = 1;
struct hub_user* user; struct hub_user* user;
user_flag_set(target, flag_user_list); user_flag_set(target, flag_user_list);
user = (struct hub_user*) list_get_first(hub->users->list); /* iterate users - only on INF or PAS msg */ user = (struct hub_user*) list_get_first(users->list); /* iterate users - only on INF or PAS msg */
while (user) while (user)
{ {
if (user_is_logged_in(user)) if (user_is_logged_in(user))
@ -235,7 +226,7 @@ int uman_send_user_list(struct hub_info* hub, struct hub_user* target)
if (!ret) if (!ret)
break; break;
} }
user = (struct hub_user*) list_get_next(hub->users->list); user = (struct hub_user*) list_get_next(users->list);
} }
#if 0 #if 0
@ -248,7 +239,7 @@ int uman_send_user_list(struct hub_info* hub, struct hub_user* target)
return ret; return ret;
} }
void uman_send_quit_message(struct hub_info* hub, struct hub_user* leaving) void uman_send_quit_message(struct hub_info* hub, struct hub_user_manager* users, struct hub_user* leaving)
{ {
struct adc_message* command = adc_msg_construct(ADC_CMD_IQUI, 6); struct adc_message* command = adc_msg_construct(ADC_CMD_IQUI, 6);
adc_msg_add_argument(command, (const char*) sid_to_string(leaving->id.sid)); adc_msg_add_argument(command, (const char*) sid_to_string(leaving->id.sid));
@ -261,9 +252,9 @@ void uman_send_quit_message(struct hub_info* hub, struct hub_user* leaving)
adc_msg_free(command); adc_msg_free(command);
} }
sid_t uman_get_free_sid(struct hub_info* hub, struct hub_user* user) sid_t uman_get_free_sid(struct hub_user_manager* users, struct hub_user* user)
{ {
sid_t sid = sid_alloc(hub->users->sids, user); sid_t sid = sid_alloc(users->sids, user);
user->id.sid = sid; user->id.sid = sid;
return sid; return sid;
} }

View File

@ -28,36 +28,38 @@ struct hub_user_manager
uint64_t shared_size; /**<< "The total number of shared bytes among fully connected users." */ uint64_t shared_size; /**<< "The total number of shared bytes among fully connected users." */
uint64_t shared_files; /**<< "The total number of shared files among fully connected users." */ uint64_t shared_files; /**<< "The total number of shared files among fully connected users." */
struct linked_list* list; /**<< "Contains all logged in users" */ struct linked_list* list; /**<< "Contains all logged in users" */
#ifdef STATS_SUPPORT
struct timeout_evt* timeout; /**<< "Timeout handler for statistics" */ struct timeout_evt* timeout; /**<< "Timeout handler for statistics" */
#endif
}; };
/** /**
* Initializes the user manager. * Initializes the user manager.
* @return 0 on success, or -1 if error (out of memory). * @return 0 on success, or -1 if error (out of memory).
*/ */
extern int uman_init(struct hub_info* hub); extern struct hub_user_manager* uman_init();
/** /**
* Shuts down the user manager. * Shuts down the user manager.
* All users will be disconnected and deleted as part of this. * All users will be disconnected and deleted as part of this.
* *
* @return 0 on success, or -1 in an error occured (hub is invalid). * @return 0 on success, or -1 in an error occured (invalid pointer).
*/ */
extern int uman_shutdown(struct hub_info* hub); extern int uman_shutdown(struct hub_user_manager* users);
/** /**
* Generate statistics for logfiles. * Generate statistics for logfiles.
*/ */
extern void uman_update_stats(struct hub_info* hub); extern void uman_update_stats(struct hub_user_manager* users);
extern void uman_print_stats(struct hub_info* hub); extern void uman_print_stats(struct hub_user_manager* users);
/** /**
* Add a user to the user manager. * Add a user to the user manager.
* *
* @param hub The hub to add the user to * @param users The usermanager to add the user to
* @param user The user to be added to the hub. * @param user The user to be added to the hub.
*/ */
extern int uman_add(struct hub_info* hub, struct hub_user* user); extern int uman_add(struct hub_user_manager* users, struct hub_user* user);
/** /**
* Remove a user from the user manager. * Remove a user from the user manager.
@ -66,12 +68,12 @@ extern int uman_add(struct hub_info* hub, struct hub_user* user);
* *
* @return 0 if successfully removed, -1 if error. * @return 0 if successfully removed, -1 if error.
*/ */
extern int uman_remove(struct hub_info* hub, struct hub_user* user); extern int uman_remove(struct hub_user_manager* users, struct hub_user* user);
/** /**
* Returns and allocates an unused session ID (SID). * Returns and allocates an unused session ID (SID).
*/ */
extern sid_t uman_get_free_sid(struct hub_info* hub, struct hub_user* user); extern sid_t uman_get_free_sid(struct hub_user_manager* users, struct hub_user* user);
/** /**
* Lookup a user based on the session ID (SID). * Lookup a user based on the session ID (SID).
@ -86,26 +88,28 @@ extern sid_t uman_get_free_sid(struct hub_info* hub, struct hub_user* user);
* *
* @return a user if found, or NULL if not found * @return a user if found, or NULL if not found
*/ */
extern struct hub_user* uman_get_user_by_sid(struct hub_info* hub, sid_t sid); extern struct hub_user* uman_get_user_by_sid(struct hub_user_manager* users, sid_t sid);
/** /**
* Lookup a user based on the client ID (CID). * Lookup a user based on the client ID (CID).
* @return a user if found, or NULL if not found * @return a user if found, or NULL if not found
*/ */
extern struct hub_user* uman_get_user_by_cid(struct hub_info* hub, const char* cid); extern struct hub_user* uman_get_user_by_cid(struct hub_user_manager* users, const char* cid);
/** /**
* Lookup a user based on the nick name. * Lookup a user based on the nick name.
* @return a user if found, or NULL if not found * @return a user if found, or NULL if not found
*/ */
extern struct hub_user* uman_get_user_by_nick(struct hub_info* hub, const char* nick); extern struct hub_user* uman_get_user_by_nick(struct hub_user_manager* users, const char* nick);
/** /**
* Lookup users based on an ip address range. * Lookup users based on an ip address range.
* *
* @param[out] target the list of users matching the address
* @param range the IP range of users to match
* @return The number of users matching the addressess, or -1 on error (mask is wrong). * @return The number of users matching the addressess, or -1 on error (mask is wrong).
*/ */
extern size_t uman_get_user_by_addr(struct hub_info* hub, struct linked_list* users, struct ip_range* range); extern size_t uman_get_user_by_addr(struct hub_user_manager* users, struct linked_list* target, struct ip_range* range);
/** /**
* Send the user list of connected clients to 'user'. * Send the user list of connected clients to 'user'.
@ -113,13 +117,13 @@ extern size_t uman_get_user_by_addr(struct hub_info* hub, struct linked_list* us
* *
* @return 1 if sending the user list succeeded, 0 otherwise. * @return 1 if sending the user list succeeded, 0 otherwise.
*/ */
extern int uman_send_user_list(struct hub_info* hub, struct hub_user* user); extern int uman_send_user_list(struct hub_info* hub, struct hub_user_manager* users, struct hub_user* user);
/** /**
* Send a quit message to all connected users when 'user' is * Send a quit message to all connected users when 'user' is
* leaving the hub (for whatever reason). * leaving the hub (for whatever reason).
*/ */
extern void uman_send_quit_message(struct hub_info* hub, struct hub_user* user); extern void uman_send_quit_message(struct hub_info* hub, struct hub_user_manager* users, struct hub_user* user);
#endif /* HAVE_UHUB_USER_MANAGER_H */ #endif /* HAVE_UHUB_USER_MANAGER_H */