Decouple hub and user manager more cleanly.
This commit is contained in:
parent
4fcf3ffc83
commit
845aefc941
@ -19,7 +19,8 @@ EXO_TEST(setup, {
|
||||
hub = hub_malloc_zero(sizeof(struct hub_info));
|
||||
cbase = command_initialize(hub);
|
||||
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)
|
||||
@ -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_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_5, { return verify("!test5 3AGHMAASJA2RFNM22AA6753V7B7DYEPNTIWHBAY", cmd_status_ok); });
|
||||
|
||||
@ -225,7 +226,7 @@ EXO_TEST(command_argument_cred_6, {
|
||||
|
||||
#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, {
|
||||
|
||||
@ -241,3 +242,10 @@ EXO_TEST(command_destroy, {
|
||||
DEL_TEST(c_test7);
|
||||
return 1;
|
||||
});
|
||||
|
||||
EXO_TEST(cleanup, {
|
||||
uman_shutdown(hub->users);
|
||||
command_shutdown(hub->commands);
|
||||
hub_free(hub);
|
||||
return 1;
|
||||
});
|
||||
|
@ -2,32 +2,19 @@
|
||||
|
||||
#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 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, {
|
||||
return uman_init(0) != 0;
|
||||
});
|
||||
|
||||
EXO_TEST(um_init_2, {
|
||||
return uman_init(&um_hub) == 0;
|
||||
sid_t s;
|
||||
uman = uman_init();
|
||||
|
||||
for (s = 0; s < MAX_USERS; s++)
|
||||
{
|
||||
memset(&um_user[s], 0, sizeof(struct hub_user));
|
||||
um_user[s].id.sid = s;
|
||||
}
|
||||
return !!uman;
|
||||
});
|
||||
|
||||
EXO_TEST(um_shutdown_1, {
|
||||
@ -35,32 +22,29 @@ EXO_TEST(um_shutdown_1, {
|
||||
});
|
||||
|
||||
EXO_TEST(um_shutdown_2, {
|
||||
return uman_shutdown(&um_hub) == 0;
|
||||
return uman_shutdown(uman) == 0;
|
||||
});
|
||||
|
||||
EXO_TEST(um_shutdown_3, {
|
||||
return uman_shutdown(&um_hub) == -1;
|
||||
});
|
||||
|
||||
EXO_TEST(um_init_3, {
|
||||
return uman_init(&um_hub) == 0;
|
||||
EXO_TEST(um_init_2, {
|
||||
uman = uman_init();
|
||||
return !!uman;
|
||||
});
|
||||
|
||||
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, {
|
||||
return hub_get_user_count(&um_hub) == 1;
|
||||
return uman->count == 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, {
|
||||
return hub_get_user_count(&um_hub) == 0;
|
||||
return uman->count == 0;
|
||||
});
|
||||
|
||||
|
||||
@ -68,25 +52,21 @@ EXO_TEST(um_add_2, {
|
||||
int 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 1;
|
||||
});
|
||||
|
||||
EXO_TEST(um_size_3, {
|
||||
return hub_get_user_count(&um_hub) == MAX_USERS;
|
||||
});
|
||||
|
||||
EXO_TEST(um_add_3, {
|
||||
return uman_add(&um_hub, &um_user[5]) != 0;
|
||||
return uman->count == MAX_USERS;
|
||||
});
|
||||
|
||||
EXO_TEST(um_remove_2, {
|
||||
int 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 1;
|
||||
@ -105,5 +85,5 @@ EXO_TEST(um_remove_2, {
|
||||
|
||||
/* Last test */
|
||||
EXO_TEST(um_shutdown_4, {
|
||||
return uman_shutdown(&um_hub) == 0;
|
||||
return uman_shutdown(uman) == 0;
|
||||
});
|
||||
|
@ -117,7 +117,7 @@ static enum command_parse_status command_extract_arguments(struct hub_info* hub,
|
||||
case 'u':
|
||||
data = hub_malloc(sizeof(*data));
|
||||
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)
|
||||
{
|
||||
hub_free(data);
|
||||
@ -129,7 +129,7 @@ static enum command_parse_status command_extract_arguments(struct hub_info* hub,
|
||||
case 'i':
|
||||
data = hub_malloc(sizeof(*data));
|
||||
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)
|
||||
{
|
||||
hub_free(data);
|
||||
|
@ -421,7 +421,7 @@ static int command_whoip(struct command_base* cbase, struct hub_user* user, stru
|
||||
struct hub_user* u;
|
||||
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)
|
||||
{
|
||||
list_clear(users, &null_free);
|
||||
|
@ -110,21 +110,21 @@ int hub_handle_message(struct hub_info* hub, struct hub_user* u, const char* lin
|
||||
|
||||
case ADC_CMD_DRES:
|
||||
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;
|
||||
/* CHECK_FLOOD(search, 0); */
|
||||
ROUTE_MSG;
|
||||
|
||||
case ADC_CMD_DRCM:
|
||||
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;
|
||||
CHECK_FLOOD(connect, 1);
|
||||
ROUTE_MSG;
|
||||
|
||||
case ADC_CMD_DCTM:
|
||||
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;
|
||||
CHECK_FLOOD(connect, 1);
|
||||
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)
|
||||
{
|
||||
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)
|
||||
status = plugin_handle_private_message(hub, u, target, message_decoded, 0);
|
||||
else
|
||||
@ -381,7 +381,7 @@ void hub_send_sid(struct hub_info* hub, struct hub_user* u)
|
||||
if (user_is_connecting(u))
|
||||
{
|
||||
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));
|
||||
route_to_user(hub, u, 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* lookup2;
|
||||
|
||||
lookup1 = uman_get_user_by_nick(hub, user->id.nick);
|
||||
lookup1 = uman_get_user_by_nick(hub->users, user->id.nick);
|
||||
if (lookup1)
|
||||
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)
|
||||
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:
|
||||
{
|
||||
uman_remove(hub, user);
|
||||
uman_send_quit_message(hub, user);
|
||||
uman_remove(hub->users, user);
|
||||
uman_send_quit_message(hub, hub->users, user);
|
||||
on_logout_user(hub, user);
|
||||
hub_schedule_destroy_user(hub, user);
|
||||
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);
|
||||
while (u)
|
||||
{
|
||||
uman_remove(hub, u);
|
||||
uman_remove(hub->users, u);
|
||||
user_destroy(u);
|
||||
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->users = NULL;
|
||||
|
||||
if (uman_init(hub) == -1)
|
||||
hub->users = uman_init();
|
||||
if (!hub->users)
|
||||
{
|
||||
net_con_close(hub->server);
|
||||
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)
|
||||
{
|
||||
net_con_close(hub->server);
|
||||
uman_shutdown(hub);
|
||||
uman_shutdown(hub->users);
|
||||
hub_free(hub);
|
||||
return 0;
|
||||
}
|
||||
@ -803,7 +804,7 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
net_con_close(hub->server);
|
||||
hub_free(hub->recvbuf);
|
||||
hub_free(hub->sendbuf);
|
||||
uman_shutdown(hub);
|
||||
uman_shutdown(hub->users);
|
||||
hub_free(hub);
|
||||
return 0;
|
||||
}
|
||||
@ -832,7 +833,7 @@ void hub_shutdown_service(struct hub_info* hub)
|
||||
event_queue_shutdown(hub->queue);
|
||||
net_con_close(hub->server);
|
||||
server_alt_port_stop(hub);
|
||||
uman_shutdown(hub);
|
||||
uman_shutdown(hub->users);
|
||||
hub->status = hub_status_stopped;
|
||||
hub_free(hub->sendbuf);
|
||||
hub_free(hub->recvbuf);
|
||||
|
@ -24,12 +24,12 @@
|
||||
void on_login_success(struct hub_info* hub, struct hub_user* u)
|
||||
{
|
||||
/* Send user list of all existing users */
|
||||
if (!uman_send_user_list(hub, u))
|
||||
if (!uman_send_user_list(hub, hub->users, u))
|
||||
return;
|
||||
|
||||
/* Mark as being in the normal state, and add user to the user list */
|
||||
user_set_state(u, state_normal);
|
||||
uman_add(hub, u);
|
||||
uman_add(hub->users, u);
|
||||
|
||||
/* Announce new user to all connected users */
|
||||
if (user_is_logged_in(u))
|
||||
|
@ -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)
|
||||
{
|
||||
struct hub_user* lookup1 = uman_get_user_by_nick(hub, user->id.nick);
|
||||
struct hub_user* lookup2 = uman_get_user_by_cid(hub, user->id.cid);
|
||||
struct hub_user* lookup1 = uman_get_user_by_nick(hub->users, user->id.nick);
|
||||
struct hub_user* lookup2 = uman_get_user_by_cid(hub->users, user->id.cid);
|
||||
|
||||
if (lookup1 == user)
|
||||
{
|
||||
|
@ -21,9 +21,6 @@
|
||||
#include "hubio.h"
|
||||
#include "probe.h"
|
||||
|
||||
/* FIXME: This should not be needed! */
|
||||
extern struct hub_info* g_hub;
|
||||
|
||||
int handle_net_read(struct hub_user* user)
|
||||
{
|
||||
static char buf[MAX_RECV_BUF];
|
||||
@ -71,9 +68,9 @@ int handle_net_read(struct hub_user* user)
|
||||
}
|
||||
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;
|
||||
}
|
||||
@ -88,7 +85,7 @@ int handle_net_read(struct hub_user* user)
|
||||
|
||||
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);
|
||||
}
|
||||
@ -144,7 +141,7 @@ void net_event(struct net_connection* con, int event, void *arg)
|
||||
{
|
||||
if (user_is_connecting(user))
|
||||
{
|
||||
hub_disconnect_user(g_hub, user, quit_timeout);
|
||||
hub_disconnect_user(user->hub, user, quit_timeout);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -154,7 +151,7 @@ void net_event(struct net_connection* con, int event, void *arg)
|
||||
flag_close = handle_net_read(user);
|
||||
if (flag_close)
|
||||
{
|
||||
hub_disconnect_user(g_hub, user, flag_close);
|
||||
hub_disconnect_user(user->hub, user, flag_close);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -164,7 +161,7 @@ void net_event(struct net_connection* con, int event, void *arg)
|
||||
flag_close = handle_net_write(user);
|
||||
if (flag_close)
|
||||
{
|
||||
hub_disconnect_user(g_hub, user, flag_close);
|
||||
hub_disconnect_user(user->hub, user, flag_close);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ int route_message(struct hub_info* hub, struct hub_user* u, struct adc_message*
|
||||
break;
|
||||
|
||||
case 'D':
|
||||
target = uman_get_user_by_sid(hub, msg->target);
|
||||
target = uman_get_user_by_sid(hub->users, msg->target);
|
||||
if (target)
|
||||
{
|
||||
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;
|
||||
|
||||
case 'E':
|
||||
target = uman_get_user_by_sid(hub, msg->target);
|
||||
target = uman_get_user_by_sid(hub->users, msg->target);
|
||||
if (target)
|
||||
{
|
||||
route_to_user(hub, target, msg);
|
||||
|
@ -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_update);
|
||||
flood_control_reset(&user->flood_extras);
|
||||
|
||||
user->hub = hub;
|
||||
return user;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
struct net_statistics* total;
|
||||
struct net_statistics* intermediate;
|
||||
net_stats_get(&intermediate, &total);
|
||||
|
||||
hub->stats.net_tx = (intermediate->tx / factor);
|
||||
hub->stats.net_rx = (intermediate->rx / factor);
|
||||
hub->stats.net_tx_peak = MAX(hub->stats.net_tx, hub->stats.net_tx_peak);
|
||||
hub->stats.net_rx_peak = MAX(hub->stats.net_rx, hub->stats.net_rx_peak);
|
||||
hub->stats.net_tx_total = total->tx;
|
||||
hub->stats.net_rx_total = total->rx;
|
||||
users->stats.net_tx = (intermediate->tx / factor);
|
||||
users->stats.net_rx = (intermediate->rx / factor);
|
||||
users->stats.net_tx_peak = MAX(users->stats.net_tx, users->stats.net_tx_peak);
|
||||
users->stats.net_rx_peak = MAX(users->stats.net_rx, users->stats.net_rx_peak);
|
||||
users->stats.net_tx_total = total->tx;
|
||||
users->stats.net_rx_total = total->rx;
|
||||
|
||||
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)",
|
||||
hub->users->count,
|
||||
hub->users->count_peak,
|
||||
(int) hub->stats.net_tx / 1024,
|
||||
(int) hub->stats.net_rx / 1024,
|
||||
(int) hub->stats.net_tx_peak / 1024,
|
||||
(int) hub->stats.net_rx_peak / 1024);
|
||||
users->users->count,
|
||||
users->users->count_peak,
|
||||
(int) users->stats.net_tx / 1024,
|
||||
(int) users->stats.net_rx / 1024,
|
||||
(int) users->stats.net_tx_peak / 1024,
|
||||
(int) users->stats.net_rx_peak / 1024);
|
||||
}
|
||||
|
||||
static void timer_statistics(struct timeout_evt* t)
|
||||
{
|
||||
struct hub_info* hub = (struct hub_info*) t->ptr;
|
||||
uman_update_stats(hub);
|
||||
timeout_queue_reschedule(net_backend_get_timeout_queue(), hub->users->timeout, TIMEOUT_STATS);
|
||||
struct hub_user_manager* users = (struct hub_user_manager*) t->ptr;
|
||||
uman_update_stats(users);
|
||||
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;
|
||||
if (!hub)
|
||||
return -1;
|
||||
|
||||
users = (struct hub_user_manager*) hub_malloc_zero(sizeof(struct hub_user_manager));
|
||||
struct hub_user_manager* users = (struct hub_user_manager*) hub_malloc_zero(sizeof(struct hub_user_manager));
|
||||
if (!users)
|
||||
return -1;
|
||||
return NULL;
|
||||
|
||||
users->list = list_create();
|
||||
users->sids = sid_pool_create(net_get_max_sockets());
|
||||
@ -90,143 +88,136 @@ int uman_init(struct hub_info* hub)
|
||||
{
|
||||
list_destroy(users->list);
|
||||
hub_free(users);
|
||||
return -1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef STATS_SUPPORT
|
||||
if (net_backend_get_timeout_queue())
|
||||
{
|
||||
users->timeout = hub_malloc_zero(sizeof(struct timeout_evt));
|
||||
timeout_evt_initialize(users->timeout, timer_statistics, hub);
|
||||
timeout_queue_insert(net_backend_get_timeout_queue(), users->timeout, TIMEOUT_STATS);
|
||||
}
|
||||
#endif // STATS_SUPPORT
|
||||
|
||||
hub->users = users;
|
||||
return 0;
|
||||
return users;
|
||||
}
|
||||
|
||||
|
||||
int uman_shutdown(struct hub_info* hub)
|
||||
int uman_shutdown(struct hub_user_manager* users)
|
||||
{
|
||||
if (!hub || !hub->users)
|
||||
if (!users)
|
||||
return -1;
|
||||
|
||||
#ifdef STATS_SUPPORT
|
||||
if (net_backend_get_timeout_queue())
|
||||
{
|
||||
timeout_queue_remove(net_backend_get_timeout_queue(), hub->users->timeout);
|
||||
hub_free(hub->users->timeout);
|
||||
timeout_queue_remove(net_backend_get_timeout_queue(), users->timeout);
|
||||
hub_free(users->timeout);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (hub->users->list)
|
||||
if (users->list)
|
||||
{
|
||||
list_clear(hub->users->list, &clear_user_list_callback);
|
||||
list_destroy(hub->users->list);
|
||||
list_clear(users->list, &clear_user_list_callback);
|
||||
list_destroy(users->list);
|
||||
}
|
||||
sid_pool_destroy(hub->users->sids);
|
||||
hub_free(hub->users);
|
||||
hub->users = 0;
|
||||
|
||||
sid_pool_destroy(users->sids);
|
||||
|
||||
hub_free(users);
|
||||
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;
|
||||
|
||||
if (user->hub)
|
||||
return -1;
|
||||
list_append(users->list, user);
|
||||
users->count++;
|
||||
users->count_peak = MAX(users->count, users->count_peak);
|
||||
|
||||
list_append(hub->users->list, user);
|
||||
hub->users->count++;
|
||||
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;
|
||||
users->shared_size += user->limits.shared_size;
|
||||
users->shared_files += user->limits.shared_files;
|
||||
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;
|
||||
|
||||
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
|
||||
{
|
||||
uhub_assert(!"negative count!");
|
||||
}
|
||||
|
||||
hub->users->shared_size -= user->limits.shared_size;
|
||||
hub->users->shared_files -= user->limits.shared_files;
|
||||
|
||||
user->hub = 0;
|
||||
|
||||
users->shared_size -= user->limits.shared_size;
|
||||
users->shared_files -= user->limits.shared_files;
|
||||
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)
|
||||
{
|
||||
if (strcmp(user->id.cid, cid) == 0)
|
||||
return user;
|
||||
user = (struct hub_user*) list_get_next(hub->users->list);
|
||||
user = (struct hub_user*) list_get_next(users->list);
|
||||
}
|
||||
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)
|
||||
{
|
||||
if (strcmp(user->id.nick, nick) == 0)
|
||||
return user;
|
||||
user = (struct hub_user*) list_get_next(hub->users->list);
|
||||
user = (struct hub_user*) list_get_next(users->list);
|
||||
}
|
||||
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;
|
||||
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)
|
||||
{
|
||||
if (ip_in_range(&user->id.addr, range))
|
||||
{
|
||||
list_append(users, user);
|
||||
list_append(target, user);
|
||||
num++;
|
||||
}
|
||||
user = (struct hub_user*) list_get_next(hub->users->list);
|
||||
user = (struct hub_user*) list_get_next(users->list);
|
||||
}
|
||||
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;
|
||||
struct hub_user* user;
|
||||
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)
|
||||
{
|
||||
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)
|
||||
break;
|
||||
}
|
||||
user = (struct hub_user*) list_get_next(hub->users->list);
|
||||
user = (struct hub_user*) list_get_next(users->list);
|
||||
}
|
||||
|
||||
#if 0
|
||||
@ -248,7 +239,7 @@ int uman_send_user_list(struct hub_info* hub, struct hub_user* target)
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
return sid;
|
||||
}
|
||||
|
@ -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_files; /**<< "The total number of shared files among fully connected users." */
|
||||
struct linked_list* list; /**<< "Contains all logged in users" */
|
||||
#ifdef STATS_SUPPORT
|
||||
struct timeout_evt* timeout; /**<< "Timeout handler for statistics" */
|
||||
#endif
|
||||
};
|
||||
|
||||
/**
|
||||
* Initializes the user manager.
|
||||
* @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.
|
||||
* 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.
|
||||
*/
|
||||
extern void uman_update_stats(struct hub_info* hub);
|
||||
extern void uman_print_stats(struct hub_info* hub);
|
||||
extern void uman_update_stats(struct hub_user_manager* users);
|
||||
extern void uman_print_stats(struct hub_user_manager* users);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
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.
|
||||
@ -66,12 +68,12 @@ extern int uman_add(struct hub_info* hub, struct hub_user* user);
|
||||
*
|
||||
* @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).
|
||||
*/
|
||||
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).
|
||||
@ -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
|
||||
*/
|
||||
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).
|
||||
* @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.
|
||||
* @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.
|
||||
*
|
||||
* @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).
|
||||
*/
|
||||
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'.
|
||||
@ -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.
|
||||
*/
|
||||
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
|
||||
* 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 */
|
||||
|
Loading…
Reference in New Issue
Block a user