|
58 | 58 | #error "Please define a POST_PATH." |
59 | 59 | #endif |
60 | 60 |
|
| 61 | +/** |
| 62 | + * @brief Delay in seconds between each iteration of the demo. |
| 63 | + */ |
| 64 | +#define DEMO_LOOP_DELAY_SECONDS ( 5U ) |
| 65 | + |
61 | 66 | /* Check that a path for Root CA certificate is defined. */ |
62 | 67 | #ifndef ROOT_CA_CERT_PATH |
63 | 68 | #error "Please define a ROOT_CA_CERT_PATH." |
|
108 | 113 | */ |
109 | 114 | #define REQUEST_BODY_LENGTH ( sizeof( REQUEST_BODY ) - 1 ) |
110 | 115 |
|
| 116 | +/** |
| 117 | + * @brief Number of HTTP paths to request. |
| 118 | + */ |
| 119 | +#define NUMBER_HTTP_PATHS ( 4 ) |
| 120 | + |
111 | 121 | /** |
112 | 122 | * @brief A buffer used in the demo for storing HTTP request headers and |
113 | 123 | * HTTP response headers and body. |
@@ -312,10 +322,6 @@ static int32_t sendHttpRequest( const TransportInterface_t * pTransportInterface |
312 | 322 | HTTPClient_strerror( httpStatus ) ) ); |
313 | 323 | } |
314 | 324 |
|
315 | | - LogInfo( ( "ABC" ) ); |
316 | | - fflush( stdout ); |
317 | | - fflush( stderr ); |
318 | | - |
319 | 325 | if( httpStatus != HTTPSuccess ) |
320 | 326 | { |
321 | 327 | returnStatus = EXIT_FAILURE; |
@@ -356,61 +362,75 @@ int main( int argc, |
356 | 362 | /* Set the pParams member of the network context with desired transport. */ |
357 | 363 | networkContext.pParams = &opensslParams; |
358 | 364 |
|
359 | | - /**************************** Connect. ******************************/ |
360 | | - |
361 | | - /* Establish TLS connection on top of TCP connection using OpenSSL. */ |
362 | | - if( returnStatus == EXIT_SUCCESS ) |
| 365 | + for( ; ; ) |
363 | 366 | { |
364 | | - LogInfo( ( "Performing TLS handshake on top of the TCP connection." ) ); |
| 367 | + int i = 0; |
365 | 368 |
|
366 | | - /* Attempt to connect to the HTTP server. If connection fails, retry after |
367 | | - * a timeout. Timeout value will be exponentially increased till the maximum |
368 | | - * attempts are reached or maximum timeout value is reached. The function |
369 | | - * returns EXIT_FAILURE if the TCP connection cannot be established to |
370 | | - * broker after configured number of attempts. */ |
371 | | - returnStatus = connectToServerWithBackoffRetries( connectToServer, |
372 | | - &networkContext ); |
| 369 | + /**************************** Connect. ******************************/ |
373 | 370 |
|
374 | | - if( returnStatus == EXIT_FAILURE ) |
| 371 | + /* Establish TLS connection on top of TCP connection using OpenSSL. */ |
| 372 | + if( returnStatus == EXIT_SUCCESS ) |
375 | 373 | { |
376 | | - /* Log error to indicate connection failure after all |
377 | | - * reconnect attempts are over. */ |
378 | | - LogError( ( "Failed to connect to HTTP server %.*s.", |
379 | | - ( int32_t ) AWS_IOT_ENDPOINT_LENGTH, |
380 | | - AWS_IOT_ENDPOINT ) ); |
| 374 | + LogInfo( ( "Performing TLS handshake on top of the TCP connection." ) ); |
| 375 | + |
| 376 | + /* Attempt to connect to the HTTP server. If connection fails, retry after |
| 377 | + * a timeout. Timeout value will be exponentially increased till the maximum |
| 378 | + * attempts are reached or maximum timeout value is reached. The function |
| 379 | + * returns EXIT_FAILURE if the TCP connection cannot be established to |
| 380 | + * broker after configured number of attempts. */ |
| 381 | + returnStatus = connectToServerWithBackoffRetries( connectToServer, |
| 382 | + &networkContext ); |
| 383 | + |
| 384 | + if( returnStatus == EXIT_FAILURE ) |
| 385 | + { |
| 386 | + /* Log error to indicate connection failure after all |
| 387 | + * reconnect attempts are over. */ |
| 388 | + LogError( ( "Failed to connect to HTTP server %.*s.", |
| 389 | + ( int32_t ) AWS_IOT_ENDPOINT_LENGTH, |
| 390 | + AWS_IOT_ENDPOINT ) ); |
| 391 | + } |
381 | 392 | } |
382 | | - } |
383 | 393 |
|
384 | | - /* Define the transport interface. */ |
385 | | - if( returnStatus == EXIT_SUCCESS ) |
386 | | - { |
387 | | - ( void ) memset( &transportInterface, 0, sizeof( transportInterface ) ); |
388 | | - transportInterface.recv = Openssl_Recv; |
389 | | - transportInterface.send = Openssl_Send; |
390 | | - transportInterface.pNetworkContext = &networkContext; |
391 | | - } |
| 394 | + /* Define the transport interface. */ |
| 395 | + if( returnStatus == EXIT_SUCCESS ) |
| 396 | + { |
| 397 | + ( void ) memset( &transportInterface, 0, sizeof( transportInterface ) ); |
| 398 | + transportInterface.recv = Openssl_Recv; |
| 399 | + transportInterface.send = Openssl_Send; |
| 400 | + transportInterface.pNetworkContext = &networkContext; |
| 401 | + } |
392 | 402 |
|
393 | | - /*********************** Send HTTPS request. ************************/ |
| 403 | + /*********************** Send HTTPS requests. ************************/ |
394 | 404 |
|
395 | | - if( returnStatus == EXIT_SUCCESS ) |
396 | | - { |
397 | | - returnStatus = sendHttpRequest( &transportInterface, |
398 | | - HTTP_METHOD_POST, |
399 | | - HTTP_METHOD_POST_LENGTH, |
400 | | - POST_PATH, |
401 | | - POST_PATH_LENGTH ); |
402 | | - } |
| 405 | + for( i = 0; i < NUMBER_HTTP_PATHS; i++ ) |
| 406 | + { |
| 407 | + if( returnStatus == EXIT_SUCCESS ) |
| 408 | + { |
| 409 | + returnStatus = sendHttpRequest( &transportInterface, |
| 410 | + HTTP_METHOD_POST, |
| 411 | + HTTP_METHOD_POST_LENGTH, |
| 412 | + POST_PATH, |
| 413 | + POST_PATH_LENGTH ); |
| 414 | + } |
| 415 | + else |
| 416 | + { |
| 417 | + break; |
| 418 | + } |
| 419 | + } |
403 | 420 |
|
404 | | - if( returnStatus == EXIT_SUCCESS ) |
405 | | - { |
406 | | - /* Log message indicating an iteration completed successfully. */ |
407 | | - LogInfo( ( "Demo completed successfully." ) ); |
408 | | - } |
| 421 | + if( returnStatus == EXIT_SUCCESS ) |
| 422 | + { |
| 423 | + /* Log message indicating an iteration completed successfully. */ |
| 424 | + LogInfo( ( "Demo completed successfully." ) ); |
| 425 | + } |
409 | 426 |
|
410 | | - /************************** Disconnect. *****************************/ |
| 427 | + /************************** Disconnect. *****************************/ |
411 | 428 |
|
412 | | - /* End TLS session, then close TCP connection. */ |
413 | | - ( void ) Openssl_Disconnect( &networkContext ); |
| 429 | + /* End TLS session, then close TCP connection. */ |
| 430 | + ( void ) Openssl_Disconnect( &networkContext ); |
414 | 431 |
|
415 | | - return returnStatus; |
416 | | -} |
| 432 | + LogInfo( ( "Short delay before starting the next iteration....\n" ) ); |
| 433 | + sleep( DEMO_LOOP_DELAY_SECONDS ); |
| 434 | + |
| 435 | + return returnStatus; |
| 436 | + } |
0 commit comments