[Olsr-dev] [PATCH v1 023/115] txtinfo: use a table of function pointers in send_info

Ferry Huberts (spam-protected)
Wed Dec 2 21:27:56 CET 2015


From: Ferry Huberts <(spam-protected)>

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 .../src/txtinfo_printers.h => info/info_types.h}   | 35 ++++++----
 lib/txtinfo/src/olsrd_txtinfo.c                    | 76 +++++++++++++---------
 2 files changed, 67 insertions(+), 44 deletions(-)
 copy lib/{txtinfo/src/txtinfo_printers.h => info/info_types.h} (75%)

diff --git a/lib/txtinfo/src/txtinfo_printers.h b/lib/info/info_types.h
similarity index 75%
copy from lib/txtinfo/src/txtinfo_printers.h
copy to lib/info/info_types.h
index 3fcd86c..4444721 100644
--- a/lib/txtinfo/src/txtinfo_printers.h
+++ b/lib/info/info_types.h
@@ -39,23 +39,30 @@
  *
  */
 
-#ifndef _OLSRD_TXTINFO_PRINTERS_H
-#define _OLSRD_TXTINFO_PRINTERS_H
+#ifndef _OLSRD_INFO_TYPES_H
+#define _OLSRD_INFO_TYPES_H
 
 #include <stdbool.h>
 
 #include "common/autobuf.h"
 
-void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop);
-void ipc_print_links(struct autobuf *abuf);
-void ipc_print_routes(struct autobuf *abuf);
-void ipc_print_topology(struct autobuf *abuf);
-void ipc_print_hna(struct autobuf *abuf);
-void ipc_print_mid(struct autobuf *abuf);
-void ipc_print_gateways(struct autobuf *abuf);
-void ipc_print_sgw(struct autobuf *abuf);
-void ipc_print_version(struct autobuf *abuf);
-void ipc_print_olsrd_conf(struct autobuf *abuf);
-void ipc_print_interfaces(struct autobuf *abuf);
+typedef void (*printer_neighbors)(struct autobuf *abuf, bool list_2hop);
+typedef void (*printer_generic)(struct autobuf *abuf);
 
-#endif /* _OLSRD_TXTINFO_PRINTERS_H */
+typedef struct {
+    printer_neighbors neighbors;
+    printer_generic links;
+    printer_generic routes;
+    printer_generic topology;
+    printer_generic hna;
+    printer_generic mid;
+    printer_generic gateways;
+    printer_generic sgw;
+    printer_generic version;
+    printer_generic olsrd_conf;
+    printer_generic interfaces;
+    printer_generic config;
+    printer_generic plugins;
+} printer_functions_t;
+
+#endif /* _OLSRD_INFO_TYPES_H */
diff --git a/lib/txtinfo/src/olsrd_txtinfo.c b/lib/txtinfo/src/olsrd_txtinfo.c
index 1c4b76a..e79b5cc 100644
--- a/lib/txtinfo/src/olsrd_txtinfo.c
+++ b/lib/txtinfo/src/olsrd_txtinfo.c
@@ -51,6 +51,7 @@
 #include "olsr.h"
 #include "scheduler.h"
 #include "../../info/http_headers.h"
+#include "../../info/info_types.h"
 #include "txtinfo_printers.h"
 
 #include "olsrd_txtinfo.h"
@@ -114,6 +115,23 @@ static int outbuffer_count = 0;
 
 static struct timer_entry *writetimer_entry;
 
+static printer_functions_t printer_functions = { //
+    //
+        .neighbors = &ipc_print_neighbors, //
+        .links = &ipc_print_links, //
+        .routes = &ipc_print_routes, //
+        .topology = &ipc_print_topology, //
+        .hna = &ipc_print_hna, //
+        .mid = &ipc_print_mid, //
+        .gateways = &ipc_print_gateways, //
+        .sgw = &ipc_print_sgw, //
+        .version = &ipc_print_version, //
+        .olsrd_conf = &ipc_print_olsrd_conf, //
+        .interfaces = &ipc_print_interfaces, //
+        .config = NULL, //
+        .plugins = NULL //
+    };
+
 static void determine_action(unsigned int *send_what, char *requ) {
   if (strstr(requ, "/con"))
     *send_what |= SIW_OLSRD_CONF;
@@ -419,37 +437,35 @@ static void send_info(unsigned int send_what, int the_socket) {
 
   // only add if normal format
   if (send_what & SIW_ALL) {
-    if (send_what & SIW_LINKS)
-      ipc_print_links(&abuf);
-    if (send_what & SIW_NEIGHBORS)
-      ipc_print_neighbors(&abuf, false);
-    if (send_what & SIW_TOPOLOGY)
-      ipc_print_topology(&abuf);
-    if (send_what & SIW_HNA)
-      ipc_print_hna(&abuf);
-    if (send_what & SIW_SGW)
-      ipc_print_sgw(&abuf);
-    if (send_what & SIW_MID)
-      ipc_print_mid(&abuf);
-    if (send_what & SIW_ROUTES)
-      ipc_print_routes(&abuf);
-    if (send_what & SIW_GATEWAYS)
-      ipc_print_gateways(&abuf);
-    if (send_what & SIW_CONFIG) {
-      /* not supported */
-    }
-    if (send_what & SIW_INTERFACES)
-      ipc_print_interfaces(&abuf);
-    if (send_what & SIW_2HOP)
-      ipc_print_neighbors(&abuf, true);
-    if (send_what & SIW_VERSION)
-      ipc_print_version(&abuf);
-    if (send_what & SIW_PLUGINS) {
-      /* not supported */
-    }
-  } else if (send_what & SIW_OLSRD_CONF) {
+    if ((send_what & SIW_LINKS) && printer_functions.links)
+      (*printer_functions.links)(&abuf);
+    if ((send_what & SIW_NEIGHBORS) && printer_functions.neighbors)
+      (*printer_functions.neighbors)(&abuf, false);
+    if ((send_what & SIW_TOPOLOGY) && printer_functions.topology)
+      (*printer_functions.topology)(&abuf);
+    if ((send_what & SIW_HNA) && printer_functions.hna)
+      (*printer_functions.hna)(&abuf);
+    if ((send_what & SIW_SGW) && printer_functions.sgw)
+      (*printer_functions.sgw)(&abuf);
+    if ((send_what & SIW_MID) && printer_functions.mid)
+      (*printer_functions.mid)(&abuf);
+    if ((send_what & SIW_ROUTES) && printer_functions.routes)
+      (*printer_functions.routes)(&abuf);
+    if ((send_what & SIW_GATEWAYS) && printer_functions.gateways)
+      (*printer_functions.gateways)(&abuf);
+    if ((send_what & SIW_CONFIG) && printer_functions.config)
+      (*printer_functions.config)(&abuf);
+    if ((send_what & SIW_INTERFACES) && printer_functions.interfaces)
+      (*printer_functions.interfaces)(&abuf);
+    if ((send_what & SIW_2HOP) && printer_functions.neighbors)
+      (*printer_functions.neighbors)(&abuf, true);
+    if ((send_what & SIW_VERSION) && printer_functions.version)
+      (*printer_functions.version)(&abuf);
+    if ((send_what & SIW_PLUGINS) && printer_functions.plugins)
+      (*printer_functions.plugins)(&abuf);
+  } else if ((send_what & SIW_OLSRD_CONF) && printer_functions.olsrd_conf) {
     /* this outputs the olsrd.conf text directly, not normal format */
-    ipc_print_olsrd_conf(&abuf);
+    (*printer_functions.olsrd_conf)(&abuf);
   }
 
   if (http_headers) {
-- 
2.5.0




More information about the Olsr-dev mailing list