Moved maintenance thread code into maintenance.c

Signed-off-by: Gergely POLONKAI <polesz@w00d5t0ck.info>
This commit is contained in:
Gergely POLONKAI 2012-03-24 20:51:06 +01:00
parent 74e84919d7
commit d17203f2a3
5 changed files with 133 additions and 91 deletions

View File

@ -1,5 +1,5 @@
bin_PROGRAMS = wmud bin_PROGRAMS = wmud
AM_CPPFLAGS = -DWMUD_STATEDIR=\""$(localstatedir)"\" -DWMUD_CONFDIR=\""$(sysconfdir)"\" $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS) AM_CPPFLAGS = -DWMUD_STATEDIR=\""$(localstatedir)"\" -DWMUD_CONFDIR=\""$(sysconfdir)"\" $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS)
wmud_SOURCES = main.c networking.c interpreter.c db.c players.c wmud_SOURCES = main.c networking.c interpreter.c db.c players.c maintenance.c
wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS) wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS)

View File

@ -32,6 +32,7 @@
#include "interpreter.h" #include "interpreter.h"
#include "db.h" #include "db.h"
#include "players.h" #include "players.h"
#include "maintenance.h"
/** /**
* debug_context_loc: * debug_context_loc:
@ -118,62 +119,6 @@ wmud_random_string(gint len)
return ret; return ret;
} }
/**
* wmud_maintenance_check_new_players:
* @player: #wmudPLayer structure of the player record to check
* @user_data: not used
*
* Callback called from within the maintenance loop. Checks if the player has
* an unset password, and generate one for them, if so.
*/
void
wmud_maintenance_check_new_players(wmudPlayer *player, gpointer user_data)
{
if (player->cpassword == NULL)
{
gchar *pw,
*salt,
*cpw;
GString *full_salt;
pw = wmud_random_string(8);
salt = wmud_random_string(8);
full_salt = g_string_new("$1$");
g_string_append(full_salt, salt);
cpw = g_strdup(crypt(pw, full_salt->str));
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Player %s has no"
" password set", player->player_name);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "New password will be %s", pw);
player->cpassword = cpw;
/* TODO: Send e-mail about the new password. Upon completion,
* set it in the database */
g_free(pw);
g_free(salt);
g_string_free(full_salt, TRUE);
}
}
/**
* wmud_maintenance:
* @user_data: not used
*
* Timeout source function for maintenance tasks
*/
gboolean
wmud_maintenance(gpointer user_data)
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Starting maintenance...");
/* Run through the player list, and generate a random password for each
* newly registered player */
g_slist_foreach(players, (GFunc)wmud_maintenance_check_new_players, NULL);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Finished maintenance...");
return TRUE;
}
#ifdef DEBUG #ifdef DEBUG
void void
/** /**
@ -323,22 +268,6 @@ game_thread_func(GMainLoop *game_loop)
return NULL; return NULL;
} }
/**
* maint_thread_func:
* @main_loop: the main loop to be associated with the maintenance thread
*
* The maintenance thread's main function.
*
* Return value: This function always returns %NULL.
*/
gpointer
maint_thread_func(GMainLoop *maint_loop)
{
g_main_loop_run(maint_loop);
return NULL;
}
/** /**
* main: * main:
* @argc: The number of arguments on the command line * @argc: The number of arguments on the command line
@ -351,12 +280,8 @@ main(int argc, char **argv)
{ {
GMainLoop *game_loop; GMainLoop *game_loop;
GSource *timeout_source; GSource *timeout_source;
guint timeout_id;
GError *err = NULL; GError *err = NULL;
GThread *game_thread, GThread *game_thread;
*maint_thread;
GMainContext *maint_context;
GMainLoop *maint_loop;
/* Initialize the thread and type system */ /* Initialize the thread and type system */
g_thread_init(NULL); g_thread_init(NULL);
@ -372,21 +297,11 @@ main(int argc, char **argv)
game_context = g_main_context_new(); game_context = g_main_context_new();
game_loop = g_main_loop_new(game_context, FALSE); game_loop = g_main_loop_new(game_context, FALSE);
/* Create the maintenance context and main loop */
maint_context = g_main_context_new();
maint_loop = g_main_loop_new(maint_context, FALSE);
/* Create the timeout source which keeps track of elapsed real-world /* Create the timeout source which keeps track of elapsed real-world
* time */ * time */
timeout_source = g_timeout_source_new(1000); timeout_source = g_timeout_source_new(1000);
g_source_set_callback(timeout_source, rl_sec_elapsed, NULL, NULL); g_source_set_callback(timeout_source, rl_sec_elapsed, NULL, NULL);
timeout_id = g_source_attach(timeout_source, game_context); g_source_attach(timeout_source, game_context);
g_source_unref(timeout_source);
/* Create the timeout source which will do the maintenance tasks */
timeout_source = g_timeout_source_new_seconds(3);
g_source_set_callback(timeout_source, wmud_maintenance, NULL, NULL);
timeout_id = g_source_attach(timeout_source, maint_context);
g_source_unref(timeout_source); g_source_unref(timeout_source);
/* TODO: Create signal handlers! */ /* TODO: Create signal handlers! */
@ -445,8 +360,7 @@ main(int argc, char **argv)
g_clear_error(&err); g_clear_error(&err);
game_thread = g_thread_create((GThreadFunc)game_thread_func, game_loop, TRUE, &err); game_thread = g_thread_create((GThreadFunc)game_thread_func, game_loop, TRUE, &err);
g_clear_error(&err); wmud_maintenance_init();
maint_thread = g_thread_create((GThreadFunc)maint_thread_func, maint_loop, FALSE, &err);
/* Initialize other threads here */ /* Initialize other threads here */

View File

@ -19,5 +19,7 @@ extern gchar *admin_email;
*/ */
#define random_number(min, max) g_rand_int_range(main_rand, (min), (max) + 1) #define random_number(min, max) g_rand_int_range(main_rand, (min), (max) + 1)
gchar *wmud_random_string(gint len);
#endif /* __WMUD_MAIN_H__ */ #endif /* __WMUD_MAIN_H__ */

125
src/maintenance.c Normal file
View File

@ -0,0 +1,125 @@
/* wMUD - Yet another MUD codebase by W00d5t0ck
* Copyright (C) 2012 - Gergely POLONKAI
*
* maintenance.c: game maintenance and self-check functions
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif
#include "wmud_types.h"
#include "maintenance.h"
#include "main.h"
#include "players.h"
/**
* wmud_maintenance_check_new_players:
* @player: #wmudPLayer structure of the player record to check
* @user_data: not used
*
* Callback called from within the maintenance loop. Checks if the player has
* an unset password, and generate one for them, if so.
*/
void
wmud_maintenance_check_new_players(wmudPlayer *player, gpointer user_data)
{
if (player->cpassword == NULL)
{
gchar *pw,
*salt,
*cpw;
GString *full_salt;
pw = wmud_random_string(8);
salt = wmud_random_string(8);
full_salt = g_string_new("$1$");
g_string_append(full_salt, salt);
cpw = g_strdup(crypt(pw, full_salt->str));
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Player %s has no"
" password set", player->player_name);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "New password will be %s", pw);
player->cpassword = cpw;
/* TODO: Send e-mail about the new password. Upon completion,
* set it in the database */
g_free(pw);
g_free(salt);
g_string_free(full_salt, TRUE);
}
}
/**
* wmud_maintenance:
* @user_data: not used
*
* Timeout source function for maintenance tasks
*/
gboolean
wmud_maintenance(gpointer user_data)
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Starting maintenance...");
/* Run through the player list, and generate a random password for each
* newly registered player */
g_slist_foreach(players, (GFunc)wmud_maintenance_check_new_players, NULL);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Finished maintenance...");
return TRUE;
}
/**
* maint_thread_func:
* @main_loop: the main loop to be associated with the maintenance thread
*
* The maintenance thread's main function.
*
* Return value: This function always returns %NULL.
*/
gpointer
maint_thread_func(GMainLoop *maint_loop)
{
g_main_loop_run(maint_loop);
return NULL;
}
void
wmud_maintenance_init(void)
{
GSource *timeout_source;
GMainLoop *maint_loop;
GMainContext *maint_context;
GError *err = NULL;
/* Create the maintenance context and main loop */
maint_context = g_main_context_new();
maint_loop = g_main_loop_new(maint_context, FALSE);
/* Create the timeout source which will do the maintenance tasks */
timeout_source = g_timeout_source_new_seconds(3);
g_source_set_callback(timeout_source, wmud_maintenance, NULL, NULL);
g_source_attach(timeout_source, maint_context);
g_source_unref(timeout_source);
g_thread_create((GThreadFunc)maint_thread_func, maint_loop, FALSE, &err);
}

1
src/maintenance.h Normal file
View File

@ -0,0 +1 @@
void wmud_maintenance_init(void);