[Asterisk-code-review] res_pjsip_outbound_registration.c: Use our own scheduler and other s... (asterisk[16])

George Joseph asteriskteam at digium.com
Tue Nov 3 08:37:13 CST 2020


George Joseph has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/15130 )


Change subject: res_pjsip_outbound_registration.c:  Use our own scheduler and other stuff
......................................................................

res_pjsip_outbound_registration.c:  Use our own scheduler and other stuff

* Instead of using the pjproject timer heap, we now use our own
  pjsip_scheduler.  This allows us to more easily debug and allows us to
  see times in "pjsip show/list registrations" as well as being able to
  see the registrations in "pjsip show scheduled_tasks".

* Added the last registration time, registration interval, and the next
  registration time to the CLI output.

* Added log messages where needed.

Change-Id: I4534a0fc78c7cb69f23b7b449dda9748c90daca2
---
M res/res_pjsip_outbound_registration.c
1 file changed, 105 insertions(+), 60 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/30/15130/1

diff --git a/res/res_pjsip_outbound_registration.c b/res/res_pjsip_outbound_registration.c
index 4d0e38e..d5daadb 100644
--- a/res/res_pjsip_outbound_registration.c
+++ b/res/res_pjsip_outbound_registration.c
@@ -343,7 +343,7 @@
 	 */
 	pjsip_tx_data *last_tdata;
 	/*! \brief Timer entry for retrying on temporal responses */
-	pj_timer_entry timer;
+	struct ast_sip_sched_task *sched_task;
 	/*! \brief Optional line parameter placed into Contact */
 	char line[LINE_PARAMETER_SIZE];
 	/*! \brief Current number of retries */
@@ -525,10 +525,10 @@
 /*! \brief Helper function which cancels the timer on a client */
 static void cancel_registration(struct sip_outbound_registration_client_state *client_state)
 {
-	if (pj_timer_heap_cancel_if_active(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()),
-		&client_state->timer, client_state->timer.id)) {
-		/* The timer was successfully cancelled, drop the refcount of client_state */
-		ao2_ref(client_state, -1);
+	if (client_state->sched_task) {
+		ast_sip_sched_task_cancel(client_state->sched_task);
+		ao2_cleanup(client_state->sched_task);
+		client_state->sched_task = NULL;
 	}
 }
 
@@ -541,10 +541,16 @@
 	pj_status_t status;
 	int *callback_invoked;
 	pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
+	pjsip_regc_info info;
+
+	pjsip_regc_get_info(client_state->client, &info);
 
 	callback_invoked = ast_threadstorage_get(&register_callback_invoked, sizeof(int));
 	if (!callback_invoked) {
 		pjsip_tx_data_dec_ref(tdata);
+		ast_log(LOG_ERROR, "Failed to get threadstorage for registration to server '%.*s' from client '%.*s'\n",
+				(int) info.server_uri.slen, info.server_uri.ptr,
+				(int) info.client_uri.slen, info.client_uri.ptr);
 		return PJ_ENOMEM;
 	}
 	*callback_invoked = 0;
@@ -572,6 +578,9 @@
 	 * drop the references we just added
 	 */
 	if ((status != PJ_SUCCESS) && !(*callback_invoked)) {
+		ast_log(LOG_ERROR, "Failed send registration to server '%.*s' from client '%.*s'\n",
+				(int) info.server_uri.slen, info.server_uri.ptr,
+				(int) info.client_uri.slen, info.client_uri.ptr);
 		pjsip_tx_data_dec_ref(tdata);
 		ao2_ref(client_state, -1);
 		return status;
@@ -593,18 +602,24 @@
 /*! \brief Callback function for registering */
 static int handle_client_registration(void *data)
 {
-	RAII_VAR(struct sip_outbound_registration_client_state *, client_state, data, ao2_cleanup);
+	struct sip_outbound_registration_client_state *client_state = data;
 	pjsip_tx_data *tdata;
+	pjsip_regc_info info;
 
-	if (client_state->status == SIP_REGISTRATION_STOPPED
-		|| pjsip_regc_register(client_state->client, PJ_FALSE, &tdata) != PJ_SUCCESS) {
+	if (client_state->status == SIP_REGISTRATION_STOPPED) {
+		return 0;
+	}
+
+	pjsip_regc_get_info(client_state->client, &info);
+
+	if (pjsip_regc_register(client_state->client, PJ_FALSE, &tdata) != PJ_SUCCESS) {
+		ast_log(LOG_ERROR, "Failed to create registration to server '%.*s' from client '%.*s'\n",
+				(int) info.server_uri.slen, info.server_uri.ptr,
+				(int) info.client_uri.slen, info.client_uri.ptr);
 		return 0;
 	}
 
 	if (DEBUG_ATLEAST(1)) {
-		pjsip_regc_info info;
-
-		pjsip_regc_get_info(client_state->client, &info);
 		ast_log(LOG_DEBUG, "Outbound REGISTER attempt %u to '%.*s' with client '%.*s'\n",
 			client_state->retries + 1,
 			(int) info.server_uri.slen, info.server_uri.ptr,
@@ -620,7 +635,10 @@
 			hdr = pjsip_supported_hdr_create(tdata->pool);
 			if (!hdr) {
 				pjsip_tx_data_dec_ref(tdata);
-				return -1;
+				ast_log(LOG_ERROR, "Failed to add SUPPORTED header to registration to server '%.*s' from client '%.*s'\n",
+						(int) info.server_uri.slen, info.server_uri.ptr,
+						(int) info.client_uri.slen, info.client_uri.ptr);
+				return 0;
 			}
 
 			pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);
@@ -635,44 +653,28 @@
 	return 0;
 }
 
-/*! \brief Timer callback function, used just for registrations */
-static void sip_outbound_registration_timer_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry)
-{
-	struct sip_outbound_registration_client_state *client_state = entry->user_data;
-
-	entry->id = 0;
-
-	/*
-	 * Transfer client_state reference to serializer task so the
-	 * nominal path will not dec the client_state ref in this
-	 * pjproject callback thread.
-	 */
-	if (ast_sip_push_task(client_state->serializer, handle_client_registration, client_state)) {
-		ast_log(LOG_WARNING, "Scheduled outbound registration could not be executed.\n");
-		ao2_ref(client_state, -1);
-	}
-}
-
 /*! \brief Helper function which sets up the timer to re-register in a specific amount of time */
-static void schedule_registration(struct sip_outbound_registration_client_state *client_state, unsigned int seconds)
+static void schedule_registration(struct sip_outbound_registration_client_state *client_state, unsigned int ms)
 {
-	pj_time_val delay = { .sec = seconds, };
 	pjsip_regc_info info;
 
+	pjsip_regc_get_info(client_state->client, &info);
+	ast_debug(1, "Scheduling outbound registration to server '%.*s' from client '%.*s' in %d ms\n",
+			(int) info.server_uri.slen, info.server_uri.ptr,
+			(int) info.client_uri.slen, info.client_uri.ptr,
+			ms);
+
 	cancel_registration(client_state);
 
-	pjsip_regc_get_info(client_state->client, &info);
-	ast_debug(1, "Scheduling outbound registration to server '%.*s' from client '%.*s' in %d seconds\n",
-			(int) info.server_uri.slen, info.server_uri.ptr,
-			(int) info.client_uri.slen, info.client_uri.ptr,
-			seconds);
+	client_state->sched_task = ast_sip_schedule_task(client_state->serializer,
+		ms, handle_client_registration, ast_taskprocessor_name(client_state->serializer),
+		client_state,
+		AST_SIP_SCHED_TASK_ONESHOT | AST_SIP_SCHED_TASK_DATA_AO2);
 
-	ao2_ref(client_state, +1);
-	if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(), &client_state->timer, &delay) != PJ_SUCCESS) {
+	if (!client_state->sched_task) {
 		ast_log(LOG_WARNING, "Failed to schedule registration to server '%.*s' from client '%.*s'\n",
 				(int) info.server_uri.slen, info.server_uri.ptr,
 				(int) info.client_uri.slen, info.client_uri.ptr);
-		ao2_ref(client_state, -1);
 	}
 }
 
@@ -709,8 +711,6 @@
 {
 	struct sip_outbound_registration_client_state *client_state = data;
 
-	cancel_registration(client_state);
-
 	if (client_state->client) {
 		pjsip_regc_info info;
 		pjsip_tx_data *tdata;
@@ -736,7 +736,7 @@
 				"Trying to unregister with server '%.*s' from client '%.*s' before destruction.\n",
 				(int) info.server_uri.slen, info.server_uri.ptr,
 				(int) info.client_uri.slen, info.client_uri.ptr);
-
+			cancel_registration(client_state);
 			update_client_state_status(client_state, SIP_REGISTRATION_STOPPING);
 			client_state->destroy = 1;
 			if (pjsip_regc_unregister(client_state->client, &tdata) == PJ_SUCCESS
@@ -746,8 +746,10 @@
 			}
 			break;
 		case SIP_REGISTRATION_REJECTED_TEMPORARY:
-		case SIP_REGISTRATION_REJECTED_PERMANENT:
 		case SIP_REGISTRATION_STOPPING:
+			cancel_registration(client_state);
+			break;
+		case SIP_REGISTRATION_REJECTED_PERMANENT:
 		case SIP_REGISTRATION_STOPPED:
 			break;
 		}
@@ -758,6 +760,7 @@
 
 	update_client_state_status(client_state, SIP_REGISTRATION_STOPPED);
 	ast_sip_auth_vector_destroy(&client_state->outbound_auths);
+
 	ao2_ref(client_state, -1);
 
 	return 0;
@@ -819,7 +822,7 @@
 			   const char *server_uri, const char *client_uri)
 {
 	update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_TEMPORARY);
-	schedule_registration(response->client_state, interval);
+	schedule_registration(response->client_state, interval * 1000);
 
 	if (response->rdata) {
 		ast_log(LOG_WARNING, "Temporal response '%d' received from '%s' on "
@@ -935,8 +938,8 @@
 	ast_copy_pj_str(server_uri, &info.server_uri, sizeof(server_uri));
 	ast_copy_pj_str(client_uri, &info.client_uri, sizeof(client_uri));
 
-	ast_debug(1, "Processing REGISTER response %d from server '%s' for client '%s'\n",
-			response->code, server_uri, client_uri);
+	ast_debug(1, "Processing REGISTER response %d from server '%s' for client '%s' with expiration '%d'\n",
+			response->code, server_uri, client_uri, response->expiration);
 
 	if (response->code == 408 || response->code == 503) {
 		if ((ast_sip_failover_request(response->old_request))) {
@@ -990,10 +993,10 @@
 			ast_debug(1, "Outbound registration to '%s' with client '%s' successful\n", server_uri, client_uri);
 			update_client_state_status(response->client_state, SIP_REGISTRATION_REGISTERED);
 			response->client_state->retries = 0;
-			next_registration_round = response->expiration - REREGISTER_BUFFER_TIME;
+			next_registration_round = (response->expiration - REREGISTER_BUFFER_TIME) * 1000;
 			if (next_registration_round < 0) {
-				/* Re-register immediately. */
-				next_registration_round = 0;
+				/* Re-register no sooner than the buffer time. */
+				next_registration_round = REREGISTER_BUFFER_TIME * 1000;
 			}
 			schedule_registration(response->client_state, next_registration_round);
 
@@ -1031,7 +1034,7 @@
 			/* A forbidden response retry interval is configured and there are retries remaining */
 			update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_TEMPORARY);
 			response->client_state->retries++;
-			schedule_registration(response->client_state, response->client_state->forbidden_retry_interval);
+			schedule_registration(response->client_state, response->client_state->forbidden_retry_interval * 1000);
 			ast_log(LOG_WARNING, "403 Forbidden fatal response received from '%s' on registration attempt to '%s', retrying in '%u' seconds\n",
 				server_uri, client_uri, response->client_state->forbidden_retry_interval);
 		} else if (response->client_state->fatal_retry_interval
@@ -1039,7 +1042,7 @@
 			/* Some kind of fatal failure response received, so retry according to configured interval */
 			update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_TEMPORARY);
 			response->client_state->retries++;
-			schedule_registration(response->client_state, response->client_state->fatal_retry_interval);
+			schedule_registration(response->client_state, response->client_state->fatal_retry_interval * 1000);
 			ast_log(LOG_WARNING, "'%d' fatal response received from '%s' on registration attempt to '%s', retrying in '%u' seconds\n",
 				response->code, server_uri, client_uri, response->client_state->fatal_retry_interval);
 		} else {
@@ -1095,8 +1098,8 @@
 	 */
 	response->client_state = client_state;
 
-	ast_debug(1, "Received REGISTER response %d(%.*s)\n",
-		param->code, (int) param->reason.slen, param->reason.ptr);
+	ast_debug(1, "Received REGISTER response %d(%.*s) Expires: %d\n",
+		param->code, (int) param->reason.slen, param->reason.ptr, param->expiration);
 
 	if (param->rdata) {
 		struct pjsip_retry_after_hdr *retry_after;
@@ -1192,8 +1195,6 @@
 	}
 
 	state->client_state->status = SIP_REGISTRATION_UNREGISTERED;
-	pj_timer_entry_init(&state->client_state->timer, 0, state->client_state,
-		sip_outbound_registration_timer_cb);
 	state->client_state->transport_name = ast_strdup(registration->transport);
 	state->client_state->registration_name =
 		ast_strdup(ast_sorcery_object_get_id(registration));
@@ -1450,8 +1451,14 @@
 {
 	struct sip_outbound_registration_state *state = data;
 	struct sip_outbound_registration *registration = ao2_bump(state->registration);
+	pjsip_regc_info info;
 	size_t i;
 
+	pjsip_regc_get_info(state->client_state->client, &info);
+
+	ast_debug(1, "Performing register to server '%s' from client '%s'  Expiration: %d\n",
+		state->registration->server_uri, state->registration->client_uri, registration->expiration);
+
 	/* Just in case the client state is being reused for this registration, free the auth information */
 	ast_sip_auth_vector_destroy(&state->client_state->outbound_auths);
 
@@ -1473,7 +1480,7 @@
 
 	pjsip_regc_update_expires(state->client_state->client, registration->expiration);
 
-	schedule_registration(state->client_state, (ast_random() % 10) + 1);
+	schedule_registration(state->client_state, ((ast_random() % 10) + 1) * 1000);
 
 	ao2_ref(registration, -1);
 	ao2_ref(state, -1);
@@ -1631,7 +1638,14 @@
 
 static int queue_register(struct sip_outbound_registration_state *state)
 {
+	pjsip_regc_info info;
 	ao2_ref(state, +1);
+
+	pjsip_regc_get_info(state->client_state->client, &info);
+
+	ast_debug(1, "Queueing register to server '%s' from client '%s'\n",
+		state->registration->server_uri, state->registration->client_uri);
+
 	if (ast_sip_push_task(state->client_state->serializer, sip_outbound_registration_perform, state)) {
 		ao2_ref(state, -1);
 		return -1;
@@ -2002,22 +2016,46 @@
 	ast_assert(context->output_buffer != NULL);
 
 	ast_str_append(&context->output_buffer, 0,
-		" <Registration/ServerURI..............................>  <Auth..........>  <Status.......>\n");
+		" <Registration/ServerURI..............................>  <Auth..........>  <Status.......>");
+	ast_str_append(&context->output_buffer, 0,
+		"  <Last Reg..>  <Intvl>  <Next Start.....secs>\n"
+		"==============================================");
 
 	return 0;
 }
 
+#define TIME_FORMAT "%a %T"
+
 static int cli_print_body(void *obj, void *arg, int flags)
 {
 	struct sip_outbound_registration *registration = obj;
 	struct ast_sip_cli_context *context = arg;
 	const char *id = ast_sorcery_object_get_id(registration);
 	struct sip_outbound_registration_state *state = get_state(id);
+	int next_run_ms = 0;
+	struct timeval next = { 0, 0 };
+	struct timeval last = { 0, 0 };
+	struct ast_tm tm;
+	char next_start[32] = "";
+	char last_start[32] = "";
+	int interval;
+
 #define REGISTRATION_URI_FIELD_LEN	53
 
 	ast_assert(context->output_buffer != NULL);
 
-	ast_str_append(&context->output_buffer, 0, " %-s/%-*.*s  %-16s  %-16s\n",
+	if (state->client_state->sched_task) {
+		ast_sip_sched_task_get_times2(state->client_state->sched_task, &last, NULL, NULL, &interval,
+			&next_run_ms, &next);
+
+		ast_localtime(&last, &tm, NULL);
+		ast_strftime(last_start, sizeof(last_start), TIME_FORMAT, &tm);
+
+		ast_localtime(&next, &tm, NULL);
+		ast_strftime(next_start, sizeof(next_start), TIME_FORMAT, &tm);
+	}
+
+	ast_str_append(&context->output_buffer, 0, " %-s/%-*.*s  %-16s  %-16s",
 		id,
 		(int) (REGISTRATION_URI_FIELD_LEN - strlen(id)),
 		(int) (REGISTRATION_URI_FIELD_LEN - strlen(id)),
@@ -2025,9 +2063,15 @@
 		AST_VECTOR_SIZE(&registration->outbound_auths)
 			? AST_VECTOR_GET(&registration->outbound_auths, 0)
 			: "n/a",
-		(state ? sip_outbound_registration_status_str(state->client_state->status) : "Unregistered"));
+		(state ? sip_outbound_registration_status_str(state->client_state->status) : "Unregistered")
+		);
+
+	ast_str_append(&context->output_buffer, 0, " %-12s  %7d  %-12s %8d\n",
+		last_start, interval / 1000, next_start, next_run_ms / 1000);
+
 	ao2_cleanup(state);
 
+
 	if (context->show_details
 		|| (context->show_details_only_level_0 && context->indent_level == 0)) {
 		ast_str_append(&context->output_buffer, 0, "\n");
@@ -2217,6 +2261,7 @@
 	ast_sorcery_instance_observer_remove(ast_sip_get_sorcery(), &observer_callbacks_registrations);
 
 	ast_sorcery_object_unregister(ast_sip_get_sorcery(), "registration");
+	unregister_all();
 
 	ao2_global_obj_release(current_states);
 

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/15130
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 16
Gerrit-Change-Id: I4534a0fc78c7cb69f23b7b449dda9748c90daca2
Gerrit-Change-Number: 15130
Gerrit-PatchSet: 1
Gerrit-Owner: George Joseph <gjoseph at digium.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20201103/5fec9ff7/attachment-0001.html>


More information about the asterisk-code-review mailing list