-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code example to doxygen and validate in CI (#30)
Add POSIX example application of using backoffAlgorithm library in doxygen documentation. Also, add a CI check mechanism to validate compilation of code example.
- Loading branch information
Showing
6 changed files
with
157 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include "backoff_algorithm.h" | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <netdb.h> | ||
#include <unistd.h> | ||
#include <time.h> | ||
|
||
/* The maximum number of retries for the example code. */ | ||
#define RETRY_MAX_ATTEMPTS ( 5U ) | ||
|
||
/* The maximum back-off delay (in milliseconds) for between retries in the example. */ | ||
#define RETRY_MAX_BACKOFF_DELAY_MS ( 5000U ) | ||
|
||
/* The base back-off delay (in milliseconds) for retry configuration in the example. */ | ||
#define RETRY_BACKOFF_BASE_MS ( 500U ) | ||
|
||
int main() | ||
{ | ||
/* @[code_example_backoffalgorithm_initializeparams] */ | ||
/* Variables used in this example. */ | ||
BackoffAlgorithmStatus_t retryStatus = BackoffAlgorithmSuccess; | ||
BackoffAlgorithmContext_t retryParams; | ||
char serverAddress[] = "amazon.com"; | ||
uint16_t nextRetryBackoff = 0; | ||
|
||
/* Initialize reconnect attempts and interval. */ | ||
BackoffAlgorithm_InitializeParams( &retryParams, | ||
RETRY_BACKOFF_BASE_MS, | ||
RETRY_MAX_BACKOFF_DELAY_MS, | ||
RETRY_MAX_ATTEMPTS ); | ||
/* @[code_example_backoffalgorithm_initializeparams] */ | ||
|
||
int32_t dnsStatus = -1; | ||
struct addrinfo hints; | ||
struct addrinfo ** pListHead = NULL; | ||
struct timespec tp; | ||
|
||
/* Add hints to retrieve only TCP sockets in getaddrinfo. */ | ||
( void ) memset( &hints, 0, sizeof( hints ) ); | ||
|
||
/* Address family of either IPv4 or IPv6. */ | ||
hints.ai_family = AF_UNSPEC; | ||
/* TCP Socket. */ | ||
hints.ai_socktype = ( int32_t ) SOCK_STREAM; | ||
hints.ai_protocol = IPPROTO_TCP; | ||
|
||
/* @[code_example_backoffalgorithm_getnextbackoff] */ | ||
|
||
/* Seed the pseudo random number generator used in this example (with call to | ||
* rand() function provided by ISO C standard library) for use in backoff period | ||
* calculation when retrying failed DNS resolution. */ | ||
|
||
/* Get current time to seed pseudo random number generator. */ | ||
( void ) clock_gettime( CLOCK_REALTIME, &tp ); | ||
/* Seed pseudo random number generator with seconds. */ | ||
srand( tp.tv_sec ); | ||
|
||
do | ||
{ | ||
/* Perform a DNS lookup on the given host name. */ | ||
dnsStatus = getaddrinfo( serverAddress, NULL, &hints, pListHead ); | ||
|
||
/* Retry if DNS resolution query failed. */ | ||
if( dnsStatus != 0 ) | ||
{ | ||
/* Generate a random number and get back-off value (in milliseconds) for the next retry. | ||
* Note: It is recommended to use a random number generator that is seeded with | ||
* device-specific entropy source so that backoff calculation across devices is different | ||
* and possibility of network collision between devices attempting retries can be avoided. | ||
* | ||
* For the simplicity of this code example, the pseudo random number generator, rand() | ||
* function is used. */ | ||
retryStatus = BackoffAlgorithm_GetNextBackoff( &retryParams, rand(), &nextRetryBackoff ); | ||
|
||
/* Wait for the calculated backoff period before the next retry attempt of querying DNS. | ||
* As usleep() takes nanoseconds as the parameter, we multiply the backoff period by 1000. */ | ||
( void ) usleep( nextRetryBackoff * 1000U ); | ||
} | ||
} while( ( dnsStatus != 0 ) && ( retryStatus != BackoffAlgorithmRetriesExhausted ) ); | ||
|
||
/* @[code_example_backoffalgorithm_getnextbackoff] */ | ||
|
||
return dnsStatus; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters