Hi,

This is just a heads up patch for anyone who's interested. The patch
implements a persistent cache of Bitzi tickets in the ~/.gtk-gnutella
directory. This is probably most sueful for developers that keep
stoppping and restarting their clients.

When the tickets are read from the cache they go through the same
validation as a live tickets, so out of date tickets will be expired at
this point.

It needs a quick tidy up before I submit the final patch.

--- src/core/bitzi.c    21 Mar 2005 22:08:56 -0000      1.13
+++ src/core/bitzi.c    6 Apr 2005 08:06:07 -0000
@@ -1,4 +1,4 @@
-/*
+/* -*- mode: cc-mode; tab-width:4; -*-
  * $Id: bitzi.c,v 1.13 2005/03/21 22:08:56 cbiere Exp $
  *
  * Copyright (c) 2004, Alex Bennee <[EMAIL PROTECTED]>
@@ -39,9 +39,11 @@
 
 #include <libxml/parser.h>
 #include <libxml/tree.h>
+#include <stdio.h>                     /* rename() */
 
 #include "http.h"                      /* http async stuff */
 #include "bitzi.h"                     /* bitzi metadata */
+#include "settings.h"                  /* settings_config_dir() */
 
 #include "if/bridge/c2ui.h"
 #include "if/gnet_property_priv.h"
@@ -92,6 +94,8 @@
 static GHashTable *bitzi_cache_ht;
 static GList *bitzi_cache;
 
+static FILE *bitzi_cache_file;
+
 /*
  * Function declarations
  */
@@ -264,6 +268,20 @@
        gchar *s = NULL;
 
        /*
+        * We extract the urn:sha1 from the ticket as we may be processing
+        * cached tickets not associated with any actual request
+        */
+       s = xml_get_string(node, "about");
+       if (s) {
+               const gchar *urnsha1;
+               urnsha1 = base32_sha1(&s[9]);
+               data->urnsha1 = atom_sha1_get(urnsha1);
+       } else {
+               g_warning("process_rdf_description: No urnsha!");
+       }
+       
+       
+       /*
         * All tickets have a ticketExpires tag which we need for cache
         * managment.
         *
@@ -425,6 +443,7 @@
 
        if (result) {
                bitzi_data_t *data = bitzi_create();
+               time_t           now;
 
                /*
                 * This just dumps the data
@@ -433,8 +452,27 @@
                root = xmlDocGetRootElement(doc);
 
                process_bitzi_ticket(root, data);
+
+#if 0
+               if (request->urnsha1)
+               {
                data->urnsha1 = atom_sha1_get(request->urnsha1);
+               }
+#endif
+
+               /*
+                * If the data has a valid date then re-echo the XML ticket
+                * to the file based cache.
+                */
 
+               now = time(NULL);
+               
+               if (now<data->expiry)
+               {
+                       xmlDocDump(bitzi_cache_file, doc);
+               }
+               
+               /* we are now finished with this XML doc */
                xmlFreeDoc(doc);
 
                /*
@@ -449,7 +487,9 @@
         * free used memory by the request
         */
 
+       if (request->urnsha1)
        atom_sha1_free(request->urnsha1);
+       
        wfree(request, sizeof *request);
 }
 
@@ -689,8 +729,105 @@
 void
 bitzi_init(void)
 {
+       gchar *path = NULL, *oldpath = NULL;
+       int result;
+       FILE *old_data;
+       
+
        bitzi_cache_ht = g_hash_table_new(NULL, NULL);
 
+       /*
+        * Rename the old file , overwritting stuff if we have to
+        */
+       oldpath = make_pathname(settings_config_dir(), "bitzi.orig");
+       path = make_pathname(settings_config_dir(), "bitzi.xml");
+
+       g_assert(NULL!=path);
+       g_assert(NULL!=oldpath);
+
+       result = rename(path, oldpath);
+       if (result)
+       {
+               g_warning("bitzi_init: failed to rename %s to %s (%d)",
+                               path, oldpath, result);
+       }
+
+       /*
+        * Set up the file cache descriptor, starting from scratch.
+        */
+       bitzi_cache_file = fopen(path, "w");
+
+       /*
+        * "play" the .orig file back through the XML parser and
+        * repopulate our internal cache
+        */
+       old_data = fopen(oldpath, "r");
+       
+       if (old_data)
+       {
+               bitzi_request_t *request=NULL;
+               char tmp[1024];
+
+               while (fgets(tmp, sizeof(tmp), old_data))
+               {
+                       gint result;
+                       int     len;
+                       
+                       /*
+                        * Each XML ticket will start with an XML header at 
which
+                        * time we submit the last piece of data and set up a 
new
+                        * context for the next ticket.
+                        */
+                       if (strncmp(tmp,"<?xml",5)==0)
+                       {
+                               if (request)
+                               {
+                                       /* finish parsing */
+                                       result = xmlParseChunk(request->ctxt, 
tmp, 0, 1);
+                                       
+                                       process_meta_data(request);
+                               }
+
+                               /* new psudo request */
+                               request = walloc(sizeof *request);
+                               request->urnsha1 = NULL;
+
+                               request->ctxt = xmlCreatePushParserCtxt(
+                                       NULL, NULL, NULL, 0, NULL);
+                       }
+
+                       /*
+                        * the first line of bitzi.orig should always be the 
start
+                        * of a ticket
+                        */
+                       g_assert(request!=NULL);
+
+                       /*
+                        * This is much the same at bitzi_data_ind
+                        */
+                       len = strlen(tmp);
+                       if (len>0)
+                       {
+                               result = xmlParseChunk(request->ctxt, tmp, len, 
0);
+
+                               if (result != 0)
+                                       g_warning("bitzi_init: bad xml parsing 
cache result %d", result);
+
+                       }
+               
+               }
+
+               fclose(old_data);
+       } else {
+               g_warning("Failed to open %s for cached Bitzi data (%s)",
+                               oldpath, strerror(errno));
+       }
+
+       /*
+        * Finally start the bitzi heart beat that will send requests when
+        * we set them up.
+        */
+       
        g_timeout_add(1 * 10000, (GSourceFunc) bitzi_heartbeat, NULL);
 }
 

--
Alex, homepage: http://www.bennee.com/~alex/
cursor address, n: "Hello, cursor!" -- Stan Kelly-Bootle, "The Devil's
DP Dictionary"



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
Gtk-gnutella-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gtk-gnutella-devel

Reply via email to