2017-07-03 18:48:46 +00:00
/*
* Copyright ( c ) 2017 , Shane Bryldt
* All rights reserved .
2017-08-01 22:48:34 +00:00
*
2017-07-03 18:48:46 +00:00
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions
* are met :
2017-08-01 22:48:34 +00:00
*
2017-07-03 18:48:46 +00:00
* * Redistributions of source code must retain the above copyright
* notice , this list of conditions and the following disclaimer .
2017-08-01 22:48:34 +00:00
*
2017-07-03 18:48:46 +00:00
* * Redistributions in binary form must reproduce the above copyright
* notice , this list of conditions and the following disclaimer in the
* documentation and / or other materials provided with the distribution .
2017-08-01 22:48:34 +00:00
*
2017-07-03 18:48:46 +00:00
* * Neither the name of the original author ; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission .
2017-08-01 22:48:34 +00:00
*
*
2017-07-03 18:48:46 +00:00
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT
* LIMITED TO , THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL ,
* EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT LIMITED TO ,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE , DATA , OR
* PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT ( INCLUDING
* NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
# include "blade.h"
struct blade_mastermgr_s {
blade_handle_t * handle ;
2017-09-21 04:07:34 +00:00
// @todo use a blade_mastermgr_config_t to store configuration, inline variable, with sane defaults for as much configuration as is possible
// then reuse this same pattern across all the manager types that invoke startup configuration processing
2017-09-13 14:41:40 +00:00
const char * master_nodeid ;
2017-09-21 04:07:34 +00:00
// @todo how does "exclusive" play into the controllers, does "exclusive" mean only one provider can exist for a given protocol? what does non exclusive mean?
ks_hash_t * protocols ;
2017-07-03 18:48:46 +00:00
} ;
2017-08-04 02:26:07 +00:00
static void blade_mastermgr_cleanup ( void * ptr , void * arg , ks_pool_cleanup_action_t action , ks_pool_cleanup_type_t type )
2017-07-03 18:48:46 +00:00
{
//blade_mastermgr_t *bmmgr = (blade_mastermgr_t *)ptr;
//ks_assert(bmmgr);
switch ( action ) {
case KS_MPCL_ANNOUNCE :
break ;
case KS_MPCL_TEARDOWN :
break ;
case KS_MPCL_DESTROY :
break ;
}
}
KS_DECLARE ( ks_status_t ) blade_mastermgr_create ( blade_mastermgr_t * * bmmgrP , blade_handle_t * bh )
{
ks_pool_t * pool = NULL ;
blade_mastermgr_t * bmmgr = NULL ;
ks_assert ( bmmgrP ) ;
2017-08-01 22:48:34 +00:00
2017-07-03 18:48:46 +00:00
ks_pool_open ( & pool ) ;
ks_assert ( pool ) ;
bmmgr = ks_pool_alloc ( pool , sizeof ( blade_mastermgr_t ) ) ;
bmmgr - > handle = bh ;
2017-09-21 04:07:34 +00:00
ks_hash_create ( & bmmgr - > protocols , KS_HASH_MODE_CASE_INSENSITIVE , KS_HASH_FLAG_RWLOCK | KS_HASH_FLAG_DUP_CHECK | KS_HASH_FLAG_FREE_KEY | KS_HASH_FLAG_FREE_VALUE , pool ) ;
ks_assert ( bmmgr - > protocols ) ;
2017-07-03 18:48:46 +00:00
2017-08-04 02:26:07 +00:00
ks_pool_set_cleanup ( bmmgr , NULL , blade_mastermgr_cleanup ) ;
2017-07-03 18:48:46 +00:00
* bmmgrP = bmmgr ;
return KS_STATUS_SUCCESS ;
}
KS_DECLARE ( ks_status_t ) blade_mastermgr_destroy ( blade_mastermgr_t * * bmmgrP )
{
blade_mastermgr_t * bmmgr = NULL ;
ks_pool_t * pool ;
ks_assert ( bmmgrP ) ;
ks_assert ( * bmmgrP ) ;
bmmgr = * bmmgrP ;
* bmmgrP = NULL ;
2017-08-04 02:26:07 +00:00
pool = ks_pool_get ( bmmgr ) ;
2017-07-03 18:48:46 +00:00
ks_pool_close ( & pool ) ;
return KS_STATUS_SUCCESS ;
}
KS_DECLARE ( blade_handle_t * ) blade_mastermgr_handle_get ( blade_mastermgr_t * bmmgr )
{
ks_assert ( bmmgr ) ;
return bmmgr - > handle ;
}
2017-09-13 14:41:40 +00:00
ks_status_t blade_mastermgr_config ( blade_mastermgr_t * bmmgr , config_setting_t * config )
{
ks_pool_t * pool = NULL ;
config_setting_t * master = NULL ;
config_setting_t * master_nodeid = NULL ;
const char * nodeid = NULL ;
ks_assert ( bmmgr ) ;
pool = ks_pool_get ( bmmgr ) ;
if ( ! config_setting_is_group ( config ) ) {
ks_log ( KS_LOG_DEBUG , " !config_setting_is_group(config) \n " ) ;
return KS_STATUS_FAIL ;
}
master = config_setting_get_member ( config , " master " ) ;
if ( master ) {
2017-10-10 12:32:59 +00:00
master_nodeid = config_setting_lookup ( master , " nodeid " ) ;
2017-09-13 14:41:40 +00:00
if ( ! master_nodeid ) return KS_STATUS_FAIL ;
if ( config_setting_type ( master_nodeid ) ! = CONFIG_TYPE_STRING ) return KS_STATUS_FAIL ;
nodeid = config_setting_get_string ( master_nodeid ) ;
}
if ( master ) {
bmmgr - > master_nodeid = ks_pstrdup ( pool , nodeid ) ;
ks_log ( KS_LOG_DEBUG , " Configured \n " ) ;
}
return KS_STATUS_SUCCESS ;
}
KS_DECLARE ( ks_status_t ) blade_mastermgr_startup ( blade_mastermgr_t * bmmgr , config_setting_t * config )
{
ks_assert ( bmmgr ) ;
if ( blade_mastermgr_config ( bmmgr , config ) ! = KS_STATUS_SUCCESS ) {
ks_log ( KS_LOG_DEBUG , " blade_mastermgr_config failed \n " ) ;
return KS_STATUS_FAIL ;
}
if ( bmmgr - > master_nodeid ) {
2017-09-21 04:07:34 +00:00
blade_routemgr_local_set ( blade_handle_routemgr_get ( bmmgr - > handle ) , bmmgr - > master_nodeid ) ;
blade_routemgr_master_set ( blade_handle_routemgr_get ( bmmgr - > handle ) , bmmgr - > master_nodeid ) ;
2017-09-13 14:41:40 +00:00
2017-09-21 04:07:34 +00:00
blade_mastermgr_protocol_controller_add ( bmmgr , " blade.presence " , bmmgr - > master_nodeid ) ;
2017-09-13 14:41:40 +00:00
2017-09-21 04:07:34 +00:00
blade_mastermgr_protocol_channel_add ( bmmgr , " blade.presence " , " join " , BLADE_CHANNEL_FLAGS_PUBLIC ) ;
blade_mastermgr_protocol_channel_add ( bmmgr , " blade.presence " , " leave " , BLADE_CHANNEL_FLAGS_PUBLIC ) ;
2017-09-13 14:41:40 +00:00
}
return KS_STATUS_SUCCESS ;
}
KS_DECLARE ( ks_status_t ) blade_mastermgr_shutdown ( blade_mastermgr_t * bmmgr )
{
ks_assert ( bmmgr ) ;
return KS_STATUS_SUCCESS ;
}
2017-07-25 17:00:45 +00:00
KS_DECLARE ( ks_status_t ) blade_mastermgr_purge ( blade_mastermgr_t * bmmgr , const char * nodeid )
{
2017-08-04 02:26:07 +00:00
ks_pool_t * pool = NULL ;
2017-07-25 17:00:45 +00:00
ks_hash_t * cleanup = NULL ;
2017-08-04 02:26:07 +00:00
ks_assert ( bmmgr ) ;
pool = ks_pool_get ( bmmgr ) ;
2017-09-21 04:07:34 +00:00
ks_hash_write_lock ( bmmgr - > protocols ) ;
2017-07-25 17:00:45 +00:00
2017-09-21 04:07:34 +00:00
for ( ks_hash_iterator_t * it = ks_hash_first ( bmmgr - > protocols , KS_UNLOCKED ) ; it ; it = ks_hash_next ( & it ) ) {
const char * protocol = NULL ;
blade_protocol_t * bp = NULL ;
ks_bool_t unlock = KS_TRUE ;
2017-09-13 14:41:40 +00:00
2017-09-21 04:07:34 +00:00
ks_hash_this ( it , ( const void * * ) & protocol , NULL , ( void * * ) & bp ) ;
2017-09-13 14:41:40 +00:00
2017-09-21 04:07:34 +00:00
blade_protocol_write_lock ( bp ) ;
2017-09-13 14:41:40 +00:00
2017-09-21 04:07:34 +00:00
if ( blade_protocol_purge ( bp , nodeid ) ) {
if ( ! blade_protocol_controller_available ( bp ) ) {
if ( ! cleanup ) ks_hash_create ( & cleanup , KS_HASH_MODE_CASE_INSENSITIVE , KS_HASH_FLAG_RWLOCK | KS_HASH_FLAG_DUP_CHECK , pool ) ;
ks_hash_insert ( cleanup , ( void * ) protocol , bp ) ;
unlock = KS_FALSE ;
2017-08-18 22:30:08 +00:00
}
2017-09-21 04:07:34 +00:00
else {
// @todo not the last controller, may need to propagate that the controller is no longer available?
2017-09-13 14:41:40 +00:00
}
2017-07-25 17:00:45 +00:00
}
2017-09-21 04:07:34 +00:00
if ( unlock ) blade_protocol_write_unlock ( bp ) ;
2017-07-25 17:00:45 +00:00
}
2017-09-21 04:07:34 +00:00
if ( cleanup ) {
for ( ks_hash_iterator_t * it2 = ks_hash_first ( cleanup , KS_UNLOCKED ) ; it2 ; it2 = ks_hash_next ( & it2 ) ) {
const char * protocol = NULL ;
blade_protocol_t * bp = NULL ;
2017-07-25 17:00:45 +00:00
2017-09-21 04:07:34 +00:00
ks_hash_this ( it2 , ( const void * * ) & protocol , NULL , ( void * * ) & bp ) ;
2017-07-25 17:00:45 +00:00
2017-09-21 04:07:34 +00:00
blade_subscriptionmgr_broadcast ( blade_handle_subscriptionmgr_get ( bmmgr - > handle ) , BLADE_RPCBROADCAST_COMMAND_PROTOCOL_REMOVE , NULL , protocol , NULL , NULL , NULL , NULL , NULL ) ;
2017-09-13 14:41:40 +00:00
2017-09-21 04:07:34 +00:00
ks_log ( KS_LOG_DEBUG , " Protocol Removed: %s \n " , protocol ) ;
blade_protocol_write_unlock ( bp ) ;
2017-09-13 14:41:40 +00:00
2017-09-21 04:07:34 +00:00
ks_hash_remove ( bmmgr - > protocols , ( void * ) protocol ) ;
}
ks_hash_destroy ( & cleanup ) ;
2017-09-13 14:41:40 +00:00
}
2017-09-21 04:07:34 +00:00
ks_hash_write_unlock ( bmmgr - > protocols ) ;
return KS_STATUS_SUCCESS ;
2017-09-13 14:41:40 +00:00
}
2017-09-21 04:07:34 +00:00
KS_DECLARE ( blade_protocol_t * ) blade_mastermgr_protocol_lookup ( blade_mastermgr_t * bmmgr , const char * protocol , ks_bool_t writelocked )
2017-07-03 18:48:46 +00:00
{
blade_protocol_t * bp = NULL ;
ks_assert ( bmmgr ) ;
ks_assert ( protocol ) ;
2017-09-21 04:07:34 +00:00
bp = ( blade_protocol_t * ) ks_hash_search ( bmmgr - > protocols , ( void * ) protocol , KS_READLOCKED ) ;
if ( bp ) {
if ( writelocked ) blade_protocol_write_lock ( bp ) ;
else blade_protocol_read_lock ( bp ) ;
}
ks_hash_read_unlock ( bmmgr - > protocols ) ;
2017-07-03 18:48:46 +00:00
return bp ;
}
2017-09-21 04:07:34 +00:00
KS_DECLARE ( ks_status_t ) blade_mastermgr_protocol_controller_add ( blade_mastermgr_t * bmmgr , const char * protocol , const char * controller )
2017-07-03 18:48:46 +00:00
{
2017-09-13 14:41:40 +00:00
ks_status_t ret = KS_STATUS_SUCCESS ;
2017-08-04 02:26:07 +00:00
ks_pool_t * pool = NULL ;
2017-07-03 18:48:46 +00:00
blade_protocol_t * bp = NULL ;
ks_assert ( bmmgr ) ;
2017-09-13 14:41:40 +00:00
ks_assert ( protocol ) ;
2017-07-03 18:48:46 +00:00
ks_assert ( controller ) ;
2017-08-04 02:26:07 +00:00
pool = ks_pool_get ( bmmgr ) ;
2017-09-21 04:07:34 +00:00
ks_hash_write_lock ( bmmgr - > protocols ) ;
2017-07-03 18:48:46 +00:00
2017-09-21 04:07:34 +00:00
bp = ( blade_protocol_t * ) ks_hash_search ( bmmgr - > protocols , ( void * ) protocol , KS_UNLOCKED ) ;
2017-07-03 18:48:46 +00:00
2017-09-21 04:07:34 +00:00
if ( bp ) blade_protocol_write_lock ( bp ) ;
else {
blade_protocol_create ( & bp , pool , protocol ) ;
2017-07-03 18:48:46 +00:00
ks_assert ( bp ) ;
2017-09-13 14:41:40 +00:00
blade_protocol_write_lock ( bp ) ;
2017-09-21 04:07:34 +00:00
ks_log ( KS_LOG_DEBUG , " Protocol Added: %s \n " , protocol ) ;
ks_hash_insert ( bmmgr - > protocols , ( void * ) ks_pstrdup ( ks_pool_get ( bmmgr ) , protocol ) , ( void * ) bp ) ;
2017-07-03 18:48:46 +00:00
}
2017-08-18 22:30:08 +00:00
blade_protocol_controller_add ( bp , controller ) ;
2017-09-21 04:07:34 +00:00
ks_log ( KS_LOG_DEBUG , " Protocol Controller Added: %s to %s \n " , controller , protocol ) ;
2017-09-13 14:41:40 +00:00
blade_protocol_write_unlock ( bp ) ;
2017-07-25 17:00:45 +00:00
2017-09-21 04:07:34 +00:00
ks_hash_write_unlock ( bmmgr - > protocols ) ;
2017-07-03 18:48:46 +00:00
2017-09-13 14:41:40 +00:00
return ret ;
2017-08-18 22:30:08 +00:00
}
2017-09-21 04:07:34 +00:00
KS_DECLARE ( ks_status_t ) blade_mastermgr_protocol_controller_remove ( blade_mastermgr_t * bmmgr , const char * protocol , const char * controller )
2017-08-18 22:30:08 +00:00
{
2017-09-13 14:41:40 +00:00
ks_status_t ret = KS_STATUS_SUCCESS ;
2017-08-18 22:30:08 +00:00
blade_protocol_t * bp = NULL ;
2017-09-21 04:07:34 +00:00
ks_bool_t remove = KS_FALSE ;
2017-08-18 22:30:08 +00:00
ks_assert ( bmmgr ) ;
2017-09-13 14:41:40 +00:00
ks_assert ( protocol ) ;
2017-08-18 22:30:08 +00:00
ks_assert ( controller ) ;
2017-09-21 04:07:34 +00:00
ks_hash_write_lock ( bmmgr - > protocols ) ;
2017-08-18 22:30:08 +00:00
2017-09-21 04:07:34 +00:00
bp = ( blade_protocol_t * ) ks_hash_search ( bmmgr - > protocols , ( void * ) protocol , KS_UNLOCKED ) ;
if ( ! bp ) {
ret = KS_STATUS_NOT_FOUND ;
2017-09-13 14:41:40 +00:00
goto done ;
}
2017-08-18 22:30:08 +00:00
2017-09-21 04:07:34 +00:00
blade_protocol_write_lock ( bp ) ;
2017-09-13 14:41:40 +00:00
2017-09-21 04:07:34 +00:00
if ( blade_protocol_controller_remove ( bp , controller ) ) {
ks_log ( KS_LOG_DEBUG , " Protocol Controller Removed: %s from %s \n " , controller , protocol ) ;
if ( ! blade_protocol_controller_available ( bp ) ) {
blade_subscriptionmgr_broadcast ( blade_handle_subscriptionmgr_get ( bmmgr - > handle ) , BLADE_RPCBROADCAST_COMMAND_PROTOCOL_REMOVE , NULL , protocol , NULL , NULL , NULL , NULL , NULL ) ;
ks_log ( KS_LOG_DEBUG , " Protocol Removed: %s \n " , protocol ) ;
remove = KS_TRUE ;
} else {
// @todo not the last controller, may need to propagate when a specific controller becomes unavailable though
2017-08-18 22:30:08 +00:00
}
}
2017-09-21 04:07:34 +00:00
blade_protocol_write_unlock ( bp ) ;
if ( remove ) ks_hash_remove ( bmmgr - > protocols , ( void * ) protocol ) ;
2017-09-13 14:41:40 +00:00
done :
2017-09-21 04:07:34 +00:00
ks_hash_write_unlock ( bmmgr - > protocols ) ;
2017-08-18 22:30:08 +00:00
2017-09-13 14:41:40 +00:00
return ret ;
2017-07-03 18:48:46 +00:00
}
2017-09-21 04:07:34 +00:00
KS_DECLARE ( ks_status_t ) blade_mastermgr_protocol_channel_add ( blade_mastermgr_t * bmmgr , const char * protocol , const char * channel , blade_channel_flags_t flags )
2017-07-03 18:48:46 +00:00
{
2017-07-25 17:00:45 +00:00
ks_status_t ret = KS_STATUS_SUCCESS ;
blade_protocol_t * bp = NULL ;
2017-09-13 14:41:40 +00:00
blade_channel_t * bc = NULL ;
2017-07-03 18:48:46 +00:00
ks_assert ( bmmgr ) ;
2017-09-13 14:41:40 +00:00
ks_assert ( protocol ) ;
2017-07-25 17:00:45 +00:00
ks_assert ( channel ) ;
2017-07-03 18:48:46 +00:00
2017-09-21 04:07:34 +00:00
ks_hash_read_lock ( bmmgr - > protocols ) ;
2017-07-03 18:48:46 +00:00
2017-09-21 04:07:34 +00:00
bp = ( blade_protocol_t * ) ks_hash_search ( bmmgr - > protocols , ( void * ) protocol , KS_UNLOCKED ) ;
2017-07-25 17:00:45 +00:00
if ( ! bp ) {
ret = KS_STATUS_NOT_FOUND ;
goto done ;
}
2017-07-03 18:48:46 +00:00
2017-09-21 04:07:34 +00:00
blade_protocol_write_lock ( bp ) ;
2017-09-13 14:41:40 +00:00
bc = blade_protocol_channel_lookup ( bp , channel , KS_TRUE ) ;
2017-09-21 04:07:34 +00:00
if ( bc ) {
2017-09-13 14:41:40 +00:00
ret = KS_STATUS_DUPLICATE_OPERATION ;
goto done ;
}
2017-07-03 18:48:46 +00:00
2017-09-21 04:07:34 +00:00
blade_channel_create ( & bc , ks_pool_get ( bp ) , channel , flags ) ;
2017-09-13 14:41:40 +00:00
ks_assert ( bc ) ;
2017-07-03 18:48:46 +00:00
2017-09-13 14:41:40 +00:00
blade_channel_write_lock ( bc ) ;
if ( blade_protocol_channel_add ( bp , bc ) = = KS_STATUS_SUCCESS ) {
2017-09-21 04:07:34 +00:00
ks_log ( KS_LOG_DEBUG , " Protocol Channel Added: %s to %s \n " , blade_channel_name_get ( bc ) , blade_protocol_name_get ( bp ) ) ;
2017-09-13 14:41:40 +00:00
}
done :
if ( bc ) blade_channel_write_unlock ( bc ) ;
if ( bp ) blade_protocol_write_unlock ( bp ) ;
2017-09-21 04:07:34 +00:00
ks_hash_read_unlock ( bmmgr - > protocols ) ;
2017-07-25 17:00:45 +00:00
return ret ;
}
2017-09-21 04:07:34 +00:00
KS_DECLARE ( ks_status_t ) blade_mastermgr_protocol_channel_remove ( blade_mastermgr_t * bmmgr , const char * protocol , const char * channel )
2017-07-25 17:00:45 +00:00
{
ks_status_t ret = KS_STATUS_SUCCESS ;
blade_protocol_t * bp = NULL ;
2017-09-13 14:41:40 +00:00
blade_channel_t * bc = NULL ;
2017-07-25 17:00:45 +00:00
ks_assert ( bmmgr ) ;
2017-09-13 14:41:40 +00:00
ks_assert ( protocol ) ;
2017-07-25 17:00:45 +00:00
ks_assert ( channel ) ;
2017-09-21 04:07:34 +00:00
ks_hash_read_lock ( bmmgr - > protocols ) ;
2017-09-13 14:41:40 +00:00
2017-09-21 04:07:34 +00:00
bp = ( blade_protocol_t * ) ks_hash_search ( bmmgr - > protocols , ( void * ) protocol , KS_UNLOCKED ) ;
2017-07-25 17:00:45 +00:00
if ( ! bp ) {
ret = KS_STATUS_NOT_FOUND ;
goto done ;
2017-07-03 18:48:46 +00:00
}
2017-09-21 04:07:34 +00:00
blade_protocol_write_lock ( bp ) ;
2017-09-13 14:41:40 +00:00
bc = blade_protocol_channel_lookup ( bp , channel , KS_TRUE ) ;
if ( ! bc ) {
ret = KS_STATUS_NOT_FOUND ;
goto done ;
}
2017-08-18 22:30:08 +00:00
if ( blade_protocol_channel_remove ( bp , channel ) ) {
2017-09-21 04:07:34 +00:00
blade_subscriptionmgr_broadcast ( blade_handle_subscriptionmgr_get ( bmmgr - > handle ) , BLADE_RPCBROADCAST_COMMAND_CHANNEL_REMOVE , NULL , protocol , channel , NULL , NULL , NULL , NULL ) ;
ks_log ( KS_LOG_DEBUG , " Protocol Channel Removed: %s from %s \n " , blade_channel_name_get ( bc ) , blade_protocol_name_get ( bp ) ) ;
2017-09-13 14:41:40 +00:00
blade_channel_write_unlock ( bc ) ;
blade_channel_destroy ( & bc ) ;
2017-08-18 22:30:08 +00:00
}
2017-07-25 17:00:45 +00:00
done :
2017-09-13 14:41:40 +00:00
if ( bp ) blade_protocol_write_unlock ( bp ) ;
2017-09-21 04:07:34 +00:00
ks_hash_read_unlock ( bmmgr - > protocols ) ;
2017-07-25 17:00:45 +00:00
return ret ;
}
2017-09-21 04:07:34 +00:00
KS_DECLARE ( ks_status_t ) blade_mastermgr_protocol_channel_authorize ( blade_mastermgr_t * bmmgr , ks_bool_t remove , const char * protocol , const char * channel , const char * controller , const char * target )
2017-07-25 17:00:45 +00:00
{
ks_status_t ret = KS_STATUS_SUCCESS ;
blade_protocol_t * bp = NULL ;
2017-09-13 14:41:40 +00:00
blade_channel_t * bc = NULL ;
2017-07-25 17:00:45 +00:00
ks_assert ( bmmgr ) ;
2017-09-13 14:41:40 +00:00
ks_assert ( protocol ) ;
2017-07-25 17:00:45 +00:00
ks_assert ( channel ) ;
ks_assert ( controller ) ;
ks_assert ( target ) ;
2017-09-21 04:07:34 +00:00
ks_hash_read_lock ( bmmgr - > protocols ) ;
2017-09-13 14:41:40 +00:00
2017-09-21 04:07:34 +00:00
bp = ( blade_protocol_t * ) ks_hash_search ( bmmgr - > protocols , ( void * ) protocol , KS_UNLOCKED ) ;
2017-07-25 17:00:45 +00:00
if ( ! bp ) {
ret = KS_STATUS_NOT_FOUND ;
goto done ;
}
2017-08-01 22:48:34 +00:00
2017-09-21 04:07:34 +00:00
blade_protocol_read_lock ( bp ) ;
2017-09-13 14:41:40 +00:00
if ( ! blade_protocol_controller_verify ( bp , controller ) ) {
ret = KS_STATUS_NOT_ALLOWED ;
goto done ;
}
2017-07-25 17:00:45 +00:00
2017-09-13 14:41:40 +00:00
bc = blade_protocol_channel_lookup ( bp , channel , KS_TRUE ) ;
if ( ! bc ) {
ret = KS_STATUS_NOT_FOUND ;
goto done ;
}
2017-07-25 17:00:45 +00:00
2017-09-13 14:41:40 +00:00
if ( remove ) {
if ( blade_channel_authorization_remove ( bc , target ) ) {
2017-09-21 04:07:34 +00:00
ks_log ( KS_LOG_DEBUG , " Protocol Channel Authorization Removed: %s from protocol %s, channel %s \n " , target , blade_protocol_name_get ( bp ) , blade_channel_name_get ( bc ) ) ;
2017-09-13 14:41:40 +00:00
} else ret = KS_STATUS_NOT_FOUND ;
} else {
if ( blade_channel_authorization_add ( bc , target ) ) {
2017-09-21 04:07:34 +00:00
ks_log ( KS_LOG_DEBUG , " Protocol Channel Authorization Added: %s to protocol %s, channel %s \n " , target , blade_protocol_name_get ( bp ) , blade_channel_name_get ( bc ) ) ;
2017-09-13 14:41:40 +00:00
}
}
done :
if ( bc ) blade_channel_write_unlock ( bc ) ;
if ( bp ) blade_protocol_read_unlock ( bp ) ;
2017-09-21 04:07:34 +00:00
ks_hash_read_unlock ( bmmgr - > protocols ) ;
2017-07-25 17:00:45 +00:00
return ret ;
}
2017-09-21 04:07:34 +00:00
KS_DECLARE ( ks_bool_t ) blade_mastermgr_protocol_channel_authorization_verify ( blade_mastermgr_t * bmmgr , const char * protocol , const char * channel , const char * target )
2017-07-25 17:00:45 +00:00
{
ks_bool_t ret = KS_FALSE ;
blade_protocol_t * bp = NULL ;
2017-09-13 14:41:40 +00:00
blade_channel_t * bc = NULL ;
2017-07-25 17:00:45 +00:00
ks_assert ( bmmgr ) ;
2017-09-13 14:41:40 +00:00
ks_assert ( protocol ) ;
2017-07-25 17:00:45 +00:00
ks_assert ( channel ) ;
ks_assert ( target ) ;
2017-09-21 04:07:34 +00:00
ks_hash_read_lock ( bmmgr - > protocols ) ;
2017-07-25 17:00:45 +00:00
2017-09-21 04:07:34 +00:00
bp = ( blade_protocol_t * ) ks_hash_search ( bmmgr - > protocols , ( void * ) protocol , KS_UNLOCKED ) ;
2017-09-13 14:41:40 +00:00
if ( ! bp ) {
2017-10-10 12:06:11 +00:00
ret = KS_FALSE ;
2017-09-13 14:41:40 +00:00
goto done ;
}
2017-07-25 17:00:45 +00:00
2017-09-21 04:07:34 +00:00
blade_protocol_read_lock ( bp ) ;
2017-09-13 14:41:40 +00:00
bc = blade_protocol_channel_lookup ( bp , channel , KS_FALSE ) ;
if ( ! bc ) {
2017-10-10 12:06:11 +00:00
ret = KS_FALSE ;
2017-09-13 14:41:40 +00:00
goto done ;
}
2017-09-21 04:07:34 +00:00
if ( ( blade_channel_flags_get ( bc ) & BLADE_CHANNEL_FLAGS_PUBLIC ) = = BLADE_CHANNEL_FLAGS_PUBLIC ) ret = KS_TRUE ;
else ret = blade_channel_authorization_verify ( bc , target ) ;
2017-09-13 14:41:40 +00:00
done :
if ( bc ) blade_channel_read_unlock ( bc ) ;
if ( bp ) blade_protocol_read_unlock ( bp ) ;
2017-09-21 04:07:34 +00:00
ks_hash_read_unlock ( bmmgr - > protocols ) ;
2017-07-25 17:00:45 +00:00
return ret ;
2017-07-03 18:48:46 +00:00
}
/* For Emacs:
* Local Variables :
* mode : c
* indent - tabs - mode : t
* tab - width : 4
* c - basic - offset : 4
* End :
* For VIM :
* vim : set softtabstop = 4 shiftwidth = 4 tabstop = 4 noet :
*/