From 5b8cb3879a74f5adf04ad5ef0094a70b05c6f8d3 Mon Sep 17 00:00:00 2001 From: Michael Jerris Date: Sun, 10 Dec 2006 22:22:28 +0000 Subject: [PATCH] separate the stream context from the parser context merged from knhor branch, thanks Neal. git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@3601 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_ivr.h | 33 +++++++++++++--- src/switch_ivr.c | 85 +++++++++++++++++++++++++++------------- 2 files changed, 85 insertions(+), 33 deletions(-) diff --git a/src/include/switch_ivr.h b/src/include/switch_ivr.h index 4ac35dfdf0..4e95d90294 100644 --- a/src/include/switch_ivr.h +++ b/src/include/switch_ivr.h @@ -406,8 +406,12 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_broadcast(char *uuid, char *path, swi SWITCH_DECLARE(switch_status_t) switch_ivr_transfer_variable(switch_core_session_t *sessa, switch_core_session_t *sessb, char *var); +/******************************************************************************************************/ + struct switch_ivr_digit_stream_parser; typedef struct switch_ivr_digit_stream_parser switch_ivr_digit_stream_parser_t; +struct switch_ivr_digit_stream; +typedef struct switch_ivr_digit_stream switch_ivr_digit_stream_t; /*! \brief Create a digit stream parser object \param pool the pool to use for the new hash @@ -418,10 +422,25 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_new(switch_memory /*! \brief Destroy a digit stream parser object - \param parser a pointer to the object pointer + \param parser a pointer to the parser object \return SWITCH_STATUS_SUCCESS if all is well */ -SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_destroy(switch_ivr_digit_stream_parser_t **parser); +SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_destroy(switch_ivr_digit_stream_parser_t *parser); + +/*! + \brief Create a new digit stream object + \param parser a pointer to the parser object created by switch_ivr_digit_stream_parser_new + \param stream a pointer to the stream object pointer + \return NULL if no match found or consumer data that was associated with a given digit string when matched +*/ +SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_new(switch_ivr_digit_stream_parser_t *parser, switch_ivr_digit_stream_t **stream); + +/*! + \brief Destroys a digit stream object + \param stream a pointer to the stream object + \return NULL if no match found or consumer data that was associated with a given digit string when matched +*/ +SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_destroy(switch_ivr_digit_stream_t *stream); /*! \brief Set a digit string to action mapping @@ -446,14 +465,14 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_del_event(switch_ \param digit a digit to collect and test against the map of digit strings \return NULL if no match found or consumer data that was associated with a given digit string when matched */ -SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stream_parser_t *parser, char digit); +SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stream_parser_t *parser, switch_ivr_digit_stream_t *stream, char digit); /*! \brief Reset the collected digit stream to nothing - \param parser a pointer to the parser object created by switch_ivr_digit_stream_parser_new + \param stream a pointer to the parser stream object created by switch_ivr_digit_stream_new \return SWITCH_STATUS_SUCCESS if all is well */ -SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_reset(switch_ivr_digit_stream_parser_t *parser); +SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_reset(switch_ivr_digit_stream_t *stream); /*! \brief Set a digit string terminator @@ -463,6 +482,10 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_reset(switch_ivr_ */ SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_set_terminator(switch_ivr_digit_stream_parser_t *parser, char digit); + +/******************************************************************************************************/ + + /** @} */ /** diff --git a/src/switch_ivr.c b/src/switch_ivr.c index b7b557983d..714b6363ee 100644 --- a/src/switch_ivr.c +++ b/src/switch_ivr.c @@ -3495,11 +3495,14 @@ struct switch_ivr_digit_stream_parser { int pool_auto_created; switch_memory_pool_t *pool; switch_hash_t *hash; - char *digits; - char terminator; switch_size_t maxlen; switch_size_t minlen; + char terminator; unsigned int digit_timeout_ms; +}; + +struct switch_ivr_digit_stream { + char *digits; switch_time_t last_digit_time; }; @@ -3544,20 +3547,45 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_new(switch_memory return status; } -SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_destroy(switch_ivr_digit_stream_parser_t **parser) +SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_destroy(switch_ivr_digit_stream_parser_t *parser) { switch_status_t status = SWITCH_STATUS_FALSE; - if (parser != NULL && *parser != NULL) { - if ((*parser)->hash != NULL) { - switch_core_hash_destroy((*parser)->hash); - (*parser)->hash = NULL; + if (parser != NULL) { + if (parser->hash != NULL) { + switch_core_hash_destroy(parser->hash); + parser->hash = NULL; } // free the memory pool if we created it - if ((*parser)->pool_auto_created && (*parser)->pool != NULL) { - status = switch_core_destroy_memory_pool(&(*parser)->pool); + if (parser->pool_auto_created && parser->pool != NULL) { + status = switch_core_destroy_memory_pool(&parser->pool); } - // clean up for the caller - *parser = NULL; + } + + return status; +} + +SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_new(switch_ivr_digit_stream_parser_t *parser, switch_ivr_digit_stream_t **stream) +{ switch_status_t status = SWITCH_STATUS_FALSE; + + // if we have a paser object memory pool and a stream object pointer that is null + if (parser != NULL && parser->pool && stream != NULL && *stream == NULL) { + *stream = (switch_ivr_digit_stream_t *)switch_core_alloc(parser->pool,sizeof(switch_ivr_digit_stream_t)); + if (*stream != NULL) { + memset(*stream,0,sizeof(switch_ivr_digit_stream_t)); + status = SWITCH_STATUS_SUCCESS; + } + } + + return status; +} + +SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_destroy(switch_ivr_digit_stream_t *stream) +{ switch_status_t status = SWITCH_STATUS_FALSE; + + if (stream == NULL && stream->digits != NULL) { + free(stream->digits); + stream->digits = NULL; + status = SWITCH_STATUS_SUCCESS; } return status; @@ -3613,11 +3641,11 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_del_event(switch_ return status; } -SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stream_parser_t *parser, char digit) +SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stream_parser_t *parser, switch_ivr_digit_stream_t *stream, char digit) { void *result = NULL; - if (parser != NULL) { - switch_size_t len = (parser->digits != NULL ? strlen(parser->digits) : 0); + if (parser != NULL && stream != NULL) { + switch_size_t len = (stream->digits != NULL ? strlen(stream->digits) : 0); // handle new digit arrivals if(digit != '\0') { @@ -3627,37 +3655,37 @@ SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stre // if collected digits length >= the max length of the keys // in the hash table, then left shift the digit string if (len > 0 && parser->maxlen != 0 && len >= parser->maxlen) { - char *src = parser->digits + 1; - char *dst = parser->digits; + char *src = stream->digits + 1; + char *dst = stream->digits; while (*src) { *(dst++) = *(src++); } *dst = digit; } else { - parser->digits = realloc(parser->digits,len+2); - *(parser->digits+(len++)) = digit; - *(parser->digits+len) = '\0'; - parser->last_digit_time = switch_time_now() / 1000; + stream->digits = realloc(stream->digits,len+2); + *(stream->digits+(len++)) = digit; + *(stream->digits+len) = '\0'; + stream->last_digit_time = switch_time_now() / 1000; } } } // don't allow collected digit string testing if there are varying sized keys until timeout if ( parser->maxlen - parser->minlen > 0 - && (switch_time_now() / 1000) - parser->last_digit_time < parser->digit_timeout_ms + && (switch_time_now() / 1000) - stream->last_digit_time < parser->digit_timeout_ms ) { len = 0; } // if we have digits to test if (len) { - result = switch_core_hash_find(parser->hash,parser->digits); + result = switch_core_hash_find(parser->hash, stream->digits); // if we matched the digit string, or this digit is the terminator // reset the collected digits for next digit string if (result != NULL || parser->terminator == digit) { - free(parser->digits); - parser->digits = NULL; + free(stream->digits); + stream->digits = NULL; } } } @@ -3665,12 +3693,13 @@ SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stre return result; } -SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_reset(switch_ivr_digit_stream_parser_t *parser) +SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_reset(switch_ivr_digit_stream_t *stream) { switch_status_t status = SWITCH_STATUS_FALSE; - if (parser != NULL && parser->digits != NULL) { - free(parser->digits); - parser->digits = NULL; + if (stream != NULL && stream->digits != NULL) { + free(stream->digits); + stream->digits = NULL; + stream->last_digit_time = 0; status = SWITCH_STATUS_SUCCESS; }