Browse code

keepalive: Added callback to run on each destination response.

- This functionality it's just available when using api.h bindings. For exported functions no callback will be used, so this doesn't break cfg or rpc api.
- Modified add_destination function to provide this new callback as a parameter.

Nacho Garcia Segovia authored on 07/05/2020 21:50:10
Showing 8 changed files
... ...
@@ -38,7 +38,8 @@ typedef int ka_state;
38 38
 #define KA_STATE_DOWN 2
39 39
 
40 40
 typedef int (*ka_add_dest_f)(str *uri, str *owner, int flags,
41
-        int ping_interval, ka_statechanged_f callback, void *user_attr);
41
+        int ping_interval, ka_statechanged_f statechanged_clb, ka_response_f response_clb,
42
+        void *user_attr);
42 43
 typedef ka_state (*ka_dest_state_f)(str *uri);
43 44
 typedef int (*ka_del_destination_f)(str *uri, str *owner);
44 45
 typedef int (*ka_find_destination_f)(str *uri, str *owner,ka_dest_t **target,ka_dest_t **head);
... ...
@@ -47,4 +47,5 @@
47 47
     <xi:include href="keepalive_admin.xml"/>
48 48
     <xi:include href="keepalive_devel.xml"/>
49 49
 
50
+
50 51
 </book>
... ...
@@ -30,7 +30,7 @@
30 30
 
31 31
 	<section id="dev-add_destination">
32 32
 		<title>
33
-		<function moreinfo="none">add_destination(uri, owner, flags, ping_interval, [callback, [user_attr]])</function>
33
+		<function moreinfo="none">add_destination(uri, owner, flags, ping_interval, [statechanged_clb, response_clb, [user_attr]])</function>
34 34
 		</title>
35 35
 		<para>
36 36
 			This function registers a new destination to monitor. 
... ...
@@ -70,7 +70,7 @@
70 70
 		</listitem>
71 71
 		<listitem>
72 72
 			<para>
73
-				<emphasis>callback (ka_statechanged_f, optional)</emphasis> - callback function, executed on destination's state change.
73
+				<emphasis>statechanged_clb (ka_statechanged_f, optional)</emphasis> - callback function, executed on destination's state change.
74 74
 			</para>
75 75
 			<para>
76 76
 				The callback function is of type <code>void (*ka_statechanged_f)(str *uri, int state, void *user_attr);</code>. Use <emphasis>NULL</emphasis> to set no callback.
... ...
@@ -92,7 +92,18 @@
92 92
 		</listitem>
93 93
 		<listitem>
94 94
 			<para>
95
-				<emphasis>user_attr (void * pointer, optional)</emphasis> - If callback function is setup, this parameter will be forwarded to it, as last parameter. Use <emphasis>NULL</emphasis> to set no user_attr parameter.
95
+				<emphasis>response_clb (ka_response_f, optional)</emphasis> - callback function, executed on destination's response provided.
96
+			</para>
97
+			<para>
98
+				The callback function is of type <code> void (*ka_response_f)(str *uri, struct tmcb_params *ps, void *user_attr);</code>. Use <emphasis>NULL</emphasis> to set no callback.
99
+			</para>
100
+			<para>
101
+				ps is a pack structure with all params passed to callback function. Defined in t_hooks.h
102
+			</para>
103
+		</listitem>
104
+		<listitem>
105
+			<para>
106
+				<emphasis>user_attr (void * pointer, optional)</emphasis> - If any callback function is setup, this parameter will be forwarded to it (or both callbacks in both are defined), as last parameter. Use <emphasis>NULL</emphasis> to set no user_attr parameter.
96 107
 			</para>
97 108
 		</listitem>
98 109
 		</itemizedlist>
... ...
@@ -128,17 +139,18 @@ if (bind_keepalive( &amp;ka_api ) != 0) {
128 139
 }
129 140
 ...
130 141
 ...
131
-/* callback function */
132
-void my_callback(str uri, int state, void *user_attr) {
133
-
142
+/* callback function (on state changed) */
143
+void my_state_changed_clb(str uri, int state, void *user_attr) {
134 144
 	printf("%.*s new state is: %d\n", uri.len, uri.str, state)
135 145
 }
136 146
 
137
-/* register a new destination */
138
-str dest  = str_init("sip:192.168.10.21:5060");
139
-str owner = str_init("mymodule");
147
+/* callback function (on each response received) */
148
+void my_response_clb(str *uri, struct tmcb_params *ps, void *user_attr) {
149
+	printf("response [%d] from %.*s\n", ps->code, uri.len, uri.str)
150
+}
140 151
 
141
-if (ka_api.add_destination(dest, owner, 0, 60, my_callback, NULL) != 0) {
152
+if (ka_api.add_destination(dest, owner, 0, 60, my_state_changed_clb,
153
+				my_response_clb, NULL) != 0) {
142 154
     LM_ERR("can't add destination\n");
143 155
     goto error;
144 156
 }
... ...
@@ -31,6 +31,7 @@
31 31
 #include <time.h>
32 32
 #include "../../core/sr_module.h"
33 33
 #include "../../core/locking.h"
34
+#include "../tm/tm_load.h"
34 35
 
35 36
 #define KA_INACTIVE_DST 1 /*!< inactive destination */
36 37
 #define KA_TRYING_DST 2   /*!< temporary trying destination */
... ...
@@ -48,6 +49,8 @@ extern int ka_ping_interval;
48 49
 #define KA_PROBE_ONLYFLAGGED 3
49 50
 
50 51
 typedef void (*ka_statechanged_f)(str *uri, int state, void *user_attr);
52
+typedef void (*ka_response_f)(
53
+		str *uri, struct tmcb_params *ps, void *user_attr);
51 54
 
52 55
 
53 56
 typedef struct _ka_dest
... ...
@@ -62,6 +65,7 @@ typedef struct _ka_dest
62 65
 
63 66
 	void *user_attr;
64 67
 	ka_statechanged_f statechanged_clb;
68
+	ka_response_f response_clb;
65 69
 	struct socket_info *sock;
66 70
 	struct ip_addr ip_address; /*!< IP-Address of the entry */
67 71
 	unsigned short int port;   /*!< Port of the URI */
... ...
@@ -82,7 +86,8 @@ extern int ka_counter_del;
82 86
 ticks_t ka_check_timer(ticks_t ticks, struct timer_ln* tl, void* param);
83 87
 
84 88
 int ka_add_dest(str *uri, str *owner, int flags, int ping_interval,
85
-        ka_statechanged_f callback, void *user_attr);
89
+        ka_statechanged_f statechanged_clb, ka_response_f response_clb,
90
+        void *user_attr);
86 91
 int ka_destination_state(str *uri);
87 92
 int ka_str_copy(str *src, str *dest, char *prefix);
88 93
 int free_destination(ka_dest_t *dest) ;
... ...
@@ -63,7 +63,7 @@ int bind_keepalive(keepalive_api_t *api)
63 63
  * Add a new destination in keepalive pool
64 64
  */
65 65
 int ka_add_dest(str *uri, str *owner, int flags, int ping_interval,
66
-    ka_statechanged_f callback, void *user_attr)
66
+    ka_statechanged_f statechanged_clb, ka_response_f response_clb, void *user_attr)
67 67
 {
68 68
 	struct sip_uri _uri;
69 69
 	ka_dest_t *dest=0,*hollow=0;
... ...
@@ -104,7 +104,8 @@ int ka_add_dest(str *uri, str *owner, int flags, int ping_interval,
104 104
 		goto err;
105 105
 
106 106
 	dest->flags = flags;
107
-	dest->statechanged_clb = callback;
107
+	dest->statechanged_clb = statechanged_clb;
108
+	dest->response_clb = response_clb;
108 109
 	dest->user_attr = user_attr;
109 110
 
110 111
     dest->timer = timer_alloc();
... ...
@@ -129,6 +129,9 @@ static void ka_options_callback(
129 129
 
130 130
 		ka_dest->state = state;
131 131
 	}
132
+	if(ka_dest->response_clb != NULL) {
133
+		ka_dest->response_clb(&ka_dest->uri, ps, ka_dest->user_attr);
134
+	}
132 135
 }
133 136
 
134 137
 /*
... ...
@@ -180,7 +180,7 @@ static int w_add_destination(sip_msg_t *msg, char *uri, char *owner)
180 180
 		return -1;
181 181
 	}
182 182
 
183
-	return ka_add_dest(&suri, &sowner, 0, ka_ping_interval, 0, 0);
183
+	return ka_add_dest(&suri, &sowner, 0, ka_ping_interval, 0, 0, 0);
184 184
 }
185 185
 
186 186
 /*!
... ...
@@ -191,7 +191,7 @@ static int ki_add_destination(sip_msg_t *msg, str *uri, str *owner)
191 191
 	if(ka_alloc_destinations_list() < 0)
192 192
 		return -1;
193 193
 
194
-	return ka_add_dest(uri, owner, 0, ka_ping_interval, 0, 0);
194
+	return ka_add_dest(uri, owner, 0, ka_ping_interval, 0, 0, 0);
195 195
 }
196 196
 
197 197
 /*!
... ...
@@ -241,7 +241,7 @@ static int ka_mod_add_destination(modparam_t type, void *val)
241 241
 	str owner = str_init("_params");
242 242
 	LM_DBG("adding destination %.*s\n", dest.len, dest.s);
243 243
 
244
-	return ka_add_dest(&dest, &owner, 0, ka_ping_interval, 0, 0);
244
+	return ka_add_dest(&dest, &owner, 0, ka_ping_interval, 0, 0, 0);
245 245
 }
246 246
 
247 247
 /*
... ...
@@ -122,7 +122,7 @@ static void keepalive_rpc_add(rpc_t *rpc, void *ctx)
122 122
 		return;
123 123
 	}
124 124
 
125
-	if(ka_add_dest(&sip_adress,&table_name,0,ka_ping_interval,0,0) < 0 ){
125
+	if(ka_add_dest(&sip_adress,&table_name,0,ka_ping_interval,0,0,0) < 0 ){
126 126
 		LM_ERR("couldn't add data to list \n"  );
127 127
 		rpc->fault(ctx, 500, "couldn't add data to list");
128 128
 		return;