Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Configrable RetryPolicy for IotHub -module #382

Merged
merged 2 commits into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion modules/iothub/devdoc/iothub.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ typedef struct IOTHUB_CONFIG_TAG
const char* IoTHubName; /*the name of the IoT hub*/
const char* IoTHubSuffix; /*the suffix used in generating the host name*/
IOTHUB_CLIENT_TRANSPORT_PROVIDER transportProvider;
IOTHUB_CLIENT_RETRY_POLICY retryPolicy;
}IOTHUB_CONFIG; /*this needs to be passed to the Module_Create function*/
```

Expand All @@ -65,7 +66,8 @@ Deserializes a JSON string into an IOTHUB_CONFIG structure suitable for passing
{
"IoTHubName" : "<the name of the IoTHub>",
"IoTHubSuffix" : "<the suffix used in generating the host name>",
"Transport" : "HTTP" | "http" | "AMQP" | "amqp" | "MQTT" | "mqtt"
"Transport" : "HTTP" | "http" | "AMQP" | "amqp" | "MQTT" | "mqtt",
"RetryPolicy" : "NONE" | "IMMEDIATE" | "INTERVAL" | "LINEAR_BACKOFF" | "EXPONENTIAL_BACKOFF" | "EXPONENTIAL_BACKOFF_WITH_JITTER" (default value) | "RANDOM"
}
```

Expand All @@ -76,6 +78,9 @@ Deserializes a JSON string into an IOTHUB_CONFIG structure suitable for passing
**SRS_IOTHUBMODULE_05_007: [** If the JSON object does not contain a value named "IoTHubSuffix" then `IotHub_ParseConfigurationFromJson` shall fail and return NULL. **]**
**SRS_IOTHUBMODULE_05_011: [** If the JSON object does not contain a value named "Transport" then `IotHub_ParseConfigurationFromJson` shall fail and return NULL. **]**
**SRS_IOTHUBMODULE_05_012: [** If the value of "Transport" is not one of "HTTP", "AMQP", or "MQTT" (case-insensitive) then `IotHub_ParseConfigurationFromJson` shall fail and return NULL. **]**
**SRS_IOTHUBMODULE_99_001: [** If the value of "RetryPolicy" is defined but is not one of "NONE", "IMMEDIATE", "INTERVAL", "LINEAR_BACKOFF", "EXPONENTIAL_BACKOFF", "EXPONENTIAL_BACKOFF_WITH_JITTER" or "RANDOM" then `IotHub_ParseConfigurationFromJson` shall fail and return NULL. **]**
**SRS_IOTHUBMODULE_99_002: [** If the value of "RetryPolicy" is not defined, retry policy is set to default value (`EXPONENTIAL_BACKOFF_WITH_JITTER`) **]**


### IotHub_FreeConfiguration
```C
Expand Down Expand Up @@ -131,6 +136,7 @@ void IoTHub_Receive(MODULE_HANDLE moduleHandle, MESSAGE_HANDLE messageHandle);
**SRS_IOTHUBMODULE_02_020: [** `IotHub_Receive` shall call IoTHubClient_SendEventAsync passing the IOTHUB_MESSAGE_HANDLE. **]**
**SRS_IOTHUBMODULE_02_021: [** If `IoTHubClient_SendEventAsync` fails then `IotHub_Receive` shall return. **]**
**SRS_IOTHUBMODULE_02_022: [** If `IoTHubClient_SendEventAsync` succeeds then `IotHub_Receive` shall return. **]**
**SRS_IOTHUBMODULE_99_003: [** If a new personality is created, then retry policy will be set by calling `IoTHubClient_SetRetryPolicy`. **]**


### IotHub_ReceiveMessageCallback
Expand Down
1 change: 1 addition & 0 deletions modules/iothub/inc/iothub.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct IOTHUB_CONFIG_TAG
const char* IoTHubName;
const char* IoTHubSuffix;
IOTHUB_CLIENT_TRANSPORT_PROVIDER transportProvider;
IOTHUB_CLIENT_RETRY_POLICY retryPolicy;
}IOTHUB_CONFIG; /*this needs to be passed to the Module_Create function*/

MODULE_EXPORT const MODULE_API* MODULE_STATIC_GETAPI(IOTHUB_MODULE)(MODULE_API_VERSION gateway_api_version);
Expand Down
62 changes: 62 additions & 0 deletions modules/iothub/src/iothub.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct IOTHUB_HANDLE_DATA_TAG
IOTHUB_CLIENT_TRANSPORT_PROVIDER transportProvider;
TRANSPORT_HANDLE transportHandle;
BROKER_HANDLE broker;
IOTHUB_CLIENT_RETRY_POLICY retryPolicy;
}IOTHUB_HANDLE_DATA;

#define SOURCE "source"
Expand All @@ -52,6 +53,7 @@ typedef struct IOTHUB_HANDLE_DATA_TAG
#define SUFFIX "IoTHubSuffix"
#define HUBNAME "IoTHubName"
#define TRANSPORT "Transport"
#define RETRY_POLICY "RetryPolicy"

static int strcmp_i(const char* lhs, const char* rhs)
{
Expand Down Expand Up @@ -101,6 +103,7 @@ static void* IotHub_ParseConfigurationFromJson(const char* configuration)
const char * IoTHubName;
const char * IoTHubSuffix;
const char * transport;

if ((IoTHubName = json_object_get_string(obj, HUBNAME)) == NULL)
{
/*Codes_SRS_IOTHUBMODULE_05_006: [ If the JSON object does not contain a value named "IoTHubName" then `IotHub_ParseConfigurationFromJson` shall fail and return NULL. ]*/
Expand Down Expand Up @@ -165,6 +168,54 @@ static void* IotHub_ParseConfigurationFromJson(const char* configuration)
config = NULL;
}

if (config != NULL)
{
const char * retryPolicy;

/*Codes_SRS_IOTHUBMODULE_99_001: [ If the value of "RetryPolicy" is defined but is not one of "NONE", "IMMEDIATE", "INTERVAL", "LINEAR_BACKOFF", "EXPONENTIAL_BACKOFF", "EXPONENTIAL_BACKOFF_WITH_JITTER" or "RANDOM" then `IotHub_ParseConfigurationFromJson` shall fail and return NULL. ]*/
/*Codes_SRS_IOTHUBMODULE_99_002: [ If the value of "RetryPolicy" is not defined, retry policy is set to default value ("EXPONENTIAL_BACKOFF_WITH_JITTER") ]*/
if ((retryPolicy = json_object_get_string(obj, RETRY_POLICY)) == NULL)
{
config->retryPolicy = IOTHUB_CLIENT_RETRY_EXPONENTIAL_BACKOFF_WITH_JITTER;
}
else if (strcmp_i(retryPolicy, "NONE") == 0)
{
config->retryPolicy = IOTHUB_CLIENT_RETRY_NONE;
}
else if (strcmp_i(retryPolicy, "IMMEDIATE") == 0)
{
config->retryPolicy = IOTHUB_CLIENT_RETRY_IMMEDIATE;
}
else if (strcmp_i(retryPolicy, "INTERVAL") == 0)
{
config->retryPolicy = IOTHUB_CLIENT_RETRY_INTERVAL;
}
else if (strcmp_i(retryPolicy, "LINEAR_BACKOFF") == 0)
{
config->retryPolicy = IOTHUB_CLIENT_RETRY_LINEAR_BACKOFF;
}
else if (strcmp_i(retryPolicy, "EXPONENTIAL_BACKOFF") == 0)
{
config->retryPolicy = IOTHUB_CLIENT_RETRY_EXPONENTIAL_BACKOFF;
}
else if (strcmp_i(retryPolicy, "EXPONENTIAL_BACKOFF_WITH_JITTER") == 0)
{
config->retryPolicy = IOTHUB_CLIENT_RETRY_EXPONENTIAL_BACKOFF_WITH_JITTER;
}
else if (strcmp_i(retryPolicy, "RANDOM") == 0)
{
config->retryPolicy = IOTHUB_CLIENT_RETRY_RANDOM;
}
else
{
LogError("Invalid RetryPolicy");
free(name);
free(suffix);
free(config);
config = NULL;
}
}

if (config != NULL)
{
strcpy(name, IoTHubName);
Expand Down Expand Up @@ -291,6 +342,7 @@ static MODULE_HANDLE IotHub_Create(BROKER_HANDLE broker, const void* configurati
}
else
{
result->retryPolicy = config->retryPolicy;
/*Codes_SRS_IOTHUBMODULE_17_004: [ `IotHub_Create` shall store the broker. ]*/
result->broker = broker;
/*Codes_SRS_IOTHUBMODULE_02_008: [ Otherwise, `IotHub_Create` shall return a non-`NULL` handle. ]*/
Expand Down Expand Up @@ -505,6 +557,16 @@ static PERSONALITY_PTR PERSONALITY_create(const char* deviceName, const char* de
free(result);
result = NULL;
}
/*Codes_SRS_IOTHUBMODULE_99_003: [ If a new personality is created, then retry policy will be set by calling `IoTHubClient_SetRetryPolicy`. ]*/
else if (IoTHubClient_SetRetryPolicy(result->iothubHandle, moduleHandleData->retryPolicy, 0))
{
LogError("unable to IoTHubClient_SetRetryPolicy");
IoTHubClient_Destroy(result->iothubHandle);
STRING_delete(result->deviceName);
STRING_delete(result->deviceKey);
free(result);
result = NULL;
}
else
{
/*it is all fine*/
Expand Down
Loading