-
Notifications
You must be signed in to change notification settings - Fork 36
Add code example to doxygen and validate in CI #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c93063a
Add mechanism to validate code example
aggarw13 1b48ca6
Minor doc updates from review comments
aggarw13 0b48d4b
Add CMakeLists.txt file update missed in previous commit
aggarw13 9841ec6
Fix indentation issue causing CI checks from not running
aggarw13 6ca727f
Fix CI failures
aggarw13 fc3fe79
Add newline
aggarw13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.