Revision: 14336
Author: adrian.chadd
Date: Mon Oct 19 02:18:59 2009
Log: Flesh out some more of the logfile helper / buffer code.


http://code.google.com/p/lusca-cache/source/detail?r=14336

Modified:
  /branches/LUSCA_HEAD/libloghelper/loghelper.c

=======================================
--- /branches/LUSCA_HEAD/libloghelper/loghelper.c       Mon Oct 19 00:14:01 2009
+++ /branches/LUSCA_HEAD/libloghelper/loghelper.c       Mon Oct 19 02:18:59 2009
@@ -3,21 +3,102 @@
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>

  #include "include/util.h"
+#include "include/Array.h"
+#include "include/Stack.h"

  #include "libcore/kb.h"
+#include "libcore/gb.h"
  #include "libcore/dlink.h"
  #include "libcore/varargs.h"
  #include "libcore/tools.h"

+#include "libmem/MemPool.h"
+#include "libmem/MemBufs.h"
+#include "libmem/MemBuf.h"
+
+#include "libsqinet/sqinet.h"
+
  #include "libsqdebug/debug.h"

  #include "libhelper/ipc.h"

+#include "libiapp/fd_types.h"
+#include "libiapp/comm_types.h"
+#include "libiapp/comm.h"
+
  #include "loghelper.h"
  #include "loghelper_commands.h"

+
+static void
+loghelper_buffer_free(loghelper_buffer_t *lb)
+{
+       safe_free(lb->buf);
+       safe_free(lb);
+}
+
+loghelper_buffer_t *
+loghelper_buffer_create(void)
+{
+       loghelper_buffer_t *lb;
+
+       lb = xcalloc(1, sizeof(*lb));
+
+       lb->buf = xmalloc(32768);
+       lb->size = 32768;
+
+       return lb;
+}
+
+/* *** */
+
+/*
+ * Free all pending buffers
+ */
+static void
+loghelper_free_buffers(loghelper_instance_t *lh)
+{
+       loghelper_buffer_t *lb;
+
+       while (lh->bufs.head == NULL) {
+               lb = lh->bufs.head->data;
+               dlinkDelete(&lb->node, &lh->bufs);
+               loghelper_buffer_free(lb);
+       }
+       lh->nbufs = 0;
+}
+
+/*
+ * destroy the loghelper.
+ *
+ * Destroy any/all pending buffers, deregister/close the sockets,  
terminate the helper.
+ * Then free the helper.
+ */
+static void
+loghelper_destroy(loghelper_instance_t *lh)
+{
+       /* free buffers */
+       loghelper_free_buffers(lh);
+
+       /* close comm sockets - which will hopefully gracefully shut down the  
helper later */
+       comm_close(lh->rfd);
+       if (lh->rfd != lh->wfd)
+               comm_close(lh->wfd);
+
+       /* Free the loghelper instance */
+       safe_free(lh);
+}
+
+static int
+loghelper_has_bufs(loghelper_instance_t *lh)
+{
+       return (lh->nbufs > 0);
+}
+
  /*
   * Create a loghelper instance.
   *
@@ -55,5 +136,15 @@
  void
  loghelper_close(loghelper_instance_t *lh)
  {
-
-}
+       /* Mark the connection as closing */
+       lh->flags.closing = 1;
+
+       /* Are there pending messages? If so, leave it for now and queue a 
write  
if needed */
+       if (loghelper_has_bufs(lh)) {
+               /* XXX check if an explicit flush needs to be scheduled here */
+               return;
+       }
+
+       /* No pending messages? Wrap up. */
+       loghelper_destroy(lh);
+}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"lusca-commit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/lusca-commit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to