diff --git a/inc/signalwire-client-c/blade/authenticate.h b/inc/signalwire-client-c/blade/authenticate.h deleted file mode 100644 index e69de29..0000000 diff --git a/inc/signalwire-client-c/blade/authority.h b/inc/signalwire-client-c/blade/authority.h deleted file mode 100644 index e69de29..0000000 diff --git a/inc/signalwire-client-c/blade/blade.h b/inc/signalwire-client-c/blade/blade.h index a1af310..0785817 100644 --- a/inc/signalwire-client-c/blade/blade.h +++ b/inc/signalwire-client-c/blade/blade.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 SignalWire, Inc + * Copyright (c) 2018-2022 SignalWire, Inc * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -31,16 +31,10 @@ #include "signalwire-client-c/blade/type.h" #include "signalwire-client-c/blade/connect.h" #include "signalwire-client-c/blade/disconnect.h" -#include "signalwire-client-c/blade/authenticate.h" -#include "signalwire-client-c/blade/authority.h" -#include "signalwire-client-c/blade/broadcast.h" #include "signalwire-client-c/blade/execute.h" #include "signalwire-client-c/blade/identity.h" #include "signalwire-client-c/blade/ping.h" #include "signalwire-client-c/blade/protocol.h" -#include "signalwire-client-c/blade/register.h" -#include "signalwire-client-c/blade/subscription.h" -#include "signalwire-client-c/blade/netcast.h" #include "signalwire-client-c/blade/util.h" /* For Emacs: diff --git a/inc/signalwire-client-c/blade/broadcast.h b/inc/signalwire-client-c/blade/broadcast.h deleted file mode 100644 index bad5e5b..0000000 --- a/inc/signalwire-client-c/blade/broadcast.h +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (c) 2018-2020 SignalWire, Inc - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#pragma once - -/* The method name for a broadcast request */ -#define BLADE_BROADCAST_METHOD "blade.broadcast" - -/* Flags for the command, in our case we don't get replies */ -#define BLADE_BROADCAST_FLAGS SWCLT_CMD_FLAG_NOREPLY - -/* Default time to live for broadcast */ -#define BLADE_BROADCAST_TTL_MS BLADE_DEFAULT_CMD_TTL_MS - -/* Create our broadcast request template */ -typedef struct blade_broadcast_rqu_s { - const char *protocol; - const char *channel; - const char *event; - const char *broadcaster_nodeid; - ks_json_t *params; -} blade_broadcast_rqu_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_BROADCAST_RQU, blade_broadcast_rqu_t) - SWCLT_JSON_MARSHAL_STRING(protocol) - SWCLT_JSON_MARSHAL_STRING(channel) - SWCLT_JSON_MARSHAL_STRING(event) - SWCLT_JSON_MARSHAL_STRING(broadcaster_nodeid) - SWCLT_JSON_MARSHAL_ITEM(params) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_BROADCAST_RQU, blade_broadcast_rqu_t) - SWCLT_JSON_DESTROY_STRING(protocol) - SWCLT_JSON_DESTROY_STRING(channel) - SWCLT_JSON_DESTROY_STRING(event) - SWCLT_JSON_DESTROY_STRING(broadcaster_nodeid) - SWCLT_JSON_DESTROY_ITEM(params) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_BROADCAST_RQU, blade_broadcast_rqu_t) - SWCLT_JSON_PARSE_STRING(protocol) - SWCLT_JSON_PARSE_STRING(channel) - SWCLT_JSON_PARSE_STRING(event) - SWCLT_JSON_PARSE_STRING(broadcaster_nodeid) - SWCLT_JSON_PARSE_ITEM(params) -SWCLT_JSON_PARSE_END() - -/** - * CREATE_BLADE_BROADCAST_CMD_ASYNC - Creates a command with a request - * in it setup to submit a broadcast method to blade. - */ -static inline swclt_cmd_t *CREATE_BLADE_BROADCAST_CMD_ASYNC( - swclt_cmd_cb_t cb, - void *cb_data, - const char *protocol, - const char *channel, - const char *event, - const char *broadcast_nodeid, - ks_json_t **params) -{ - ks_json_t *obj = NULL; - blade_broadcast_rqu_t broadcast_rqu; - swclt_cmd_t *cmd = NULL; - - /* Fill in the broadcast request then marshal it, it will create copies - * of all the fields so caller doesn't lose ownership here */ - broadcast_rqu.protocol = protocol; - broadcast_rqu.channel = channel; - broadcast_rqu.event = event; - broadcast_rqu.broadcaster_nodeid = broadcast_nodeid; - broadcast_rqu.params = (!params || !*params) ? ks_json_create_object() : *params; - - if (!(obj = BLADE_BROADCAST_RQU_MARSHAL(&broadcast_rqu))) { - - /* Since params is last, on error here we can be sure params was - * not freed so do not set the callers params to NULL */ - return cmd; - } - - /* We now own the callers params, null it to indicate ownership transfer */ - if (params) - *params = NULL; - - /* Now give it to the new command */ - if (swclt_cmd_create_ex( - &cmd, - cb, - cb_data, - BLADE_BROADCAST_METHOD, - &obj, - BLADE_BROADCAST_TTL_MS, - BLADE_BROADCAST_FLAGS, - ks_uuid_null())) - goto done; - -done: - ks_json_delete(&obj); - return cmd; -} - -static inline swclt_cmd_t *CREATE_BLADE_BROADCAST_CMD( - const char *protocol, - const char *channel, - const char *event, - const char *broadcast_nodeid, - ks_json_t **params) -{ - return CREATE_BLADE_BROADCAST_CMD_ASYNC( - NULL, - NULL, - protocol, - channel, - event, - broadcast_nodeid, - params); -} diff --git a/inc/signalwire-client-c/blade/connect.h b/inc/signalwire-client-c/blade/connect.h index 905cf8c..9e89cba 100644 --- a/inc/signalwire-client-c/blade/connect.h +++ b/inc/signalwire-client-c/blade/connect.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 SignalWire, Inc + * Copyright (c) 2018-2022 SignalWire, Inc * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/inc/signalwire-client-c/blade/netcast.h b/inc/signalwire-client-c/blade/netcast.h deleted file mode 100644 index 2ebe936..0000000 --- a/inc/signalwire-client-c/blade/netcast.h +++ /dev/null @@ -1,374 +0,0 @@ -/* - * Copyright (c) 2018-2020 SignalWire, Inc - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#pragma once - -/* Define all our commands */ -#define BLADE_NETCAST_CMD_ROUTE_ADD "route.add" -#define BLADE_NETCAST_CMD_ROUTE_REMOVE "route.remove" -#define BLADE_NETCAST_CMD_IDENTITY_ADD "identity.add" -#define BLADE_NETCAST_CMD_IDENTITY_REMOVE "identity.remove" -#define BLADE_NETCAST_CMD_PROTOCOL_ADD "protocol.add" -#define BLADE_NETCAST_CMD_PROTOCOL_REMOVE "protocol.remove" -#define BLADE_NETCAST_CMD_PROTOCOL_UPDATE "protocol.update" -#define BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_ADD "protocol.provider.add" -#define BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_REMOVE "protocol.provider.remove" -#define BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_RANK_UPDATE "protocol.provider.rank.update" -#define BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_DATA_UPDATE "protocol.provider.data.update" -#define BLADE_NETCAST_CMD_PROTOCOL_CHANNEL_ADD "protocol.channel.add" -#define BLADE_NETCAST_CMD_PROTOCOL_CHANNEL_REMOVE "protocol.channel.remove" -#define BLADE_NETCAST_CMD_SUBSCRIPTION_ADD "subscription.add" -#define BLADE_NETCAST_CMD_SUBSCRIPTION_REMOVE "subscription.remove" -#define BLADE_NETCAST_CMD_AUTHORITY_ADD "authority.add" -#define BLADE_NETCAST_CMD_AUTHORITY_REMOVE "authority.remove" -#define BLADE_NETCAST_CMD_AUTHORIZATION_ADD "authorization.add" -#define BLADE_NETCAST_CMD_AUTHORIZATION_UPDATE "authorization.update" -#define BLADE_NETCAST_CMD_AUTHORIZATION_REMOVE "authorization.remove" -#define BLADE_NETCAST_CMD_ACCESS_ADD "access.add" -#define BLADE_NETCAST_CMD_ACCESS_REMOVE "access.remove" - -/* The method name for a netcast request */ -#define BLADE_NETCAST_METHOD "blade.netcast" - -/* Flags for the command, in our case we don't get replies */ -#define BLADE_NETCAST_FLAGS SWCLT_CMD_FLAG_NOREPLY - -/* Create our netcast request template */ -typedef struct blade_netcast_rqu_s { - const char *command; - ks_bool_t certified_only; - const char *netcaster_nodeid; - ks_json_t *params; -} blade_netcast_rqu_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_RQU, blade_netcast_rqu_t) - SWCLT_JSON_MARSHAL_STRING(command) - SWCLT_JSON_MARSHAL_BOOL(certified_only) - SWCLT_JSON_MARSHAL_STRING(netcaster_nodeid) - SWCLT_JSON_MARSHAL_ITEM(params) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_RQU, blade_netcast_rqu_t) - SWCLT_JSON_DESTROY_STRING(command) - SWCLT_JSON_DESTROY_BOOL(certified_only) - SWCLT_JSON_DESTROY_STRING(netcaster_nodeid) - SWCLT_JSON_DESTROY_ITEM(params) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_RQU, blade_netcast_rqu_t) - SWCLT_JSON_PARSE_STRING(command) - SWCLT_JSON_PARSE_BOOL_OPT(certified_only) - SWCLT_JSON_PARSE_STRING(netcaster_nodeid) - SWCLT_JSON_PARSE_ITEM(params) -SWCLT_JSON_PARSE_END() - -/* The params definition for BLADE_NETCAST_CMD_PROTOCOL_ADD */ -typedef struct blade_netcast_protocol_add_param_s { - const char *protocol; -} blade_netcast_protocol_add_param_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_PROTOCOL_ADD_PARAM, blade_netcast_protocol_add_param_t) - SWCLT_JSON_MARSHAL_STRING(protocol) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_PROTOCOL_ADD_PARAM, blade_netcast_protocol_add_param_t) - SWCLT_JSON_DESTROY_STRING(protocol) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_PROTOCOL_ADD_PARAM, blade_netcast_protocol_add_param_t) - SWCLT_JSON_PARSE_STRING(protocol) -SWCLT_JSON_PARSE_END() - -/* The params definition for BLADE_NETCAST_CMD_PROTOCOL_REMOVE */ -typedef struct blade_netcast_protocol_remove_param_s { - const char *protocol; -} blade_netcast_protocol_remove_param_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_PROTOCOL_REMOVE_PARAM, blade_netcast_protocol_remove_param_t) - SWCLT_JSON_MARSHAL_STRING(protocol) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_PROTOCOL_REMOVE_PARAM, blade_netcast_protocol_remove_param_t) - SWCLT_JSON_DESTROY_STRING(protocol) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_PROTOCOL_REMOVE_PARAM, blade_netcast_protocol_remove_param_t) - SWCLT_JSON_PARSE_STRING(protocol) -SWCLT_JSON_PARSE_END() - -/* The params definition for BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_ADD */ -typedef struct blade_netcast_protocol_provider_add_param_s { - const char *protocol; - const char *nodeid; - blade_access_control_t default_method_execute_access; - blade_access_control_t default_channel_subscribe_access; - blade_access_control_t default_channel_broadcast_access; - ks_json_t *channels; - int rank; - ks_json_t *data; -} blade_netcast_protocol_provider_add_param_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM, blade_netcast_protocol_provider_add_param_t) - SWCLT_JSON_MARSHAL_STRING(protocol) - SWCLT_JSON_MARSHAL_STRING(nodeid) - SWCLT_JSON_MARSHAL_INT(default_method_execute_access) - SWCLT_JSON_MARSHAL_INT(default_channel_subscribe_access) - SWCLT_JSON_MARSHAL_INT(default_channel_broadcast_access) - SWCLT_JSON_MARSHAL_ITEM_OPT(channels) - SWCLT_JSON_MARSHAL_INT(rank) - SWCLT_JSON_MARSHAL_ITEM_OPT(data) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM, blade_netcast_protocol_provider_add_param_t) - SWCLT_JSON_DESTROY_STRING(protocol) - SWCLT_JSON_DESTROY_STRING(nodeid) - SWCLT_JSON_DESTROY_INT(default_method_execute_access) - SWCLT_JSON_DESTROY_INT(default_channel_subscribe_access) - SWCLT_JSON_DESTROY_INT(default_channel_broadcast_access) - SWCLT_JSON_DESTROY_ITEM(channels) - SWCLT_JSON_DESTROY_INT(rank) - SWCLT_JSON_DESTROY_ITEM(data) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM, blade_netcast_protocol_provider_add_param_t) - SWCLT_JSON_PARSE_STRING(protocol) - SWCLT_JSON_PARSE_STRING(nodeid) - SWCLT_JSON_PARSE_INT_OPT_DEF(default_method_execute_access, BLADE_ACL_SYSTEM) - SWCLT_JSON_PARSE_INT_OPT_DEF(default_channel_subscribe_access, BLADE_ACL_SYSTEM) - SWCLT_JSON_PARSE_INT_OPT_DEF(default_channel_broadcast_access, BLADE_ACL_SYSTEM) - SWCLT_JSON_PARSE_ITEM_OPT(channels) - SWCLT_JSON_PARSE_INT_OPT_DEF(rank, 1) - SWCLT_JSON_PARSE_ITEM_OPT(data) -SWCLT_JSON_PARSE_END() - -/* The params definition for BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_REMOVE */ -typedef struct blade_netcast_protocol_provider_remove_param_s { - const char *protocol; - const char *nodeid; -} blade_netcast_protocol_provider_remove_param_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_PROTOCOL_PROVIDER_REMOVE_PARAM, blade_netcast_protocol_provider_remove_param_t) - SWCLT_JSON_MARSHAL_STRING(protocol) - SWCLT_JSON_MARSHAL_STRING(nodeid) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_PROTOCOL_PROVIDER_REMOVE_PARAM, blade_netcast_protocol_provider_remove_param_t) - SWCLT_JSON_DESTROY_STRING(protocol) - SWCLT_JSON_DESTROY_STRING(nodeid) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_PROTOCOL_PROVIDER_REMOVE_PARAM, blade_netcast_protocol_provider_remove_param_t) - SWCLT_JSON_PARSE_STRING(protocol) - SWCLT_JSON_PARSE_STRING(nodeid) -SWCLT_JSON_PARSE_END() - -/* The params definition for BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_RANK_UPDATE */ -typedef struct blade_netcast_protocol_provider_rank_update_param_s { - const char *protocol; - const char *nodeid; - int rank; -} blade_netcast_protocol_provider_rank_update_param_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_PROTOCOL_PROVIDER_RANK_UPDATE_PARAM, blade_netcast_protocol_provider_rank_update_param_t) - SWCLT_JSON_MARSHAL_STRING(protocol) - SWCLT_JSON_MARSHAL_STRING(nodeid) - SWCLT_JSON_MARSHAL_INT(rank) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_PROTOCOL_PROVIDER_RANK_UPDATE_PARAM, blade_netcast_protocol_provider_rank_update_param_t) - SWCLT_JSON_DESTROY_STRING(protocol) - SWCLT_JSON_DESTROY_STRING(nodeid) - SWCLT_JSON_DESTROY_INT(rank) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_PROTOCOL_PROVIDER_RANK_UPDATE_PARAM, blade_netcast_protocol_provider_rank_update_param_t) - SWCLT_JSON_PARSE_STRING(protocol) - SWCLT_JSON_PARSE_STRING(nodeid) - SWCLT_JSON_PARSE_INT_OPT_DEF(rank, 1) -SWCLT_JSON_PARSE_END() - -/* The params definition for BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_DATA_UPDATE */ -typedef struct blade_netcast_protocol_provider_data_update_param_s { - const char *protocol; - const char *nodeid; - ks_json_t *data; -} blade_netcast_protocol_provider_data_update_param_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_PROTOCOL_PROVIDER_DATA_UPDATE_PARAM, blade_netcast_protocol_provider_data_update_param_t) - SWCLT_JSON_MARSHAL_STRING(protocol) - SWCLT_JSON_MARSHAL_STRING(nodeid) - SWCLT_JSON_MARSHAL_ITEM_OPT(data) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_PROTOCOL_PROVIDER_DATA_UPDATE_PARAM, blade_netcast_protocol_provider_data_update_param_t) - SWCLT_JSON_DESTROY_STRING(protocol) - SWCLT_JSON_DESTROY_STRING(nodeid) - SWCLT_JSON_DESTROY_ITEM(data) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_PROTOCOL_PROVIDER_DATA_UPDATE_PARAM, blade_netcast_protocol_provider_data_update_param_t) - SWCLT_JSON_PARSE_STRING(protocol) - SWCLT_JSON_PARSE_STRING(nodeid) - SWCLT_JSON_PARSE_ITEM_OPT(data) -SWCLT_JSON_PARSE_END() - - -/* The params definition for BLADE_NETCAST_CMD_AUTHORITY_ADD */ -typedef struct blade_netcast_authority_add_param_s { - const char *nodeid; -} blade_netcast_authority_add_param_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_AUTHORITY_ADD_PARAM, blade_netcast_authority_add_param_t) - SWCLT_JSON_MARSHAL_STRING(nodeid) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_AUTHORITY_ADD_PARAM, blade_netcast_authority_add_param_t) - SWCLT_JSON_DESTROY_STRING(nodeid) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_AUTHORITY_ADD_PARAM, blade_netcast_authority_add_param_t) - SWCLT_JSON_PARSE_STRING(nodeid) -SWCLT_JSON_PARSE_END() - -/* The params definition for BLADE_NETCAST_CMD_AUTHORITY_REMOVE */ -typedef struct blade_netcast_authority_remove_param_s { - const char *nodeid; -} blade_netcast_authority_remove_param_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_AUTHORITY_REMOVE_PARAM, blade_netcast_authority_remove_param_t) - SWCLT_JSON_MARSHAL_STRING(nodeid) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_AUTHORITY_REMOVE_PARAM, blade_netcast_authority_remove_param_t) - SWCLT_JSON_DESTROY_STRING(nodeid) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_AUTHORITY_REMOVE_PARAM, blade_netcast_authority_remove_param_t) - SWCLT_JSON_PARSE_STRING(nodeid) -SWCLT_JSON_PARSE_END() - -/* The params definition for BLADE_NETCAST_CMD_SUBSCRIPTION_ADD */ -typedef struct blade_netcast_subscription_add_param_s { - const char *protocol; - const char *nodeid; - ks_json_t *channels; -} blade_netcast_subscription_add_param_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_SUBSCRIPTION_ADD_PARAM, blade_netcast_subscription_add_param_t) - SWCLT_JSON_MARSHAL_STRING(protocol) - SWCLT_JSON_MARSHAL_STRING(nodeid) - SWCLT_JSON_MARSHAL_ITEM_OPT(channels) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_SUBSCRIPTION_ADD_PARAM, blade_netcast_subscription_add_param_t) - SWCLT_JSON_DESTROY_STRING(protocol) - SWCLT_JSON_DESTROY_STRING(nodeid) - SWCLT_JSON_DESTROY_ITEM(channels) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_SUBSCRIPTION_ADD_PARAM, blade_netcast_subscription_add_param_t) - SWCLT_JSON_PARSE_STRING(protocol) - SWCLT_JSON_PARSE_STRING(nodeid) - SWCLT_JSON_PARSE_ITEM_OPT(channels) -SWCLT_JSON_PARSE_END() - -/* The params definition for BLADE_NETCAST_CMD_SUBSCRIPTION_REMOVE */ -typedef struct blade_netcast_subscription_remove_param_s { - const char *protocol; - const char *nodeid; - ks_json_t *channels; -} blade_netcast_subscription_remove_param_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_SUBSCRIPTION_REMOVE_PARAM, blade_netcast_subscription_remove_param_t) - SWCLT_JSON_MARSHAL_STRING(protocol) - SWCLT_JSON_MARSHAL_STRING(nodeid) - SWCLT_JSON_MARSHAL_ITEM_OPT(channels) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_SUBSCRIPTION_REMOVE_PARAM, blade_netcast_subscription_remove_param_t) - SWCLT_JSON_DESTROY_STRING(protocol) - SWCLT_JSON_DESTROY_STRING(nodeid) - SWCLT_JSON_DESTROY_ITEM(channels) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_SUBSCRIPTION_REMOVE_PARAM, blade_netcast_subscription_remove_param_t) - SWCLT_JSON_PARSE_STRING(protocol) - SWCLT_JSON_PARSE_STRING(nodeid) - SWCLT_JSON_PARSE_ITEM_OPT(channels) -SWCLT_JSON_PARSE_END() - -/* The params definition for BLADE_NETCAST_CMD_ROUTE_ADD */ -typedef blade_node_t blade_netcast_route_add_param_t; - -#define BLADE_NETCAST_ROUTE_ADD_PARAM_MARSHAL BLADE_NODE_MARSHAL -#define BLADE_NETCAST_ROUTE_ADD_PARAM_DESTROY BLADE_NODE_DESTROY -#define BLADE_NETCAST_ROUTE_ADD_PARAM_PARSE BLADE_NODE_PARSE - -/* The params definition for BLADE_NETCAST_CMD_ROUTE_REMOVE */ -typedef blade_node_t blade_netcast_route_remove_param_t; - -#define BLADE_NETCAST_ROUTE_REMOVE_PARAM_MARSHAL BLADE_NODE_MARSHAL -#define BLADE_NETCAST_ROUTE_REMOVE_PARAM_DESTROY BLADE_NODE_DESTROY -#define BLADE_NETCAST_ROUTE_REMOVE_PARAM_PARSE BLADE_NODE_PARSE - -/* The params definition for BLADE_NETCAST_CMD_IDENTITY_ADD */ -typedef struct blade_netcast_identity_add_param_s { - const char *nodeid; - const char *identity; -} blade_netcast_identity_add_param_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_IDENTITY_ADD_PARAM, blade_netcast_identity_add_param_t) - SWCLT_JSON_MARSHAL_STRING(nodeid) - SWCLT_JSON_MARSHAL_STRING(identity) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_IDENTITY_ADD_PARAM, blade_netcast_identity_add_param_t) - SWCLT_JSON_DESTROY_STRING(nodeid) - SWCLT_JSON_DESTROY_STRING(identity) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_IDENTITY_ADD_PARAM, blade_netcast_identity_add_param_t) - SWCLT_JSON_PARSE_STRING(nodeid) - SWCLT_JSON_PARSE_STRING(identity) -SWCLT_JSON_PARSE_END() - -/* The params definition for BLADE_NETCAST_CMD_IDENTITY_REMOVE */ -typedef struct blade_netcast_identity_remove_param_s { - const char *nodeid; - const char *identity; -} blade_netcast_identity_remove_param_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_NETCAST_IDENTITY_REMOVE_PARAM, blade_netcast_identity_remove_param_t) - SWCLT_JSON_MARSHAL_STRING(nodeid) - SWCLT_JSON_MARSHAL_STRING(identity) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_NETCAST_IDENTITY_REMOVE_PARAM, blade_netcast_identity_remove_param_t) - SWCLT_JSON_DESTROY_STRING(nodeid) - SWCLT_JSON_DESTROY_STRING(identity) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_NETCAST_IDENTITY_REMOVE_PARAM, blade_netcast_identity_remove_param_t) - SWCLT_JSON_PARSE_STRING(nodeid) - SWCLT_JSON_PARSE_STRING(identity) -SWCLT_JSON_PARSE_END() - diff --git a/inc/signalwire-client-c/blade/register.h b/inc/signalwire-client-c/blade/register.h deleted file mode 100644 index e69de29..0000000 diff --git a/inc/signalwire-client-c/blade/subscription.h b/inc/signalwire-client-c/blade/subscription.h deleted file mode 100644 index 97252c3..0000000 --- a/inc/signalwire-client-c/blade/subscription.h +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (c) 2018-2020 SignalWire, Inc - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#pragma once - -KS_BEGIN_EXTERN_C - -#define BLADE_SUBSCRIPTION_CMD_ADD "add" -#define BLADE_SUBSCRIPTION_CMD_REMOVE "remove" - -/* The method name for a subscription request */ -#define BLADE_SUBSCRIPTION_METHOD "blade.subscription" - -/* Flags for the command */ -#define BLADE_SUBSCRIPTION_FLAGS 0 - -/* Default ttl for subscription cmd */ -#define BLADE_SUBSCRIPTION_TTL_MS BLADE_DEFAULT_CMD_TTL_MS - -typedef struct blade_subscription_rpl_s { - const char *command; - const char *protocol; - ks_json_t *failed_channels; - ks_json_t *subscribe_channels; -} blade_subscription_rpl_t; - -SWCLT_JSON_DESTROY_BEG(BLADE_SUBSCRIPTION_RPL, blade_subscription_rpl_t) - SWCLT_JSON_DESTROY_STRING(command) - SWCLT_JSON_DESTROY_STRING(protocol) - SWCLT_JSON_DESTROY_ITEM(failed_channels) - SWCLT_JSON_DESTROY_ITEM(subscribe_channels) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_SUBSCRIPTION_RPL, blade_subscription_rpl_t) - SWCLT_JSON_PARSE_STRING(command) - SWCLT_JSON_PARSE_STRING(protocol) - SWCLT_JSON_PARSE_ITEM_OPT(failed_channels) - SWCLT_JSON_PARSE_ITEM_OPT(subscribe_channels) -SWCLT_JSON_PARSE_END() - -typedef struct blade_subscription_rqu_s { - const char *command; - const char *protocol; - ks_json_t *channels; -} blade_subscription_rqu_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_SUBSCRIPTION_RQU, blade_subscription_rqu_t) - SWCLT_JSON_MARSHAL_STRING(command) - SWCLT_JSON_MARSHAL_STRING(protocol) - SWCLT_JSON_MARSHAL_ITEM(channels) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_SUBSCRIPTION_RQU, blade_subscription_rqu_t) - SWCLT_JSON_DESTROY_STRING(command) - SWCLT_JSON_DESTROY_STRING(protocol) - SWCLT_JSON_DESTROY_ITEM(channels) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_SUBSCRIPTION_RQU, blade_subscription_rqu_t) - SWCLT_JSON_PARSE_STRING(command) - SWCLT_JSON_PARSE_STRING(protocol) - SWCLT_JSON_PARSE_ITEM(channels) -SWCLT_JSON_PARSE_END() - -/** - * CREATE_BLADE_SUBSCRIPTION_CMD_ASYNC - Creates a command which holds - * and owns the request json for a subscription request. - */ -static inline swclt_cmd_t *CREATE_BLADE_SUBSCRIPTION_CMD_ASYNC( - swclt_cmd_cb_t cb, - void *cb_data, - const char *command, - const char *protocol, - const char *channel) -{ - ks_json_t *request_obj; - ks_status_t status; - swclt_cmd_t *cmd = NULL; - - ks_json_t *channels = ks_json_create_array(); - ks_json_add_string_to_array(channels, channel); - - /* Create a blade subscription request structure then marshal it */ - blade_subscription_rqu_t request = { - command, - protocol, - channels, - }; - - if (!(request_obj = BLADE_SUBSCRIPTION_RQU_MARSHAL(&request))) { - ks_log(KS_LOG_WARNING, "Failed to create subscription request"); - - /* Don't forget to free the channels we instantiated inline above */ - ks_json_delete(&request.channels); - return cmd; - } - - /* Request object has been created, channels will now reside in it */ - - /* Now wrap it in a command */ - if ((status = swclt_cmd_create_ex( - &cmd, - cb, - cb_data, - BLADE_SUBSCRIPTION_METHOD, - &request_obj, - BLADE_SUBSCRIPTION_FLAGS, - BLADE_SUBSCRIPTION_TTL_MS, - ks_uuid_null()))) { - ks_log(KS_LOG_WARNING, "Failed to allocate subscription command: %lu", status); - ks_json_delete(&request_obj); - return cmd; - } - - /* Success */ - return cmd; -} - -static inline swclt_cmd_t *CREATE_BLADE_SUBSCRIPTION_CMD( - const char *command, - const char *protocol, - const char *channel) -{ - return CREATE_BLADE_SUBSCRIPTION_CMD_ASYNC( - NULL, - NULL, - command, - protocol, - channel); -} - -/* Creates a subscription request */ -static inline ks_json_t *BLADE_SUBSCRIPTION_RQU( - const char * const command, - const char * const protocol, - const char * const channel, - blade_access_control_t broadcast_access, - blade_access_control_t subscribe_access) -{ - ks_json_t *request = ks_json_create_object(); - ks_json_add_string_to_object(request, "command", command); - ks_json_add_string_to_object(request, "protocol", protocol); - ks_json_t *channels = ks_json_add_array_to_object(request, "channels"); - ks_json_add_string_to_array(channels, channel); - (void)(broadcast_access); // unused - (void)(subscribe_access); // unused - return request; -} - -KS_END_EXTERN_C diff --git a/inc/signalwire-client-c/blade/type.h b/inc/signalwire-client-c/blade/type.h index 0cdb335..4216553 100644 --- a/inc/signalwire-client-c/blade/type.h +++ b/inc/signalwire-client-c/blade/type.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 SignalWire, Inc + * Copyright (c) 2018-2022 SignalWire, Inc * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -134,30 +134,6 @@ SWCLT_JSON_PARSE_BEG(BLADE_PROVIDER, blade_provider_t) SWCLT_JSON_PARSE_ITEM_OPT(data) SWCLT_JSON_PARSE_END() -typedef struct blade_subscription_s { - const char *protocol; - const char *channel; - ks_json_t *subscribers; /* list of identities */ -} blade_subscription_t; - -SWCLT_JSON_MARSHAL_BEG(BLADE_SUBSCRIPTION, blade_subscription_t) - SWCLT_JSON_MARSHAL_STRING(protocol) - SWCLT_JSON_MARSHAL_STRING(channel) - SWCLT_JSON_MARSHAL_ITEM(subscribers) -SWCLT_JSON_MARSHAL_END() - -SWCLT_JSON_DESTROY_BEG(BLADE_SUBSCRIPTION, blade_subscription_t) - SWCLT_JSON_DESTROY_STRING(protocol) - SWCLT_JSON_DESTROY_STRING(channel) - SWCLT_JSON_DESTROY_ITEM(subscribers) -SWCLT_JSON_DESTROY_END() - -SWCLT_JSON_PARSE_BEG(BLADE_SUBSCRIPTION, blade_subscription_t) - SWCLT_JSON_PARSE_STRING(protocol) - SWCLT_JSON_PARSE_STRING(channel) - SWCLT_JSON_PARSE_ITEM(subscribers) -SWCLT_JSON_PARSE_END() - typedef struct blade_protocol_s { const char *name; uint32_t default_method_execute_access; diff --git a/inc/signalwire-client-c/blade/util.h b/inc/signalwire-client-c/blade/util.h index 0774f76..87f17c3 100644 --- a/inc/signalwire-client-c/blade/util.h +++ b/inc/signalwire-client-c/blade/util.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 SignalWire, Inc + * Copyright (c) 2018-2022 SignalWire, Inc * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -30,20 +30,14 @@ KS_BEGIN_EXTERN_C static inline uint32_t BLADE_METHOD_FLAGS(const char * const method) { ks_assertd(method != NULL); - if (!strcmp(method, BLADE_BROADCAST_METHOD)) - return BLADE_BROADCAST_FLAGS; - else if (!strcmp(method, BLADE_DISCONNECT_METHOD)) + if (!strcmp(method, BLADE_DISCONNECT_METHOD)) return BLADE_DISCONNECT_FLAGS; - else if (!strcmp(method, BLADE_NETCAST_METHOD)) - return BLADE_NETCAST_FLAGS; else if (!strcmp(method, BLADE_PROTOCOL_METHOD)) return BLADE_PROTOCOL_FLAGS; else if (!strcmp(method, BLADE_IDENTITY_METHOD)) return BLADE_IDENTITY_FLAGS; else if (!strcmp(method, BLADE_EXECUTE_METHOD)) return BLADE_EXECUTE_FLAGS; - else if (!strcmp(method, BLADE_SUBSCRIPTION_METHOD)) - return BLADE_SUBSCRIPTION_FLAGS; else if (!strcmp(method, BLADE_PING_METHOD)) return BLADE_PING_FLAGS; else { diff --git a/inc/signalwire-client-c/client.h b/inc/signalwire-client-c/client.h index 8f3c488..402fff6 100644 --- a/inc/signalwire-client-c/client.h +++ b/inc/signalwire-client-c/client.h @@ -36,9 +36,7 @@ #include "signalwire-client-c/identity.h" #include "signalwire-client-c/config.h" #include "signalwire-client-c/connection.h" -#include "signalwire-client-c/subscription.h" #include "signalwire-client-c/pmethod.h" -#include "signalwire-client-c/nodestore.h" #include "signalwire-client-c/session.h" #include "signalwire-client-c/version.h" diff --git a/inc/signalwire-client-c/nodestore.h b/inc/signalwire-client-c/nodestore.h deleted file mode 100644 index c2f4e10..0000000 --- a/inc/signalwire-client-c/nodestore.h +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright (c) 2018-2020 SignalWire, Inc - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#pragma once - -KS_BEGIN_EXTERN_C - -struct swclt_store { - ks_pool_t *pool; - - // callbacks keyed by netcast command, pointing to value of callback to call - ks_hash_t *callbacks; - - /* Hash of nodes keyed by their nodeid. They point to a Node - * class which contains their certified status. */ - ks_hash_t *routes; - - /* Hash keyed by identity mapped to nodeid */ - ks_hash_t *identities; - - /* Last index position we selected for random protocol gathering */ - uint32_t last_random_protocol_idx; - - /* Hash of protocols, keyed by the protocol name, each protocol - * contains channels. */ - ks_hash_t *protocols; - - /* Hash of Subscription objects, keyed by their protocol name. */ - ks_hash_t *subscriptions; - - /* Hash of authorities, keyed by their node uuid. */ - ks_hash_t *authorities; - - /* Hash of protocols available to uncertified clients only, keyed by protocol name */ - ks_hash_t *protocols_uncertified; - - swclt_sess_t *sess; -}; - -typedef struct swclt_store swclt_store_t; - -typedef struct swclt_sess swclt_sess_t; - -typedef void (*swclt_store_cb_protocol_add_t)(swclt_sess_t *sess, - const char *protocol); - -typedef void (*swclt_store_cb_protocol_remove_t)(swclt_sess_t *sess, - const char *protocol); - -typedef void (*swclt_store_cb_protocol_provider_add_t)(swclt_sess_t *sess, - const blade_netcast_rqu_t *rqu, - const blade_netcast_protocol_provider_add_param_t *params); - -typedef void (*swclt_store_cb_protocol_provider_remove_t)( - swclt_sess_t *sess, - const blade_netcast_rqu_t* rqu, - const blade_netcast_protocol_provider_remove_param_t *params); - -typedef void (*swclt_store_cb_protocol_provider_rank_update_t)(swclt_sess_t *sess, - const blade_netcast_rqu_t *rqu, - const blade_netcast_protocol_provider_rank_update_param_t *params); - -typedef void (*swclt_store_cb_protocol_provider_data_update_t)(swclt_sess_t *sess, - const blade_netcast_rqu_t *rqu, - const blade_netcast_protocol_provider_data_update_param_t *params); - -typedef void (*swclt_store_cb_route_add_t)(swclt_sess_t *sess, - const blade_netcast_rqu_t *rqu, - const blade_netcast_route_add_param_t *params); - -typedef void (*swclt_store_cb_route_remove_t)( - swclt_sess_t *sess, - const blade_netcast_rqu_t* rqu, - const blade_netcast_route_remove_param_t *params); - -typedef void (*swclt_store_cb_authority_add_t)(swclt_sess_t *sess, - const blade_netcast_rqu_t *rqu, - const blade_netcast_authority_add_param_t *params); - -typedef void (*swclt_store_cb_authority_remove_t)( - swclt_sess_t *sess, - const blade_netcast_rqu_t* rqu, - const blade_netcast_authority_remove_param_t *params); - -typedef void (*swclt_store_cb_subscription_add_t)(swclt_sess_t *sess, - const blade_netcast_rqu_t *rqu, - const blade_netcast_subscription_add_param_t *params); - -typedef void (*swclt_store_cb_subscription_remove_t)( - swclt_sess_t *sess, - const blade_netcast_rqu_t* rqu, - const blade_netcast_subscription_remove_param_t *params); - -typedef void (*swclt_store_cb_identity_add_t)(swclt_sess_t *sess, - const blade_netcast_rqu_t *rqu, - const blade_netcast_identity_add_param_t *params); - -typedef void (*swclt_store_cb_identity_remove_t)( - swclt_sess_t *sess, - const blade_netcast_rqu_t* rqu, - const blade_netcast_identity_remove_param_t *params); - -SWCLT_DECLARE(ks_status_t) swclt_store_create(swclt_store_t **store); -SWCLT_DECLARE(ks_status_t) swclt_store_destroy(swclt_store_t **store); -SWCLT_DECLARE(char *) swclt_store_describe(swclt_store_t *store); -SWCLT_DECLARE(ks_status_t) swclt_store_reset(swclt_store_t *store); -SWCLT_DECLARE(ks_status_t) swclt_store_populate(swclt_store_t *store, const blade_connect_rpl_t *connect_rpl); -SWCLT_DECLARE(ks_status_t) swclt_store_update(swclt_store_t *store, const blade_netcast_rqu_t *netcast_rqu); -SWCLT_DECLARE(ks_status_t) swclt_store_get_node_identities(swclt_store_t *store, const char *nodeid, ks_pool_t *pool, ks_hash_t **identities); -SWCLT_DECLARE(ks_status_t) swclt_store_get_protocols(swclt_store_t *store, ks_json_t **protocols); -SWCLT_DECLARE(ks_status_t) swclt_store_check_protocol(swclt_store_t *store, const char *name); - -SWCLT_DECLARE(ks_status_t) swclt_store_select_random_protocol_provider( - swclt_store_t *store, - const char *name, - ks_pool_t *pool, - char **providerid); - -SWCLT_DECLARE(ks_status_t) swclt_store_get_protocol_providers( - swclt_store_t *store, - const char *name, - ks_json_t **providers); - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_route_add(swclt_store_t *store, swclt_store_cb_route_add_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_route_remove(swclt_store_t *store, swclt_store_cb_route_remove_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_identity_add(swclt_store_t *store, swclt_store_cb_identity_add_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_identity_remove(swclt_store_t *store, swclt_store_cb_identity_remove_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_protocol_add(swclt_store_t *store, swclt_store_cb_protocol_add_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_protocol_remove(swclt_store_t *store, swclt_store_cb_protocol_remove_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_protocol_provider_add(swclt_store_t *store, swclt_store_cb_protocol_provider_add_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_protocol_provider_remove(swclt_store_t *store, swclt_store_cb_protocol_provider_remove_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_protocol_provider_rank_update(swclt_store_t *store, swclt_store_cb_protocol_provider_rank_update_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_protocol_provider_data_update(swclt_store_t *store, swclt_store_cb_protocol_provider_data_update_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_authority_add(swclt_store_t *store, swclt_store_cb_authority_add_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_authority_remove(swclt_store_t *store, swclt_store_cb_authority_remove_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_subscription_add(swclt_store_t *store, swclt_store_cb_subscription_add_t cb); -SWCLT_DECLARE(ks_status_t) swclt_store_cb_subscription_remove(swclt_store_t *store, swclt_store_cb_subscription_remove_t cb); - -KS_END_EXTERN_C diff --git a/inc/signalwire-client-c/session.h b/inc/signalwire-client-c/session.h index f9139c6..9d147e3 100644 --- a/inc/signalwire-client-c/session.h +++ b/inc/signalwire-client-c/session.h @@ -69,10 +69,6 @@ struct swclt_sess { /* The pool that manages all the allocations */ ks_pool_t *pool; - /* The node store, an api to keep the cache in sync with blade, contains - * a lotta info about stuff. */ - swclt_store_t *store; - /* Our connection */ swclt_conn_t *conn; @@ -90,17 +86,9 @@ struct swclt_sess { /* Optional callback for authentication failure */ swclt_sess_auth_failed_cb_t auth_failed_cb; - /* We keep track of subscriptions in a hash here, each call to subscribe - * from the client will add an entry in this hash. The hash points to the - * subscription handle which will contain the users callback */ - ks_hash_t *subscriptions; - /* Registry for Protocol RPC methods */ ks_hash_t *methods; - /* Setups completed for provider event channels */ - ks_hash_t *setups; - /* Registry for metric rank updates for local protocols */ ks_hash_t *metrics; @@ -154,52 +142,6 @@ SWCLT_DECLARE(ks_status_t) swclt_sess_register_protocol_method( swclt_pmethod_cb_t pmethod, void *cb_data); -SWCLT_DECLARE(ks_status_t) swclt_sess_register_subscription_method( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - swclt_sub_cb_t cb, - void *cb_data); - -SWCLT_DECLARE(ks_status_t) swclt_sess_broadcast( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - const char *event, - ks_json_t **params); - -SWCLT_DECLARE(ks_status_t) swclt_sess_subscription_add( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - swclt_sub_cb_t cb, - void *cb_data, - swclt_cmd_reply_t **reply); - -SWCLT_DECLARE(ks_status_t) swclt_sess_subscription_add_async( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - swclt_sub_cb_t cb, - void *cb_data, - swclt_cmd_cb_t response_callback, - void *response_callback_data, - swclt_cmd_future_t **future); - -SWCLT_DECLARE(ks_status_t) swclt_sess_subscription_remove( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - swclt_cmd_reply_t **reply); - -SWCLT_DECLARE(ks_status_t) swclt_sess_subscription_remove_async( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - swclt_cmd_cb_t response_callback, - void *response_callback_data, - swclt_cmd_future_t **future); - SWCLT_DECLARE(ks_status_t) swclt_sess_protocol_provider_add( swclt_sess_t *sess, const char * protocol, @@ -295,24 +237,7 @@ SWCLT_DECLARE(ks_status_t) swclt_sess_execute_with_id_async( swclt_cmd_cb_t response_callback, void *response_callback_data, swclt_cmd_future_t **future); - -SWCLT_DECLARE(ks_status_t) swclt_sess_signalwire_setup(swclt_sess_t *sess, const char *service, swclt_sub_cb_t cb, void *cb_data); -SWCLT_DECLARE(ks_status_t) swclt_sess_provisioning_setup(swclt_sess_t *sess, swclt_sub_cb_t cb, void *cb_data); - -SWCLT_DECLARE(ks_status_t) swclt_sess_provisioning_configure(swclt_sess_t *sess, - const char *target, - const char *local_endpoint, - const char *external_endpoint, - const char *relay_connector_id, - swclt_cmd_reply_t **reply); -SWCLT_DECLARE(ks_status_t) swclt_sess_provisioning_configure_async(swclt_sess_t *sess, - const char *target, - const char *local_endpoint, - const char *external_endpoint, - const char *relay_connector_id, - swclt_cmd_cb_t response_callback, - void *response_callback_data, - swclt_cmd_future_t **future); + KS_END_EXTERN_C diff --git a/inc/signalwire-client-c/subscription.h b/inc/signalwire-client-c/subscription.h deleted file mode 100644 index 3f42f0e..0000000 --- a/inc/signalwire-client-c/subscription.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2018-2020 SignalWire, Inc - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#pragma once - -KS_BEGIN_EXTERN_C - -typedef struct swclt_sess swclt_sess_t; - -typedef struct swclt_sub swclt_sub_t; - -typedef void (*swclt_sub_cb_t)( - swclt_sess_t *sess, - blade_broadcast_rqu_t *rqu, - void *cb_data); - -/* A subscription represents a logical registration of - * a listener on a channel */ -struct swclt_sub { - const char *protocol; - const char *channel; - - swclt_sub_cb_t cb; - - void *cb_data; -}; - - -SWCLT_DECLARE(ks_status_t) swclt_sub_create( - swclt_sub_t **sub, - ks_pool_t *pool, - const char * const protocol, - const char * const channel, - swclt_sub_cb_t cb, - void *data); - -SWCLT_DECLARE(ks_status_t) swclt_sub_invoke(swclt_sub_t *sub, swclt_sess_t *sess, blade_broadcast_rqu_t *broadcast_rqu); -SWCLT_DECLARE(char *) swclt_sub_describe(swclt_sub_t *sub); -SWCLT_DECLARE(ks_status_t) swclt_sub_destroy(swclt_sub_t **sub); - -KS_END_EXTERN_C diff --git a/src/nodestore.c b/src/nodestore.c deleted file mode 100644 index c756eb0..0000000 --- a/src/nodestore.c +++ /dev/null @@ -1,1670 +0,0 @@ -/* - * Copyright (c) 2018-2020 SignalWire, Inc - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "signalwire-client-c/client.h" - - -static ks_status_t __add_cb_route_add(swclt_store_t *store, swclt_store_cb_route_add_t cb) -{ - ks_log(KS_LOG_DEBUG, "Adding route add handler for method: %s", BLADE_NETCAST_CMD_ROUTE_ADD); - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_ROUTE_ADD, (void *)cb); -} - -static void __invoke_cb_route_add(swclt_store_t *store, const blade_netcast_rqu_t *rqu, const blade_netcast_route_add_param_t *params) -{ - swclt_store_cb_route_add_t cb; - - ks_log(KS_LOG_DEBUG, "Looking up route add handler for method: %s", BLADE_NETCAST_CMD_ROUTE_ADD); - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_route_add_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_ROUTE_ADD, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - - if (cb) { - ks_log(KS_LOG_DEBUG, "Invoking callback for node store add"); - cb(store->sess, rqu, params); - } else { - ks_log(KS_LOG_DEBUG, "No callback registered for route add method: %s", BLADE_NETCAST_CMD_ROUTE_ADD); - } -} - -static ks_status_t __add_cb_route_remove(swclt_store_t *store, swclt_store_cb_route_remove_t cb) -{ - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_ROUTE_REMOVE, (void *)cb); -} - -static void __invoke_cb_route_remove(swclt_store_t *store, const blade_netcast_rqu_t *rqu, const blade_netcast_route_remove_param_t *params) -{ - swclt_store_cb_route_remove_t cb; - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_route_remove_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_ROUTE_REMOVE, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - if (cb) cb(store->sess, rqu, params); -} - -static ks_status_t __add_cb_identity_add(swclt_store_t *store, swclt_store_cb_identity_add_t cb) -{ - ks_log(KS_LOG_DEBUG, "Adding identity add handler for method: %s", BLADE_NETCAST_CMD_IDENTITY_ADD); - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_IDENTITY_ADD, (void *)cb); -} - -static void __invoke_cb_identity_add(swclt_store_t *store, const blade_netcast_rqu_t *rqu, const blade_netcast_identity_add_param_t *params) -{ - swclt_store_cb_identity_add_t cb; - - ks_log(KS_LOG_DEBUG, "Looking up identity add handler for method: %s", BLADE_NETCAST_CMD_IDENTITY_ADD); - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_identity_add_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_IDENTITY_ADD, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - - if (cb) { - ks_log(KS_LOG_DEBUG, "Invoking callback for node store identity add"); - cb(store->sess, rqu, params); - } else { - ks_log(KS_LOG_DEBUG, "No callback registered for identity add method: %s", BLADE_NETCAST_CMD_IDENTITY_ADD); - } -} - -static ks_status_t __add_cb_identity_remove(swclt_store_t *store, swclt_store_cb_identity_remove_t cb) -{ - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_IDENTITY_REMOVE, (void *)cb); -} - -static void __invoke_cb_identity_remove(swclt_store_t *store, const blade_netcast_rqu_t *rqu, const blade_netcast_identity_remove_param_t *params) -{ - swclt_store_cb_identity_remove_t cb; - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_identity_remove_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_IDENTITY_REMOVE, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - if (cb) cb(store->sess, rqu, params); -} - -static ks_status_t __add_cb_protocol_add(swclt_store_t *store, swclt_store_cb_protocol_add_t cb) -{ - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_PROTOCOL_ADD, (void *)cb); -} - -static void __invoke_cb_protocol_add(swclt_store_t *store, - const char *protocol) -{ - swclt_store_cb_protocol_add_t cb; - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_protocol_add_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_PROTOCOL_ADD, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - if (cb) cb(store->sess, protocol); -} - -static ks_status_t __add_cb_protocol_remove(swclt_store_t *store, swclt_store_cb_protocol_remove_t cb) -{ - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_PROTOCOL_REMOVE, (void *)cb); -} - -static void __invoke_cb_protocol_remove(swclt_store_t *store, - const char *protocol) -{ - swclt_store_cb_protocol_remove_t cb; - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_protocol_remove_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_PROTOCOL_REMOVE, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - if (cb) cb(store->sess, protocol); -} - -static ks_status_t __add_cb_protocol_provider_add(swclt_store_t *store, swclt_store_cb_protocol_provider_add_t cb) -{ - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_ADD, (void *)cb); -} - -static void __invoke_cb_protocol_provider_add(swclt_store_t *store, - const blade_netcast_rqu_t *rqu, - const blade_netcast_protocol_provider_add_param_t *params) -{ - swclt_store_cb_protocol_provider_add_t cb; - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_protocol_provider_add_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_ADD, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - if (cb) cb(store->sess, rqu, params); -} - -static ks_status_t __add_cb_protocol_provider_remove(swclt_store_t *store, swclt_store_cb_protocol_provider_remove_t cb) -{ - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_REMOVE, (void *)cb); -} - -static void __invoke_cb_protocol_provider_remove(swclt_store_t *store, - const blade_netcast_rqu_t *rqu, - const blade_netcast_protocol_provider_remove_param_t *params) -{ - swclt_store_cb_protocol_provider_remove_t cb; - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_protocol_provider_remove_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_REMOVE, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - if (cb) cb(store->sess, rqu, params); -} - -static ks_status_t __add_cb_protocol_provider_rank_update(swclt_store_t *store, swclt_store_cb_protocol_provider_rank_update_t cb) -{ - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_RANK_UPDATE, (void *)cb); -} - -static void __invoke_cb_protocol_provider_rank_update(swclt_store_t *store, - const blade_netcast_rqu_t *rqu, - const blade_netcast_protocol_provider_rank_update_param_t *params) -{ - swclt_store_cb_protocol_provider_rank_update_t cb; - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_protocol_provider_rank_update_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_RANK_UPDATE, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - if (cb) cb(store->sess, rqu, params); -} - -static ks_status_t __add_cb_protocol_provider_data_update(swclt_store_t *store, swclt_store_cb_protocol_provider_data_update_t cb) -{ - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_DATA_UPDATE, (void *)cb); -} - -static void __invoke_cb_protocol_provider_data_update(swclt_store_t *store, - const blade_netcast_rqu_t *rqu, - const blade_netcast_protocol_provider_data_update_param_t *params) -{ - swclt_store_cb_protocol_provider_data_update_t cb; - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_protocol_provider_data_update_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_DATA_UPDATE, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - if (cb) cb(store->sess, rqu, params); -} - -static ks_status_t __add_cb_authority_add(swclt_store_t *store, swclt_store_cb_authority_add_t cb) -{ - ks_log(KS_LOG_DEBUG, "Adding authority add handler for method: %s", BLADE_NETCAST_CMD_AUTHORITY_ADD); - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_AUTHORITY_ADD, (void *)cb); -} - -static void __invoke_cb_authority_add(swclt_store_t *store, const blade_netcast_rqu_t *rqu, const blade_netcast_authority_add_param_t *params) -{ - swclt_store_cb_authority_add_t cb; - - ks_log(KS_LOG_DEBUG, "Looking up authority add handler for method: %s", BLADE_NETCAST_CMD_AUTHORITY_ADD); - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_authority_add_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_AUTHORITY_ADD, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - - if (cb) { - ks_log(KS_LOG_DEBUG, "Invoking callback for node store add"); - cb(store->sess, rqu, params); - } else { - ks_log(KS_LOG_DEBUG, "No callback registered for authority add method: %s", BLADE_NETCAST_CMD_AUTHORITY_ADD); - } -} - -static ks_status_t __add_cb_authority_remove(swclt_store_t *store, swclt_store_cb_authority_remove_t cb) -{ - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_AUTHORITY_REMOVE, (void *)cb); -} - -static void __invoke_cb_authority_remove(swclt_store_t *store, const blade_netcast_rqu_t *rqu, const blade_netcast_authority_remove_param_t *params) -{ - swclt_store_cb_authority_remove_t cb; - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_authority_remove_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_AUTHORITY_REMOVE, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - if (cb) cb(store->sess, rqu, params); -} - -static ks_status_t __add_cb_subscription_add(swclt_store_t *store, swclt_store_cb_subscription_add_t cb) -{ - ks_log(KS_LOG_DEBUG, "Adding authority add handler for method: %s", BLADE_NETCAST_CMD_SUBSCRIPTION_ADD); - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_SUBSCRIPTION_ADD, (void *)cb); -} - -static void __invoke_cb_subscription_add(swclt_store_t *store, const blade_netcast_rqu_t *rqu, const blade_netcast_subscription_add_param_t *params) -{ - swclt_store_cb_subscription_add_t cb; - - ks_log(KS_LOG_DEBUG, "Looking up subscription add handler for method: %s", BLADE_NETCAST_CMD_SUBSCRIPTION_ADD); - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_subscription_add_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_SUBSCRIPTION_ADD, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - - if (cb) { - ks_log(KS_LOG_DEBUG, "Invoking callback for node store add"); - cb(store->sess, rqu, params); - } else { - ks_log(KS_LOG_DEBUG, "No callback registered for subscription add method: %s", BLADE_NETCAST_CMD_SUBSCRIPTION_ADD); - } -} - -static ks_status_t __add_cb_subscription_remove(swclt_store_t *store, swclt_store_cb_subscription_remove_t cb) -{ - return ks_hash_insert(store->callbacks, (const void *)BLADE_NETCAST_CMD_SUBSCRIPTION_REMOVE, (void *)cb); -} - -static void __invoke_cb_subscription_remove(swclt_store_t *store, const blade_netcast_rqu_t *rqu, const blade_netcast_subscription_remove_param_t *params) -{ - swclt_store_cb_subscription_remove_t cb; - - ks_hash_read_lock(store->callbacks); - cb = (swclt_store_cb_subscription_remove_t)ks_hash_search(store->callbacks, - (const void *)BLADE_NETCAST_CMD_SUBSCRIPTION_REMOVE, - KS_UNLOCKED); - ks_hash_read_unlock(store->callbacks); - - if (cb) cb(store->sess, rqu, params); -} - -static void __destroy_protocol(void *protocol) -{ - blade_protocol_t *proto = (blade_protocol_t *)protocol; - BLADE_PROTOCOL_DESTROY(&proto); -} - -static ks_status_t __add_protocol_obj(swclt_store_t *store, ks_json_t *obj) -{ - blade_protocol_t *protocol; - ks_status_t status; - - if (status = BLADE_PROTOCOL_PARSE(store->pool, obj, &protocol)) { - ks_log(KS_LOG_ERROR, "Failed to parse protocol: %d", status); - return status; - } - - - if (status = ks_hash_insert(store->protocols, protocol->name, protocol)) { - ks_log(KS_LOG_ERROR, "Failed to insert protocol: %d", status); - BLADE_PROTOCOL_DESTROY(&protocol); - return status; - } - - return KS_STATUS_SUCCESS; -} - -static ks_status_t __add_protocol_uncertified_obj(swclt_store_t *store, ks_json_t *obj) -{ - char *key; - ks_status_t status; - - if (!ks_json_type_is_string(obj)) { - status = KS_STATUS_ARG_INVALID; - ks_log(KS_LOG_ERROR, "Failed to parse protocol uncertified: %d", status); - return status; - } - const char *str; - ks_json_value_string(obj, &str); - key = ks_pstrdup(ks_pool_get(store->protocols_uncertified), str); - - if (status = ks_hash_insert(store->protocols_uncertified, key, (void *)KS_TRUE)) { - ks_log(KS_LOG_ERROR, "Failed to insert protocol: %d", status); - ks_pool_free(&key); - return status; - } - - return KS_STATUS_SUCCESS; -} - -static void __destroy_node(void *node) -{ - blade_node_t *n = (blade_node_t *)node; - BLADE_NODE_DESTROY(&n); -} - -static ks_status_t __add_route_obj(swclt_store_t *store, ks_json_t *obj) -{ - blade_node_t *node; - ks_status_t status; - - if (status = BLADE_NODE_PARSE(store->pool, obj, &node)) { - ks_log(KS_LOG_ERROR, "Failed to parse route: %d", status); - return status; - } - - if (status = ks_hash_insert(store->routes, ks_pstrdup(store->pool, node->nodeid), node)) { - ks_log(KS_LOG_ERROR, "Failed to insert route: %d", status); - BLADE_NODE_DESTROY(&node); - return status; - } - - return KS_STATUS_SUCCESS; -} - -static ks_status_t __lookup_protocol(swclt_store_t *store, const char *name, blade_protocol_t **protocol) -{ - blade_protocol_t *found_protocol = (blade_protocol_t *) - ks_hash_search(store->protocols, name, KS_UNLOCKED); - - if (found_protocol) { - if (protocol) - *protocol = found_protocol; - return KS_STATUS_SUCCESS; - } - - return KS_STATUS_NOT_FOUND; -} - -static ks_status_t __lookup_protocol_uncertified(swclt_store_t *store, const char *name) -{ - if (ks_hash_search(store->protocols_uncertified, name, KS_UNLOCKED) == NULL) return KS_STATUS_NOT_FOUND; - return KS_STATUS_SUCCESS; -} - -static void __remove_identities_by_nodeid(swclt_store_t *store, const char *nodeid) -{ - ks_hash_iterator_t *itt; - ks_hash_t *cleanup = NULL; - - ks_hash_write_lock(store->identities); - // iterate all identities - for (itt = ks_hash_first(store->identities, KS_UNLOCKED); itt; itt = ks_hash_next(&itt)) { - const char *val; - const char *key; - - ks_hash_this(itt, (const void **)&key, NULL, (void **)&val); - - if (strcmp(nodeid, val)) continue; - - if (!cleanup) - ks_hash_create(&cleanup, KS_HASH_MODE_CASE_INSENSITIVE, KS_HASH_FLAG_NOLOCK, store->pool); - - ks_log(KS_LOG_INFO, "Removing identity %s from node %s", key, val); - - ks_hash_insert(cleanup, key, (void *)KS_TRUE); - } - - if (cleanup) { - for (itt = ks_hash_first(cleanup, KS_UNLOCKED); itt; itt = ks_hash_next(&itt)) { - const char *key = NULL; - void *val = NULL; - - ks_hash_this(itt, (const void **)&key, NULL, (void **)&val); - - ks_hash_remove(store->identities, (const void *)key); - } - ks_hash_destroy(&cleanup); - } - - ks_hash_write_unlock(store->identities); -} - -static ks_status_t __get_node_identities(swclt_store_t *store, - const char *nodeid, - ks_pool_t *pool, - ks_hash_t **identities) -{ - ks_status_t status = KS_STATUS_SUCCESS; - ks_hash_iterator_t *itt; - - ks_hash_create(identities, KS_HASH_MODE_CASE_INSENSITIVE, KS_HASH_FLAG_FREE_KEY, pool); - - ks_hash_read_lock(store->identities); - // iterate all identities - for (itt = ks_hash_first(store->identities, KS_UNLOCKED); itt; itt = ks_hash_next(&itt)) { - const char *val; - const char *key; - - ks_hash_this(itt, (const void **)&key, NULL, (void **)&val); - - if (strcmp(nodeid, val)) continue; - - ks_hash_insert(*identities, ks_pstrdup(pool, key), (void *)KS_TRUE); - } - - ks_hash_read_unlock(store->identities); -} - -static void __remove_provider_from_protocols(swclt_store_t *store, const char *nodeid) -{ - ks_hash_iterator_t *itt; - ks_hash_t *cleanup = NULL; - ks_json_t *entry = NULL; - - ks_log(KS_LOG_INFO, "Request to remove provider %s", nodeid); - - ks_hash_write_lock(store->protocols); - // iterate all protocols - for (itt = ks_hash_first(store->protocols, KS_UNLOCKED); itt; ) { - blade_protocol_t *protocol; - const char *key; - int32_t index = 0; - - ks_hash_this(itt, (const void **)&key, NULL, (void **)&protocol); - - itt = ks_hash_next(&itt); - - ks_log(KS_LOG_INFO, "Iterating protocol %s with %lu providers", - protocol->name, ks_json_get_array_size(protocol->providers)); - - // iterate protocol providers - KS_JSON_ARRAY_FOREACH(entry, protocol->providers) { - blade_provider_t *provider; - ks_bool_t match = KS_FALSE; - - // parse provider - ks_assertd(!BLADE_PROVIDER_PARSE(store->pool, entry, &provider)); - - ks_log(KS_LOG_INFO, "Iterating provider %s", provider->nodeid); - - if (!strcmp(provider->nodeid, nodeid)) { - match = KS_TRUE; - - ks_log(KS_LOG_INFO, "Removing provider %s from protocol %s", provider->nodeid, protocol->name); - - // delete provider from providers - ks_json_delete_item_from_array(protocol->providers, index); - - if (ks_json_get_array_size(protocol->providers) == 0) { - ks_log(KS_LOG_INFO, "No more providers present in protocol %s, removing", protocol->name); - - // Lazily init the cleanup hash which we'll walk if we decide to remove this at the end - if (!cleanup) - ks_hash_create(&cleanup, KS_HASH_MODE_CASE_INSENSITIVE, KS_HASH_FLAG_NOLOCK, store->pool); - - // Just add the protocol no dupe her needed as the protocol and name are one item, we'll delete - // the protocol which will free the key too when we iterate the cleanup hash below - ks_hash_insert(cleanup, protocol->name, (void *)protocol); - } else { - ks_log(KS_LOG_INFO, "%lu providers remain in protocol %s, not removing", ks_json_get_array_size(protocol->providers), protocol->name); - } - } - // cleanup parsed provider - BLADE_PROVIDER_DESTROY(&provider); - - // if provider was found, stop searching - if (match) break; - ++index; - } - } - - // cleanup protocols with no providers left - if (cleanup) { - for (itt = ks_hash_first(cleanup, KS_UNLOCKED); itt; ) { - const char *key = NULL; - blade_protocol_t *protocol = NULL; - - ks_hash_this(itt, (const void **)&key, NULL, (void **)&protocol); - - itt = ks_hash_next(&itt); - char *protocol_name = ks_pstrdup(store->pool, protocol->name); - ks_hash_remove(store->protocols, (const void *)key); // now destroy it... - __invoke_cb_protocol_remove(store, protocol_name); - ks_pool_free(&protocol_name); - } - ks_hash_destroy(&cleanup); - } - - ks_hash_write_unlock(store->protocols); -} - -static ks_status_t __select_random_protocol_provider(swclt_store_t *store, const char *name, ks_pool_t *pool, char **providerid) -{ - blade_protocol_t *protocol = NULL; - int32_t count = 0; - int32_t index = 0; - ks_json_t *entry = NULL; - blade_provider_t *provider = NULL; - ks_status_t status = KS_STATUS_SUCCESS; - - *providerid = NULL; - - ks_hash_write_lock(store->protocols); - - // find the protocol - if (status = __lookup_protocol(store, name, &protocol)) - goto done; - - // get provider count - if ((count = ks_json_get_array_size(protocol->providers)) == 0) { - status = KS_STATUS_NOT_FOUND; - goto done; - } - - // pick random provider - index = (int32_t)(rand() % ks_json_get_array_size(protocol->providers)); - // parse provider - entry = ks_json_get_array_item(protocol->providers, index); - ks_assertd(!BLADE_PROVIDER_PARSE(store->pool, entry, &provider)); - - // copy provider id - *providerid = ks_pstrdup(pool, provider->nodeid); - - // cleanup provider - BLADE_PROVIDER_DESTROY(&provider); - -done: - ks_hash_write_unlock(store->protocols); - - return status; -} - -static ks_status_t __get_protocols(swclt_store_t *store, - ks_json_t **protocols) -{ - ks_status_t status = KS_STATUS_SUCCESS; - - *protocols = ks_json_create_array(); - - ks_hash_read_lock(store->protocols); - - for (ks_hash_iterator_t *it = ks_hash_first(store->protocols, KS_UNLOCKED); it; it = ks_hash_next(&it)) { - const char *key = NULL; - blade_protocol_t *proto = NULL; - - ks_hash_this(it, (const void **)&key, NULL, (void **)&proto); - - ks_json_add_string_to_array(*protocols, key); - } - - ks_hash_read_unlock(store->protocols); - return status; -} - -static ks_status_t __get_protocol_providers(swclt_store_t *store, - const char *name, - ks_json_t **providers) -{ - ks_status_t status = KS_STATUS_SUCCESS; - blade_protocol_t *protocol = NULL; - - *providers = NULL; - - ks_hash_read_lock(store->protocols); - - if (status = __lookup_protocol(store, name, &protocol)) - goto done; - - if (ks_json_get_array_size(protocol->providers) == 0) { - status = KS_STATUS_NOT_FOUND; - goto done; - } - - *providers = ks_json_duplicate(protocol->providers, KS_TRUE); - -done: - ks_hash_read_unlock(store->protocols); - return status; -} - -static ks_status_t __add_authority_obj(swclt_store_t *store, ks_json_t *obj) -{ - blade_netcast_authority_add_param_t *params; - ks_status_t status; - - if (status = BLADE_NETCAST_AUTHORITY_ADD_PARAM_PARSE(store->pool, obj, ¶ms)) { - ks_log(KS_LOG_ERROR, "Failed to parse authority: %d", status); - return status; - } - - if (status = ks_hash_insert(store->authorities, ks_pstrdup(store->pool, params->nodeid), (void *)KS_TRUE)) { - ks_log(KS_LOG_ERROR, "Failed to insert authority: %d", status); - BLADE_NETCAST_AUTHORITY_ADD_PARAM_DESTROY(¶ms); - return status; - } - - BLADE_NETCAST_AUTHORITY_ADD_PARAM_DESTROY(¶ms); - - return KS_STATUS_SUCCESS; -} - -// Protocol add/remove for uncertified clients only -static ks_status_t __update_protocol_add(swclt_store_t *store, const blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_protocol_add_param_t *params = NULL; - ks_status_t status = KS_STATUS_SUCCESS; - - if (status = BLADE_NETCAST_PROTOCOL_ADD_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - ks_log(KS_LOG_INFO, "Request to add new protocol %s", params->protocol); - - /* Lookup the protocol */ - if (status = __lookup_protocol_uncertified(store, params->protocol)) { - char *key = ks_pstrdup(ks_pool_get(store->protocols_uncertified), params->protocol); - - ks_log(KS_LOG_INFO, "Protocol %s does not exist yet, adding new entry", params->protocol); - - /* And add it */ - if (status = ks_hash_insert(store->protocols_uncertified, key, (void *)KS_TRUE)) { - ks_pool_free(&key); - BLADE_NETCAST_PROTOCOL_ADD_PARAM_DESTROY(¶ms); - return status; - } - - ks_log(KS_LOG_INFO, "Protocol %s added", params->protocol); - - __invoke_cb_protocol_add(store, params->protocol); - - return status; - - } - - ks_log(KS_LOG_INFO, "Protocol %s exists already", params->protocol); - - BLADE_NETCAST_PROTOCOL_ADD_PARAM_DESTROY(¶ms); - - return status; -} - -static ks_status_t __update_protocol_remove(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_protocol_remove_param_t *params = NULL; - ks_status_t status = KS_STATUS_SUCCESS; - ks_bool_t match = KS_FALSE; - - if (status = BLADE_NETCAST_PROTOCOL_REMOVE_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - match = ks_hash_remove(store->protocols_uncertified, (const void *)params->protocol) != NULL; - -done: - - if (match) __invoke_cb_protocol_remove(store, params->protocol); - - BLADE_NETCAST_PROTOCOL_REMOVE_PARAM_DESTROY(¶ms); - - return status; -} - -// Provider add/remove -static ks_status_t __update_protocol_provider_add(swclt_store_t *store, const blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_protocol_provider_add_param_t *params = NULL; - blade_protocol_t *protocol = NULL; - ks_status_t status = KS_STATUS_SUCCESS; - ks_json_t *provider_data = NULL; - - if (status = BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - ks_log(KS_LOG_INFO, "Request to add new provider %s for protocol %s", params->nodeid, params->protocol); - - ks_hash_write_lock(store->protocols); - - /* Lookup the protocol */ - if (status = __lookup_protocol(store, params->protocol, &protocol)) { - ks_log(KS_LOG_INFO, "Protocol %s does not exist yet, adding new entry", params->protocol); - - /* Gotta add a new one */ - if (!(protocol = ks_pool_alloc(store->pool, sizeof(blade_protocol_t)))) { - ks_hash_write_unlock(store->protocols); - BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM_DESTROY(¶ms); - return KS_STATUS_NO_MEM; - } - - protocol->channels = ks_json_duplicate(params->channels, KS_TRUE); - - protocol->default_channel_broadcast_access = params->default_channel_broadcast_access; - protocol->default_channel_subscribe_access = params->default_channel_subscribe_access; - protocol->default_method_execute_access = params->default_method_execute_access; - - protocol->name = ks_pstrdup(store->pool, params->protocol); - - if (params->data) { - provider_data = ks_json_duplicate(params->data, KS_TRUE); - } - - if (!(protocol->providers = ks_json_create_array())) { - ks_json_delete(&protocol->channels); - ks_pool_free(&protocol); - ks_json_delete(&provider_data); - BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM_DESTROY(¶ms); - ks_hash_write_unlock(store->protocols); - return KS_STATUS_NO_MEM; - } - ks_json_add_item_to_array(protocol->providers, - BLADE_PROVIDER_MARSHAL( - &(blade_provider_t){params->nodeid, NULL, params->rank, provider_data} - ) - ); - - /* And add it */ - if (status = ks_hash_insert(store->protocols, protocol->name, protocol)) { - ks_json_delete(&protocol->channels); - ks_json_delete(&protocol->providers); - ks_pool_free(&protocol); - BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM_DESTROY(¶ms); - ks_hash_write_unlock(store->protocols); - return status; - - } - - ks_log(KS_LOG_INFO, "Protocol %s added with provider %s", protocol->name, params->nodeid); - - // @todo come back and fix this, params->protocol is going to be NULL, need to duplicate - // the string so it remains valid in params - __invoke_cb_protocol_add(store, params->protocol); - __invoke_cb_protocol_provider_add(store, netcast_rqu, params); - - ks_hash_write_unlock(store->protocols); - - BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM_DESTROY(¶ms); - - return status; - - } - - ks_log(KS_LOG_INFO, "Protocol %s exists already, has %lu providers currently", - params->protocol, ks_json_get_array_size(protocol->providers)); - - /* Now add any provider entries to the protocol */ - if (params->data) { - provider_data = ks_json_duplicate(params->data, KS_TRUE); - } - ks_json_add_item_to_array(protocol->providers, - BLADE_PROVIDER_MARSHAL(&(blade_provider_t){params->nodeid, NULL, params->rank, provider_data})); - - ks_log(KS_LOG_INFO, "Protocol %s add complete, provider count %lu", protocol->name, ks_json_get_array_size(protocol->providers)); - - __invoke_cb_protocol_provider_add(store, netcast_rqu, params); - - BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM_DESTROY(¶ms); - - ks_hash_write_unlock(store->protocols); - - return status; -} - -static ks_status_t __update_protocol_provider_remove(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_protocol_provider_remove_param_t *params = NULL; - blade_protocol_t *protocol = NULL; - ks_json_t *entry = NULL; - int32_t index = 0; - ks_status_t status = KS_STATUS_SUCCESS; - ks_bool_t match = KS_FALSE; - ks_bool_t cleanup = KS_FALSE; - - if (status = BLADE_NETCAST_PROTOCOL_PROVIDER_REMOVE_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - ks_hash_write_lock(store->protocols); - - if (status = __lookup_protocol(store, params->protocol, &protocol)) { - ks_log(KS_LOG_WARNING, "Received protocol provider remove for protocol '%s' which does not exist", params->protocol); - status = KS_STATUS_SUCCESS; - goto done; - } - - // iterate protocol providers - KS_JSON_ARRAY_FOREACH(entry, protocol->providers) { - blade_provider_t *provider; - - // parse provider - ks_assertd(!BLADE_PROVIDER_PARSE(store->pool, entry, &provider)); - - if (!strcmp(provider->nodeid, params->nodeid)) { - match = KS_TRUE; - - // delete provider from providers - ks_json_delete_item_from_array(protocol->providers, index); - - cleanup = ks_json_get_array_size(protocol->providers) == 0; - } - // cleanup parsed provider - BLADE_PROVIDER_DESTROY(&provider); - - // if provider was found, stop searching - if (match) break; - ++index; - } - - if (cleanup) { - // cleanup protocol if no providers left - char *protocol_name = ks_pstrdup(store->pool, protocol->name); - ks_hash_remove(store->protocols, (const void *)protocol->name); // now destroy it... - __invoke_cb_protocol_remove(store, protocol_name); - ks_pool_free(&protocol_name); - } - -done: - ks_hash_write_unlock(store->protocols); - - if (match) __invoke_cb_protocol_provider_remove(store, netcast_rqu, params); - - BLADE_NETCAST_PROTOCOL_PROVIDER_REMOVE_PARAM_DESTROY(¶ms); - - return status; -} - -// Provider rank update -static ks_status_t __update_protocol_provider_rank_update(swclt_store_t *store, const blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_protocol_provider_rank_update_param_t *params = NULL; - blade_protocol_t *protocol = NULL; - ks_json_t *entry = NULL; - blade_provider_t *provider = NULL; - ks_status_t status = KS_STATUS_SUCCESS; - ks_bool_t found = KS_FALSE; - - if (status = BLADE_NETCAST_PROTOCOL_PROVIDER_RANK_UPDATE_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - ks_hash_write_lock(store->protocols); - - ks_log(KS_LOG_INFO, "Request to update rank for provider %s to %d for protocol %s", params->nodeid, params->rank, params->protocol); - - /* Lookup the protocol */ - if (status = __lookup_protocol(store, params->protocol, &protocol)) - goto done; - - // find provider - for (int32_t index = 0; index < ks_json_get_array_size(protocol->providers); ++index) { - entry = ks_json_get_array_item(protocol->providers, index); - - const char *provider_nodeid = ks_json_get_object_string(entry, "nodeid", ""); - if (!strcmp(provider_nodeid, params->nodeid)) { - found = KS_TRUE; - ks_json_delete_item_from_object(entry, "rank"); - ks_json_add_number_to_object(entry, "rank", params->rank); - } - } - -done: - ks_hash_write_unlock(store->protocols); - - if (found) __invoke_cb_protocol_provider_rank_update(store, netcast_rqu, params); - - BLADE_NETCAST_PROTOCOL_PROVIDER_RANK_UPDATE_PARAM_DESTROY(¶ms); - - return status; -} - -// Provider data update -static ks_status_t __update_protocol_provider_data_update(swclt_store_t *store, const blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_protocol_provider_data_update_param_t *params = NULL; - blade_protocol_t *protocol = NULL; - ks_json_t *entry = NULL; - blade_provider_t *provider = NULL; - ks_status_t status = KS_STATUS_SUCCESS; - ks_bool_t found = KS_FALSE; - - if (status = BLADE_NETCAST_PROTOCOL_PROVIDER_DATA_UPDATE_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - ks_hash_write_lock(store->protocols); - - ks_log(KS_LOG_INFO, "Request to update data for provider %s for protocol %s", params->nodeid, params->protocol); - - /* Lookup the protocol */ - if (status = __lookup_protocol(store, params->protocol, &protocol)) - goto done; - - // find provider - for (int32_t index = 0; index < ks_json_get_array_size(protocol->providers); ++index) { - entry = ks_json_get_array_item(protocol->providers, index); - const char *provider_nodeid = ks_json_get_object_string(entry, "nodeid", ""); - if (!strcmp(provider_nodeid, params->nodeid)) { - found = KS_TRUE; - ks_json_delete_item_from_object(entry, "data"); - ks_json_add_item_to_object(entry, "data", ks_json_duplicate(params->data, KS_TRUE)); - } - } - -done: - ks_hash_write_unlock(store->protocols); - - if (found) __invoke_cb_protocol_provider_data_update(store, netcast_rqu, params); - - BLADE_NETCAST_PROTOCOL_PROVIDER_DATA_UPDATE_PARAM_DESTROY(¶ms); - - return status; -} - - -// Route add/remove -static ks_status_t __update_route_add(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_route_add_param_t *params; - ks_status_t status; - - if (status = BLADE_NETCAST_ROUTE_ADD_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - status = __add_route_obj(store, netcast_rqu->params); - - __invoke_cb_route_add(store, netcast_rqu, params); - - BLADE_NETCAST_ROUTE_ADD_PARAM_DESTROY(¶ms); - - return status; -} - -static ks_status_t __update_route_remove(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_route_remove_param_t *params; - ks_status_t status; - - if (status = BLADE_NETCAST_ROUTE_REMOVE_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - __invoke_cb_route_remove(store, netcast_rqu, params); - - /* Remove the node from the hash */ - ks_hash_remove(store->routes, params->nodeid); - - /* Remove the identities that map to the nodeid */ - __remove_identities_by_nodeid(store, params->nodeid); - - /* Now we have to rummage through the protocols hash and remove any protocols hosted by this node */ - __remove_provider_from_protocols(store, params->nodeid); - - // @todo purge subscribers with the given nodeid - - ks_hash_remove(store->authorities, params->nodeid); - //ks_hash_remove(store->accesses, params->nodeid); - - /* Done with the request */ - BLADE_NETCAST_ROUTE_REMOVE_PARAM_DESTROY(¶ms); - - return KS_STATUS_SUCCESS; -} - -// Identity add/remove -static ks_status_t __update_identity_add(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_identity_add_param_t *params; - ks_status_t status; - - if (status = BLADE_NETCAST_IDENTITY_ADD_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - if (status = ks_hash_insert(store->identities, ks_pstrdup(store->pool, params->identity), ks_pstrdup(store->pool, params->nodeid))) { - ks_log(KS_LOG_ERROR, "Failed to insert identity: %d", status); - goto done; - } - - __invoke_cb_identity_add(store, netcast_rqu, params); - -done: - BLADE_NETCAST_IDENTITY_ADD_PARAM_DESTROY(¶ms); - - return status; -} - -static ks_status_t __update_identity_remove(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_identity_remove_param_t *params; - ks_status_t status; - const char *nodeid = NULL; - - if (status = BLADE_NETCAST_IDENTITY_REMOVE_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - ks_hash_write_lock(store->identities); - - /* Make sure the identity is owned by the right nodeid */ - nodeid = (const char *)ks_hash_search(store->identities, params->identity, KS_UNLOCKED); - if (nodeid && !strcmp(nodeid, params->nodeid)) - { - /* Remove the identity from the hash */ - __invoke_cb_identity_remove(store, netcast_rqu, params); - ks_hash_remove(store->identities, params->identity); - } - - ks_hash_write_unlock(store->identities); - - /* Done with the request */ - BLADE_NETCAST_IDENTITY_REMOVE_PARAM_DESTROY(¶ms); - - return KS_STATUS_SUCCESS; -} - -// Channel add/remove -static ks_status_t __update_channel_add(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - /* @@ TODO */ - return KS_STATUS_SUCCESS; -} - -static ks_status_t __update_channel_remove(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - /* @@ TODO */ - return KS_STATUS_SUCCESS; -} - -// Subscription add/remove -static ks_status_t __update_subscription_add(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_subscription_add_param_t *params; - ks_status_t status; - - if (status = BLADE_NETCAST_SUBSCRIPTION_ADD_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - /* @TODO add subscription to store->subscriptions */ - - __invoke_cb_subscription_add(store, netcast_rqu, params); - - BLADE_NETCAST_SUBSCRIPTION_ADD_PARAM_DESTROY(¶ms); - - return status; -} - -static ks_status_t __update_subscription_remove(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_subscription_remove_param_t *params; - ks_status_t status; - - if (status = BLADE_NETCAST_SUBSCRIPTION_REMOVE_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - /* @TODO Remove the subscription from store->subscriptions */ - - __invoke_cb_subscription_remove(store, netcast_rqu, params); - - /* Done with the request */ - BLADE_NETCAST_SUBSCRIPTION_REMOVE_PARAM_DESTROY(¶ms); - - return KS_STATUS_SUCCESS; -} - -// Authorization add/remove -static ks_status_t __update_authorization_add(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - /* @@ TODO */ - return KS_STATUS_SUCCESS; -} - -static ks_status_t __update_authorization_remove(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - /* @@ TODO */ - return KS_STATUS_SUCCESS; -} - -// Authority add/remove -static ks_status_t __update_authority_add(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_authority_add_param_t *params; - ks_status_t status; - - if (status = BLADE_NETCAST_AUTHORITY_ADD_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - status = __add_authority_obj(store, netcast_rqu->params); - - __invoke_cb_authority_add(store, netcast_rqu, params); - - BLADE_NETCAST_AUTHORITY_ADD_PARAM_DESTROY(¶ms); - - return status; -} - -static ks_status_t __update_authority_remove(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - blade_netcast_authority_remove_param_t *params; - ks_status_t status; - - if (status = BLADE_NETCAST_AUTHORITY_REMOVE_PARAM_PARSE(store->pool, netcast_rqu->params, ¶ms)) - return status; - - /* Remove the node from the hash */ - ks_hash_remove(store->authorities, params->nodeid); - - __invoke_cb_authority_remove(store, netcast_rqu, params); - - /* Done with the request */ - BLADE_NETCAST_AUTHORITY_REMOVE_PARAM_DESTROY(¶ms); - - return KS_STATUS_SUCCESS; -} - -// Access add/remove -static ks_status_t __update_access_add(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - /* @@ TODO */ - return KS_STATUS_SUCCESS; -} - -static ks_status_t __update_access_remove(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - /* @@ TODO */ - return KS_STATUS_SUCCESS; -} - -static ks_status_t __update(swclt_store_t *store, blade_netcast_rqu_t *netcast_rqu) -{ - /* Based on the command, do the appropriate update call */ - if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_PROTOCOL_ADD)) - return __update_protocol_add(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_PROTOCOL_REMOVE)) - return __update_protocol_remove(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_ADD)) - return __update_protocol_provider_add(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_REMOVE)) - return __update_protocol_provider_remove(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_RANK_UPDATE)) - return __update_protocol_provider_rank_update(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_DATA_UPDATE)) - return __update_protocol_provider_data_update(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_ROUTE_ADD)) - return __update_route_add(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_ROUTE_REMOVE)) - return __update_route_remove(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_IDENTITY_ADD)) - return __update_identity_add(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_IDENTITY_REMOVE)) - return __update_identity_remove(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_SUBSCRIPTION_ADD)) - return __update_subscription_add(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_SUBSCRIPTION_REMOVE)) - return __update_subscription_remove(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_AUTHORITY_ADD)) - return __update_authority_add(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_AUTHORITY_REMOVE)) - return __update_authority_remove(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_AUTHORIZATION_ADD)) - return __update_authorization_add(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_AUTHORIZATION_REMOVE)) - return __update_authorization_remove(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_ACCESS_ADD)) - return __update_access_add(store, netcast_rqu); - else if (!strcmp(netcast_rqu->command, BLADE_NETCAST_CMD_ACCESS_REMOVE)) - return __update_access_remove(store, netcast_rqu); - else { - ks_log(KS_LOG_WARNING, "Unknown netcast subcommand: %s", netcast_rqu->command); - return KS_STATUS_SUCCESS; - } -} - -static ks_status_t __populate_routes(swclt_store_t *store, blade_connect_rpl_t *connect_rpl) -{ - ks_json_t *entry; - ks_status_t status; - const char *nodeid; - ks_json_t *identities; - - /* Walk the routes array and add them */ - KS_JSON_ARRAY_FOREACH(entry, connect_rpl->routes) { - if (status = __add_route_obj(store, entry)) { - ks_log(KS_LOG_ERROR, "Failed to populate route: %d", status); - return status; - } - - // Identities - nodeid = ks_json_get_object_string(entry, "nodeid", NULL); - identities = ks_json_get_object_item(entry, "identities"); - if (nodeid && identities && ks_json_type_is_array(identities)) - { - int size = ks_json_get_array_size(identities); - for (int index = 0; index < size; ++index) - { - const char *identity = ks_json_get_array_string(identities, index, NULL); - if (!identity) continue; - ks_hash_insert(store->identities, ks_pstrdup(store->pool, identity), ks_pstrdup(store->pool, nodeid)); - } - } - } - - return KS_STATUS_SUCCESS; -} - -static ks_status_t __populate_protocols(swclt_store_t *store, blade_connect_rpl_t *connect_rpl) -{ - ks_json_t *entry; - ks_status_t status; - - /* Walk the protocols and add them */ - KS_JSON_ARRAY_FOREACH(entry, connect_rpl->protocols) { - if (status = __add_protocol_obj(store, entry)) { - ks_log(KS_LOG_ERROR, "Failed to populate protocol: %d", status); - return status; - } - } - - return KS_STATUS_SUCCESS; -} - -static void __destroy_subscription(void *subscription) -{ - blade_subscription_t *sub = (blade_subscription_t *)subscription; - BLADE_SUBSCRIPTION_DESTROY(&sub); -} - -static ks_status_t __populate_subscriptions(swclt_store_t *store, blade_connect_rpl_t *connect_rpl) -{ - ks_json_t *entry; - ks_status_t status; - - /* Walk the protocols and add them */ - KS_JSON_ARRAY_FOREACH(entry, connect_rpl->subscriptions) { - blade_subscription_t *subscription; - - if (status = BLADE_SUBSCRIPTION_PARSE(store->pool, entry, &subscription)) { - ks_log(KS_LOG_ERROR, "Failed to parse subscription: %d", status); - return status; - } - - /* Subscriptions get keyed by a combo key with the channel/protocol */ - if (status = ks_hash_insert(store->subscriptions, ks_psprintf(store->pool, "%s:%s", subscription->protocol, subscription->channel), subscription)) { - ks_log(KS_LOG_ERROR, "Failed to insert subscription: %d", status); - BLADE_SUBSCRIPTION_DESTROY(&subscription); - return status; - } - } - - return KS_STATUS_SUCCESS; -} - -static ks_status_t __populate_authorities(swclt_store_t *store, blade_connect_rpl_t *connect_rpl) -{ - ks_json_t *entry; - ks_status_t status; - - /* Walk the protocols and add them */ - KS_JSON_ARRAY_FOREACH(entry, connect_rpl->authorities) { - const char *authority; - if (!ks_json_value_string(entry, &authority)) { - authority = ks_pstrdup(ks_pool_get(store->authorities), authority); - - if (status = ks_hash_insert(store->authorities, authority, (void *)KS_TRUE)) { - ks_log(KS_LOG_ERROR, "Failed to insert authority: %d", status); - ks_pool_free(&authority); - return status; - } - } - } - - return KS_STATUS_SUCCESS; -} - -static ks_status_t __populate_protocols_uncertified(swclt_store_t *store, blade_connect_rpl_t *connect_rpl) -{ - ks_json_t *entry; - ks_status_t status; - - /* Walk the protocols and add them */ - KS_JSON_ARRAY_FOREACH(entry, connect_rpl->protocols_uncertified) { - if (status = __add_protocol_uncertified_obj(store, entry)) { - ks_log(KS_LOG_ERROR, "Failed to populate protocol for uncertified client: %d", status); - return status; - } - } - - return KS_STATUS_SUCCESS; -} - -SWCLT_DECLARE(char *) swclt_store_describe(swclt_store_t *store) -{ - return ks_psprintf(store->pool, "SWCLT Store: protocols %d routes %d authorities: %d subscriptions: %d", - ks_hash_count(store->protocols), ks_hash_count(store->routes), ks_hash_count(store->authorities), ks_hash_count(store->subscriptions)); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_destroy(swclt_store_t **storeP) -{ - if (storeP && *storeP) { - swclt_store_t *store = *storeP; - ks_pool_t *pool = store->pool; - *storeP = NULL; - ks_hash_destroy(&store->callbacks); - ks_hash_destroy(&store->subscriptions); - ks_hash_destroy(&store->protocols); - ks_hash_destroy(&store->identities); - ks_hash_destroy(&store->routes); - ks_hash_destroy(&store->authorities); - ks_hash_destroy(&store->protocols_uncertified); - ks_pool_close(&pool); - } - return KS_STATUS_SUCCESS; -} - -SWCLT_DECLARE(ks_status_t) swclt_store_create(swclt_store_t **storeP) -{ - ks_status_t status; - swclt_store_t *store = NULL; - ks_pool_t *pool = NULL; - - ks_pool_open(&pool); - store = ks_pool_alloc(pool, sizeof(swclt_store_t)); - store->pool = pool; - *storeP = store; - - if (status = ks_hash_create( - &store->callbacks, - KS_HASH_MODE_INT, - KS_HASH_FLAG_DUP_CHECK | KS_HASH_FLAG_RWLOCK, - store->pool)) - goto done; - - /* Create our authorities hash, keyed by the node id of the authority */ - if (status = ks_hash_create( - &store->authorities, - KS_HASH_MODE_CASE_INSENSITIVE, - KS_HASH_FLAG_DUP_CHECK | KS_HASH_FLAG_FREE_KEY, - store->pool)) - goto done; - - /* Create our subscriptions hash, keyed by the protocol + channel name */ - if (status = ks_hash_create( - &store->subscriptions, - KS_HASH_MODE_CASE_INSENSITIVE, - KS_HASH_FLAG_DUP_CHECK | KS_HASH_FLAG_FREE_KEY, - store->pool)) - goto done; - ks_hash_set_destructor(store->subscriptions, __destroy_subscription); - - /* Create our protocols hash, keyed by the protocol name */ - if (status = ks_hash_create( - &store->protocols, - KS_HASH_MODE_CASE_INSENSITIVE, - KS_HASH_FLAG_DUP_CHECK, /* the key is inside the value struct - only need to destroy value */ - store->pool)) - goto done; - ks_hash_set_destructor(store->protocols, __destroy_protocol); - - /* Create our routes hash, keyed by the nodeid */ - if (status = ks_hash_create( - &store->routes, - KS_HASH_MODE_CASE_INSENSITIVE, - KS_HASH_FLAG_DUP_CHECK | KS_HASH_FLAG_FREE_KEY, - store->pool)) - goto done; - ks_hash_set_destructor(store->routes, __destroy_node); - - /* Create our identities hash, keyed by the identity */ - if (status = ks_hash_create( - &store->identities, - KS_HASH_MODE_CASE_INSENSITIVE, - KS_HASH_FLAG_DUP_CHECK | KS_HASH_FLAG_FREE_KEY | KS_HASH_FLAG_FREE_VALUE, - store->pool)) - goto done; - - /* Create our uncertified client protocols hash, keyed by the protocol name */ - if (status = ks_hash_create( - &store->protocols_uncertified, - KS_HASH_MODE_CASE_INSENSITIVE, - KS_HASH_FLAG_RWLOCK | KS_HASH_FLAG_DUP_CHECK | KS_HASH_FLAG_FREE_KEY, - store->pool)) - goto done; - -done: - if (status != KS_STATUS_SUCCESS) { - swclt_store_destroy(storeP); - } - - return status; -} - -SWCLT_DECLARE(ks_status_t) swclt_store_reset(swclt_store_t *store) -{ - ks_hash_iterator_t *itt; - ks_status_t status = KS_STATUS_SUCCESS; - - ks_hash_write_lock(store->routes); - for (itt = ks_hash_first(store->routes, KS_UNLOCKED); itt; ) { - const char *key = NULL; - void *value = NULL; - - ks_hash_this(itt, (const void **)&key, NULL, (void **)&value); - - itt = ks_hash_next(&itt); - ks_hash_remove(store->routes, (const void *)key); - } - ks_hash_write_unlock(store->routes); - - ks_hash_write_lock(store->identities); - for (itt = ks_hash_first(store->identities, KS_UNLOCKED); itt; ) { - const char *key = NULL; - void *value = NULL; - - ks_hash_this(itt, (const void **)&key, NULL, (void **)&value); - - itt = ks_hash_next(&itt); - ks_hash_remove(store->identities, (const void *)key); - } - ks_hash_write_unlock(store->identities); - - ks_hash_write_lock(store->protocols); - for (itt = ks_hash_first(store->protocols, KS_UNLOCKED); itt; ) { - const char *key = NULL; - blade_protocol_t *protocol = NULL; - - ks_hash_this(itt, (const void **)&key, NULL, (void **)&protocol); - - itt = ks_hash_next(&itt); - ks_hash_remove(store->protocols, (const void *)key); - } - ks_hash_write_unlock(store->protocols); - - ks_hash_write_lock(store->subscriptions); - for (itt = ks_hash_first(store->subscriptions, KS_UNLOCKED); itt; ) { - const char *key = NULL; - void *value = NULL; - - ks_hash_this(itt, (const void **)&key, NULL, (void **)&value); - - itt = ks_hash_next(&itt); - ks_hash_remove(store->subscriptions, (const void *)key); - } - ks_hash_write_unlock(store->subscriptions); - - ks_hash_write_lock(store->authorities); - for (itt = ks_hash_first(store->authorities, KS_UNLOCKED); itt; ) { - const char *key = NULL; - void *value = NULL; - - ks_hash_this(itt, (const void **)&key, NULL, (void **)&value); - - itt = ks_hash_next(&itt); - ks_hash_remove(store->authorities, (const void *)key); - } - ks_hash_write_unlock(store->authorities); - - ks_hash_write_lock(store->protocols_uncertified); - for (itt = ks_hash_first(store->protocols_uncertified, KS_UNLOCKED); itt; ) { - const char *key = NULL; - void *value = NULL; - - ks_hash_this(itt, (const void **)&key, NULL, (void **)&value); - - itt = ks_hash_next(&itt); - ks_hash_remove(store->protocols_uncertified, (const void *)key); - } - ks_hash_write_unlock(store->protocols_uncertified); - - return status; -} - -SWCLT_DECLARE(ks_status_t) swclt_store_populate(swclt_store_t *store, const blade_connect_rpl_t *connect_rpl) -{ - blade_connect_rpl_t *rpl = (blade_connect_rpl_t *)connect_rpl; - ks_status_t status = KS_STATUS_SUCCESS; - - /* Now popualte the fields from the connect result */ - if (status = __populate_routes(store, rpl)) - return status; - - if (status = __populate_protocols(store, rpl)) - return status; - - if (status = __populate_subscriptions(store, rpl)) - return status; - - if (status = __populate_authorities(store, rpl)) - return status; - - if (status = __populate_protocols_uncertified(store, rpl)) - return status; - - return status; -} - -SWCLT_DECLARE(ks_status_t) swclt_store_get_node_identities(swclt_store_t *store, - const char *nodeid, - ks_pool_t *pool, - ks_hash_t **identities) -{ - return __get_node_identities(store, nodeid, pool, identities); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_get_protocols(swclt_store_t *store, ks_json_t **protocols) -{ - return __get_protocols(store, protocols); -} - -/* Returns success if the protocol exists and the store is valid, also works for uncertified client protocols check. */ -SWCLT_DECLARE(ks_status_t) swclt_store_check_protocol(swclt_store_t *store, const char *name) -{ - ks_status_t status = KS_STATUS_SUCCESS; - if (status = __lookup_protocol(store, name, NULL)) { - status = __lookup_protocol_uncertified(store, name); - } - return status; -} - -SWCLT_DECLARE(ks_status_t) swclt_store_select_random_protocol_provider( - swclt_store_t *store, - const char *name, - ks_pool_t *pool, - char **providerid) -{ - return __select_random_protocol_provider(store, name, pool, providerid); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_get_protocol_providers(swclt_store_t *store, - const char *name, - ks_json_t **providers) -{ - return __get_protocol_providers(store, name, providers); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_update(swclt_store_t *store, const blade_netcast_rqu_t *netcast_rqu) -{ - return __update(store, (blade_netcast_rqu_t *) netcast_rqu); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_route_add(swclt_store_t *store, swclt_store_cb_route_add_t cb) -{ - return __add_cb_route_add(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_route_remove(swclt_store_t *store, swclt_store_cb_route_remove_t cb) -{ - return __add_cb_route_remove(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_identity_add(swclt_store_t *store, swclt_store_cb_identity_add_t cb) -{ - return __add_cb_identity_add(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_identity_remove(swclt_store_t *store, swclt_store_cb_identity_remove_t cb) -{ - return __add_cb_identity_remove(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_protocol_add(swclt_store_t *store, swclt_store_cb_protocol_add_t cb) -{ - return __add_cb_protocol_add(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_protocol_remove(swclt_store_t *store, swclt_store_cb_protocol_remove_t cb) -{ - return __add_cb_protocol_remove(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_protocol_provider_add(swclt_store_t *store, swclt_store_cb_protocol_provider_add_t cb) -{ - return __add_cb_protocol_provider_add(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_protocol_provider_remove(swclt_store_t *store, swclt_store_cb_protocol_provider_remove_t cb) -{ - return __add_cb_protocol_provider_remove(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_protocol_provider_rank_update(swclt_store_t *store, swclt_store_cb_protocol_provider_rank_update_t cb) -{ - return __add_cb_protocol_provider_rank_update(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_protocol_provider_data_update(swclt_store_t *store, swclt_store_cb_protocol_provider_data_update_t cb) -{ - return __add_cb_protocol_provider_data_update(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_authority_add(swclt_store_t *store, swclt_store_cb_authority_add_t cb) -{ - return __add_cb_authority_add(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_authority_remove(swclt_store_t *store, swclt_store_cb_authority_remove_t cb) -{ - return __add_cb_authority_remove(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_subscription_add(swclt_store_t *store, swclt_store_cb_subscription_add_t cb) -{ - return __add_cb_subscription_add(store, cb); -} - -SWCLT_DECLARE(ks_status_t) swclt_store_cb_subscription_remove(swclt_store_t *store, swclt_store_cb_subscription_remove_t cb) -{ - return __add_cb_subscription_remove(store, cb); -} diff --git a/src/session.c b/src/session.c index aa096ab..734f510 100644 --- a/src/session.c +++ b/src/session.c @@ -193,15 +193,12 @@ SWCLT_DECLARE(ks_status_t) swclt_sess_destroy(swclt_sess_t **sessP) ks_rwl_write_lock(sess->rwlock); // now destroy everything - ks_hash_destroy(&sess->subscriptions); ks_hash_destroy(&sess->methods); - ks_hash_destroy(&sess->setups); ks_hash_destroy(&sess->metrics); if (sess->ssl) { swclt_ssl_destroy_context(&sess->ssl); } swclt_ident_destroy(&sess->ident); - swclt_store_destroy(&sess->store); ks_rwl_destroy(&sess->rwlock); flush_results(sess); ks_mutex_destroy(&sess->result_mutex); @@ -210,11 +207,6 @@ SWCLT_DECLARE(ks_status_t) swclt_sess_destroy(swclt_sess_t **sessP) return KS_STATUS_SUCCESS; } -static const char * __make_subscription_key(swclt_sess_t *sess, const char * protocol, const char * channel) -{ - return ks_psprintf(sess->pool, "%s:%s", protocol, channel); -} - static const char * __make_pmethod_key(swclt_sess_t *sess, const char *protocol, const char *method) { return ks_psprintf(sess->pool, "%s:%s", protocol, method); @@ -342,37 +334,7 @@ static ks_status_t __on_incoming_cmd(swclt_conn_t *conn, swclt_cmd_t *cmd, swclt request = cmd->json; /* Keep it locked until we parse the request */ - if (!strcmp(method, BLADE_BROADCAST_METHOD)) { - /* Locate the protocol */ - blade_broadcast_rqu_t *rqu; - swclt_sub_t *sub; - const char *key; - - status = BLADE_BROADCAST_RQU_PARSE(cmd_pool, request, &rqu); - - if (status) { - ks_log(KS_LOG_ERROR, "Failed to parse broadcast command: %s (%lu)", - cmd_str, status); - goto done; - } - - key = __make_subscription_key(sess, rqu->protocol, rqu->channel); - sub = ks_hash_search(sess->subscriptions, key, KS_UNLOCKED); - ks_pool_free(&key); - - if (!sub) { - ks_log(KS_LOG_WARNING, "Could not locate sub for protocol: %s channel: %s command: %s", - rqu->protocol, rqu->channel, cmd_str); - BLADE_BROADCAST_RQU_DESTROY(&rqu); - status = KS_STATUS_NOT_FOUND; - goto done; - } - - status = swclt_sub_invoke(sub, sess, rqu); - - BLADE_BROADCAST_RQU_DESTROY(&rqu); - goto done; - } else if (!strcmp(method, BLADE_DISCONNECT_METHOD)) { + if (!strcmp(method, BLADE_DISCONNECT_METHOD)) { blade_disconnect_rqu_t *rqu; status = BLADE_DISCONNECT_RQU_PARSE(cmd_pool, request, &rqu); @@ -425,26 +387,6 @@ static ks_status_t __on_incoming_cmd(swclt_conn_t *conn, swclt_cmd_t *cmd, swclt ks_log(KS_LOG_DEBUG, "Sent reply back from ping request: %s", cmd_str); } goto done; - } else if (!strcmp(method, BLADE_NETCAST_METHOD)) { - blade_netcast_rqu_t *rqu; - - status = BLADE_NETCAST_RQU_PARSE(cmd_pool, request, &rqu); - - if (status) { - ks_log(KS_LOG_ERROR, "Failed to parse netcast command: %s (%lu)", cmd_str, status); - goto done; - } - - if (status = swclt_store_update(sess->store, rqu)) { - ks_log(KS_LOG_WARNING, "Failed to update nodestore from netcast command: %s (%lu)", - cmd_str, status); - BLADE_NETCAST_RQU_DESTROY(&rqu); - goto done; - } - - ks_log(KS_LOG_DEBUG, "Updated nodestore with netcast command: %s", cmd_str); - BLADE_NETCAST_RQU_DESTROY(&rqu); - goto done; } else if (!strcmp(method, BLADE_EXECUTE_METHOD)) { blade_execute_rqu_t *rqu; const char *key; @@ -518,16 +460,7 @@ static ks_status_t __on_connect_reply(swclt_conn_t *conn, ks_json_t *error, cons if (connect_rpl) { status = KS_STATUS_SUCCESS; - if (!connect_rpl->session_restored) - { - /* Great we got the reply populate the node store */ - swclt_store_reset(sess->store); - if (status = swclt_store_populate(sess->store, connect_rpl)) { - ks_log(KS_LOG_WARNING, "Failed to populate node store from connect reply (%lu)", status); - } else { - ks_log(KS_LOG_DEBUG, "Populated node store from connect reply"); - } - } else { + if (connect_rpl->session_restored) { ks_log(KS_LOG_DEBUG, "Restored session"); } } @@ -747,14 +680,6 @@ SWCLT_DECLARE(ks_status_t) swclt_sess_create( goto done; } - /* Allocate the subscriptions hash */ - if (status = ks_hash_create( - &sess->subscriptions, - KS_HASH_MODE_CASE_SENSITIVE, - KS_HASH_FLAG_FREE_KEY | KS_HASH_FLAG_FREE_VALUE | KS_HASH_FLAG_MUTEX, - sess->pool)) - goto done; - /* Allocate the methods hash */ if (status = ks_hash_create( &sess->methods, @@ -763,13 +688,6 @@ SWCLT_DECLARE(ks_status_t) swclt_sess_create( sess->pool)) goto done; - if (status = ks_hash_create( - &sess->setups, - KS_HASH_MODE_CASE_SENSITIVE, - KS_HASH_FLAG_DUP_CHECK | KS_HASH_FLAG_FREE_KEY | KS_HASH_FLAG_FREE_VALUE | KS_HASH_FLAG_RWLOCK, - sess->pool)) - goto done; - if (status = ks_hash_create( &sess->metrics, KS_HASH_MODE_CASE_SENSITIVE, @@ -788,11 +706,6 @@ SWCLT_DECLARE(ks_status_t) swclt_sess_create( } } - if (status = swclt_store_create(&sess->store)) { - ks_log(KS_LOG_ERROR, "Failed to initialize node store (%lu)", status); - goto done; - } - if (status = ks_cond_create(&sess->monitor_cond, NULL)) { ks_log(KS_LOG_ERROR, "Failed to allocate session monitor condition: %lu", status); goto done; @@ -820,43 +733,6 @@ static ks_status_t __nodeid_local(swclt_sess_t *sess, const char *nodeid) return status; } -static ks_status_t __unregister_subscription( - swclt_sess_t *sess, - const char *protocol, - const char *channel) -{ - const char *key = __make_subscription_key(sess, protocol, channel); - swclt_sub_t *sub = ks_hash_remove(sess->subscriptions, key); - ks_pool_free(&key); - if (!sub) - return KS_STATUS_NOT_FOUND; - - swclt_sub_destroy(&sub); - - return KS_STATUS_SUCCESS; -} - -static ks_status_t __register_subscription( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - swclt_sub_t **sub) -{ - ks_rwl_read_lock(sess->rwlock); - /* unregister if already registered so it does not leak anything, even the handle for the sub - * should be cleaned up to avoid leaking for the duration of the session */ - __unregister_subscription(sess, protocol, channel); - - /* And add it to the hash */ - ks_status_t status = ks_hash_insert(sess->subscriptions, __make_subscription_key(sess, protocol, channel), *sub); - ks_rwl_read_unlock(sess->rwlock); - - if (status == KS_STATUS_SUCCESS) { - *sub = NULL; // take ownership - } - return status; -} - static ks_status_t __register_pmethod( swclt_sess_t *sess, const char *protocol, @@ -873,34 +749,6 @@ static ks_status_t __register_pmethod( return ks_hash_insert(sess->methods, __make_pmethod_key(sess, protocol, method), __make_pmethod_value(sess, pmethod, cb_data)); } -static ks_status_t __lookup_setup(swclt_sess_t *sess, const char *service, ks_pool_t *pool, char **protocol) -{ - ks_status_t status = KS_STATUS_NOT_FOUND; - ks_bool_t exists = KS_FALSE; - const char *proto = NULL; - - ks_hash_read_lock(sess->setups); - proto = ks_hash_search(sess->setups, service, KS_UNLOCKED); - if (proto) { - *protocol = ks_pstrdup(pool, proto); - status = KS_STATUS_SUCCESS; - } - ks_hash_read_unlock(sess->setups); - - return status; -} - -static ks_status_t __register_setup(swclt_sess_t *sess, const char *service, const char *protocol) -{ - ks_status_t status; - - ks_hash_write_lock(sess->setups); - status = ks_hash_insert(sess->setups, ks_pstrdup(ks_pool_get(sess->setups), service), ks_pstrdup(ks_pool_get(sess->setups), protocol)); - ks_hash_write_unlock(sess->setups); - - return status; -} - SWCLT_DECLARE(ks_status_t) swclt_sess_set_auth_failed_cb(swclt_sess_t *sess, swclt_sess_auth_failed_cb_t cb) { sess->auth_failed_cb = cb; @@ -1049,189 +897,6 @@ SWCLT_DECLARE(ks_status_t) swclt_sess_register_protocol_method( return __register_pmethod(sess, protocol, method, pmethod_cb, cb_data); } -SWCLT_DECLARE(ks_status_t) swclt_sess_register_subscription_method( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - swclt_sub_cb_t cb, - void *cb_data) -{ - ks_status_t status = KS_STATUS_SUCCESS; - swclt_sub_t *sub = NULL; - if (status = swclt_sub_create(&sub, sess->pool, protocol, channel, cb, cb_data)) - return status; - - /* Register this subscription, if request fails we just won't receive the events, - * but registering the callback is fine and can be replaced if client tries again */ - status = __register_subscription(sess, protocol, channel, &sub); - swclt_sub_destroy(&sub); - return status; -} - -SWCLT_DECLARE(ks_status_t) swclt_sess_broadcast( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - const char *event, - ks_json_t **params) -{ - ks_status_t status = KS_STATUS_SUCCESS; - swclt_cmd_t *cmd = NULL; - - /* Create the command */ - if (!(cmd = CREATE_BLADE_BROADCAST_CMD( - protocol, - channel, - event, - sess->info.nodeid, - params))) { - status = KS_STATUS_NO_MEM; - goto done; - } - - /* Now submit it */ - ks_rwl_read_lock(sess->rwlock); - status = swclt_conn_submit_request(sess->conn, &cmd, NULL); - ks_rwl_read_unlock(sess->rwlock); - -done: - swclt_cmd_destroy(&cmd); - - return status; -} - -SWCLT_DECLARE(ks_status_t) swclt_sess_subscription_add( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - swclt_sub_cb_t cb, - void *cb_data, - swclt_cmd_reply_t **reply) -{ - swclt_cmd_future_t *future = NULL; - swclt_sess_subscription_add_async( - sess, - protocol, - channel, - cb, - cb_data, - NULL, - NULL, - &future); - return swclt_sess_wait_for_cmd_reply(sess, &future, reply); -} - -SWCLT_DECLARE(ks_status_t) swclt_sess_subscription_add_async( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - swclt_sub_cb_t cb, - void *cb_data, - swclt_cmd_cb_t response_callback, - void *response_callback_data, - swclt_cmd_future_t **future) -{ - ks_status_t status = KS_STATUS_SUCCESS; - swclt_cmd_t *cmd = NULL; - blade_subscription_rpl_t *subscription_rpl = NULL; - swclt_sub_t *sub = NULL; - - /* @todo remove the next 2 calls, and call swclt_sess_register_subscription_method externally, and - * update to allow multiple channels to be subscribed via ks_json_t array of channel name strings - * can also verify that the callbacks are already registered here as a sanity check */ - - /* We also will track this subscription with a handle */ - if (status = swclt_sub_create(&sub, sess->pool, protocol, channel, cb, cb_data)) - goto done; - - /* Register this subscription, if request fails we just won't receive the events, - * but registering the callback is fine and can be replaced if client tries again */ - if (status = __register_subscription(sess, protocol, channel, &sub)) - goto done; - - /* Allocate the request */ - if (!(cmd = CREATE_BLADE_SUBSCRIPTION_CMD( - BLADE_SUBSCRIPTION_CMD_ADD, - protocol, - channel))) { - goto done; - } - - /* If the caller wants to do async, set the callback in the cmd */ - if (response_callback) { - if (status = swclt_cmd_set_cb(cmd, response_callback, response_callback_data)) - goto done; - } - - /* Now submit the command */ - ks_rwl_read_lock(sess->rwlock); - status = swclt_conn_submit_request(sess->conn, &cmd, future); - ks_rwl_read_unlock(sess->rwlock); - -done: - swclt_cmd_destroy(&cmd); - swclt_sub_destroy(&sub); - - return status; -} - -SWCLT_DECLARE(ks_status_t) swclt_sess_subscription_remove( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - swclt_cmd_reply_t **reply) -{ - swclt_cmd_future_t *future = NULL; - swclt_sess_subscription_remove_async( - sess, - protocol, - channel, - NULL, - NULL, - &future); - return swclt_sess_wait_for_cmd_reply(sess, &future, reply); -} - -SWCLT_DECLARE(ks_status_t) swclt_sess_subscription_remove_async( - swclt_sess_t *sess, - const char *protocol, - const char *channel, - swclt_cmd_cb_t response_callback, - void *response_callback_data, - swclt_cmd_future_t **future) -{ - ks_status_t status = KS_STATUS_SUCCESS; - swclt_cmd_t *cmd = NULL; - - /* Unregister this subscription if it exists, if not then just continue with subscription removal - * because the callback may have been removed manually */ - __unregister_subscription(sess, protocol, channel); - - /* Allocate the request */ - if (!(cmd = CREATE_BLADE_SUBSCRIPTION_CMD( - BLADE_SUBSCRIPTION_CMD_REMOVE, - protocol, - channel))) { - goto done; - } - - /* If the caller wants to do async, set the callback in the cmd */ - if (response_callback) { - if (status = swclt_cmd_set_cb(cmd, response_callback, response_callback_data)) - goto done; - } - - /* Now submit the command */ - ks_rwl_read_lock(sess->rwlock); - status = swclt_conn_submit_request(sess->conn, &cmd, future); - ks_rwl_read_unlock(sess->rwlock); - -done: - swclt_cmd_destroy(&cmd); - - return status; -} - SWCLT_DECLARE(ks_status_t) swclt_sess_protocol_provider_add( swclt_sess_t *sess, const char * protocol, @@ -1618,178 +1283,6 @@ SWCLT_DECLARE(ks_status_t) swclt_sess_execute_with_id_async( return status; } -// signalwire consumer - -SWCLT_DECLARE(ks_status_t) swclt_sess_signalwire_setup(swclt_sess_t *sess, const char *service, swclt_sub_cb_t cb, void *cb_data) -{ - ks_status_t status = KS_STATUS_SUCCESS; - - swclt_store_t *store; - ks_json_t *params = NULL; - swclt_cmd_reply_t *reply = NULL; - ks_json_t *result = NULL; - const char *protocol = NULL; - ks_bool_t instance_found = KS_FALSE; - - if (!service) { - ks_log(KS_LOG_ERROR, "Missing service for signalwire.setup"); - return KS_STATUS_ARG_INVALID; - } - - // Make sure we are at least connected - if (!swclt_sess_connected(sess)) { - ks_log(KS_LOG_ERROR, "Setup for '%s' failed because session is not connected", service); - status = KS_STATUS_INACTIVE; - goto done; - } - - params = ks_json_create_object(); - ks_json_add_string_to_object(params, "service", service); - - // Send the setup request syncronously, if it fails bail out - if (status = swclt_sess_execute(sess, - NULL, - "signalwire", - "setup", - ¶ms, - &reply)) { - ks_log(KS_LOG_ERROR, "Setup for '%s' execute failed: %d", service, status); - goto done; - } - - // Get protocol from result, duplicate it so we can destroy the command - protocol = ks_json_get_object_string(ks_json_get_object_item(reply->json, "result"), "protocol", NULL); - if (protocol) protocol = ks_pstrdup(sess->pool, protocol); - - if (!protocol) { - ks_log(KS_LOG_ERROR, "Setup for '%s' response has no result.result.protocol", service); - status = KS_STATUS_ARG_NULL; - goto done; - } - - // Destroy the reply, taking the result data with it, protocol is duplicated in session pool - swclt_cmd_reply_destroy(&reply); - - ks_log(KS_LOG_DEBUG, "Setup for '%s' waiting for provider of protocol instance: %s", service, protocol); - - // poll nodestore until protocol is seen locally (or timeout after 2 seconds of waiting), which - // ensures our upstream also sees it which means we can subscribe to the channel without failing - // if it's not known by upstream yet - { - int nodestore_attempts = 20; - while (!instance_found && nodestore_attempts) { - if (!(instance_found = !swclt_store_check_protocol(sess->store, protocol))) { - ks_sleep_ms(100); - --nodestore_attempts; - } - } - } - - // If we didn't see the protocol, we timed out waiting, gandalf issue? - if (!instance_found) { - ks_log(KS_LOG_ERROR, "Setup for '%s' protocol instance timeout", service); - status = KS_STATUS_TIMEOUT; - goto done; - } - - // Now that protocol is available, sync subscribe to the notifications channel - if (status = swclt_sess_subscription_add(sess, protocol, "notifications", cb, cb_data, NULL)) { - ks_log(KS_LOG_ERROR, "Setup for '%s' subscription add failed: %d", service, status); - goto done; - } - __register_setup(sess, service, protocol); - -done: - if (protocol) ks_pool_free(&protocol); - swclt_cmd_reply_destroy(&reply); - if (params) ks_json_delete(¶ms); - - return status; -} - -// signalwire provisioning consumer - -SWCLT_DECLARE(ks_status_t) swclt_sess_provisioning_setup(swclt_sess_t *sess, swclt_sub_cb_t cb, void *cb_data) -{ - return swclt_sess_signalwire_setup(sess, "provisioning", cb, cb_data); -} - -SWCLT_DECLARE(ks_status_t) swclt_sess_provisioning_configure(swclt_sess_t *sess, - const char *target, - const char *local_endpoint, - const char *external_endpoint, - const char *relay_connector_id, - swclt_cmd_reply_t **reply) -{ - swclt_cmd_future_t *future = NULL; - swclt_sess_provisioning_configure_async(sess, - target, - local_endpoint, - external_endpoint, - relay_connector_id, - NULL, - NULL, - &future); - return swclt_sess_wait_for_cmd_reply(sess, &future, reply); -} - -SWCLT_DECLARE(ks_status_t) swclt_sess_provisioning_configure_async(swclt_sess_t *sess, - const char *target, - const char *local_endpoint, - const char *external_endpoint, - const char *relay_connector_id, - swclt_cmd_cb_t response_callback, - void *response_callback_data, - swclt_cmd_future_t **future) -{ - ks_status_t status = KS_STATUS_SUCCESS; - - swclt_store_t *store; - ks_pool_t *pool = NULL; - char *protocol = NULL; - ks_json_t *params = NULL; - - if (!target || !local_endpoint || !external_endpoint || !relay_connector_id) { - ks_log(KS_LOG_ERROR, "Missing required parameter"); - return KS_STATUS_ARG_INVALID; - } - - if (!swclt_sess_connected(sess)) { - goto done; - } - - pool = sess->pool; - - if (__lookup_setup(sess, "provisioning", pool, &protocol)) { - ks_log(KS_LOG_ERROR, "Provisioning setup has not been performed"); - status = KS_STATUS_FAIL; - goto done; - } - - params = ks_json_create_object(); - - ks_json_add_string_to_object(params, "target", target); - ks_json_add_string_to_object(params, "local_endpoint", local_endpoint); - ks_json_add_string_to_object(params, "external_endpoint", external_endpoint); - ks_json_add_string_to_object(params, "relay_connector_id", relay_connector_id); - - status = swclt_sess_execute_async(sess, - NULL, - protocol, - "configure", - ¶ms, - response_callback, - response_callback_data, - future); -done: - if (protocol) ks_pool_free(&protocol); - if (params) ks_json_delete(¶ms); - - return status; -} - - - /* For Emacs: * Local Variables: diff --git a/src/subscription.c b/src/subscription.c deleted file mode 100644 index 2718c4d..0000000 --- a/src/subscription.c +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2018-2020 SignalWire, Inc - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "signalwire-client-c/client.h" - -SWCLT_DECLARE(ks_status_t) swclt_sub_destroy(swclt_sub_t **subP) -{ - if (subP && *subP) { - swclt_sub_t *sub = *subP; - *subP = NULL; - ks_pool_free(&sub->protocol); - ks_pool_free(&sub->channel); - } - return KS_STATUS_SUCCESS; -} - -SWCLT_DECLARE(char *) swclt_sub_describe(swclt_sub_t *sub) -{ - return ks_psprintf(NULL, "SWCLT Subscription to protocol: %s channel: %s", sub->protocol, sub->channel); -} - -SWCLT_DECLARE(ks_status_t) swclt_sub_create( - swclt_sub_t **subP, - ks_pool_t *pool, - const char * const protocol, - const char * const channel, - swclt_sub_cb_t cb, - void *cb_data) -{ - ks_status_t status = KS_STATUS_SUCCESS; - - swclt_sub_t *sub = ks_pool_alloc(pool, sizeof(swclt_sub_t)); - *subP = sub; - - if (!(sub->protocol = ks_pstrdup(NULL, protocol))) { - status = KS_STATUS_NO_MEM; - goto done; - } - - if (!(sub->channel = ks_pstrdup(NULL, channel))) { - status = KS_STATUS_NO_MEM; - goto done; - } - - sub->cb = cb; - sub->cb_data = cb_data; - -done: - if (status) { - swclt_sub_destroy(subP); - } - - return status; -} - -SWCLT_DECLARE(ks_status_t) swclt_sub_invoke( - swclt_sub_t *sub, - swclt_sess_t *sess, - blade_broadcast_rqu_t *broadcast_rqu) -{ - sub->cb(sess, broadcast_rqu, sub->cb_data); - return KS_STATUS_SUCCESS; -} diff --git a/swclt_test/cases/json.c b/swclt_test/cases/json.c index c577d1a..1186a43 100644 --- a/swclt_test/cases/json.c +++ b/swclt_test/cases/json.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 SignalWire, Inc + * Copyright (c) 2018-2022 SignalWire, Inc * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,26 +22,6 @@ #include "swclt_test.h" -static blade_netcast_rqu_t __netcast_protocol_provider_add_request(ks_pool_t *pool, const char *protocol, ks_uuid_t nodeid, const char *channel) -{ - blade_netcast_rqu_t request; - request.command = BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_ADD; - request.certified_only = KS_TRUE; - request.netcaster_nodeid = ks_uuid_null_thr_str(); - - /* Fill in the params too */ - blade_netcast_protocol_provider_add_param_t params = {0}; - params.protocol = protocol; - params.nodeid = ks_uuid_str(NULL, &nodeid); - params.channels = ks_json_create_array(); - ks_json_add_item_to_array(params.channels, BLADE_CHANNEL_MARSHAL(&(blade_channel_t){channel, BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC})); - - /* Marshal it into its parent request */ - request.params = BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM_MARSHAL(¶ms); - - return request; -} - static void test_blade_execute(ks_pool_t *pool) { ks_json_t *params = ks_json_create_object(); diff --git a/swclt_test/cases/nodestore.c b/swclt_test/cases/nodestore.c deleted file mode 100644 index 5a2d960..0000000 --- a/swclt_test/cases/nodestore.c +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright (c) 2018-2020 SignalWire, Inc - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -#include "swclt_test.h" - -ks_uuid_t g_route_nodeid_1, g_route_nodeid_2, g_sessionid; - -blade_netcast_rqu_t __netcast_protocol_provider_add_request(ks_pool_t *pool, const char *protocol, ks_uuid_t nodeid, const char *channel) -{ - blade_netcast_rqu_t request = { 0 }; - request.command = BLADE_NETCAST_CMD_PROTOCOL_PROVIDER_ADD; - request.certified_only = KS_TRUE; - request.netcaster_nodeid = ks_uuid_null_str(pool); - - /* Fill in the params too */ - blade_netcast_protocol_provider_add_param_t params = { 0 }; - params.protocol = protocol; - params.nodeid = ks_uuid_str(pool, &nodeid); - params.channels = ks_json_create_array(); - ks_json_add_item_to_array(params.channels, BLADE_CHANNEL_MARSHAL(&(blade_channel_t){channel, BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC})); - - /* Marshal it into its parent request */ - request.params = BLADE_NETCAST_PROTOCOL_PROVIDER_ADD_PARAM_MARSHAL(¶ms); - - return request; -} - -blade_netcast_rqu_t __netcast_route_add_request(ks_pool_t *pool, const char *nodeid, ks_bool_t certified) -{ - blade_netcast_rqu_t request = { 0 }; - request.command = BLADE_NETCAST_CMD_ROUTE_ADD; - request.certified_only = KS_TRUE; - request.netcaster_nodeid = ks_uuid_null_str(pool); - - /* Fill in the params too */ - blade_netcast_route_add_param_t params = { 0 }; - params.certified = certified; - params.nodeid = nodeid; - - /* Marshal it into its parent request */ - request.params = BLADE_NETCAST_ROUTE_ADD_PARAM_MARSHAL(¶ms); - - return request; -} - -blade_netcast_rqu_t __netcast_route_remove_request(ks_pool_t *pool, const char *nodeid) -{ - blade_netcast_rqu_t request = { 0 }; - request.command = BLADE_NETCAST_CMD_ROUTE_REMOVE; - request.certified_only = KS_TRUE; - request.netcaster_nodeid = ks_uuid_null_str(pool); - - /* Fill in the params too */ - blade_netcast_route_remove_param_t params = { 0 }; - params.nodeid = nodeid; - params.certified = KS_TRUE; - - /* Marshal it into its parent request */ - request.params = BLADE_NETCAST_ROUTE_REMOVE_PARAM_MARSHAL(¶ms); - - return request; -} - -blade_connect_rpl_t *__connect_reply(ks_pool_t *pool) -{ - ks_json_t *routes = ks_json_create_array(); - ks_json_t *protocols = ks_json_create_array(); - ks_json_t *subscriptions = ks_json_create_array(); - - /* Add a couple routes */ - ks_json_add_item_to_array(routes, BLADE_NODE_MARSHAL(&(blade_node_t){ks_uuid_str(pool, &g_route_nodeid_1)})); - ks_json_add_item_to_array(routes, BLADE_NODE_MARSHAL(&(blade_node_t){ks_uuid_str(pool, &g_route_nodeid_2)})); - - /* Add a couple protocols each with one provider and one channel */ - ks_json_t *channels = ks_json_create_array(); - ks_json_add_item_to_array(channels, BLADE_CHANNEL_MARSHAL(&(blade_channel_t){"bobo_channel_1", BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC})); - ks_json_t *providers = ks_json_create_array(); - ks_json_add_item_to_array(providers, BLADE_PROVIDER_MARSHAL(&(blade_provider_t){ks_uuid_str(pool, &g_route_nodeid_1), ks_json_create_array()})); - ks_json_add_item_to_array(protocols, - BLADE_PROTOCOL_MARSHAL(&(blade_protocol_t){ - "bobo_protocol_1", BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC, - providers, - channels, - })); - - channels = ks_json_create_array(); - ks_json_add_item_to_array(channels, BLADE_CHANNEL_MARSHAL(&(blade_channel_t){"bobo_channel_2", BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC})); - providers = ks_json_create_array(); - ks_json_add_item_to_array(providers, BLADE_PROVIDER_MARSHAL(&(blade_provider_t){ks_uuid_str(pool, &g_route_nodeid_2), ks_json_create_array()})); - ks_json_add_item_to_array(protocols, - BLADE_PROTOCOL_MARSHAL(&(blade_protocol_t){ - "bobo_protocol_2", BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC, BLADE_ACL_PUBLIC, - providers, - channels, - })); - - /* Have the second node be subscribed to the first */ - ks_json_t *nodeids = ks_json_create_array(); - ks_json_add_string_to_array(nodeids, ks_uuid_str(pool, &g_route_nodeid_2)); - ks_json_add_item_to_array(subscriptions, - BLADE_SUBSCRIPTION_MARSHAL(&(blade_subscription_t){ - "bobo_protocol_1", - "bobo_channel_1", - nodeids, - })); - - /* Now compose it altogether in a connect result */ - blade_connect_rpl_t reply_s = (blade_connect_rpl_t){ - KS_FALSE, - *ks_uuid(&g_sessionid), - ks_uuid_str(pool, &g_route_nodeid_1), - ks_uuid_null_str(pool), - NULL, - routes, - protocols, - subscriptions, - ks_json_create_array() - }; - blade_connect_rpl_t *reply = ks_pool_alloc(pool, sizeof (*reply)); - memcpy(reply, &reply_s, sizeof(blade_connect_rpl_t)); // copy to struct allocated off of pool so it can be destroyed - return reply; -} - -void test_nodestore_update(ks_pool_t *pool) -{ - blade_connect_rpl_t *connect_rpl = __connect_reply(pool); - swclt_store_t *store = NULL; - ks_uuid_t new_route_nodeid = { 0 }; - const char *new_route_nodeid_str; - - ks_uuid(&new_route_nodeid); - new_route_nodeid_str = ks_uuid_str(pool, &new_route_nodeid); - - REQUIRE(!swclt_store_create(&store)); - REQUIRE(!swclt_store_populate(store, connect_rpl)); - - /* The store should properly render the types */ - REQUIRE(ks_hash_count(store->protocols) == 2); - REQUIRE(ks_hash_count(store->subscriptions) == 1); - REQUIRE(ks_hash_count(store->routes) == 2); - REQUIRE(ks_hash_count(store->authorities) == 0); - - /* Now update with a new node */ - blade_netcast_rqu_t netcast_rqu = __netcast_route_add_request(pool, new_route_nodeid_str, KS_TRUE); - REQUIRE(!swclt_store_update(store, &netcast_rqu)); - REQUIRE(ks_hash_count(store->routes) == 3); - REQUIRE(((blade_node_t *)ks_hash_search(store->routes, new_route_nodeid_str, KS_UNLOCKED))->certified == KS_TRUE); - ks_json_delete(&netcast_rqu.params); - - /* And a new provider with a new provider + channel */ - netcast_rqu = __netcast_protocol_provider_add_request(pool, "bobo_protocol_new", new_route_nodeid, "bobo_channel_new"); - REQUIRE(!swclt_store_update(store, &netcast_rqu)); - REQUIRE(ks_hash_count(store->protocols) == 3); - { - blade_protocol_t *protocol = (blade_protocol_t *)ks_hash_search(store->protocols, "bobo_protocol_new", KS_UNLOCKED); - REQUIRE(!strcmp(protocol->name, "bobo_protocol_new")); - - /* Should have one channel in it called something silly */ - REQUIRE(ks_json_get_array_size(protocol->channels) == 1); - - { - blade_channel_t *channel; - REQUIRE(!BLADE_CHANNEL_PARSE(pool, ks_json_get_array_item(protocol->channels, 0), &channel)); - REQUIRE(!strcmp(channel->name, "bobo_channel_new")); - BLADE_CHANNEL_DESTROY(&channel); - } - } - ks_json_delete(&netcast_rqu.params); - - /* And remove it, should also remove the protocol */ - netcast_rqu = __netcast_route_remove_request(pool, new_route_nodeid_str); - REQUIRE(!swclt_store_update(store, &netcast_rqu)); - REQUIRE(ks_hash_count(store->routes) == 2); - REQUIRE(ks_hash_count(store->protocols) == 2); - ks_json_delete(&netcast_rqu.params); - - swclt_store_destroy(&store); - BLADE_CONNECT_RPL_DESTROY(&connect_rpl); -} - -void test_nodestore_protocol_select(ks_pool_t *pool) -{ - -} - -void test_nodestore(ks_pool_t *pool) -{ - ks_uuid(&g_route_nodeid_1); - ks_uuid(&g_route_nodeid_2); - ks_uuid(&g_sessionid); - - test_nodestore_update(pool); - test_nodestore_protocol_select(pool); -} diff --git a/swclt_test/main.c b/swclt_test/main.c index 3fb3d02..d1ef8fb 100644 --- a/swclt_test/main.c +++ b/swclt_test/main.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020 SignalWire, Inc + * Copyright (c) 2018-2022 SignalWire, Inc * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,7 +26,6 @@ DECLARE_TEST(command); DECLARE_TEST(execute); DECLARE_TEST(connection); DECLARE_TEST(session); -DECLARE_TEST(nodestore); DECLARE_TEST(uncert_exp); test_entry_t g_test_methods[] = { @@ -35,7 +34,6 @@ test_entry_t g_test_methods[] = { TEST_ENTRY(execute), TEST_ENTRY(connection), TEST_ENTRY(session), - TEST_ENTRY(nodestore), TEST_ENTRY(uncert_exp), };