From 8a72b48bf7112f5dcd515cb46ac7534daa492566 Mon Sep 17 00:00:00 2001
From: Sven Gestegard Robertz <sven.robertz@cs.lth.se>
Date: Mon, 27 Oct 2014 18:47:37 +0100
Subject: [PATCH] sketched pragma handler lookup

---
 lib/c/labcomm_decoder.c | 21 ++++++++++++++++-----
 lib/c/labcomm_pragma.h  | 32 +++++++++++++++++++++++---------
 2 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/lib/c/labcomm_decoder.c b/lib/c/labcomm_decoder.c
index 37679ce..aa33d13 100644
--- a/lib/c/labcomm_decoder.c
+++ b/lib/c/labcomm_decoder.c
@@ -41,7 +41,8 @@ static int internal_decode_one(struct labcomm_decoder *d,
 		               struct labcomm_decoder *registry);
 int default_pragma_handler(struct labcomm_decoder *d,
 		           struct labcomm_decoder *registry,
-		           char *pragma_type)
+		           char *pragma_type,
+			   void *context)
 {
   printf("Default pragma handler got pragma: %s\n", pragma_type);
   int res = internal_decoder_run(d, registry);
@@ -49,6 +50,13 @@ int default_pragma_handler(struct labcomm_decoder *d,
   return LABCOMM_PRAGMA;
 }
 
+labcomm_pragma_handler_callback default_pragma_handler_lookup(
+                                        char *pragma_type,
+                                        struct labcomm_decoder *d,
+                                        void *context)
+{
+	return default_pragma_handler;
+}
 
 struct labcomm_decoder {
   struct labcomm_reader *reader;
@@ -61,7 +69,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;
+  labcomm_pragma_handler_lookup pragma_handler_lookup;
 };
 
 struct labcomm_decoder *labcomm_decoder_new(
@@ -89,7 +97,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;
+    result->pragma_handler_lookup = default_pragma_handler_lookup;
   }
   return result;
 }
@@ -279,6 +287,7 @@ static int decode_pragma(struct labcomm_decoder *d,
 {
   char *pragma_type;
   int result;
+  labcomm_pragma_handler_callback handler;
   pragma_type = labcomm_read_string(d->reader);
   if (d->reader->error < 0) {
     result = d->reader->error;
@@ -286,7 +295,8 @@ static int decode_pragma(struct labcomm_decoder *d,
   }
   int bytes = labcomm_size_string(pragma_type);
   int psize = len-bytes;
-  if(d->pragma_handler) {
+  handler = d->pragma_handler_lookup(pragma_type, d, 0);
+  if(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)
@@ -309,8 +319,9 @@ static int decode_pragma(struct labcomm_decoder *d,
      struct labcomm_decoder *pd = labcomm_decoder_new(
                                      pr, d->error, d->memory, d->scheduler);
      pd->version_ok = 1;
+     void *pragma_context = 0;
      printf("calling pragma_handler\n");
-     result = d->pragma_handler(pd, registry, pragma_type);
+     result = handler(pd, registry, pragma_type,pragma_context);
      printf("returned from pragma_handler\n");
 
      internal_labcomm_decoder_free(pd);
diff --git a/lib/c/labcomm_pragma.h b/lib/c/labcomm_pragma.h
index a9097c3..13c3b79 100644
--- a/lib/c/labcomm_pragma.h
+++ b/lib/c/labcomm_pragma.h
@@ -2,6 +2,7 @@
   labcomm_pragma.h -- user interface for handling labcomm pragma packets.
 
   Copyright 2014 Sven GestegÄrd Robertz <sven.robertz@cs.lth.se>
+                 Anders Blomdell <anders.blomdell@control.lth.se>
 
   This file is part of LabComm.
 
@@ -25,24 +26,37 @@
 typedef int (*labcomm_pragma_handler_callback)(
   struct labcomm_decoder *decoder,
   struct labcomm_decoder *registry,
-  char *pragma_type);
+  char *pragma_type,
+  void *context);
 
+typedef labcomm_pragma_handler_callback (*labcomm_pragma_handler_lookup)(
+  char *pragma_type,
+  struct labcomm_decoder *d,
+  void *context);
+
+void labcomm_decoder_register_pragma_handler_lookup(struct labcomm_decoder *d,
+		labcomm_pragma_handler_lookup pragma_lookup,
+		void *context);
+
+/*XXX TODO: rename to _callback? */
 void labcomm_decoder_register_pragma_handler(struct labcomm_decoder *d,
-		labcomm_pragma_handler_callback pragma_dispatcher);
+		labcomm_pragma_handler_callback pragma_dispatcher,
+		void *context);
 
+/*XXX TODO: rename to _callback? */
 int default_pragma_handler(struct labcomm_decoder *d,
 		           struct labcomm_decoder *registry,
-		           char *pragma_type);
+		           char *pragma_type,
+			   void *context);
+
+labcomm_pragma_handler_callback default_pragma_handler_lookup(
+                                        char *pragma_type,
+                                        struct labcomm_decoder *d,
+                                        void *context);
 
 struct labcomm_encoder *labcomm_pragma_builder_new(
                                    struct labcomm_encoder *e,
 				   char * pragma_type) ;
 int labcomm_pragma_send(struct labcomm_encoder* e);
-struct labcomm_pragma_handler {
-  //struct labcomm_decoder_registry *registry;
-  //TODO: implement  map (char * pragma_type) --> decoder_registry *
-  //to allow having different sets of handlers for different pragma types
-  int foo;
-};
 
 #endif
-- 
GitLab