Bläddra i källkod

cfg_rpc: cfg.set and cfg.set_delayed commands added

cfg.set is a shortcut to cfg.set_now_int and cfg.set_now_string
depending on the parameter type, i.e the syntax is:

cfg.set <group> <var> <int/string value>
or
cfg.set <group>[id] <var> <int/string value>

Similarly, cfg.set_delayed is a shortcut to cfg.set_delayed_int/string
Miklos Tirpak 15 år sedan
förälder
incheckning
cd8c071951
3 ändrade filer med 92 tillägg och 0 borttagningar
  1. 12 0
      modules/cfg_rpc/README
  2. 58 0
      modules/cfg_rpc/cfg_rpc.c
  3. 22 0
      modules/cfg_rpc/doc/rpc.xml

+ 12 - 0
modules/cfg_rpc/README

@@ -29,6 +29,12 @@ Miklos Tirpak
        commit the change immediately. The function accepts three
        parameters: group name, variable name, string value. The group name
        can optionally contain the group instance id, for example foo[5].
+     * cfg.set - Set the value of a configuration variable and commit the
+       change immediately. This is a wrapper command for cfg.set_now_int
+       and cfg.set_now_string depending on the type of the value provided.
+       The function accepts three parameters: group name, variable name,
+       int/string value. The group name can optionally contain the group
+       instance id, for example foo[5].
      * cfg.set_delayed_int - Prepare the change of a configuration
        variable, but does not commit the new value yet. The function
        accepts three parameters: group name, variable name, integer value.
@@ -39,6 +45,12 @@ Miklos Tirpak
        accepts three parameters: group name, variable name, string value.
        The group name can optionally contain the group instance id, for
        example foo[5].
+     * cfg.set_delayed - Prepare the change of a configuration variable,
+       but does not commit the new value yet. This is a wrapper command
+       for cfg.set_delayed_int and cfg.set_delayed_string depending on the
+       type of the value provided. The function accepts three parameters:
+       group name, variable name, int/string value. The group name can
+       optionally contain the group instance id, for example foo[5].
      * cfg.commit - Commit the previously prepared configuration changes.
        The function does not have any parameters.
      * cfg.rollback - Drop the prepared configuration changes. The

+ 58 - 0
modules/cfg_rpc/cfg_rpc.c

@@ -128,6 +128,34 @@ static void rpc_set_now_string(rpc_t* rpc, void* c)
 	}
 }
 
+static void rpc_set(rpc_t* rpc, void* c)
+{
+	str	group, var;
+	int	i, err;
+	char	*ch;
+	unsigned int	*group_id;
+
+	if (rpc->scan(c, "SS", &group, &var) < 2)
+		return;
+
+	if (get_group_id(&group, &group_id)) {
+		rpc->fault(c, 400, "Wrong group syntax. Use either \"group\", or \"group[id]\"");
+		return;
+	}
+
+	if (rpc->scan(c, "d", &i) == 1)
+		err = cfg_set_now_int(ctx, &group, group_id, &var, i);
+	else if (rpc->scan(c, "s", &ch) == 1)
+		err = cfg_set_now_string(ctx, &group, group_id, &var, ch);
+	else
+		return; /* error */
+
+	if (err) {
+		rpc->fault(c, 400, "Failed to set the variable");
+		return;
+	}
+}
+
 static const char* rpc_set_delayed_doc[2] = {
         "Prepare the change of a configuration variable, but does not commit the new value yet",
         0
@@ -173,6 +201,34 @@ static void rpc_set_delayed_string(rpc_t* rpc, void* c)
 	}
 }
 
+static void rpc_set_delayed(rpc_t* rpc, void* c)
+{
+	str	group, var;
+	int	i, err;
+	char	*ch;
+	unsigned int	*group_id;
+
+	if (rpc->scan(c, "SS", &group, &var) < 2)
+		return;
+
+	if (get_group_id(&group, &group_id)) {
+		rpc->fault(c, 400, "Wrong group syntax. Use either \"group\", or \"group[id]\"");
+		return;
+	}
+
+	if (rpc->scan(c, "d", &i) == 1)
+		err = cfg_set_delayed_int(ctx, &group, group_id, &var, i);
+	else if (rpc->scan(c, "s", &ch) == 1)
+		err = cfg_set_delayed_string(ctx, &group, group_id, &var, ch);
+	else
+		return; /* error */
+
+	if (err) {
+		rpc->fault(c, 400, "Failed to set the variable");
+		return;
+	}
+}
+
 static const char* rpc_commit_doc[2] = {
         "Commit the previously prepared configuration changes",
         0
@@ -419,8 +475,10 @@ static void rpc_del_group_inst(rpc_t* rpc, void* c)
 }
 
 static rpc_export_t rpc_calls[] = {
+	{"cfg.set",		rpc_set,		rpc_set_now_doc,	0},
 	{"cfg.set_now_int",	rpc_set_now_int,	rpc_set_now_doc,	0},
 	{"cfg.set_now_string",	rpc_set_now_string,	rpc_set_now_doc,	0},
+	{"cfg.set_delayed",	rpc_set_delayed,	rpc_set_delayed_doc,	0},
 	{"cfg.set_delayed_int",	rpc_set_delayed_int,	rpc_set_delayed_doc,	0},
 	{"cfg.set_delayed_string",	rpc_set_delayed_string,	rpc_set_delayed_doc,	0},
 	{"cfg.commit",		rpc_commit,		rpc_commit_doc,		0},

+ 22 - 0
modules/cfg_rpc/doc/rpc.xml

@@ -28,6 +28,17 @@
 		group instance id, for example foo[5].
 	    </para>
 	</listitem>
+	<listitem>
+	    <para>
+		<emphasis>cfg.set</emphasis> - Set the value of
+		a configuration variable and commit the change immediately.
+		This is a wrapper command for cfg.set_now_int and cfg.set_now_string
+		depending on the type of the value provided.
+		The function accepts three parameters: group name, variable
+		name, int/string value. The group name can optionally contain the
+		group instance id, for example foo[5].
+	    </para>
+	</listitem>
 	<listitem>
 	    <para>
 		<emphasis>cfg.set_delayed_int</emphasis> - Prepare the change of
@@ -46,6 +57,17 @@
 		group instance id, for example foo[5].
 	    </para>
 	</listitem>
+	<listitem>
+	    <para>
+		<emphasis>cfg.set_delayed</emphasis> - Prepare the change of
+		a configuration variable, but does not commit the new value yet.
+		This is a wrapper command for cfg.set_delayed_int and cfg.set_delayed_string
+		depending on the type of the value provided.
+		The function accepts three parameters: group name, variable
+		name, int/string value. The group name can optionally contain the
+		group instance id, for example foo[5].
+	    </para>
+	</listitem>
 	<listitem>
 	    <para>
 		<emphasis>cfg.commit</emphasis> - Commit the previously