Skip to content
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

Added provisioning retry after to sdk #533

Merged
merged 2 commits into from
May 31, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ public void setApplicationProperty(Map<String, Object> userProperties)
this.messageImpl.setApplicationProperties(applicationProperties);
}

/**
* Set the application property for the message
* @return Map of properties
*/
public Map<String, Object> getApplicationProperty()
{
ApplicationProperties appProperty = this.messageImpl.getApplicationProperties();
if (appProperty == null)
{
return null;
}
else
{
return appProperty.getValue();
}
}

/**
* Sets the data value
* @param data the {@code byte[]} to be decoded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public String getHeaderField(String field)
return values;
}

public boolean isFieldAvailable(String field)
{
String canonicalizedField = canonicalizeFieldName(field);
return this.headerFields.containsKey(canonicalizedField);
}

/**
* Getter for the header fields.
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@

public abstract class ProvisioningDeviceClientContract
{
private int retryValue = DEFAULT_RETRY_AFTER_VALUE;
protected static final String RETRY_AFTER = "retry-after";
private static final Integer DEFAULT_RETRY_AFTER_VALUE = 2;
private static final Integer MAX_PROV_GET_THROTTLE_TIME = 5;

protected void setRetrieveRetryAfterValue(String protocolRetryValue)
{
if (protocolRetryValue != null && !protocolRetryValue.isEmpty())
{
retryValue = Integer.parseInt(protocolRetryValue);
// ensure the value is between the tolerances
if (retryValue < DEFAULT_RETRY_AFTER_VALUE || retryValue > MAX_PROV_GET_THROTTLE_TIME)
{
retryValue = DEFAULT_RETRY_AFTER_VALUE;
}
}
}

/**
* Static method to create contracts with the service over the specified protocol
* @param provisioningDeviceClientConfig Config used for provisioning
Expand Down Expand Up @@ -58,4 +76,13 @@ public static ProvisioningDeviceClientContract createProvisioningContract(Provis
public abstract void authenticateWithProvisioningService(RequestData requestData, ResponseCallback responseCallback, Object dpsAuthorizationCallbackContext) throws ProvisioningDeviceClientException;
public abstract void getRegistrationStatus(RequestData requestData, ResponseCallback responseCallback, Object dpsAuthorizationCallbackContext) throws ProvisioningDeviceClientException;
public abstract void close() throws ProvisioningDeviceConnectionException;

/**
* Method to get the DPS retry after value
* @return integer value of the number of milliseconds to wait to call dps service
*/
public int getRetryValue()
{
return this.retryValue*1000;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.util.Map;

public class ContractAPIAmqp extends ProvisioningDeviceClientContract
{
Expand All @@ -27,6 +28,18 @@ public class ContractAPIAmqp extends ProvisioningDeviceClientContract
private String idScope;
private SaslHandler amqpSaslHandler;

private void processRetryAfterValue(Map<String, Object> appProperties)
{
if (appProperties != null)
{
if (appProperties.containsKey(RETRY_AFTER))
{
Object retryAfterValue = appProperties.get(RETRY_AFTER);
setRetrieveRetryAfterValue(retryAfterValue.toString());
}
}
}

/**
* This constructor creates an instance of DpsAPIAmqps class and initializes member variables
* @param provisioningDeviceClientConfig Config used for provisioning Cannot be {@code null}.
Expand Down Expand Up @@ -173,6 +186,8 @@ public synchronized void authenticateWithProvisioningService(RequestData request

// SRS_ContractAPIAmqp_07_005: [This method shall send an AMQP message with the property of iotdps-register.]
this.provisioningAmqpOperations.sendRegisterMessage(responseCallback, callbackContext, payload);

processRetryAfterValue(this.provisioningAmqpOperations.getAmqpMessageProperties());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ProvisioningAmqpOperations extends AmqpDeviceOperations implements
private final Queue<AmqpMessage> receivedMessages = new LinkedBlockingQueue<>();
private final ObjectLock receiveLock = new ObjectLock();

private Map<String, Object> messageAppProperties;
private String idScope;
private String hostName;

Expand Down Expand Up @@ -94,6 +95,9 @@ private void retrieveAmqpMessage(ResponseCallback responseCallback, Object callb
if (this.receivedMessages.size() > 0)
{
AmqpMessage message = this.receivedMessages.remove();

// Need to keep property around to get the retry-after value
this.messageAppProperties = message.getApplicationProperty();
byte[] msgData = message.getAmqpBody();
if (msgData != null)
{
Expand Down Expand Up @@ -286,6 +290,15 @@ public void sendRegisterMessage(ResponseCallback responseCallback, Object callba
}
}

/**
* Returns the message properties of the current message
* @return Map of application properties
*/
public Map<String, Object> getAmqpMessageProperties()
{
return this.messageAppProperties;
}

/**
* connectionEstablished Unused
*/
Expand Down
Loading