Browse code

pv Add RPC commands shvGet and shvSet to manipulate and list shared variables

Olle E. Johansson authored on 02/03/2013 14:53:08
Showing 5 changed files
... ...
@@ -45,6 +45,11 @@ Daniel-Constantin Mierla
45 45
               5.1. shv_set
46 46
               5.2. shv_get
47 47
 
48
+        6. RPC Commands
49
+
50
+              6.1. pv.shvSet
51
+              6.2. pv.shvGet
52
+
48 53
    List of Examples
49 54
 
50 55
    1.1. shvset parameter usage
... ...
@@ -87,6 +92,11 @@ Chapter 1. Admin Guide
87 92
         5.1. shv_set
88 93
         5.2. shv_get
89 94
 
95
+   6. RPC Commands
96
+
97
+        6.1. pv.shvSet
98
+        6.2. pv.shvGet
99
+
90 100
 1. Overview
91 101
 
92 102
    This module collects the core pseudo-variables that can be used in
... ...
@@ -207,7 +217,7 @@ if(pv_isset("$avp("s:x")"))
207 217
 pv_unset("$avp("s:x")");
208 218
 ...
209 219
 
210
-4.3. is_int(pvar)
220
+4.3.  is_int(pvar)
211 221
 
212 222
    Function checks if pvar argument contains integer value and returns 1
213 223
    if it does and -1 otherwise.
... ...
@@ -221,7 +231,7 @@ if (is_int("$var(foo)")) {
221 231
 }
222 232
 ...
223 233
 
224
-4.4. typeof(pvar, vtype)
234
+4.4.  typeof(pvar, vtype)
225 235
 
226 236
    Returns true if the type of pseudo-variable matches the second
227 237
    parameter. The second parameter can be: 'int' - type is integer; 'str'
... ...
@@ -236,7 +246,7 @@ if (typeof("$var(foo)", "str")) {
236 246
 }
237 247
 ...
238 248
 
239
-4.5. not_empty(pvar)
249
+4.5.  not_empty(pvar)
240 250
 
241 251
    Returns true if the pseudo-variables has the type string and is not
242 252
    empty value.
... ...
@@ -296,3 +306,28 @@ $ kamctl fifo shv_set debug int 0
296 306
 $ kamctl fifo shv_get debug
297 307
 $ kamctl fifo shv_get
298 308
 ...
309
+
310
+6. RPC Commands
311
+
312
+   6.1. pv.shvSet
313
+   6.2. pv.shvGet
314
+
315
+6.1. pv.shvSet
316
+
317
+   Set the value of a shared variable ($shv(name)).
318
+
319
+   Parameters:
320
+     * _name_: shared variable name
321
+     * _type_: type of the value
322
+          + "int": integer value
323
+          + "str": string value
324
+     * _value_: value to be set
325
+
326
+6.2. pv.shvGet
327
+
328
+   Get the value of a shared variable ($shv(name)).
329
+
330
+   Parameters:
331
+     * _name_: shared variable name
332
+
333
+   If no name is given, all shared variables are listed
... ...
@@ -336,5 +336,39 @@ $ &ctltool; fifo shv_get
336 336
 			</example>
337 337
 		</section>
338 338
 	</section>
339
+	<section>
340
+        <title>RPC Commands</title>
341
+		<section>
342
+			<title><function moreinfo="none">pv.shvSet</function></title>
343
+			<para>
344
+				Set the value of a shared variable ($shv(name)).
345
+			</para>
346
+		<para>Parameters:</para>
347
+		<itemizedlist>
348
+			<listitem><para>_name_: shared variable name</para></listitem>
349
+			
350
+			<listitem><para>_type_: type of the value</para>
351
+			      <itemizedlist>
352
+	    <listitem><para> <quote>int</quote>: integer value </para></listitem> 
353
+		<listitem><para> <quote>str</quote>: string value </para></listitem>	
354
+				  </itemizedlist>
355
+			</listitem>	  
356
+
357
+			<listitem><para>_value_: value to be set</para></listitem>
358
+		</itemizedlist>
359
+		</section>
360
+		<section>
361
+			<title><function moreinfo="none">pv.shvGet</function></title>
362
+			<para>
363
+				Get the value of a shared variable ($shv(name)).
364
+			</para>
365
+		<para>Parameters:</para>
366
+		<itemizedlist>
367
+			<listitem><para>_name_: shared variable name</para></listitem>
368
+		</itemizedlist>
369
+		<para>If no name is given, all shared variables are listed</para>
370
+		</section>
371
+	</section>
372
+	
339 373
 </chapter>
340 374
 
... ...
@@ -28,6 +28,9 @@
28 28
 #include "../../pvar.h"
29 29
 #include "../../mod_fix.h"
30 30
 #include "../../lib/kmi/mi.h"
31
+#include "../../rpc.h"
32
+#include "../../rpc_lookup.h"
33
+
31 34
 
32 35
 #include "pv_branch.h"
33 36
 #include "pv_core.h"
... ...
@@ -448,6 +451,7 @@ static int pv_unset(struct sip_msg* msg, char* pvid, char *foo);
448 451
 static int is_int(struct sip_msg* msg, char* pvar, char* s2);
449 452
 static int pv_typeof(sip_msg_t *msg, char *pv, char *t);
450 453
 static int pv_not_empty(sip_msg_t *msg, char *pv, char *s2);
454
+static int pv_init_rpc(void);
451 455
 
452 456
 static cmd_export_t cmds[]={
453 457
 	{"pv_isset",  (cmd_function)pv_isset,  1, fixup_pvar_null, 0, 
... ...
@@ -495,6 +499,11 @@ static int mod_init(void)
495 499
 		LM_ERR("failed to register MI commands\n");
496 500
 		return -1;
497 501
 	}
502
+	if(pv_init_rpc()!=0)
503
+        {
504
+                LM_ERR("failed to register RPC commands\n");
505
+                return -1;
506
+        }
498 507
 
499 508
 	return 0;
500 509
 }
... ...
@@ -629,3 +638,29 @@ static int is_int(struct sip_msg* msg, char* pvar, char* s2)
629 638
 
630 639
 	return -1;
631 640
 }
641
+
642
+static const char* rpc_shv_set_doc[2] = {
643
+	"Set a shared variable (args: name type value)",
644
+	0
645
+};
646
+
647
+static const char* rpc_shv_get_doc[2] = {
648
+	"Get the value of a shared variable. If no argument, dumps all",
649
+	0
650
+};
651
+
652
+rpc_export_t pv_rpc[] = {
653
+	{"pv.shvSet", rpc_shv_set, rpc_shv_set_doc, 0},
654
+	{"pv.shvGet", rpc_shv_get, rpc_shv_get_doc, 0},
655
+	{0, 0, 0, 0}
656
+};
657
+
658
+static int pv_init_rpc(void)
659
+{
660
+	if (rpc_register_array(pv_rpc)!=0)
661
+	{
662
+		LM_ERR("failed to register RPC commands\n");
663
+		return -1;
664
+	}
665
+	return 0;
666
+}
... ...
@@ -2,6 +2,7 @@
2 2
  * $Id$
3 3
  *
4 4
  * Copyright (C) 2007 Elena-Ramona Modroiu
5
+ * Copyright (C) 2013 Olle E. Johansson
5 6
  *
6 7
  * This file is part of Kamailio, a free SIP server.
7 8
  *
... ...
@@ -615,6 +616,152 @@ error:
615 616
 	return NULL;
616 617
 }
617 618
 
619
+
620
+void rpc_shv_get(rpc_t* rpc, void* c)
621
+{
622
+	str varname;
623
+	int allvars = 0;
624
+	sh_var_t *shv = NULL;
625
+	void* th;
626
+        void* ih;
627
+        void* vh;
628
+
629
+	if (rpc->scan(c, "S", &varname) != 1) {
630
+		allvars = 1;
631
+        }
632
+
633
+	if (!allvars) {
634
+		/* Get one variable value */
635
+		shv = get_shvar_by_name(&varname);
636
+		if(shv==NULL) {
637
+			rpc->fault(c, 404, "Variable not found");
638
+			return;
639
+		}
640
+		if (rpc->add(c, "{",  &ih) < 0)
641
+        	{
642
+               		rpc->fault(c, 500, "Internal error creating rpc");
643
+                	return;
644
+        	}
645
+		
646
+		lock_shvar(shv);
647
+		if(shv->v.flags&VAR_VAL_STR)
648
+		{
649
+			if(rpc->struct_add(ih, "sss", "name", varname.s, "type", "string", "value", shv->v.value.s.s) < 0)
650
+			{
651
+				rpc->fault(c, 500, "Internal error creating rpc data (str)");
652
+				unlock_shvar(shv);
653
+				return;
654
+			}
655
+		} else {
656
+			if(rpc->struct_add(ih, "ssd", "name", varname.s, "type", "int", "value", shv->v.value.n) < 0)
657
+			{
658
+				rpc->fault(c, 500, "Internal error creating rpc data (int)");
659
+				unlock_shvar(shv);
660
+				return;
661
+			}
662
+		}
663
+		unlock_shvar(shv);
664
+
665
+		return;
666
+	}
667
+	if (rpc->add(c, "{", &th) < 0)
668
+       	{
669
+         	rpc->fault(c, 500, "Internal error creating rpc");
670
+               	return;
671
+       	}
672
+
673
+	if(rpc->struct_add(th, "{", "items", &ih) < 0)
674
+               {
675
+                         rpc->fault(c, 500, "Internal error creating rpc th");
676
+                         return;
677
+               }
678
+
679
+	for(shv=sh_vars; shv; shv=shv->next)
680
+	{
681
+		lock_shvar(shv);
682
+		if(rpc->struct_add(ih, "{", "shv", &vh) < 0)
683
+               {
684
+                         rpc->fault(c, 500, "Internal error creating rpc th");
685
+                         return;
686
+               }
687
+		if(shv->v.flags&VAR_VAL_STR)
688
+		{
689
+			if(rpc->struct_add(vh, "sss", "name", shv->name.s, "type", "string", "value", shv->v.value.s.s) < 0)
690
+			{
691
+				rpc->fault(c, 500, "Internal error creating rpc data");
692
+				unlock_shvar(shv);
693
+				return;
694
+			}
695
+		} else {
696
+			if(rpc->struct_add(vh, "ssd", "name", shv->name.s, "type", "int", "value", shv->v.value.n) < 0)
697
+			{
698
+				rpc->fault(c, 500, "Internal error creating rpc data");
699
+				unlock_shvar(shv);
700
+				return;
701
+			}
702
+		}
703
+		unlock_shvar(shv);
704
+	}
705
+
706
+	return ;
707
+}
708
+
709
+void rpc_shv_set(rpc_t* rpc, void* c)
710
+{
711
+	str varname, type, value;
712
+	int ival = 0;
713
+	int_str isv;
714
+	sh_var_t *shv = NULL;
715
+	int flags = 0;
716
+	LM_DBG("Entering SHV_set\n");
717
+
718
+	if (rpc->scan(c, "S", &varname) != 1) {
719
+		rpc->fault(c, 500, "Missing parameter varname (Parameters: varname type value)");
720
+		return;
721
+        }
722
+	LM_DBG("SHV_set Varname %.*s \n", varname.len, varname.s);
723
+	if (rpc->scan(c, "S", &type) != 1) {
724
+		rpc->fault(c, 500, "Missing parameter type (Parameters: varname type value)");
725
+		return;
726
+        }
727
+	if (strcasecmp(type.s, "int") == 0 ) {
728
+		if (rpc->scan(c, "d", &ival) != 1) {
729
+			rpc->fault(c, 500, "Missing integer parameter value (Parameters: varname type value)");
730
+			return;
731
+        	}
732
+		isv.n = ival;
733
+	} else  if (strcasecmp(type.s, "str") == 0 ) {
734
+		/* String value */
735
+		if (rpc->scan(c, "S", &value) != 1) {
736
+			rpc->fault(c, 500, "Missing parameter value (Parameters: varname type value)");
737
+			return;
738
+        	}
739
+		isv.s = value;
740
+		flags = VAR_VAL_STR;
741
+	} else {
742
+		rpc->fault(c, 500, "Unknown parameter type (Types: int or str)");
743
+		return;
744
+	}
745
+
746
+	shv = get_shvar_by_name(&varname);
747
+	if(shv==NULL) {
748
+		rpc->fault(c, 404, "Variable not found");
749
+		return;
750
+	}
751
+		
752
+	lock_shvar(shv);
753
+	if(set_shvar_value(shv, &isv, flags)==NULL)
754
+	{
755
+		rpc->fault(c, 500, "Cannot set shared variable value");
756
+		LM_ERR("cannot set shv value\n");
757
+	} else {
758
+		rpc->printf(c, "Ok. Variable set to new value.");
759
+	}
760
+
761
+	unlock_shvar(shv);
762
+	return;
763
+}
764
+
618 765
 int param_set_xvar( modparam_t type, void* val, int mode)
619 766
 {
620 767
 	str s;
... ...
@@ -69,5 +69,8 @@ struct mi_root* mi_shvar_set(struct mi_root* cmd_tree, void* param);
69 69
 int param_set_var( modparam_t type, void* val);
70 70
 int param_set_shvar( modparam_t type, void* val);
71 71
 
72
+void rpc_shv_get(rpc_t* rpc, void* c);
73
+void rpc_shv_set(rpc_t* rpc, void* c);
74
+
72 75
 #endif
73 76