static enum pubnub_res await(struct UserData *pUserData) { start_await(pUserData); return end_await(pUserData); }
/// Awaits the end of transaction and returns its final result /// (outcome). pubnub_res await() { start_await(); return end_await(); }
int main() { char const *msg; enum pubnub_res res; struct UserData user_data; struct UserData user_data_2; char const *chan = "hello_world"; pubnub_t *pbp = pubnub_alloc(); pubnub_t *pbp_2 = pubnub_alloc(); if (NULL == pbp) { printf("Failed to allocate Pubnub context!\n"); return -1; } if (NULL == pbp_2) { printf("Failed to allocate Pubnub context!\n"); return -1; } InitUserData(&user_data, pbp); InitUserData(&user_data_2, pbp_2); pubnub_init(pbp, "demo", "demo"); pubnub_register_callback(pbp, sample_callback, &user_data); pubnub_init(pbp_2, "demo", "demo"); pubnub_register_callback(pbp_2, sample_callback, &user_data_2); puts("-----------------------"); puts("Subscribing..."); puts("-----------------------"); /* First subscribe, to get the time token */ res = pubnub_subscribe(pbp, chan, NULL); if (res != PNR_STARTED) { printf("pubnub_subscribe() returned unexpected: %d\n", res); pubnub_free(pbp); pubnub_free(pbp_2); return -1; } puts("Await subscribe"); res = await(&user_data); if (res == PNR_STARTED) { printf("await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); pubnub_free(pbp_2); return -1; } if (PNR_OK == res) { puts("Subscribed!"); } else { printf("Subscribing failed with code: %d\n", res); } /* The "real" subscribe, with the just acquired time token */ res = pubnub_subscribe(pbp, chan, NULL); if (res != PNR_STARTED) { printf("pubnub_subscribe() returned unexpected: %d\n", res); pubnub_free(pbp); pubnub_free(pbp_2); return -1; } /* Don't do "full" await here, because we didn't publish anything yet! */ start_await(&user_data); puts("-----------------------"); puts("Publishing..."); puts("-----------------------"); /* Since the subscribe is ongoing in the `pbp` context, we can't publish on it, so we use a different context to publish */ res = pubnub_publish(pbp_2, chan, "\"Hello world from subscribe-publish callback sample!\""); if (res != PNR_STARTED) { printf("pubnub_publish() returned unexpected: %d\n", res); pubnub_free(pbp); pubnub_free(pbp_2); return -1; } puts("Await publish"); res = await(&user_data_2); if (res == PNR_STARTED) { printf("await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); pubnub_free(pbp_2); return -1; } if (PNR_OK == res) { printf("Published! Response from Pubnub: %s\n", pubnub_last_publish_result(pbp_2)); } else if (PNR_PUBLISH_FAILED == res) { printf("Published failed on Pubnub, description: %s\n", pubnub_last_publish_result(pbp_2)); } else { printf("Publishing failed with code: %d\n", res); } /* Don't need `pbp_2` no more */ if (pubnub_free(pbp_2) != 0) { printf("Failed to free the Pubnub context `pbp_2`\n"); } /* Now we await the subscribe on `pbp` */ puts("Await subscribe"); res = end_await(&user_data); if (res == PNR_STARTED) { printf("await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); return -1; } if (PNR_OK == res) { puts("Subscribed! Got messages:"); for (;;) { msg = pubnub_get(pbp); if (NULL == msg) { break; } puts(msg); } } else { printf("Subscribing failed with code: %d\n", res); } /* We're done */ if (pubnub_free(pbp) != 0) { printf("Failed to free the Pubnub context `pbp`\n"); } puts("Pubnub subscribe-publish callback demo over."); return 0; }