Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime api enforcement #204

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 68 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ log_option(ASAN "Use Address sanitizer" OFF)

if (WITH_CPP)
set(DEFAULT_USE_CALLBACK_API ON)
set(DEFAULT_USE_SYNC_API OFF)
else ()
set(DEFAULT_USE_CALLBACK_API OFF)
set(DEFAULT_USE_SYNC_API ON)
endif ()

log_option(USE_CPP11 "Use C++11 [WITH_CPP=ON needed]" OFF)
Expand All @@ -79,12 +81,14 @@ num_option(USE_REVOKE_TOKEN_API "Use revoke token API [OPENSSL ONLY]" OFF)
num_option(USE_GRANT_TOKEN_API "Use grant token API [OPENSSL ONLY]" OFF)
num_option(USE_FETCH_HISTORY "Use fetch history" ON)
num_option(USE_CRYPTO_API "Use crypto API [OPENSSL ONLY]" OFF)
num_option(USE_CALLBACK_API "Use callback API [CALLBACK=ON SYNC=OFF]" ${DEFAULT_USE_CALLBACK_API})
num_option(USE_SYNC_API "Use sync API" ${DEFAULT_USE_SYNC_API})
num_option(USE_CALLBACK_API "Use callback API" ${DEFAULT_USE_CALLBACK_API})
num_option(USE_IPV6 "Use IPv6" ON)
num_option(USE_SET_DNS_SERVERS "Use set DNS servers [CALLBACK=ON]" ${DEFAULT_USE_CALLBACK_API})
num_option(USE_EXTERN_API "Use extern C API [WITH_CPP=ON]" ON)
num_option(USE_LEGACY_CRYPTO_RANDOM_IV "Use random IV for legacy crypto module [OpenSSL only]" ON)
num_option(USE_LOG_CALLBACK "Use possibility to replace default logging function with user provided callback" OFF)
log_option(COMPILE_COMMANDS "Generate compile_commands.json" OFF)
log_set(OPENSSL_ROOT_DIR "" "OpenSSL root directory (leave empty for find_package() defaults)[OPENSSL=ON needed]")
log_set(CUSTOM_OPENSSL_LIB_DIR "lib" "OpenSSL lib directory relative to OPENSSL_ROOT_DIR [used only if find_package() failed]")
log_set(CUSTOM_OPENSSL_INCLUDE_DIR "include" "OpenSSL include directory relative to OPENSSL_ROOT_DIR [used only if find_package() failed]")
Expand All @@ -97,6 +101,15 @@ if (${OPENSSL} AND ${MBEDTLS})
message(FATAL_ERROR "You can't use both OpenSSL and mbedTLS at the same time!")
endif ()

if (NOT ${USE_CALLBACK_API} AND NOT ${USE_SYNC_API})
message(FATAL_ERROR "You must select at least one API type!")
endif ()

if(${COMPILE_COMMANDS})
message(STATUS "Generating compile_commands.json")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "")
endif()

# Flags configuration

set(FLAGS "\
Expand Down Expand Up @@ -143,7 +156,13 @@ if (${USE_CALLBACK_API})
${FLAGS} \
-D PUBNUB_SET_DNS_SERVERS=${USE_SET_DNS_SERVERS} \
-D PUBNUB_USE_IPV6=${USE_IPV6} \
-D PUBNUB_CALLBACK_API")
-D PUBNUB_CALLBACK_API=${USE_CALLBACK_API}")
endif ()

if (${USE_SYNC_API})
set(FLAGS "\
${FLAGS} \
-D PUBNUB_USE_SYNC_API=${USE_SYNC_API}")
endif ()

if (${ASAN})
Expand Down Expand Up @@ -276,8 +295,6 @@ endif ()
set(INTF_SOURCEFILES)

if (${USE_CALLBACK_API})
message(STATUS "Using callback API")

set(CORE_SOURCEFILES
${CORE_SOURCEFILES}
${CMAKE_CURRENT_LIST_DIR}/core/pubnub_memory_block.c
Expand Down Expand Up @@ -308,16 +325,33 @@ if (${USE_CALLBACK_API})
${CMAKE_CURRENT_LIST_DIR}/windows/pubnub_dns_system_servers.c
${CMAKE_CURRENT_LIST_DIR}/windows/pubnub_get_native_socket.c)
endif ()
else ()
message(STATUS "Using sync API")
endif ()

if (${USE_SYNC_API})
set(INTF_SOURCEFILES
${INTF_SOURCEFILES}
${CMAKE_CURRENT_LIST_DIR}/core/pubnub_ntf_sync.c
${CMAKE_CURRENT_LIST_DIR}/core/pubnub_sync_subscribe_loop.c
${CMAKE_CURRENT_LIST_DIR}/core/srand_from_pubnub_time.c)
endif ()

if (${USE_SYNC_API} AND ${USE_CALLBACK_API})
message(STATUS "Using runtime API selection")

set(CMAKE_C_FLAGS "\
${CMAKE_C_FLAGS} \
-D PUBNUB_NTF_RUNTIME_SELECTION")

set(INTF_SOURCEFILES
${INTF_SOURCEFILES}
${CMAKE_CURRENT_LIST_DIR}/core/pubnub_ntf_enforcement.c)

elseif (${USE_SYNC_API})
message(STATUS "Using sync API")
elseif (${USE_CALLBACK_API})
message(STATUS "Using callback API")
endif ()

set(FEATURE_SOURCEFILES)

if (${USE_PROXY})
Expand Down Expand Up @@ -732,11 +766,18 @@ if (${EXAMPLES})
message(STATUS "Building callback examples")
set(EXAMPLE_LIST
pubnub_callback_sample
pubnub_callback_subloop_sample
subscribe_publish_callback_sample
subscribe_publish_from_callback
publish_callback_subloop_sample
publish_queue_callback_subloop)
${EXAMPLE_LIST})

if (NOT ${USE_SYNC_API})
set(EXAMPLE_LIST
pubnub_callback_subloop_sample
publish_callback_subloop_sample
publish_queue_callback_subloop
${EXAMPLE_LIST})
endif ()

if (${USE_SUBSCRIBE_EVENT_ENGINE})
set(EXAMPLE_LIST
subscribe_event_engine_sample
Expand All @@ -747,16 +788,24 @@ if (${EXAMPLES})
subscribe_publish_callback_sample # Only supports callback!
${CPP_EXAMPLE_LIST})
endif ()
else ()
endif ()
if (${USE_SYNC_API})
message(STATUS "Building sync examples")
set(EXAMPLE_LIST
metadata
pubnub_sync_subloop_sample
pubnub_sync_publish_retry
pubnub_publish_via_post_sample
pubnub_advanced_history_sample
pubnub_fetch_history_sample
cancel_subscribe_sync_sample)
cancel_subscribe_sync_sample
${EXAMPLE_LIST})

if (NOT ${USE_CALLBACK_API})
set(EXAMPLE_LIST
pubnub_sync_subloop_sample
${EXAMPLE_LIST})
endif ()

if (OPENSSL)
set(EXAMPLE_LIST
pubnub_crypto_module_sample
Expand All @@ -773,6 +822,13 @@ if (${EXAMPLES})
${EXAMPLE_LIST})
endif ()
endif ()

if (${USE_SYNC_API} AND ${USE_CALLBACK_API})
set(EXAMPLE_LIST
pubnub_api_enforcement_sample
${EXAMPLE_LIST})
endif ()

else ()
message(STATUS "Building example ${EXAMPLE}")
set(EXAMPLE_LIST ${EXAMPLE})
Expand Down
16 changes: 14 additions & 2 deletions core/pbauto_heartbeat.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static bool pubsub_keys_changed(pubnub_t const* pb_clone, pubnub_t const* pb)
|| (pb_clone->core.subscribe_key != pb->core.subscribe_key);
}

#if defined(PUBNUB_CALLBACK_API)
#if defined(PUBNUB_CALLBACK_API) && !defined(PUBNUB_NTF_RUNTIME_SELECTION)
#define add_heartbeat_in_progress(thumper_index)
#else
static void add_heartbeat_in_progress(unsigned thumper_index)
Expand All @@ -131,7 +131,13 @@ static void heartbeat_thump(pubnub_t* pb, pubnub_t* heartbeat_pb)

if (keys_changed) {
/** Used in sync environment while for callback it's an empty macro */
#if defined(PUBNUB_NTF_RUNTIME_SELECTION)
if (PNA_SYNC == pb->api_policy) {
add_heartbeat_in_progress(pb->thumperIndex);
}
#else
add_heartbeat_in_progress(pb->thumperIndex);
#endif
pubnub_mutex_unlock(pb->monitor);
pubnub_cancel(heartbeat_pb);
return;
Expand All @@ -156,7 +162,13 @@ static void heartbeat_thump(pubnub_t* pb, pubnub_t* heartbeat_pb)
pubnub_res_2_string(res));
}
/** Used in sync environment while for callback it's an empty macro */
#if defined(PUBNUB_NTF_RUNTIME_SELECTION)
if (PNA_SYNC == pb->api_policy) {
add_heartbeat_in_progress(pb->thumperIndex);
}
#else
add_heartbeat_in_progress(pb->thumperIndex);
#endif
}
pubnub_mutex_unlock(pb->monitor);
}
Expand Down Expand Up @@ -240,7 +252,7 @@ void pbauto_take_the_node_out(unsigned* indexes, unsigned i, unsigned* dimension
memmove(node_out, node_out + 1, (*dimension - i) * sizeof(unsigned));
}

#if defined(PUBNUB_CALLBACK_API)
#if defined(PUBNUB_CALLBACK_API) && !defined(PUBNUB_NTF_RUNTIME_SELECTION)
#define handle_heartbeats_in_progress()
#else
static void handle_heartbeats_in_progress(void)
Expand Down
2 changes: 1 addition & 1 deletion core/pbauto_heartbeat.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct pubnub_heartbeat_data {

struct HeartbeatWatcherData {
struct pubnub_heartbeat_data heartbeat_data[PUBNUB_MAX_HEARTBEAT_THUMPERS] pubnub_guarded_by(mutw);
#if !defined(PUBNUB_CALLBACK_API)
#if !defined(PUBNUB_CALLBACK_API) || defined(PUBNUB_NTF_RUNTIME_SELECTION)
/** Array of thumper indices for which auto heartbeat transactions are currently in progress.
Used in sync environment.
*/
Expand Down
10 changes: 9 additions & 1 deletion core/pbcc_request_retry_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ void pbcc_request_retry_timer_start(
timer->pb->core.http_retry_count++;
pubnub_mutex_unlock(timer->pb->monitor);

#if defined(PUBNUB_NTF_RUNTIME_SELECTION)
if (PNA_SYNC == timer->pb->api_policy) {
pbcc_request_retry_timer_run_(timer);
} else {
#endif
#if defined(PUBNUB_CALLBACK_API)
if (pthread_create(&timer->timer_thread,
NULL,
Expand All @@ -144,6 +149,9 @@ void pbcc_request_retry_timer_start(
}
pubnub_mutex_unlock(timer->pb->monitor);
}
#if defined(PUBNUB_NTF_RUNTIME_SELECTION)
} /* if (PNA_SYNC == timer->pb->api_policy) */
#endif
#else
pbcc_request_retry_timer_run_(timer);
#endif // #if defined(PUBNUB_CALLBACK_API)
Expand Down Expand Up @@ -195,4 +203,4 @@ void* pbcc_request_retry_timer_run_(pbcc_request_retry_timer_t* timer)
pbcc_request_retry_timer_stop(timer);

return NULL;
}
}
13 changes: 10 additions & 3 deletions core/pbcc_subscribe_event_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,15 @@ void pbcc_event_listener_free(pbcc_event_listener_t** event_listener)
if (NULL == event_listener || NULL == *event_listener) { return; }

pubnub_mutex_lock((*event_listener)->mutw);
pbarray_free(&(*event_listener)->global_status);
pbarray_free(&(*event_listener)->global_events);

if (NULL != (*event_listener)->global_events) {
pbarray_free(&(*event_listener)->global_events);
}

if (NULL != (*event_listener)->listeners) {
pbhash_set_free(&(*event_listener)->listeners);
}

pbhash_set_free(&(*event_listener)->listeners);
pubnub_mutex_unlock((*event_listener)->mutw);
pubnub_mutex_destroy((*event_listener)->mutw);
Expand Down Expand Up @@ -678,4 +685,4 @@ pbarray_t* pbcc_initialize_array_(
PBARRAY_RESIZE_BALANCED,
PBARRAY_GENERIC_CONTENT_TYPE,
free_fn);
}
}
30 changes: 27 additions & 3 deletions core/pbpal_ntf_callback_admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@

#include "pubnub_log.h"
#include "pubnub_assert.h"
#include "core/pubnub_ntf_enforcement.h"


#if PUBNUB_NTF_RUNTIME_SELECTION
#define MAYBE_INLINE
#else
#if __STDC_VERSION__ >= 199901L
#define MAYBE_INLINE static inline
#else
#define MAYBE_INLINE static
#endif
#endif // 1


void pbntf_trans_outcome(pubnub_t* pb, enum pubnub_state state)
{
Expand Down Expand Up @@ -48,7 +59,7 @@ void pbntf_trans_outcome(pubnub_t* pb, enum pubnub_state state)
}
}

void pbnc_tr_cxt_state_reset(pubnub_t* pb)
MAYBE_INLINE void pbnc_tr_cxt_state_reset_callback(pubnub_t* pb)
{
if (pb->trans == PBTT_SET_STATE)
{
Expand All @@ -60,7 +71,7 @@ void pbnc_tr_cxt_state_reset(pubnub_t* pb)
}
}

enum pubnub_res pubnub_last_result(pubnub_t* pb)
MAYBE_INLINE enum pubnub_res pubnub_last_result_callback(pubnub_t* pb)
{
enum pubnub_res rslt;

Expand All @@ -69,14 +80,27 @@ enum pubnub_res pubnub_last_result(pubnub_t* pb)
pubnub_mutex_lock(pb->monitor);
rslt = pb->core.last_result;
if (rslt != PNR_OK){
pbnc_tr_cxt_state_reset(pb);
pbnc_tr_cxt_state_reset_callback(pb);
}
pubnub_mutex_unlock(pb->monitor);

return rslt;
}


#if !defined(PUBNUB_NTF_RUNTIME_SELECTION)
void pbnc_tr_cxt_state_reset(pubnub_t* pb)
{
pbnc_tr_cxt_state_reset_callback(pb);
}


enum pubnub_res pubnub_last_result(pubnub_t* pb)
{
return pubnub_last_result_callback(pb);
}
#endif

enum pubnub_res pubnub_register_callback(pubnub_t* pb,
pubnub_callback_t cb,
void* user_data)
Expand Down
11 changes: 10 additions & 1 deletion core/pubnub_alloc_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pubnub_log.h"

#include "pbpal.h"
#include "pubnub_ntf_enforcement.h"


static struct pubnub_ m_aCtx[PUBNUB_CTX_MAX];
Expand Down Expand Up @@ -78,7 +79,15 @@ int pubnub_free(pubnub_t *pb)
pubnub_disable_auto_heartbeat(pb);
pbauto_heartbeat_free_channelInfo(pb);
pb->state = PBS_NULL;
#if defined(PUBNUB_CALLBACK_API)
#if defined(PUBNUB_NTF_RUNTIME_SELECTION)
if (PNA_CALLBACK == pb->api_policy) {
pbntf_requeue_for_processing(pb);
pubnub_mutex_unlock(pb->monitor);
} else {
pubnub_mutex_unlock(pb->monitor);
pballoc_free_at_last(pb);
}
#elif defined(PUBNUB_CALLBACK_API)
pbntf_requeue_for_processing(pb);
pubnub_mutex_unlock(pb->monitor);
#else
Expand Down
11 changes: 10 additions & 1 deletion core/pubnub_alloc_std.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pubnub_log.h"

#include "pbpal.h"
#include "pubnub_ntf_enforcement.h"

#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -154,7 +155,15 @@ int pubnub_free(pubnub_t* pb)
pubnub_disable_auto_heartbeat(pb);
pbauto_heartbeat_free_channelInfo(pb);
pb->state = PBS_NULL;
#if defined(PUBNUB_CALLBACK_API)
#if defined(PUBNUB_NTF_RUNTIME_SELECTION)
if (PNA_CALLBACK == pb->api_policy) {
pbntf_requeue_for_processing(pb);
pubnub_mutex_unlock(pb->monitor);
} else {
pubnub_mutex_unlock(pb->monitor);
pballoc_free_at_last(pb);
}
#elif defined(PUBNUB_CALLBACK_API)
pbntf_requeue_for_processing(pb);
pubnub_mutex_unlock(pb->monitor);
#else
Expand Down
Loading
Loading