Skip to content

Commit

Permalink
cloud: add support for multiple servers for a single cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Feb 9, 2024
1 parent 213a991 commit ed0d5e5
Show file tree
Hide file tree
Showing 31 changed files with 1,286 additions and 409 deletions.
4 changes: 2 additions & 2 deletions api/client/unittest/clientcbtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ TEST_F(TestClientCBWithServer, RemoveAsync)

oc_set_delayed_callback(cb, &oc_client_cb_remove_async, 0);
EXPECT_TRUE(oc_has_delayed_callback(cb, &oc_client_cb_remove_async, false));
oc::TestDevice::PoolEventsMsV1(1s);
oc::TestDevice::PoolEventsMsV1(50ms);

EXPECT_FALSE(oc_ri_is_client_cb_valid(cb));
}
Expand All @@ -275,7 +275,7 @@ TEST_F(TestClientCBWithServer, RemoveWithTimeoutAsync)
0);
EXPECT_TRUE(oc_has_delayed_callback(
cb, &oc_client_cb_remove_with_notify_timeout_async, false));
oc::TestDevice::PoolEventsMsV1(1s);
oc::TestDevice::PoolEventsMsV1(50ms);

EXPECT_TRUE(TestClientCB::responseHandlerInvoked);
EXPECT_FALSE(oc_ri_is_client_cb_valid(cb));
Expand Down
59 changes: 32 additions & 27 deletions api/cloud/oc_cloud.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@

#ifdef OC_CLOUD

#include "api/cloud/oc_cloud_context_internal.h"
#include "api/cloud/oc_cloud_deregister_internal.h"
#include "api/cloud/oc_cloud_internal.h"
#include "api/cloud/oc_cloud_log_internal.h"
#include "api/cloud/oc_cloud_manager_internal.h"
#include "api/cloud/oc_cloud_rd_internal.h"
#include "api/cloud/oc_cloud_store_internal.h"
#include "api/oc_ri_internal.h"
#include "api/oc_ri_server_internal.h"
#include "api/oc_server_api_internal.h"
#include "oc_api.h"
#include "oc_cloud_context_internal.h"
#include "oc_cloud_deregister_internal.h"
#include "oc_cloud_internal.h"
#include "oc_cloud_log_internal.h"
#include "oc_cloud_manager_internal.h"
#include "oc_cloud_store_internal.h"
#include "oc_collection.h"
#include "oc_core_res.h"
#include "oc_network_monitor.h"
Expand Down Expand Up @@ -101,19 +102,19 @@ restart_manager(void *user_data)
}

void
cloud_close_endpoint(const oc_endpoint_t *ep)
oc_cloud_close_endpoint(const oc_endpoint_t *ep)
{
OC_CLOUD_DBG("cloud_close_endpoint");
OC_CLOUD_DBG("oc_cloud_close_endpoint");
#ifdef OC_SECURITY
const oc_tls_peer_t *peer = oc_tls_get_peer(ep);
if (peer != NULL) {
OC_CLOUD_DBG("cloud_close_endpoint: oc_tls_close_connection");
OC_CLOUD_DBG("oc_cloud_close_endpoint: oc_tls_close_connection");
oc_tls_close_connection(ep);
} else
#endif /* OC_SECURITY */
{
#ifdef OC_TCP
OC_CLOUD_DBG("cloud_close_endpoint: oc_connectivity_end_session");
OC_CLOUD_DBG("oc_cloud_close_endpoint: oc_connectivity_end_session");
oc_connectivity_end_session(ep);
#endif /* OC_TCP */
}
Expand Down Expand Up @@ -144,7 +145,7 @@ cloud_reset(size_t device, bool force, bool sync, uint16_t timeout)
}

void
cloud_set_cloudconf(oc_cloud_context_t *ctx, const cloud_conf_update_t *data)
cloud_set_cloudconf(oc_cloud_context_t *ctx, const oc_cloud_conf_update_t *data)
{
assert(ctx != NULL);
assert(data != NULL);
Expand All @@ -157,7 +158,9 @@ cloud_set_cloudconf(oc_cloud_context_t *ctx, const cloud_conf_update_t *data)
data->access_token_len);
}
if (data->ci_server_len > 0) {
oc_set_string(&ctx->store.ci_server, data->ci_server, data->ci_server_len);
oc_cloud_endpoints_reinit(
&ctx->store.ci_servers,
oc_string_view(data->ci_server, data->ci_server_len));
}
if (data->sid_len > 0) {
oc_set_string(&ctx->store.sid, data->sid, data->sid_len);
Expand All @@ -172,6 +175,8 @@ oc_cloud_provision_conf_resource_v1(oc_cloud_context_t *ctx, const char *server,
const char *auth_provider,
size_t auth_provider_len)
{
// TODO: server -> list of servers?

assert(ctx != NULL);
if (server_len >= OC_MAX_STRING_LENGTH ||
access_token_len >= OC_MAX_STRING_LENGTH ||
Expand All @@ -180,14 +185,14 @@ oc_cloud_provision_conf_resource_v1(oc_cloud_context_t *ctx, const char *server,
return -1;
}

cloud_close_endpoint(ctx->cloud_ep);
oc_cloud_close_endpoint(ctx->cloud_ep);
memset(ctx->cloud_ep, 0, sizeof(oc_endpoint_t));
ctx->cloud_ep_state = OC_SESSION_DISCONNECTED;
cloud_store_initialize(&ctx->store);
oc_cloud_store_initialize(&ctx->store);
cloud_manager_stop(ctx);
cloud_deregister_stop(ctx);

cloud_conf_update_t data = {
oc_cloud_conf_update_t data = {
.access_token = access_token,
.access_token_len = access_token_len,
.ci_server = server,
Expand All @@ -203,7 +208,7 @@ oc_cloud_provision_conf_resource_v1(oc_cloud_context_t *ctx, const char *server,
ctx->store.status = OC_CLOUD_INITIALIZED;
ctx->store.cps = OC_CPS_READYTOREGISTER;

cloud_store_dump_async(&ctx->store);
oc_cloud_store_dump_async(&ctx->store);

if (ctx->cloud_manager) {
oc_cloud_manager_restart(ctx);
Expand Down Expand Up @@ -231,8 +236,8 @@ oc_cloud_provision_conf_resource(oc_cloud_context_t *ctx, const char *server,
}

void
cloud_update_by_resource(oc_cloud_context_t *ctx,
const cloud_conf_update_t *data)
oc_cloud_update_by_resource(oc_cloud_context_t *ctx,
const oc_cloud_conf_update_t *data)
{
if (data->ci_server_len == 0) {
OC_CLOUD_DBG("got forced deregister via provisioning of empty cis");
Expand All @@ -245,10 +250,10 @@ cloud_update_by_resource(oc_cloud_context_t *ctx,
// if deregistering or other cloud API was active then closing of the endpoint
// triggers the handler with timeout error, which ensures that/ the operation
// is interrupted
cloud_close_endpoint(ctx->cloud_ep);
oc_cloud_close_endpoint(ctx->cloud_ep);
memset(ctx->cloud_ep, 0, sizeof(oc_endpoint_t));
ctx->cloud_ep_state = OC_SESSION_DISCONNECTED;
cloud_store_initialize(&ctx->store);
oc_cloud_store_initialize(&ctx->store);
cloud_manager_stop(ctx);
cloud_deregister_stop(ctx);

Expand Down Expand Up @@ -368,7 +373,7 @@ oc_cloud_manager_restart(oc_cloud_context_t *ctx)
#ifdef OC_SESSION_EVENTS
if (ctx->cloud_ep_state == OC_SESSION_CONNECTED) {
bool is_tcp = (ctx->cloud_ep->flags & TCP) != 0;
cloud_close_endpoint(ctx->cloud_ep);
oc_cloud_close_endpoint(ctx->cloud_ep);
if (is_tcp) {
return;
}
Expand Down Expand Up @@ -422,29 +427,29 @@ 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);
cloud_store_initialize(&ctx->store);
cloud_close_endpoint(ctx->cloud_ep);
oc_cloud_store_initialize(&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;
ctx->cloud_manager = false;

return 0;
}

int
bool
oc_cloud_init(void)
{
if (!oc_ri_on_delete_resource_add_callback(oc_cloud_delete_resource)) {
return -1;
return false;
}
for (size_t device = 0; device < oc_core_get_num_devices(); ++device) {
if (cloud_context_init(device) == NULL) {
return -1;
return false;
}
oc_cloud_add_resource(oc_core_get_resource_by_index(OCF_P, 0));
oc_cloud_add_resource(oc_core_get_resource_by_index(OCF_D, device));
}
return 0;
return true;
}

void
Expand Down
Loading

0 comments on commit ed0d5e5

Please sign in to comment.