From e303db16872a6d48bb8497ee703e5ae02bdbc979 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 20 Feb 2004 02:39:34 +0000 Subject: [PATCH] start working on server connections. can add, list and delete servers --- qtv/include/Makefile.am | 2 +- qtv/include/server.h | 45 +++++++++++++ qtv/source/Makefile.am | 2 +- qtv/source/qtv.c | 12 ++++ qtv/source/server.c | 140 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 199 insertions(+), 2 deletions(-) create mode 100644 qtv/include/server.h create mode 100644 qtv/source/server.c diff --git a/qtv/include/Makefile.am b/qtv/include/Makefile.am index 1c1a63240..8f0c060ce 100644 --- a/qtv/include/Makefile.am +++ b/qtv/include/Makefile.am @@ -1,4 +1,4 @@ ## Process this file with automake to produce Makefile.in AUTOMAKE_OPTIONS= foreign -EXTRA_DIST = +EXTRA_DIST = server.h diff --git a/qtv/include/server.h b/qtv/include/server.h new file mode 100644 index 000000000..ef45b0356 --- /dev/null +++ b/qtv/include/server.h @@ -0,0 +1,45 @@ +/* + #FILENAME# + + #DESCRIPTION# + + Copyright (C) 2004 #AUTHOR# + + Author: #AUTHOR# + Date: #DATE# + + 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, write to: + + Free Software Foundation, Inc. + 59 Temple Place - Suite 330 + Boston, MA 02111-1307, USA + + $Id$ +*/ + +#ifndef __server_h +#define __server_h + +#include "netchan.h" + +typedef struct server_s { + const char *name; + const char *address; + netchan_t netchan; +} server_t; + +void Server_Init (void); + +#endif//__server_h diff --git a/qtv/source/Makefile.am b/qtv/source/Makefile.am index cb6f7782b..69425d965 100644 --- a/qtv/source/Makefile.am +++ b/qtv/source/Makefile.am @@ -43,7 +43,7 @@ qtv_LIBS= \ $(top_builddir)/libs/console/libQFconsole.la \ $(top_builddir)/libs/util/libQFutil.la -qtv_SOURCES= qtv.c +qtv_SOURCES= qtv.c server.c qtv_LDADD= $(qtv_LIBS) $(NET_LIBS) $(DL_LIBS) $(CURSES_LIBS) qtv_LDFLAGS= $(common_ldflags) qtv_DEPENDENCIES= $(qtv_LIBS) diff --git a/qtv/source/qtv.c b/qtv/source/qtv.c index 06203432a..75d6e6d80 100644 --- a/qtv/source/qtv.c +++ b/qtv/source/qtv.c @@ -49,6 +49,7 @@ static __attribute__ ((unused)) const char rcsid[] = #include "QF/zone.h" #include "netchan.h" +#include "server.h" SERVER_PLUGIN_PROTOS static plugin_list_t server_plugin_list[] = { @@ -80,6 +81,13 @@ qtv_memory_init (void) Memory_Init (mem_base, mem_size); } +static void +qtv_shutdown (void) +{ + NET_Shutdown (); + Con_Shutdown (); +} + static void qtv_quit_f (void) { @@ -92,6 +100,8 @@ qtv_init (void) { qtv_cbuf = Cbuf_New (&id_interp); + Sys_RegisterShutdown (qtv_shutdown); + Cvar_Init_Hash (); Cmd_Init_Hash (); Cvar_Init (); @@ -137,6 +147,8 @@ qtv_init (void) Cmd_StuffCmds (qtv_cbuf); Cbuf_Execute_Sets (qtv_cbuf); + Server_Init (); + Cmd_AddCommand ("quit", qtv_quit_f, "Shut down qtv"); Cmd_StuffCmds (qtv_cbuf); diff --git a/qtv/source/server.c b/qtv/source/server.c new file mode 100644 index 000000000..bde54306a --- /dev/null +++ b/qtv/source/server.c @@ -0,0 +1,140 @@ +/* + #FILENAME# + + #DESCRIPTION# + + Copyright (C) 2004 #AUTHOR# + + Author: #AUTHOR# + Date: #DATE# + + 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, write to: + + Free Software Foundation, Inc. + 59 Temple Place - Suite 330 + Boston, MA 02111-1307, USA + +*/ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +static __attribute__ ((unused)) const char rcsid[] = + "$Id$"; + +#ifdef HAVE_STRING_H +# include +#endif +#ifdef HAVE_STRINGS_H +# include +#endif + +#include +#include + +#include "QF/cmd.h" +#include "QF/console.h" +#include "QF/hash.h" + +#include "server.h" + +static hashtab_t *servers; + +static const char * +server_get_key (void *sv, void *unused) +{ + return ((server_t *) sv)->name; +} + +static void +server_free (void *_sv, void *unused) +{ +} + +static int +server_compare (const void *a, const void *b) +{ + return strcmp ((*(server_t **) a)->name, (*(server_t **) b)->name); +} + +static void +sv_new_f (void) +{ + const char *name; + const char *address; + server_t *sv; + + if (Cmd_Argc () != 3) { + Con_Printf ("Usage: sv_new
\n"); + return; + } + name = Cmd_Argv (1); + if (Hash_Find (servers, name)) { + Con_Printf ("sv_new: %s already exists\n", name); + return; + } + address = Cmd_Argv (2); + sv = calloc (1, sizeof (server_t)); + sv->name = strdup (name); + sv->address = strdup (address); + Hash_Add (servers, sv); +} + +static void +sv_del_f (void) +{ + const char *name; + server_t *sv; + + if (Cmd_Argc () != 2) { + Con_Printf ("Usage: sv_del \n"); + return; + } + name = Cmd_Argv (1); + if (!(sv = Hash_Del (servers, name))) { + Con_Printf ("sv_new: %s unkown\n", name); + return; + } + Hash_Free (servers, sv); +} + +static void +sv_list_f (void) +{ + server_t **list, **l, *sv; + int count; + + list = (server_t **) Hash_GetList (servers); + for (l = list, count = 0; *l; l++) + count++; + if (!count) { + Con_Printf ("no servers\n"); + return; + } + qsort (list, count, sizeof (*list), server_compare); + for (l = list; *l; l++) { + sv = *l; + Con_Printf ("%-20s %s\n", sv->name, sv->address); + } +} + +void +Server_Init (void) +{ + servers = Hash_NewTable (61, server_get_key, server_free, 0); + Cmd_AddCommand ("sv_new", sv_new_f, "Add a new server"); + Cmd_AddCommand ("sv_del", sv_del_f, "Remove an existing server"); + Cmd_AddCommand ("sv_list", sv_list_f, "List available servers"); +}