txtinfo: use a table of function pointers in send_info
authorFerry Huberts <ferry.huberts@pelagic.nl>
Tue, 1 Dec 2015 08:53:38 +0000 (09:53 +0100)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Wed, 2 Dec 2015 17:41:13 +0000 (18:41 +0100)
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
lib/info/info_types.h [new file with mode: 0644]
lib/txtinfo/src/olsrd_txtinfo.c

diff --git a/lib/info/info_types.h b/lib/info/info_types.h
new file mode 100644 (file)
index 0000000..4444721
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * The olsr.org Optimized Link-State Routing daemon(olsrd)
+ * Copyright (c) 2004, Andreas Tonnesen(andreto@olsr.org)
+ *                     includes code by Bruno Randolf
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in
+ *   the documentation and/or other materials provided with the
+ *   distribution.
+ * * Neither the name of olsr.org, olsrd nor the names of its
+ *   contributors may be used to endorse or promote products derived
+ *   from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Visit http://www.olsr.org for more information.
+ *
+ * If you find this software useful feel free to make a donation
+ * to the project. For more information see the website or contact
+ * the copyright holders.
+ *
+ */
+
+#ifndef _OLSRD_INFO_TYPES_H
+#define _OLSRD_INFO_TYPES_H
+
+#include <stdbool.h>
+
+#include "common/autobuf.h"
+
+typedef void (*printer_neighbors)(struct autobuf *abuf, bool list_2hop);
+typedef void (*printer_generic)(struct autobuf *abuf);
+
+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 */
index 1c4b76a..e79b5cc 100644 (file)
@@ -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) {