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

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


From: Ferry Huberts <(spam-protected)>

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 lib/jsoninfo/src/olsrd_jsoninfo.c | 74 ++++++++++++++++++++++++---------------
 1 file changed, 46 insertions(+), 28 deletions(-)

diff --git a/lib/jsoninfo/src/olsrd_jsoninfo.c b/lib/jsoninfo/src/olsrd_jsoninfo.c
index 190ed8d..29dfff5 100644
--- a/lib/jsoninfo/src/olsrd_jsoninfo.c
+++ b/lib/jsoninfo/src/olsrd_jsoninfo.c
@@ -52,6 +52,7 @@
 #include "olsr.h"
 #include "scheduler.h"
 #include "../../info/http_headers.h"
+#include "../../info/info_types.h"
 #include "jsoninfo_printers.h"
 #include "olsrd_jsoninfo_helpers.h"
 #include "olsrd_jsoninfo.h"
@@ -115,6 +116,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 = &ipc_print_config, //
+        .plugins = &ipc_print_plugins //
+    };
+
 static void determine_action(unsigned int *send_what, char *requ) {
   if (strstr(requ, "/olsrd.conf"))
     *send_what |= SIW_OLSRD_CONF;
@@ -426,38 +444,38 @@ static void send_info(unsigned int send_what, int the_socket) {
     if (*uuid)
       abuf_json_string(&abuf, "uuid", uuid);
 
-    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)
-      ipc_print_config(&abuf);
-    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)
-      ipc_print_plugins(&abuf);
+    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);
 
     abuf_json_mark_output(false, &abuf);
     abuf_puts(&abuf, "\n");
-  } else if (send_what & SIW_OLSRD_CONF) {
+  } 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