|
| 1 | +#include "backoff_algorithm.h" |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <netdb.h> |
| 5 | +#include <unistd.h> |
| 6 | +#include <time.h> |
| 7 | + |
| 8 | +/* The maximum number of retries for the example code. */ |
| 9 | +#define RETRY_MAX_ATTEMPTS ( 5U ) |
| 10 | + |
| 11 | +/* The maximum back-off delay (in milliseconds) for between retries in the example. */ |
| 12 | +#define RETRY_MAX_BACKOFF_DELAY_MS ( 5000U ) |
| 13 | + |
| 14 | +/* The base back-off delay (in milliseconds) for retry configuration in the example. */ |
| 15 | +#define RETRY_BACKOFF_BASE_MS ( 500U ) |
| 16 | + |
| 17 | +int main() |
| 18 | +{ |
| 19 | + /* @[code_example_backoffalgorithm_initializeparams] */ |
| 20 | + /* Variables used in this example. */ |
| 21 | + BackoffAlgorithmStatus_t retryStatus = BackoffAlgorithmSuccess; |
| 22 | + BackoffAlgorithmContext_t retryParams; |
| 23 | + char serverAddress[] = "amazon.com"; |
| 24 | + uint16_t nextRetryBackoff = 0; |
| 25 | + |
| 26 | + /* Initialize reconnect attempts and interval. */ |
| 27 | + BackoffAlgorithm_InitializeParams( &retryParams, |
| 28 | + RETRY_BACKOFF_BASE_MS, |
| 29 | + RETRY_MAX_BACKOFF_DELAY_MS, |
| 30 | + RETRY_MAX_ATTEMPTS ); |
| 31 | + /* @[code_example_backoffalgorithm_initializeparams] */ |
| 32 | + |
| 33 | + int32_t dnsStatus = -1; |
| 34 | + struct addrinfo hints; |
| 35 | + struct addrinfo ** pListHead = NULL; |
| 36 | + struct timespec tp; |
| 37 | + |
| 38 | + /* Add hints to retrieve only TCP sockets in getaddrinfo. */ |
| 39 | + ( void ) memset( &hints, 0, sizeof( hints ) ); |
| 40 | + |
| 41 | + /* Address family of either IPv4 or IPv6. */ |
| 42 | + hints.ai_family = AF_UNSPEC; |
| 43 | + /* TCP Socket. */ |
| 44 | + hints.ai_socktype = ( int32_t ) SOCK_STREAM; |
| 45 | + hints.ai_protocol = IPPROTO_TCP; |
| 46 | + |
| 47 | + /* @[code_example_backoffalgorithm_getnextbackoff] */ |
| 48 | + |
| 49 | + /* Seed the pseudo random number generator used in this example (with call to |
| 50 | + * rand() function provided by ISO C standard library) for use in backoff period |
| 51 | + * calculation when retrying failed DNS resolution. */ |
| 52 | + |
| 53 | + /* Get current time to seed pseudo random number generator. */ |
| 54 | + ( void ) clock_gettime( CLOCK_REALTIME, &tp ); |
| 55 | + /* Seed pseudo random number generator with seconds. */ |
| 56 | + srand( tp.tv_sec ); |
| 57 | + |
| 58 | + do |
| 59 | + { |
| 60 | + /* Perform a DNS lookup on the given host name. */ |
| 61 | + dnsStatus = getaddrinfo( serverAddress, NULL, &hints, pListHead ); |
| 62 | + |
| 63 | + /* Retry if DNS resolution query failed. */ |
| 64 | + if( dnsStatus != 0 ) |
| 65 | + { |
| 66 | + /* Generate a random number and get back-off value (in milliseconds) for the next retry. |
| 67 | + * Note: It is recommended to use a random number generator that is seeded with |
| 68 | + * device-specific entropy source so that backoff calculation across devices is different |
| 69 | + * and possibility of network collision between devices attempting retries can be avoided. |
| 70 | + * |
| 71 | + * For the simplicity of this code example, the pseudo random number generator, rand() |
| 72 | + * function is used. */ |
| 73 | + retryStatus = BackoffAlgorithm_GetNextBackoff( &retryParams, rand(), &nextRetryBackoff ); |
| 74 | + |
| 75 | + /* Wait for the calculated backoff period before the next retry attempt of querying DNS. |
| 76 | + * As usleep() takes nanoseconds as the parameter, we multiply the backoff period by 1000. */ |
| 77 | + ( void ) usleep( nextRetryBackoff * 1000U ); |
| 78 | + } |
| 79 | + } while( ( dnsStatus != 0 ) && ( retryStatus != BackoffAlgorithmRetriesExhausted ) ); |
| 80 | + |
| 81 | + /* @[code_example_backoffalgorithm_getnextbackoff] */ |
| 82 | + |
| 83 | + return dnsStatus; |
| 84 | +} |
0 commit comments