Code

grpc plugin: Use std::vector to store listener configs.
authorSebastian Harl <sh@tokkee.org>
Fri, 6 May 2016 22:36:40 +0000 (00:36 +0200)
committerSebastian Harl <sh@tokkee.org>
Mon, 30 May 2016 21:44:19 +0000 (23:44 +0200)
src/grpc.cc

index 4e47098629364b4b06806fa8fc808a5be9e83005..4e10783d6d3984849835af987daa5eae5e8e4dc6 100644 (file)
@@ -27,6 +27,8 @@
 #include <grpc++/grpc++.h>
 #include <google/protobuf/util/time_util.h>
 
+#include <vector>
+
 #include "collectd.grpc.pb.h"
 
 extern "C" {
@@ -40,14 +42,6 @@ extern "C" {
 #include "plugin.h"
 
 #include "daemon/utils_cache.h"
-
-       typedef struct {
-               char *addr;
-               char *port;
-       } listener_t;
-
-       static listener_t *listeners;
-       static size_t listeners_num;
 }
 
 using collectd::Collectd;
@@ -59,6 +53,17 @@ using collectd::QueryValuesReply;
 
 using google::protobuf::util::TimeUtil;
 
+/*
+ * private types
+ */
+
+struct Listener {
+       grpc::string addr;
+       grpc::string port;
+};
+static std::vector<Listener> listeners;
+static grpc::string default_addr("0.0.0.0:50051");
+
 /*
  * helper functions
  */
@@ -390,17 +395,13 @@ public:
 
                grpc::ServerBuilder builder;
 
-               if (!listeners_num) {
-                       std::string default_addr("0.0.0.0:50051");
+               if (listeners.empty()) {
                        builder.AddListeningPort(default_addr, auth);
                        INFO("grpc: Listening on %s", default_addr.c_str());
                }
                else {
-                       size_t i;
-                       for (i = 0; i < listeners_num; i++) {
-                               auto l = listeners[i];
-                               std::string addr(l.addr);
-                               addr += std::string(":") + std::string(l.port);
+                       for (auto l : listeners) {
+                               grpc::string addr = l.addr + ":" + l.port;
                                builder.AddListeningPort(addr, auth);
                                INFO("grpc: Listening on %s", addr.c_str());
                        }
@@ -466,9 +467,6 @@ extern "C" {
 
        static int c_grpc_config_listen(oconfig_item_t *ci)
        {
-               listener_t *listener;
-               int i;
-
                if ((ci->values_num != 2)
                                || (ci->values[0].type != OCONFIG_TYPE_STRING)
                                || (ci->values[1].type != OCONFIG_TYPE_STRING)) {
@@ -477,20 +475,12 @@ extern "C" {
                        return -1;
                }
 
-               listener = (listener_t *)realloc(listeners,
-                               (listeners_num + 1) * sizeof(*listeners));
-               if (!listener) {
-                       ERROR("grpc: Failed to allocate listeners");
-                       return -1;
-               }
-               listeners = listener;
-               listener = listeners + listeners_num;
-               listeners_num++;
-
-               listener->addr = strdup(ci->values[0].value.string);
-               listener->port = strdup(ci->values[1].value.string);
+               auto listener = Listener();
+               listener.addr = grpc::string(ci->values[0].value.string);
+               listener.port = grpc::string(ci->values[1].value.string);
+               listeners.push_back(listener);
 
-               for (i = 0; i < ci->children_num; i++) {
+               for (int i = 0; i < ci->children_num; i++) {
                        oconfig_item_t *child = ci->children + i;
                        WARNING("grpc: Option `%s` not allowed in <%s> block.",
                                        child->key, ci->key);