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