Skip to content

Commit

Permalink
it (almost) works!!!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Jan 22, 2025
1 parent 5d17ba8 commit 296fa92
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 28 deletions.
13 changes: 13 additions & 0 deletions core/pubnub_ntf_dynamic.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "pubnub_log.h"
#if defined PUBNUB_NTF_RUNTIME_SELECTION

#include "core/pubnub_assert.h"
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions core/pubnub_pubsubapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
49 changes: 27 additions & 22 deletions core/samples/pubnub_api_enforcement_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
28 changes: 28 additions & 0 deletions lib/sockets/pbpal_sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions posix/pubnub_ntf_callback_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@ 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
#define MAYBE_INLINE static inline
#else
#define MAYBE_INLINE static
#endif
#endif // 1
#endif // PUBNUB_NTF_RUNTIME_SELECTION


MAYBE_INLINE int pbntf_watch_in_events_callback(pubnub_t* pbp)
Expand Down

0 comments on commit 296fa92

Please sign in to comment.