From 296fa922cdef614057948fb74788a5ca2521b734 Mon Sep 17 00:00:00 2001 From: Xavrax Date: Wed, 22 Jan 2025 19:08:42 +0100 Subject: [PATCH] it (almost) works!!!!! --- core/pubnub_ntf_dynamic.c | 13 ++++++ core/pubnub_pubsubapi.c | 4 +- core/samples/pubnub_api_enforcement_sample.c | 49 +++++++++++--------- lib/sockets/pbpal_sockets.c | 28 +++++++++++ posix/pubnub_ntf_callback_posix.c | 6 +-- 5 files changed, 72 insertions(+), 28 deletions(-) diff --git a/core/pubnub_ntf_dynamic.c b/core/pubnub_ntf_dynamic.c index 9e2a2130..0ae1ea88 100644 --- a/core/pubnub_ntf_dynamic.c +++ b/core/pubnub_ntf_dynamic.c @@ -1,3 +1,4 @@ +#include "pubnub_log.h" #if defined PUBNUB_NTF_RUNTIME_SELECTION #include "core/pubnub_assert.h" @@ -8,12 +9,14 @@ #include "pubnub_config.h" void pubnub_enforce_api(pubnub_t* p, enum pubnub_api_enforcement policy) { + PUBNUB_LOG_INFO("pubnub_enforce_api(pb=%p, policy=%d)\n", p, policy); p->api_policy = policy; } int pbntf_watch_in_events(pubnub_t* pbp) { + PUBNUB_LOG_TRACE("pbntf_watch_in_events(pb=%p, policy=%d)\n", pbp, pbp->api_policy); switch (pbp->api_policy) { case PNA_SYNC: return pbntf_watch_in_events_sync(pbp); @@ -25,6 +28,7 @@ int pbntf_watch_in_events(pubnub_t* pbp) int pbntf_watch_out_events(pubnub_t* pbp) { + PUBNUB_LOG_TRACE("pbntf_watch_out_events(pb=%p, policy=%d)\n", pbp, pbp->api_policy); switch (pbp->api_policy) { case PNA_SYNC: return pbntf_watch_out_events_sync(pbp); @@ -36,6 +40,7 @@ int pbntf_watch_out_events(pubnub_t* pbp) int pbntf_init(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_init(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: return pbntf_init_sync(); @@ -47,6 +52,7 @@ int pbntf_init(pubnub_t* pb) int pbntf_enqueue_for_processing(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_enqueue_for_processing(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: return pbntf_enqueue_for_processing_sync(pb); @@ -58,6 +64,7 @@ int pbntf_enqueue_for_processing(pubnub_t* pb) int pbntf_requeue_for_processing(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_requeue_for_processing(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: return pbntf_requeue_for_processing_sync(pb); @@ -69,6 +76,7 @@ int pbntf_requeue_for_processing(pubnub_t* pb) int pbntf_got_socket(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_got_socket(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: return pbntf_got_socket_sync(pb); @@ -80,6 +88,7 @@ int pbntf_got_socket(pubnub_t* pb) void pbntf_lost_socket(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_lost_socket(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: pbntf_lost_socket_sync(pb); @@ -93,6 +102,7 @@ void pbntf_lost_socket(pubnub_t* pb) void pbntf_start_wait_connect_timer(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_start_wait_connect_timer(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: pbntf_start_wait_connect_timer_sync(pb); @@ -106,6 +116,7 @@ void pbntf_start_wait_connect_timer(pubnub_t* pb) void pbntf_start_transaction_timer(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_start_transaction_timer(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: pbntf_start_transaction_timer_sync(pb); @@ -119,6 +130,7 @@ void pbntf_start_transaction_timer(pubnub_t* pb) void pbntf_update_socket(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pbntf_update_socket(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: pbntf_update_socket_sync(pb); @@ -130,6 +142,7 @@ void pbntf_update_socket(pubnub_t* pb) } enum pubnub_res pubnub_last_result(pubnub_t* pb) { + PUBNUB_LOG_TRACE("pubnub_last_result(pb=%p, policy=%d)\n", pb, pb->api_policy); switch (pb->api_policy) { case PNA_SYNC: return pubnub_last_result_sync(pb); diff --git a/core/pubnub_pubsubapi.c b/core/pubnub_pubsubapi.c index bc8ab7df..ca99a38f 100644 --- a/core/pubnub_pubsubapi.c +++ b/core/pubnub_pubsubapi.c @@ -26,7 +26,7 @@ pubnub_t* pubnub_init(pubnub_t* p, const char* publish_key, const char* subscrib p->wait_connect_timeout_ms = PUBNUB_DEFAULT_WAIT_CONNECT_TIMER; #if defined(PUBNUB_CALLBACK_API) #if defined(PUBNUB_NTF_RUNTIME_SELECTION) - if (p->api_policy == PNA_CALLBACK) { + if (PNA_CALLBACK == p->api_policy) { p->previous = p->next = NULL; } #else @@ -36,7 +36,7 @@ pubnub_t* pubnub_init(pubnub_t* p, const char* publish_key, const char* subscrib } #if defined(PUBNUB_CALLBACK_API) #if defined(PUBNUB_NTF_RUNTIME_SELECTION) - if (p->api_policy == PNA_CALLBACK) { + if (PNA_CALLBACK == p->api_policy) { p->cb = NULL; p->user_data = NULL; p->flags.sent_queries = 0; diff --git a/core/samples/pubnub_api_enforcement_sample.c b/core/samples/pubnub_api_enforcement_sample.c index 1903d53f..28bca278 100644 --- a/core/samples/pubnub_api_enforcement_sample.c +++ b/core/samples/pubnub_api_enforcement_sample.c @@ -26,14 +26,14 @@ static void subscribe_message_listener( int main() { - time_t t0; - enum pubnub_res res; char const* user_id = "my_user_id"; char const* channel_id = "my_channel"; pubnub_t* pbp_sync = pubnub_alloc(); pubnub_t* pbp_callback = pubnub_alloc(); + printf("PubNub API enforcement demo\n"); + if (NULL == pbp_sync || NULL == pbp_callback) { printf("Failed to allocate Pubnub context!\n"); return -1; @@ -45,8 +45,8 @@ int main() char* my_env_publish_key = getenv("PUBNUB_PUBLISH_KEY"); char* my_env_subscribe_key = getenv("PUBNUB_SUBSCRIBE_KEY"); - if (NULL == my_env_publish_key) { my_env_publish_key = "demo"; } - if (NULL == my_env_subscribe_key) { my_env_subscribe_key = "demo"; } + if (NULL == my_env_publish_key) { my_env_publish_key = "pub-c-3c19dc89-6dd9-4ecf-be7e-01ea81d284d2"; } + if (NULL == my_env_subscribe_key) { my_env_subscribe_key = "sub-c-e9c43746-6c7f-44da-b292-8c50c0e4c39e"; } pubnub_init(pbp_sync, my_env_publish_key, my_env_subscribe_key); pubnub_init(pbp_callback, my_env_publish_key, my_env_subscribe_key); @@ -75,24 +75,29 @@ int main() /** Sync context */ -// enum pubnub_res publish_result = pubnub_publish(pbp_sync, channel_id, "\"Hello world from sync!\""); -// if (PNR_OK != publish_result && PNR_STARTED != publish_result) { -// printf("Failed to publish message from sync context!\n"); -// -// sync_sample_free(pbp_sync); -// pubnub_subscription_free(&subscription); -// callback_sample_free(pbp_callback); -// return -1; -// } - -// if (PNR_OK != pubnub_await(pbp_sync)) { -// printf("Failed to await message from sync context!\n"); -// -// sync_sample_free(pbp_sync); -// pubnub_subscription_free(&subscription); -// callback_sample_free(pbp_callback); -// return -1; -// } + /** Wait for the subscription to be established */ + wait_seconds(2); + + printf("Publishing message from sync context...\n"); + enum pubnub_res publish_result = pubnub_publish(pbp_sync, channel_id, "\"Hello world from sync!\""); + if (PNR_OK != publish_result && PNR_STARTED != publish_result) { + printf("Failed to publish message from sync context!\n"); + + sync_sample_free(pbp_sync); + pubnub_subscription_free(&subscription); + callback_sample_free(pbp_callback); + return -1; + } + + if (PNR_OK != pubnub_await(pbp_sync)) { + printf("Failed to await message from sync context!\n"); + + sync_sample_free(pbp_sync); + pubnub_subscription_free(&subscription); + callback_sample_free(pbp_callback); + return -1; + } + printf("Message published from sync context!\n"); wait_seconds(5); diff --git a/lib/sockets/pbpal_sockets.c b/lib/sockets/pbpal_sockets.c index 8b592b23..8d7fcfe4 100644 --- a/lib/sockets/pbpal_sockets.c +++ b/lib/sockets/pbpal_sockets.c @@ -20,6 +20,7 @@ static void buf_setup(pubnub_t* pb) pb->left = sizeof pb->core.http_buf / sizeof pb->core.http_buf[0]; } +#if !defined(PUBNUB_NTF_RUNTIME_SELECTION) static int pal_init(pubnub_t* pb) { static bool s_init = false; @@ -33,6 +34,33 @@ static int pal_init(pubnub_t* pb) } return 0; } +#else +static int pal_init(pubnub_t* pb) +{ + bool* s_init = NULL; + static bool s_init_sync = false; + static bool s_init_callback = false; + + switch(pb->api_policy) { + case PNA_SYNC: + s_init = &s_init_sync; + break; + case PNA_CALLBACK: + s_init = &s_init_callback; + break; + } + + if (!*s_init) { + if (0 != socket_platform_init()) { + return -1; + } + + pbntf_init(pb); + *s_init = true; + } + return 0; +} +#endif void pbpal_init(pubnub_t* pb) diff --git a/posix/pubnub_ntf_callback_posix.c b/posix/pubnub_ntf_callback_posix.c index bcafbec0..0d0a8d2b 100644 --- a/posix/pubnub_ntf_callback_posix.c +++ b/posix/pubnub_ntf_callback_posix.c @@ -37,9 +37,7 @@ struct SocketWatcherData { static struct SocketWatcherData m_watcher; -// TODO: decide if it is worth to keep that here -// 1 - till the flag is fixed -#if 1 +#if PUBNUB_NTF_RUNTIME_SELECTION #define MAYBE_INLINE #else #if __STDC_VERSION__ >= 199901L @@ -47,7 +45,7 @@ static struct SocketWatcherData m_watcher; #else #define MAYBE_INLINE static #endif -#endif // 1 +#endif // PUBNUB_NTF_RUNTIME_SELECTION MAYBE_INLINE int pbntf_watch_in_events_callback(pubnub_t* pbp)