nabto_client_future_set_callback()
DEPRECATED
This function is deprecated and should not be used for new implementations. use nabto_client_future_set_callback2()
DESCRIPTION
Set a callback to be called when the future resolves
This must never be called after nabto_client_stop() since that would mean the future cannot be resolved.
It is not allowed to call the following functions from the callback as they could either crash the application or lead to the callback thread being blocked.
- nabto_client_future_wait
- nabto_client_future_timed_wait
- nabto_client_stop
- nabto_client_free
DECLARATION
NABTO_CLIENT_DECL_PREFIX void NABTO_CLIENT_API
nabto_client_future_set_callback(NabtoClientFuture* future,
NabtoClientFutureCallback callback,
void* data)
PARAMETERS
future:
- [in] The future.
callback:
- [in] The callback.
data:
- [in] User data for the callback.
EXAMPLES
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <nabto/nabto_client.h>
struct ClientContext {
NabtoClient* client;
NabtoClientConnection* connection;
NabtoClientFuture* future;
};
static void connect();
static void cb_connected(NabtoClientFuture *future, NabtoClientError ec, void *data);
static void print_connection_info(struct ClientContext* ctx);
static void print_connection_type(struct ClientContext* ctx);
static void cleanup(struct ClientContext* ctx);
static int finished = 0;
int main(int argc, char** argv) {
struct ClientContext* ctx = (struct ClientContext*)malloc(sizeof(struct ClientContext));
memset(ctx, 0, sizeof(struct ClientContext));
ctx->client = nabto_client_new();
connect(ctx);
while (!finished) {
// do stuff here while the connect future asynchonously resolves - or use
// nabto_client_future_wait for synchronous behavior instead of setting async callback
sleep(1);
print_connection_type(ctx);
}
nabto_client_free(ctx->client);
return 0;
}
static void connect(struct ClientContext* ctx) {
const char* clientPrivateKey =
"-----BEGIN EC PARAMETERS-----"
"BggqhkjOPQMBBw=="
"-----END EC PARAMETERS-----"
"-----BEGIN EC PRIVATE KEY-----"
"MHcCAQEEIPzp7QHuqMZLXMmigdEtUsJ/sgE0IKRONnAHhmB1/OBdoAoGCCqGSM49"
"AwEHoUQDQgAEHOb4U+Jervfn/cSe+zg7SdhdFbxVkYWZU35dsycrGYhsOcD8jyYI"
"gvjMfjwJ6wNV3aFXyWdPdfY/brfP/dyjUw=="
"-----END EC PRIVATE KEY-----";
ctx->connection = nabto_client_connection_new(ctx->client);
nabto_client_connection_set_private_key(ctx->connection, clientPrivateKey);
nabto_client_connection_set_product_id(ctx->connection, "pr-xruwhj1");
nabto_client_connection_set_device_id(ctx->connection, "de-1cgtwfm3");
nabto_client_connection_set_server_url(ctx->connection, "https://pr-xruwhj1.clients.nabto.net");
nabto_client_connection_set_server_key(ctx->connection, "sk-33c11dece0517aaa1837b5e49fe1bdd1");
nabto_client_connection_set_application_name(ctx->connection, "DemoApp");
nabto_client_connection_set_application_version(ctx->connection, "1.0.0");
ctx->future = nabto_client_future_new(ctx->client);
nabto_client_connection_connect(ctx->connection, ctx->future);
nabto_client_future_set_callback(ctx->future, cb_connected, ctx);
}
static void cb_connected(NabtoClientFuture *future, NabtoClientError ec, void *data) {
struct ClientContext* ctx = (struct ClientContext*)data;
if (ec == NABTO_CLIENT_EC_OK) {
printf("Connection attempt succeeded.\n");
print_connection_info(ctx);
print_connection_type(ctx);
// TODO: use connection - for instance, open a stream or send a CoAP request, close connection when done
} else {
printf("Connection attempt failed, ec=%d.\n", ec);
print_connection_info(ctx);
cleanup(ctx);
}
}
static void print_connection_info(struct ClientContext* ctx) {
char* json;
if (!ctx->connection) {
return;
}
if (nabto_client_connection_get_options(ctx->connection, &json) == NABTO_CLIENT_EC_OK) {
printf("Connection options: %s\n", json);
nabto_client_string_free(json);
} else {
printf("Could not get connection options.\n");
}
if (nabto_client_connection_get_info(ctx->connection, &json) == NABTO_CLIENT_EC_OK) {
printf("Connection info: %s\n", json);
nabto_client_string_free(json);
} else {
printf("Could not get connection info.\n");
}
}
static void print_connection_type(struct ClientContext* ctx) {
if (!ctx->connection) {
return;
}
NabtoClientConnectionType type;
NabtoClientError ec = nabto_client_connection_get_type(ctx->connection, &type);
const char* typeStr;
if (ec == NABTO_CLIENT_EC_OK) {
switch (type) {
case NABTO_CLIENT_CONNECTION_TYPE_RELAY: typeStr = "RELAY"; break;
case NABTO_CLIENT_CONNECTION_TYPE_DIRECT: typeStr = "DIRECT"; break;
default: assert(!"Unexpected connection type");
}
} else {
typeStr = "not determined";
}
printf("Connection type is currently %s\n", typeStr);
}
static void cleanup(struct ClientContext* ctx) {
if (ctx->connection) {
nabto_client_connection_close(ctx->connection, ctx->future);
if (ctx->future) {
nabto_client_future_wait(ctx->future);
}
nabto_client_connection_free(ctx->connection);
ctx->connection = NULL;
}
if (ctx->future) {
nabto_client_future_free(ctx->future);
ctx->future = NULL;
}
finished = 1;
}