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
This commit is contained in:
Michael Jerris 2006-12-10 22:22:28 +00:00
parent ea20e92e59
commit 5b8cb3879a
2 changed files with 85 additions and 33 deletions

View File

@ -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); 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; struct switch_ivr_digit_stream_parser;
typedef struct switch_ivr_digit_stream_parser switch_ivr_digit_stream_parser_t; 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 \brief Create a digit stream parser object
\param pool the pool to use for the new hash \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 \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 \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 \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 \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 \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 \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 \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 \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); SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_set_terminator(switch_ivr_digit_stream_parser_t *parser, char digit);
/******************************************************************************************************/
/** @} */ /** @} */
/** /**

View File

@ -3495,11 +3495,14 @@ struct switch_ivr_digit_stream_parser {
int pool_auto_created; int pool_auto_created;
switch_memory_pool_t *pool; switch_memory_pool_t *pool;
switch_hash_t *hash; switch_hash_t *hash;
char *digits;
char terminator;
switch_size_t maxlen; switch_size_t maxlen;
switch_size_t minlen; switch_size_t minlen;
char terminator;
unsigned int digit_timeout_ms; unsigned int digit_timeout_ms;
};
struct switch_ivr_digit_stream {
char *digits;
switch_time_t last_digit_time; 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; 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; { switch_status_t status = SWITCH_STATUS_FALSE;
if (parser != NULL && *parser != NULL) { if (parser != NULL) {
if ((*parser)->hash != NULL) { if (parser->hash != NULL) {
switch_core_hash_destroy((*parser)->hash); switch_core_hash_destroy(parser->hash);
(*parser)->hash = NULL; parser->hash = NULL;
} }
// free the memory pool if we created it // free the memory pool if we created it
if ((*parser)->pool_auto_created && (*parser)->pool != NULL) { if (parser->pool_auto_created && parser->pool != NULL) {
status = switch_core_destroy_memory_pool(&(*parser)->pool); 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; return status;
@ -3613,11 +3641,11 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_digit_stream_parser_del_event(switch_
return status; 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; { void *result = NULL;
if (parser != NULL) { if (parser != NULL && stream != NULL) {
switch_size_t len = (parser->digits != NULL ? strlen(parser->digits) : 0); switch_size_t len = (stream->digits != NULL ? strlen(stream->digits) : 0);
// handle new digit arrivals // handle new digit arrivals
if(digit != '\0') { 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 // if collected digits length >= the max length of the keys
// in the hash table, then left shift the digit string // in the hash table, then left shift the digit string
if (len > 0 && parser->maxlen != 0 && len >= parser->maxlen) { if (len > 0 && parser->maxlen != 0 && len >= parser->maxlen) {
char *src = parser->digits + 1; char *src = stream->digits + 1;
char *dst = parser->digits; char *dst = stream->digits;
while (*src) { while (*src) {
*(dst++) = *(src++); *(dst++) = *(src++);
} }
*dst = digit; *dst = digit;
} else { } else {
parser->digits = realloc(parser->digits,len+2); stream->digits = realloc(stream->digits,len+2);
*(parser->digits+(len++)) = digit; *(stream->digits+(len++)) = digit;
*(parser->digits+len) = '\0'; *(stream->digits+len) = '\0';
parser->last_digit_time = switch_time_now() / 1000; stream->last_digit_time = switch_time_now() / 1000;
} }
} }
} }
// don't allow collected digit string testing if there are varying sized keys until timeout // don't allow collected digit string testing if there are varying sized keys until timeout
if ( parser->maxlen - parser->minlen > 0 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; len = 0;
} }
// if we have digits to test // if we have digits to test
if (len) { 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 // if we matched the digit string, or this digit is the terminator
// reset the collected digits for next digit string // reset the collected digits for next digit string
if (result != NULL || parser->terminator == digit) { if (result != NULL || parser->terminator == digit) {
free(parser->digits); free(stream->digits);
parser->digits = NULL; stream->digits = NULL;
} }
} }
} }
@ -3665,12 +3693,13 @@ SWITCH_DECLARE(void *) switch_ivr_digit_stream_parser_feed(switch_ivr_digit_stre
return result; 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; { switch_status_t status = SWITCH_STATUS_FALSE;
if (parser != NULL && parser->digits != NULL) { if (stream != NULL && stream->digits != NULL) {
free(parser->digits); free(stream->digits);
parser->digits = NULL; stream->digits = NULL;
stream->last_digit_time = 0;
status = SWITCH_STATUS_SUCCESS; status = SWITCH_STATUS_SUCCESS;
} }