tunnel-manager: Make the tunnel enabling policy configurable 42/31442/1
authorTanu Kaskinen <tanu.kaskinen@linux.intel.com>
Wed, 3 Dec 2014 12:05:44 +0000 (14:05 +0200)
committerTanu Kaskinen <tanu.kaskinen@linux.intel.com>
Thu, 4 Dec 2014 17:23:47 +0000 (19:23 +0200)
There's no real configurability yet, because only one condition is
supported. Support for more conditions will be added later.

The syntax of the configuration option pretends to conform to some
programming language, but there's no real parser for the language.
A proper parser may be implemented later, but for now the option
values are parsed simply by comparing to a list of predefined string
constants.

Change-Id: I57bbb0cd2b91691415be78f9732aef6b3bebde56

src/modules/tunnel-manager/remote-device.c
src/modules/tunnel-manager/tunnel-manager-config.c
src/modules/tunnel-manager/tunnel-manager-config.h
src/modules/tunnel-manager/tunnel-manager.c
src/modules/tunnel-manager/tunnel-manager.h

index 859800b..137f1e1 100644 (file)
@@ -343,6 +343,13 @@ static void apply_tunnel_enabled_policy(pa_tunnel_manager_remote_device *device)
 
     pa_assert(device);
 
-    enabled = !device->is_monitor;
+    enabled = device->tunnel_enabled;
+
+    switch (device->server->manager->remote_device_tunnel_enabled_condition) {
+        case PA_TUNNEL_MANAGER_REMOTE_DEVICE_TUNNEL_ENABLED_CONDITION_NOT_MONITOR:
+            enabled = !device->is_monitor;
+            break;
+    }
+
     set_tunnel_enabled(device, enabled);
 }
index 55c9fcd..f89f0fd 100644 (file)
@@ -31,6 +31,7 @@
 #include <pulsecore/core-util.h>
 #include <pulsecore/namereg.h>
 
+#define GENERAL_SECTION_NAME "General"
 #define REMOTE_SERVER_SECTION_NAME "RemoteServer"
 #define REMOTE_SERVER_SECTION_PREFIX REMOTE_SERVER_SECTION_NAME " "
 
@@ -100,12 +101,17 @@ static int parse_config_value(pa_config_parser_state *state) {
 
     manager_config = state->userdata;
 
-    if (!state->section) {
-        pa_log("[%s:%u] \"%s\" is not valid in the General section.", state->filename, state->lineno, state->lvalue);
-        return 0;
-    }
+    if (!state->section || pa_streq(state->section, GENERAL_SECTION_NAME)) {
+        if (pa_streq(state->lvalue, "remote_device_tunnel_enabled_condition")) {
+            if (manager_config->remote_device_tunnel_enabled_condition)
+                config_value_free(manager_config->remote_device_tunnel_enabled_condition);
 
-    if (pa_startswith(state->section, REMOTE_SERVER_SECTION_PREFIX)) {
+            manager_config->remote_device_tunnel_enabled_condition = config_value_new(state->rvalue, state->filename,
+                                                                                      state->lineno);
+        } else
+            pa_log("[%s:%u] \"%s\" is not valid in the " GENERAL_SECTION_NAME " section.", state->filename,
+                   state->lineno, state->lvalue);
+    } else if (pa_startswith(state->section, REMOTE_SERVER_SECTION_PREFIX)) {
         int r;
         pa_tunnel_manager_remote_server_config *server_config;
 
@@ -166,6 +172,9 @@ void pa_tunnel_manager_config_free(pa_tunnel_manager_config *manager_config) {
         pa_hashmap_free(manager_config->remote_servers);
     }
 
+    if (manager_config->remote_device_tunnel_enabled_condition)
+        config_value_free(manager_config->remote_device_tunnel_enabled_condition);
+
     pa_xfree(manager_config);
 }
 
index 8eac0e8..1a476f5 100644 (file)
@@ -35,6 +35,7 @@ struct pa_tunnel_manager_config_value {
 };
 
 struct pa_tunnel_manager_config {
+    pa_tunnel_manager_config_value *remote_device_tunnel_enabled_condition;
     pa_hashmap *remote_servers; /* name -> pa_tunnel_manager_remote_server_config */
 };
 
index e391209..be90343 100644 (file)
 #include <pulsecore/core-util.h>
 #include <pulsecore/shared.h>
 
+const char *pa_tunnel_manager_remote_device_tunnel_enabled_condition_to_string(
+        pa_tunnel_manager_remote_device_tunnel_enabled_condition_t condition) {
+    switch (condition) {
+        case PA_TUNNEL_MANAGER_REMOTE_DEVICE_TUNNEL_ENABLED_CONDITION_NOT_MONITOR:
+            return "!device.is_monitor";
+    }
+
+    pa_assert_not_reached();
+}
+
+int pa_tunnel_manager_remote_device_tunnel_enabled_condition_from_string(
+        const char *str, pa_tunnel_manager_remote_device_tunnel_enabled_condition_t *_r) {
+    pa_tunnel_manager_remote_device_tunnel_enabled_condition_t condition;
+
+    pa_assert(str);
+    pa_assert(_r);
+
+    if (pa_streq(str, "!device.is_monitor"))
+        condition = PA_TUNNEL_MANAGER_REMOTE_DEVICE_TUNNEL_ENABLED_CONDITION_NOT_MONITOR;
+    else
+        return -PA_ERR_INVALID;
+
+    *_r = condition;
+    return 0;
+}
+
 static pa_tunnel_manager *tunnel_manager_new(pa_core *core) {
     pa_tunnel_manager *manager;
     pa_tunnel_manager_config *manager_config;
+    pa_tunnel_manager_config_value *config_value;
     pa_tunnel_manager_remote_server_config *server_config;
     void *state;
 
@@ -41,12 +68,28 @@ static pa_tunnel_manager *tunnel_manager_new(pa_core *core) {
 
     manager = pa_xnew0(pa_tunnel_manager, 1);
     manager->core = core;
+    manager->remote_device_tunnel_enabled_condition = PA_TUNNEL_MANAGER_REMOTE_DEVICE_TUNNEL_ENABLED_CONDITION_NOT_MONITOR;
     manager->remote_servers = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
     manager->refcnt = 1;
 
     manager_config = pa_tunnel_manager_config_new();
 
+    config_value = manager_config->remote_device_tunnel_enabled_condition;
+    if (config_value) {
+        int r;
+        pa_tunnel_manager_remote_device_tunnel_enabled_condition_t condition;
+
+        r = pa_tunnel_manager_remote_device_tunnel_enabled_condition_from_string(config_value->value, &condition);
+        if (r >= 0)
+            manager->remote_device_tunnel_enabled_condition = condition;
+        else
+            pa_log("[%s:%u] Invalid condition: \"%s\"", config_value->filename, config_value->lineno, config_value->value);
+    }
+
     pa_log_debug("Created the tunnel manager.");
+    pa_log_debug("    Remote device tunnel enabled condition: %s",
+                 pa_tunnel_manager_remote_device_tunnel_enabled_condition_to_string(
+                         manager->remote_device_tunnel_enabled_condition));
 
     PA_HASHMAP_FOREACH(server_config, manager_config->remote_servers, state)
         pa_tunnel_manager_remote_server_new(manager, server_config);
index fbddb35..75f567d 100644 (file)
 
 typedef struct pa_tunnel_manager pa_tunnel_manager;
 
+typedef enum {
+    PA_TUNNEL_MANAGER_REMOTE_DEVICE_TUNNEL_ENABLED_CONDITION_NOT_MONITOR,
+} pa_tunnel_manager_remote_device_tunnel_enabled_condition_t;
+
+const char *pa_tunnel_manager_remote_device_tunnel_enabled_condition_to_string(
+        pa_tunnel_manager_remote_device_tunnel_enabled_condition_t condition);
+int pa_tunnel_manager_remote_device_tunnel_enabled_condition_from_string(
+        const char *str, pa_tunnel_manager_remote_device_tunnel_enabled_condition_t *_r);
+
 struct pa_tunnel_manager {
     pa_core *core;
+    pa_tunnel_manager_remote_device_tunnel_enabled_condition_t remote_device_tunnel_enabled_condition;
     pa_hashmap *remote_servers; /* name -> pa_tunnel_manager_remote_server */
 
     unsigned refcnt;