Skip to content

Commit

Permalink
cloud: select next server when a retry loop is over
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Feb 22, 2024
1 parent 117993e commit 2adf36c
Show file tree
Hide file tree
Showing 15 changed files with 260 additions and 94 deletions.
6 changes: 3 additions & 3 deletions api/cloud/oc_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ oc_cloud_provision_conf_resource(oc_cloud_context_t *ctx, const char *server,
oc_cloud_close_endpoint(ctx->cloud_ep);
memset(ctx->cloud_ep, 0, sizeof(oc_endpoint_t));
ctx->cloud_ep_state = OC_SESSION_DISCONNECTED;
oc_cloud_store_initialize(&ctx->store);
oc_cloud_store_reinitialize(&ctx->store);
cloud_manager_stop(ctx);
oc_cloud_deregister_stop(ctx);

Expand Down Expand Up @@ -286,7 +286,7 @@ oc_cloud_update_by_resource(oc_cloud_context_t *ctx,
oc_cloud_close_endpoint(ctx->cloud_ep);
memset(ctx->cloud_ep, 0, sizeof(oc_endpoint_t));
ctx->cloud_ep_state = OC_SESSION_DISCONNECTED;
oc_cloud_store_initialize(&ctx->store);
oc_cloud_store_reinitialize(&ctx->store);
cloud_manager_stop(ctx);
oc_cloud_deregister_stop(ctx);

Expand Down Expand Up @@ -460,7 +460,7 @@ oc_cloud_manager_stop(oc_cloud_context_t *ctx)
oc_remove_delayed_callback(ctx, start_manager);
cloud_rd_reset_context(ctx);
cloud_manager_stop(ctx);
oc_cloud_store_initialize(&ctx->store);
oc_cloud_store_reinitialize(&ctx->store);
oc_cloud_close_endpoint(ctx->cloud_ep);
memset(ctx->cloud_ep, 0, sizeof(oc_endpoint_t));
ctx->cloud_ep_state = OC_SESSION_DISCONNECTED;
Expand Down
30 changes: 23 additions & 7 deletions api/cloud/oc_cloud_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
OC_LIST(g_cloud_context_list);
OC_MEMB(g_cloud_context_pool, oc_cloud_context_t, OC_MAX_NUM_DEVICES);

void
cloud_retry_reset(oc_cloud_retry_t *retry)
{
retry->count = 0;
retry->refresh_token_count = 0;
}

static bool
need_to_reinitialize_cloud_storage(const oc_cloud_context_t *ctx)
{
Expand All @@ -52,14 +59,21 @@ need_to_reinitialize_cloud_storage(const oc_cloud_context_t *ctx)
return cloud_is_deregistering(ctx);
}

static void
cloud_on_server_change(void *data)
{
oc_cloud_context_t *ctx = (oc_cloud_context_t *)data;
oc_cloud_store_dump_async(&ctx->store);
}

static void
reinitialize_cloud_storage(oc_cloud_context_t *ctx)
{
if (!need_to_reinitialize_cloud_storage(ctx)) {
return;
}
OC_CLOUD_DBG("reinitializing cloud context in storage");
oc_cloud_store_initialize(&ctx->store);
oc_cloud_store_initialize(&ctx->store, cloud_on_server_change, ctx);
if (oc_cloud_store_dump(&ctx->store) < 0) {
OC_CLOUD_ERR("failed to dump cloud store");
}
Expand Down Expand Up @@ -185,7 +199,7 @@ oc_cloud_context_clear(oc_cloud_context_t *ctx, bool dump_async)
ctx->cloud_ep_state = OC_SESSION_DISCONNECTED;
cloud_manager_stop(ctx);
oc_cloud_deregister_stop(ctx);
oc_cloud_store_initialize(&ctx->store);
oc_cloud_store_reinitialize(&ctx->store);
ctx->last_error = 0;
ctx->store.cps = 0;
ctx->selected_identity_cred_id = -1;
Expand Down Expand Up @@ -283,11 +297,13 @@ bool
oc_cloud_select_server(oc_cloud_context_t *ctx,
const oc_cloud_endpoint_t *server)
{
if (!oc_list_has_item(ctx->store.ci_servers.endpoints, server)) {
return false;
}
ctx->store.ci_servers.selected = server;
return true;
return oc_cloud_endpoint_select(&ctx->store.ci_servers, server);
}

const oc_cloud_endpoint_t *
oc_cloud_selected_server(const oc_cloud_context_t *ctx)
{
return ctx->store.ci_servers.selected;
}

#endif /* OC_CLOUD */
15 changes: 12 additions & 3 deletions api/cloud/oc_cloud_context_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef OC_CLOUD_CONTEXT_INTERNAL_H
#define OC_CLOUD_CONTEXT_INTERNAL_H

#include "api/cloud/oc_cloud_endpoint_internal.h"
#include "api/cloud/oc_cloud_store_internal.h"
#include "oc_cloud.h"
#include "util/oc_compiler.h"
Expand Down Expand Up @@ -59,6 +60,15 @@ typedef struct oc_cloud_schedule_action_t
uint16_t timeout; /**< Timeout for the action in seconds. */
} oc_cloud_schedule_action_t;

typedef struct
{
uint8_t count;
uint8_t refresh_token_count;
} oc_cloud_retry_t;

/** Reset the retry counters */
void cloud_retry_reset(oc_cloud_retry_t *retry) OC_NONNULL(1);

struct oc_cloud_context_t
{
struct oc_cloud_context_t *next;
Expand All @@ -67,6 +77,8 @@ struct oc_cloud_context_t
oc_cloud_on_status_change_cb_t on_status_change;
oc_cloud_store_t store;

oc_cloud_retry_t retry; /**< Retry configuration */

oc_cloud_keepalive_t keepalive; /**< Keepalive configuration */
oc_cloud_schedule_action_t
schedule_action; /**< Schedule action configuration */
Expand All @@ -85,9 +97,6 @@ struct oc_cloud_context_t
uint32_t time_to_live; /**< Time to live of published resources in seconds */

bool cloud_manager; /**< cloud manager has been started */

uint8_t retry_count;
uint8_t retry_refresh_token_count;
};

/**
Expand Down
35 changes: 28 additions & 7 deletions api/cloud/oc_cloud_endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ cloud_endpoint_uri_is_valid(oc_string_view_t uri)
}

static void
cloud_endpoints_set_selected(oc_cloud_endpoints_t *ce,
const oc_cloud_endpoint_t *selected)
cloud_endpoint_select(oc_cloud_endpoints_t *ce,
const oc_cloud_endpoint_t *selected)
{
if (ce->selected == selected) {
return;
Expand All @@ -46,6 +46,17 @@ cloud_endpoints_set_selected(oc_cloud_endpoints_t *ce,
}
}

bool
oc_cloud_endpoint_select(oc_cloud_endpoints_t *ce,
const oc_cloud_endpoint_t *selected)
{
if (!oc_list_has_item(ce->endpoints, selected)) {
return false;
}
cloud_endpoint_select(ce, selected);
return true;
}

static oc_cloud_endpoint_t *
cloud_endpoint_item_allocate_and_add(oc_cloud_endpoints_t *ce,
oc_string_view_t uri, oc_uuid_t id)
Expand All @@ -68,7 +79,7 @@ cloud_endpoint_item_allocate_and_add(oc_cloud_endpoints_t *ce,
// automatically select the first endpoint added
if (ce->selected == NULL) {
assert(oc_list_length(ce->endpoints) == 0);
cloud_endpoints_set_selected(ce, cei);
cloud_endpoint_select(ce, cei);
}

oc_list_add(ce->endpoints, cei);
Expand Down Expand Up @@ -182,7 +193,7 @@ oc_cloud_endpoints_clear(oc_cloud_endpoints_t *ce)
cloud_endpoint_item_free(cei);
cei = oc_list_pop(ce->endpoints);
}
cloud_endpoints_set_selected(ce, NULL);
cloud_endpoint_select(ce, NULL);
}

typedef struct
Expand Down Expand Up @@ -270,8 +281,8 @@ oc_cloud_endpoint_remove(oc_cloud_endpoints_t *ce,
return false;
}
if (ce->selected == ep) {
cloud_endpoints_set_selected(
ce, cloud_endpoint_item_next(ce, ce->selected, ep_next));
cloud_endpoint_select(ce,
cloud_endpoint_item_next(ce, ce->selected, ep_next));
}
cloud_endpoint_item_free(found);
return true;
Expand All @@ -294,10 +305,20 @@ oc_cloud_endpoint_select_by_uri(oc_cloud_endpoints_t *ce, oc_string_view_t uri)
if (found == NULL) {
return false;
}
cloud_endpoints_set_selected(ce, found);
cloud_endpoint_select(ce, found);
return true;
}

void
oc_cloud_endpoint_select_next(oc_cloud_endpoints_t *ce)
{
if (ce->selected == NULL) {
return;
}
cloud_endpoint_select(
ce, cloud_endpoint_item_next(ce, ce->selected, ce->selected->next));
}

bool
oc_cloud_endpoint_is_selected(const oc_cloud_endpoints_t *ce,
oc_string_view_t uri)
Expand Down
13 changes: 11 additions & 2 deletions api/cloud/oc_cloud_endpoint_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,23 @@ bool oc_cloud_endpoint_remove_by_uri(oc_cloud_endpoints_t *ce,
* @brief Select a cloud server endpoint from the list of endpoints
*
* @param ce cloud endpoints (cannot be NULL)
* @param uri cloud endpoint URI to select
* @param selected cloud endpoint to select (cannot be NULL, must be in the list
* of endpoints)
*
* @return true if the endpoint was selected
* @return false if the endpoint was not found (the previous selection remains)
* @return false if the endpoint was not found in the list of endpoints (the
* previous selection remains)
*/
bool oc_cloud_endpoint_select(oc_cloud_endpoints_t *ce,
const oc_cloud_endpoint_t *selected) OC_NONNULL();

/** Select a cloud server endpoint by URI from the list of endpoints */
bool oc_cloud_endpoint_select_by_uri(oc_cloud_endpoints_t *ce,
oc_string_view_t uri) OC_NONNULL();

/** Select the next cloud server endpoint from the list of endpoints */
void oc_cloud_endpoint_select_next(oc_cloud_endpoints_t *ce) OC_NONNULL();

/** Check if a cloud server endpoint matching the given URI is selected */
bool oc_cloud_endpoint_is_selected(const oc_cloud_endpoints_t *ce,
oc_string_view_t uri) OC_NONNULL();
Expand Down
Loading

0 comments on commit 2adf36c

Please sign in to comment.