From f1cf1aa579e02f452368b4c8201b21f134d18153 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Sun, 26 Oct 2014 20:05:54 +0100
Subject: [PATCH] refactored c pragma. still TODO: share decoder registry

---
 lib/c/labcomm.h         |  6 +++---
 lib/c/labcomm_decoder.c | 31 +++++++++++++++++++++++--------
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/lib/c/labcomm.h b/lib/c/labcomm.h
index b543e98..d3e177f 100644
--- a/lib/c/labcomm.h
+++ b/lib/c/labcomm.h
@@ -117,7 +117,7 @@ void labcomm_decoder_free(
   struct labcomm_decoder *decoder);
 int labcomm_decoder_decode_one(
   struct labcomm_decoder *decoder);
-void labcomm_decoder_run(
+int labcomm_decoder_run(
   struct labcomm_decoder *decoder);
 
 /* See labcomm_ioctl.h for predefined ioctl_action values */
@@ -137,12 +137,12 @@ struct labcomm_pragma_handler {
   int foo;
 };
 
-typedef int (*labcomm_handle_pragma_callback)(
+typedef int (*labcomm_pragma_handler_callback)(
   struct labcomm_decoder *decoder,
   char *pragma_type);
 
 void labcomm_decoder_register_pragma_handler(struct labcomm_decoder *d,
-		labcomm_handle_pragma_callback pragma_dispatcher);
+		labcomm_pragma_handler_callback pragma_dispatcher);
 
 /*
  * Encoder
diff --git a/lib/c/labcomm_decoder.c b/lib/c/labcomm_decoder.c
index c6ae216..38fb6bf 100644
--- a/lib/c/labcomm_decoder.c
+++ b/lib/c/labcomm_decoder.c
@@ -35,6 +35,17 @@ struct sample_entry {
   void *context;
 };
 
+//XXX move to pragma.ch
+//int default_pragma_handler(struct labcomm_decoder *d, char *pragma_type);
+int default_pragma_handler(struct labcomm_decoder *d, char *pragma_type)
+{
+  printf("Default pragma handler got pragma: %s\n", pragma_type);
+  int res = labcomm_decoder_run(d);
+  printf("... %d:%s\n",res,strerror(-res));
+  return LABCOMM_PRAGMA;
+}
+
+
 struct labcomm_decoder {
   struct labcomm_reader *reader;
   int reader_allocated;
@@ -46,6 +57,7 @@ struct labcomm_decoder {
   labcomm_handle_new_datatype_callback on_new_datatype;
   LABCOMM_SIGNATURE_ARRAY_DEF(local, struct sample_entry);
   LABCOMM_SIGNATURE_ARRAY_DEF(remote_to_local, int);
+  labcomm_pragma_handler_callback pragma_handler;
 };
 
 struct labcomm_decoder *labcomm_decoder_new(
@@ -73,6 +85,7 @@ struct labcomm_decoder *labcomm_decoder_new(
     result->on_error = on_error_fprintf;
     LABCOMM_SIGNATURE_ARRAY_INIT(result->local, struct sample_entry);
     LABCOMM_SIGNATURE_ARRAY_INIT(result->remote_to_local, int);
+    result->pragma_handler = default_pragma_handler;
   }
   return result;
 }
@@ -253,8 +266,7 @@ static int decode_pragma(struct labcomm_decoder *d, int len)
   }
   int bytes = labcomm_size_string(pragma_type);
   int psize = len-bytes;
-  printf("got pragma: %s\n", pragma_type);
-  if(0 /*d->pragma_handler*/) {
+  if(d->pragma_handler) {
   //read the entire packet to a buffer and then run
   // decode on that through a bytearray_reader.
   // (to easily handle multiple labcomm packets in one metadata packet)
@@ -267,18 +279,19 @@ static int decode_pragma(struct labcomm_decoder *d, int len)
          goto out;
        }
      }
-    printf("read %d bytes\n", psize);
+    //printf("read %d bytes\n", psize);
     //TODO: add wrapped decoders, and create pragma decoder that does not
     //      expect a version packet
      struct labcomm_reader *pr = labcomm_bytearray_reader_new(
                                      d->reader->memory, pragma_data, psize);
      struct labcomm_decoder *pd = labcomm_decoder_new(
                                      pr, d->error, d->memory, d->scheduler);
-    /* d->prama_handler(pd, ptype, plen); */
-     labcomm_decoder_run(pd);
+     pd->version_ok = 1;  
+     printf("calling pragma_handler\n");
+     result = d->pragma_handler(pd, pragma_type);
+     printf("returned from pragma_handler\n");
 
      internal_labcomm_decoder_free(pd);
-     result = 0;
    } else {
      result = decoder_skip(d, psize, LABCOMM_PRAGMA);
    }
@@ -356,10 +369,12 @@ out:
   return result;
 }
 
-void labcomm_decoder_run(struct labcomm_decoder *d)
+int labcomm_decoder_run(struct labcomm_decoder *d)
 {
-  while (labcomm_decoder_decode_one(d) > 0) {
+  int res;	
+  while ( (res=labcomm_decoder_decode_one(d)) > 0) {
   }
+  return res;
 }
 
 int labcomm_decoder_ioctl(struct labcomm_decoder *d, 
-- 
GitLab