Back to index

gcompris  8.2.2
cache.c
Go to the documentation of this file.
00001 /* gcompris - gameutil_net.c
00002  *
00003  * Time-stamp: <2006/07/10 01:24:04 bruno>
00004  *
00005  * Copyright (C) 2006 Bruno Coudoin
00006  *
00007  *   This program is free software; you can redistribute it and/or modify
00008  *   it under the terms of the GNU General Public License as published by
00009  *   the Free Software Foundation; either version 2 of the License, or
00010  *   (at your option) any later version.
00011  *
00012  *   This program is distributed in the hope that it will be useful,
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *   GNU General Public License for more details.
00016  *
00017  *   You should have received a copy of the GNU General Public License
00018  *   along with this program; if not, write to the Free Software
00019  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  */
00021 
00022 #include "gcompris.h"
00023 #include "gc_core.h"
00024 #include <string.h>
00025 
00030 /* The max size of the cache in bytes,
00031  * 0  = NO LIMITS
00032  * -1 = NO CACHE
00033  */
00034 #ifdef USE_GNET
00035 static int cache_max_size = 1000;
00036 
00037 static GHashTable *hash_cache = NULL;
00038 #endif
00039 
00042 #ifdef USE_GNET
00043 static void
00044 _dump_cache(gchar *key,
00045            gchar *value,
00046            gpointer dummy)
00047 {
00048   printf("cache %s = %s\n", key, value);
00049 }
00050 
00051 static void dump_cache(void)
00052 {
00053   g_hash_table_foreach(hash_cache,
00054                      (GHFunc) _dump_cache,
00055                      NULL);
00056 }
00057 #endif
00058 
00062 void _cache_init(const gchar *basedir, const gchar *currentdir)
00063 {
00064 #ifdef USE_GNET
00065   GcomprisProperties *properties = gc_prop_get();
00066   GDir *dir;
00067   const gchar *file;
00068 
00069   if(!currentdir)
00070     currentdir = basedir;
00071 
00072   dir = g_dir_open(currentdir, 0, NULL);
00073 
00074   printf("  _cache_init %s\n", currentdir);
00075 
00076   while((file = g_dir_read_name(dir)))
00077     {
00078       gchar *fullfile = g_strconcat(currentdir, "/", file, NULL);
00079       printf("  processing file = %s\n", fullfile);
00080 
00081       if(g_file_test(fullfile, G_FILE_TEST_IS_DIR))
00082        {
00083          _cache_init(basedir, fullfile);
00084        }
00085       else if(g_file_test(fullfile, G_FILE_TEST_IS_REGULAR))
00086        {
00087          /* Strip file to get the KEY part out of it */
00088          printf("  Adding '%s' in the cache\n", fullfile);
00089          g_hash_table_insert(hash_cache,
00090                            g_strconcat(properties->server, "/",
00091                                      fullfile + strlen(basedir) + 1,
00092                                      NULL),
00093                            g_strdup(fullfile));
00094 
00095        }
00096       g_free(fullfile);
00097     }
00098 
00099   g_dir_close(dir);
00100 #endif
00101 }
00102 
00108 void gc_cache_init(int max_size)
00109 {
00110 #ifdef USE_GNET
00111   cache_max_size = max_size;
00112 
00113   /* No server defined, the cache is useless */
00114   if(!gc_prop_get()->server ||
00115      !gc_prop_get()->cache_dir)
00116     return;
00117 
00118   hash_cache = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
00119 
00120   printf("gc_cache_init\n");
00121 
00122   /* try to create it */
00123   printf("Creating cache dir %s\n", gc_prop_get()->cache_dir);
00124   if(g_mkdir_with_parents(gc_prop_get()->cache_dir, 0755))
00125     {
00126       g_error("Failed to create the cache directory");
00127     }
00128   printf("  opened top directory\n");
00129   /* Load the previous cache directory if any */
00130   _cache_init(gc_prop_get()->cache_dir, NULL);
00131 
00132   dump_cache();
00133 #endif
00134 }
00135 
00139 void gc_cache_end()
00140 {
00141 #ifdef USE_GNET
00142 
00143   if(!hash_cache)
00144     return;
00145 
00146   g_hash_table_destroy (hash_cache);
00147   hash_cache = NULL;
00148 #endif
00149 }
00150 
00151 
00152 #ifdef USE_GNET
00153 static void
00154 _clear_cache(gchar *key,
00155             gchar *value,
00156             gpointer dummy)
00157 {
00158   printf("NOT IMPLEMENTED: Clearing chache %s:%s\n", key, value);
00159 }
00160 #endif
00161 
00165 void gc_cache_clear()
00166 {
00167 #ifdef USE_GNET
00168   g_hash_table_foreach(hash_cache,
00169                      (GHFunc) _clear_cache,
00170                      NULL);
00171 
00172 #endif
00173 }
00174 
00178 gchar *gc_cache_get(gchar *url)
00179 {
00180 #ifdef USE_GNET
00181   return((char *)g_hash_table_lookup(hash_cache, url));
00182 #else
00183   return NULL;
00184 #endif
00185 }
00186 
00196 gchar *gc_cache_insert(const gchar *url, const char *buffer, gssize length)
00197 {
00198 #ifdef USE_GNET
00199   /* Save the buffer in the cache */
00200   if(g_file_set_contents("TBD", buffer, length, NULL))
00201     g_hash_table_replace(hash_cache, (gpointer) url, (gpointer) "TBD");
00202 #endif
00203   return("TBD");
00204 }