} TEST_ENDDEF TEST_DEF(connect_and_send_over_several_channels_simultaneously) { static pubnub_t *pbp; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, "demo", "demo"); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test M1\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "two", "\"Test M1\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test M1\"", NULL)); expect_pnr(pubnub_subscribe(pbp, "two", NULL), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test M1\"", NULL)); TEST_POP_DEFERRED; } TEST_ENDDEF
} TEST_ENDDEF TEST_DEF(connect_disconnect_and_connect_again) { static pubnub_t *pbp; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, g_pubkey, g_keysub); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test M4\""), PNR_STARTED); pubnub_cancel(pbp); await_timed(5*SECONDS, PNR_CANCELLED, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test M4 - 2\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test M4 - 2\"", NULL)); pubnub_set_non_blocking_io(pbp); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); pubnub_cancel(pbp); await_timed(5*SECONDS, PNR_CANCELLED, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test M4 - 3\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test M4 - 3\"", NULL)); TEST_POP_DEFERRED; } TEST_ENDDEF
} TEST_ENDDEF TEST_DEF(connect_and_send_over_several_channels_in_group_simultaneously) { static pubnub_t *pbp; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, "demo", "demo"); expect_pnr(pubnub_remove_channel_group(pbp, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_add_channel_to_group(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_SLEEP_FOR(CHANNEL_REGISTRY_PROPAGATION_DELAY); expect_pnr(pubnub_subscribe(pbp, NULL, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test M1\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "two", "\"Test M2\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect(pnfntst_subscribe_and_check(pbp, NULL, "gr", 5*SECONDS, "\"Test M1\"", "ch", "\"Test M2\"", "two", NULL)); expect_pnr(pubnub_remove_channel_from_group(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_POP_DEFERRED; } TEST_ENDDEF
} TEST_ENDDEF TEST_DEF(wrong_api_usage) { static pubnub_t *pbp; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, g_pubkey, g_keysub); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test \""), PNR_STARTED); expect_pnr(pubnub_publish(pbp, "ch", "\"Test - 2\""), PNR_IN_PROGRESS); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_IN_PROGRESS); await_timed(5*SECONDS, PNR_OK, pbp); pubnub_set_non_blocking_io(pbp); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_IN_PROGRESS); expect_pnr(pubnub_publish(pbp, "ch", "\"Test - 2\""), PNR_IN_PROGRESS); pubnub_cancel(pbp); await_timed(5*SECONDS, PNR_CANCELLED, pbp); expect_pnr(pubnub_subscribe(pbp, NULL, NULL), PNR_INVALID_CHANNEL); TEST_POP_DEFERRED; } TEST_ENDDEF
} TEST_ENDDEF TEST_DEF(simple_connect_and_receiver_over_single_channel) { static pubnub_t *pbp; static pubnub_t *pbp_2; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, "demo", "demo"); pbp_2 = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp_2); pubnub_init(pbp_2, "demo", "demo"); expect_pnr(pubnub_subscribe(pbp_2, "ch", NULL), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp_2); pubnub_set_non_blocking_io(pbp_2); expect_pnr(pubnub_subscribe(pbp_2, "ch", NULL), PNR_STARTED); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 2\""), PNR_STARTED); await_timed_2(5*SECONDS, PNR_OK, pbp, PNR_OK, pbp_2); expect(pnfntst_got_messages(pbp_2, "\"Test 2\"", NULL)); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 2 - 2\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp_2, "ch", NULL), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp_2); expect(pnfntst_got_messages(pbp_2, "\"Test 2 - 2\"", NULL)); TEST_POP_DEFERRED; TEST_POP_DEFERRED; } TEST_ENDDEF
} TEST_ENDDEF TEST_DEF(broken_connection_test_group_multichannel_out) { static pubnub_t *pbp; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, "demo", "demo"); expect_pnr(pubnub_remove_channel_group(pbp, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_add_channel_to_group(pbp, "three", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_SLEEP_FOR(CHANNEL_REGISTRY_PROPAGATION_DELAY); expect_pnr(pubnub_subscribe(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 3\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "two", "\"Test 4\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "three", "\"Test 5\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test 3\"", "\"Test 4\"", "\"Test 5\"", NULL)); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 3 - 2\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "two", "\"Test 4 - 2\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "three", "\"Test 5 - 2\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); printf("Please disconnect from Internet. Press Enter when done."); await_console(); expect_pnr(pubnub_subscribe(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(6*SECONDS, PNR_ADDR_RESOLUTION_FAILED, pbp); printf("Please reconnect to Internet. Press Enter when done."); await_console(); expect_pnr(pubnub_subscribe(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test 3 - 2\"", "\"Test 4 - 2\"", "\"Test 5 - 2\"", NULL)); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 3 - 4\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "two", "\"Test 4 - 4\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "three", "\"Test 5 - 4\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp, "ch,two", "gr"), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test 3 - 4\"", "\"Test 4 - 4\"", "\"Test 5 - 4\"", NULL)); expect_pnr(pubnub_remove_channel_from_group(pbp, "three", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_POP_DEFERRED; } TEST_ENDDEF
PUBNUB_API void PubNub::publish(const std::string &channel, json_object &message, long timeout, PubNub_publish_cb cb, void *cb_data) { if (cb) { publish_pair *cb_info = new publish_pair(std::pair<PubNub_publish_cb, PubNub *>(cb, this), cb_data); pubnub_publish(p, channel.c_str(), &message, timeout, pubnub_cpp_publish_cb, cb_info); } else { pubnub_publish(p, channel.c_str(), &message, timeout, NULL, NULL); } }
} TEST_ENDDEF TEST_DEF(simple_connect_and_receiver_over_single_channel_in_group) { static pubnub_t *pbp; static pubnub_t *pbp_2; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, "demo", "demo"); pbp_2 = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp_2); pubnub_init(pbp_2, "demo", "demo"); expect_pnr(pubnub_remove_channel_group(pbp_2, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp_2); expect_pnr(pubnub_add_channel_to_group(pbp_2, "ch", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp_2); TEST_SLEEP_FOR(CHANNEL_REGISTRY_PROPAGATION_DELAY); expect_pnr(pubnub_subscribe(pbp_2, NULL, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp_2); pubnub_set_non_blocking_io(pbp_2); expect_pnr(pubnub_subscribe(pbp_2, NULL, "gr"), PNR_STARTED); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 2\""), PNR_STARTED); await_timed_2(5*SECONDS, PNR_OK, pbp, PNR_OK, pbp_2); expect(pnfntst_got_messages(pbp_2, "\"Test 2\"", NULL)); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 2 - 2\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp_2, NULL, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp_2); expect(pnfntst_got_messages(pbp_2, "\"Test 2 - 2\"", NULL)); expect_pnr(pubnub_remove_channel_from_group(pbp, "ch", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_POP_DEFERRED; TEST_POP_DEFERRED; } TEST_ENDDEF
} TEST_ENDDEF TEST_DEF(connect_disconnect_and_connect_again_group) { static pubnub_t *pbp; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, g_pubkey, g_keysub); expect_pnr(pubnub_remove_channel_group(pbp, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_add_channel_to_group(pbp, "ch", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_SLEEP_FOR(CHANNEL_REGISTRY_PROPAGATION_DELAY); expect_pnr(pubnub_subscribe(pbp, NULL, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test M4\""), PNR_STARTED); pubnub_cancel(pbp); await_timed(5*SECONDS, PNR_CANCELLED, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test M4 - 2\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp, NULL, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test M4 - 2\"", NULL)); pubnub_set_non_blocking_io(pbp); expect_pnr(pubnub_subscribe(pbp, NULL, "gr"), PNR_STARTED); pubnub_cancel(pbp); await_timed(5*SECONDS, PNR_CANCELLED, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test M4 - 3\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp, NULL, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test M4 - 3\"", NULL)); expect_pnr(pubnub_remove_channel_from_group(pbp, "ch", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_POP_DEFERRED; } TEST_ENDDEF
} TEST_ENDDEF TEST_DEF(broken_connection_test) { static pubnub_t *pbp; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, "demo", "demo"); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 3\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test 3\"", NULL)); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 3 - 2\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); printf("Please disconnect from Internet. Press Enter when done."); await_console(); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); await_timed(6*SECONDS, PNR_ADDR_RESOLUTION_FAILED, pbp); printf("Please reconnect to Internet. Press Enter when done."); await_console(); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test 3 - 2\"", NULL)); printf("Please disconnect from Internet. Press Enter when done."); await_console(); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 3 - 3\""), PNR_STARTED); await_timed(6*SECONDS, PNR_ADDR_RESOLUTION_FAILED, pbp); printf("Please reconnect to Internet. Press Enter when done."); await_console(); expect_pnr(pubnub_publish(pbp, "ch", "\"Test 3 - 4\""), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_subscribe(pbp, "ch", NULL), PNR_STARTED); await_timed(6*SECONDS, PNR_OK, pbp); expect(pnfntst_got_messages(pbp, "\"Test 3 - 4\"", NULL)); TEST_POP_DEFERRED; } TEST_ENDDEF
// see www.pubnub.com/blog/build-real-time-web-apps-easy void pubnub_demo_chat(void) { static struct Pubnub nub; static struct PubnubRequest nubreq; /* The underlying tcp connection, should reconnect in case of failure.. */ pubnub_init(&nub, "chat", "demo", "demo", "0", "pubsub.pubnub.com", 80, NULL); pubnub_req_init(&nub, &nubreq, 512, 512); pubnub_publish(&nubreq, "\"Hello World From c\"", print_nub); }
} TEST_ENDDEF TEST_DEF(complex_send_and_receive_over_channel_plus_group_simultaneously) { static pubnub_t *pbp; static pubnub_t *pbp_2; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, g_pubkey, g_keysub); pbp_2 = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp_2); pubnub_init(pbp_2, g_pubkey, g_keysub); expect_pnr(pubnub_remove_channel_group(pbp, "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_add_channel_to_group(pbp, "two,three", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_SLEEP_FOR(CHANNEL_REGISTRY_PROPAGATION_DELAY); expect_pnr(pubnub_subscribe(pbp, NULL, "gr"), PNR_STARTED); expect_pnr(pubnub_subscribe(pbp_2, "ch", NULL), PNR_STARTED); await_timed_2(5*SECONDS, PNR_OK, pbp, PNR_OK, pbp_2); expect_pnr(pubnub_publish(pbp, "two", "\"Test M3\""), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); expect_pnr(pubnub_publish(pbp_2, "ch", "\"Test M3\""), PNR_STARTED); expect_pnr(pubnub_publish(pbp, "three", "\"Test M3 - 1\""), PNR_STARTED); await_timed_2(5*SECONDS, PNR_OK, pbp, PNR_OK, pbp_2); expect_pnr(pubnub_subscribe(pbp, NULL, "gr"), PNR_STARTED); expect_pnr(pubnub_subscribe(pbp_2, "ch", NULL), PNR_STARTED); await_timed_2(5*SECONDS, PNR_OK, pbp, PNR_OK, pbp_2); expect(pnfntst_got_messages(pbp, "\"Test M3\"", "\"Test M3 - 1\"", NULL)); expect(pnfntst_got_messages(pbp_2, "\"Test M3\"", NULL)); expect_pnr(pubnub_remove_channel_from_group(pbp, "two,three", "gr"), PNR_STARTED); await_timed(5*SECONDS, PNR_OK, pbp); TEST_POP_DEFERRED; TEST_POP_DEFERRED; } TEST_ENDDEF
int pubnub_send(char *p_data) { enum pubnub_res l_response; char const *l_responseChannel = "parkingdevice-resp"; struct Pubnub_UUID uuid; struct Pubnub_UUID_String str_uuid; pubnub_t *l_publish = pubnub_alloc(); if (NULL == l_publish) { printf("Failed to allocate Pubnub context!\n"); return -1; } pubnub_init(l_publish, pub_key, sub_key); if (0 != pubnub_generate_uuid_v4_random(&uuid)) { pubnub_set_uuid(l_publish, "zeka-peka-iz-jendeka"); } else { str_uuid = pubnub_uuid_to_string(&uuid); pubnub_set_uuid(l_publish, str_uuid.uuid); printf("Generated UUID: %s\n", str_uuid.uuid); } pubnub_set_auth(l_publish, "danaske"); puts("Publishing..."); l_response = pubnub_publish(l_publish, l_responseChannel, p_data); if(l_response != PNR_STARTED) { printf("pubnub_publish() returned unexpected: %d\n", l_response); pubnub_free(l_publish); return -1; } l_response = pubnub_await(l_publish); if (l_response == PNR_STARTED) { printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", l_response); pubnub_free(l_publish); return -1; } if (PNR_OK == l_response) { printf("Published! Response from Pubnub: %s\n", pubnub_last_publish_result(l_publish)); return 0; } else if (PNR_PUBLISH_FAILED == l_response) { printf("Published failed on Pubnub, description: %s\n", pubnub_last_publish_result(l_publish)); } else { printf("Publishing failed with code: %d\n", l_response); } pubnub_free(l_publish); return 0; }
void pubnub_account(void) { static struct Pubnub nub; static struct PubnubRequest nubreq; /* The underlying tcp connection, should reconnect in case of failure.. */ pubnub_init(&nub, "6356800465306", dev_regs.pubnubPublishKey, dev_regs.pubnubSubscribeKey, "0", "pubsub.pubnub.com", 80, NULL); pubnub_req_init(&nub, &nubreq, 512, 512); pubnub_publish(&nubreq, "\"Hello World From c\"", print_nub); }
} TEST_ENDDEF TEST_DEF(handling_errors_from_pubnub) { static pubnub_t *pbp; pbp = pubnub_alloc(); TEST_DEFER(pubnub_free, pbp); pubnub_init(pbp, g_pubkey, g_keysub); expect_pnr(pubnub_publish(pbp, "ch", "\"Test "), PNR_STARTED); await_timed(5*SECONDS, PNR_PUBLISH_FAILED, pbp); expect(400 == pubnub_last_http_code(pbp)); expect(pubnub_parse_publish_result(pubnub_last_publish_result(pbp)) == PNPUB_INVALID_JSON); expect_pnr(pubnub_publish(pbp, ",", "\"Test \""), PNR_STARTED); await_timed(5*SECONDS, PNR_PUBLISH_FAILED, pbp); expect(400 == pubnub_last_http_code(pbp)); expect(pubnub_parse_publish_result(pubnub_last_publish_result(pbp)) == PNPUB_INVALID_CHAR_IN_CHAN_NAME); TEST_POP_DEFERRED; } TEST_ENDDEF
veBool pubnub_atPublishN(struct PubnubAt* nubat, char const *buf, size_t buf_len) { struct PubnubRequest *nubreq; u8 const *json; size_t json_len; /* allocate request */ if (!nubat->g) { nubat->g = yajl_gen_alloc(NULL); if (!nubat->g) return veFalse; } nubreq = (struct PubnubRequest *) ve_malloc(sizeof(struct PubnubRequest)); if (!nubreq) { /* reuse the nubat->q next time */ return veFalse; } pubnub_req_init(&nubat->nub, nubreq, 512, 512); /* build json data.. */ if (yajl_gen_string(nubat->g, (u8*) buf, buf_len) != yajl_gen_status_ok) { ve_error("json: not a valid string"); goto error; } if (yajl_gen_get_buf(nubat->g, &json, &json_len) != yajl_gen_status_ok) { ve_error("json: could not get buf"); return veFalse; } /* sent it */ if (pubnub_publish(nubreq, (char*) json, publish_callback) != RET_OK) { ve_error("could not pubnub_publish"); goto error; } yajl_gen_clear(nubat->g); /* empty buffers */ yajl_gen_free(nubat->g); nubat->g = NULL; return veTrue; error: pubnub_req_deinit(nubreq); ve_free(nubreq); yajl_gen_free(nubat->g); nubat->g = NULL; return veFalse; }
static void clock_update(int fd, short kind, void *userp) { /* Print current time. */ time_t t = time(NULL); int now_s = t % 60; int now_m = (t / 60) % 60; int now_h = (t / 3600) % 24; /* The trailing \r will make cursor return to the beginning * of the current line. */ printf("%02d:%02d:%02d\r", now_h, now_m, now_s); fflush(stdout); /* Next clock update in one second. */ /* (A more prudent timer strategy would be to update clock * on the next second _boundary_.) */ struct timeval timeout = { .tv_sec = 1, .tv_usec = 0 }; evtimer_add(&clock_update_timer, &timeout); } /* The callback chain. * * Below, we have many separate functions, but the control flow * is mostly linear, so just continue reading in next function * when you finish the previous one. The code is split to functions * (i) when issuing a call that must be handled asynchronously, and * (ii) for clarity. */ static void publish(struct pubnub *p); static void publish_done(struct pubnub *p, enum pubnub_res result, struct json_object *response, void *ctx_data, void *call_data); static void history(struct pubnub *p); static void history_received(struct pubnub *p, enum pubnub_res result, struct json_object *msg, void *ctx_data, void *call_data); static void subscribe(struct pubnub *p); static void subscribe_received(struct pubnub *p, enum pubnub_res result, char **channels, struct json_object *msg, void *ctx_data, void *call_data); static void publish(struct pubnub *p) { json_object *msg = json_object_new_object(); json_object_object_add(msg, "num", json_object_new_int(42)); json_object_object_add(msg, "str", json_object_new_string("\"Hello, world!\" she said.")); pubnub_publish(p, "my_channel", msg, -1, publish_done, NULL); json_object_put(msg); /* ...continues later in publish_done(). */ }
int pubnub_publishStatus(char *p_data){ enum pubnub_res res; char const *chan = "sensorStatus-resp"; pubnub_t *pbp = pubnub_alloc(); if (NULL == pbp) { printf("Failed to allocate Pubnub context!\n"); return -1; } pubnub_init(pbp, g_pub_key,g_sub_key); pubnub_set_transaction_timeout(pbp, PUBNUB_DEFAULT_NON_SUBSCRIBE_TIMEOUT); /* Leave this commented out to use the default - which is blocking I/O on most platforms. Uncomment to use non- blocking I/O. */ // pubnub_set_non_blocking_io(pbp); generate_uuid(pbp); pubnub_set_auth(pbp, "danaske"); res = pubnub_publish(pbp, chan, p_data); if (res != PNR_STARTED) { printf("pubnub_publish() returned unexpected: %d\n", res); pubnub_free(pbp); return -1; } res = pubnub_await(pbp); if (res == PNR_STARTED) { printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); return -1; } if (PNR_OK == res) { //printf("Published! Response from Pubnub: %s\n", pubnub_last_publish_result(pbp)); } else if (PNR_PUBLISH_FAILED == res) { printf("Published failed on Pubnub, description: %s\n", pubnub_last_publish_result(pbp)); } else { printf("Publishing failed with code: %d\n", res); } return 0; }
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; }
int main() { int option; char opt_string[4]; char channel[PNC_CHANNEL_NAME_SIZE]; char channel_group[PNC_CHANNEL_NAME_SIZE]; char auth_key[PNC_AUTH_KEY_SIZE]; char uuid[PNC_UUID_SIZE + 1]; char state[2048]; pubnub_t *pn = pubnub_alloc(); pubnub_t *pn_sub = pubnub_alloc(); if (NULL == pn || NULL == pn_sub) { printf("Failed to allocate pubnub context"); return -1; } pubnub_init(pn, pubkey, pubkey); pubnub_init(pn_sub, pubkey, pubkey); pubnub_set_uuid(pn, PNC_DEFAULT_UUID); pubnub_set_uuid(pn, PNC_DEFAULT_SUBSCRIBE_UUID); pnc_ops_init(pn, pn_sub); displayMenuOptions(pn); option = 0; while (option != MENU_EXIT) { fgets(opt_string, sizeof(opt_string), stdin); opt_string[strlen(opt_string) - 1] = '\0'; option = atoi(opt_string); printf("You entered %d(%s)\n", option, opt_string); switch (option) { case MENU_DISPLAY: break; case MENU_PUBLISH: { char message[PNC_READ_STRING_SIZE]; bool store = false; // TODO: add json built string pnc_read_string_from_console("Channel Name", channel, PNC_CHANNEL_NAME_SIZE); pnc_read_bool_from_console_optional("Store", &store, true); puts("Enter the message for publish. To exit loop enter Q"); while (fgets(message, PNC_READ_STRING_SIZE, stdin) != NULL && strcmp(message, "Q\n")) { if (strlen(message) == 1) { puts("Invalid input"); continue; } message[strlen(message) - 1] = '\0'; if (message[0] == '{' && message[strlen(message) - 1] == '}') { pnc_ops_parse_response("pubnub_publish()", pubnub_publish(pn, channel, message), pn); } else { char encoded[PNC_READ_STRING_SIZE]; snprintf(encoded, sizeof encoded, "\"%s\"", message); pnc_ops_parse_response("pubnub_publish()", pubnub_publish(pn, channel, encoded), pn); } puts("Enter the message for publish. To exit loop enter Q."); } break; } case MENU_HISTORY: { int count = 100; bool include_token = false; pnc_read_int_from_console_optional("Count", &count, true); if (count <= 0) { count = 100; puts("Count value cannot be less than 1. Default value(100) was applied."); } pnc_read_string_from_console("Channel Name", channel, PNC_CHANNEL_NAME_SIZE); // WARNING: Default buffer can be not big enough to get 100-items // history response with timetokens pnc_read_bool_from_console("Include timetokens", &include_token); pnc_ops_parse_response("pubnub_history()", pubnub_history(pn, channel, count, include_token), pn); break; } case MENU_HERE_NOW: pnc_read_string_from_console("Channel Name", channel, PNC_CHANNEL_NAME_SIZE); pnc_ops_parse_response("pubnub_here_now()", pubnub_here_now(pn, channel, NULL), pn); break; case MENU_TIME: pnc_ops_parse_response("pubnub_time()", pubnub_time(pn), pn); break; case MENU_AUTH: pnc_read_string_from_console("Auth key", auth_key, PNC_AUTH_KEY_SIZE); pubnub_set_auth(pn, auth_key); break; case MENU_UUID: pnc_read_string_from_console("UUID", uuid, PNC_UUID_SIZE); pubnub_set_uuid(pn, uuid); break; case MENU_STATE_GET: pnc_read_string_from_console("Channel Name", channel, PNC_CHANNEL_NAME_SIZE); pnc_read_string_from_console_optional("UUID", uuid, PNC_UUID_SIZE, true); if (strlen(uuid) == 0) { strcpy(uuid, pubnub_uuid_get(pn)); } pnc_ops_parse_response("pubnub_get_state()", pubnub_state_get(pn, channel, NULL, uuid), pn); break; case MENU_SET_STATE: // Set State pnc_read_string_from_console("Channel Name", channel, PNC_CHANNEL_NAME_SIZE); pnc_read_string_from_console("JSON state without escaping", state, PNC_READ_STRING_SIZE); pnc_ops_parse_response("pubnub_set_state()", pubnub_set_state(pn, channel, NULL, pubnub_uuid_get(pn), state), pn); break; case MENU_WHERE_NOW: pnc_read_string_from_console_optional("UUID", uuid, PNC_UUID_SIZE, true); if (strlen(uuid) == 0) { puts("Using current UUID"); strcpy(uuid, PNC_DEFAULT_UUID); } pnc_ops_parse_response("pubnub_where_now()", pubnub_where_now(pn, uuid), pn); break; case MENU_ADD_PRESENCE_CHANNEL: { char presence_channel[PNC_CHANNEL_NAME_SIZE + sizeof(PNC_PRESENCE_SUFFIX)]; pnc_read_string_from_console("Channel name", channel, PNC_CHANNEL_NAME_SIZE); strcpy(presence_channel, channel); strcat(presence_channel, PNC_PRESENCE_SUFFIX); pnc_subscribe_add_channel(presence_channel); break; } case MENU_SUBSCRIBE: pnc_ops_subscribe(pn_sub); break; case MENU_UNSUBSCRIBE: pnc_ops_unsubscribe(pn_sub); break; case MENU_SUBSCRIPTION_ADD_CHANNEL: pnc_read_string_from_console("Enter Channel name", channel, PNC_CHANNEL_NAME_SIZE); pnc_subscribe_add_channel(channel); break; case MENU_SUBSCRIPTION_ADD_GROUP: pnc_read_string_from_console("Enter Group name", channel, PNC_CHANNEL_NAME_SIZE); pnc_subscribe_add_channel_group(channel); break; case MENU_SUBSCRIPTION_REMOVE_CHANNEL: pnc_read_string_from_console("Enter Channel name", channel, PNC_CHANNEL_NAME_SIZE); pnc_subscribe_remove_channel(channel); break; case MENU_SUBSCRIPTION_REMOVE_GROUP: pnc_read_string_from_console("Enter Group name", channel, PNC_CHANNEL_NAME_SIZE); pnc_subscribe_remove_channel_group(channel); break; case MENU_GROUP_ADD_CHANNEL: pnc_read_string_from_console("Group name", channel_group, PNC_CHANNEL_NAME_SIZE); pnc_read_string_from_console("Channel to add", channel, PNC_CHANNEL_NAME_SIZE); pnc_ops_parse_response("pubnub_add_channel_to_group()", pubnub_add_channel_to_group(pn, channel, channel_group), pn); break; case MENU_GROUP_REMOVE_CHANNEL: pnc_read_string_from_console("Group name", channel_group, PNC_CHANNEL_NAME_SIZE); pnc_read_string_from_console("Channel to remove", channel, PNC_CHANNEL_NAME_SIZE); pnc_ops_parse_response("pubnub_remove_channel_from_group()", pubnub_remove_channel_from_group(pn, channel, channel_group), pn); break; case MENU_GROUP_LIST_CHANNELS: pnc_read_string_from_console("Group to list", channel_group, PNC_CHANNEL_NAME_SIZE); pnc_ops_parse_response("pubnub_list_channel_group()", pubnub_list_channel_group(pn, channel_group), pn); break; case MENU_GROUP_REMOVE_GROUP: pnc_read_string_from_console("Group to remove", channel, PNC_CHANNEL_NAME_SIZE); pubnub_remove_channel_group(pn, channel); break; default: printf("Invalid input: %d\n", option); break; } printf("\n================================\n\n"); displayMenuOptions(pn); } puts("Exiting"); if (pubnub_free(pn) != 0) { puts("Failed to free the Pubnub context"); } return 0; }
int main() { char const *msg; enum pubnub_res res; char const *chan = "hello_world"; pubnub_t *pbp = pubnub_alloc(); if (NULL == pbp) { printf("Failed to allocate Pubnub context!\n"); return -1; } pubnub_init(pbp, "demo", "demo"); pubnub_set_transaction_timeout(pbp, PUBNUB_DEFAULT_NON_SUBSCRIBE_TIMEOUT); /* Leave this commented out to use the default - which is blocking I/O on most platforms. Uncomment to use non- blocking I/O. */ // pubnub_set_non_blocking_io(pbp); generate_uuid(pbp); pubnub_set_auth(pbp, "danaske"); puts("Publishing..."); res = pubnub_publish(pbp, chan, "\"Hello world from sync!\""); if (res != PNR_STARTED) { printf("pubnub_publish() returned unexpected: %d\n", res); pubnub_free(pbp); return -1; } res = pubnub_await(pbp); if (res == PNR_STARTED) { printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); return -1; } if (PNR_OK == res) { printf("Published! Response from Pubnub: %s\n", pubnub_last_publish_result(pbp)); } else if (PNR_PUBLISH_FAILED == res) { printf("Published failed on Pubnub, description: %s\n", pubnub_last_publish_result(pbp)); } else { printf("Publishing failed with code: %d\n", res); } puts("Subscribing..."); res = pubnub_subscribe(pbp, chan, NULL); if (res != PNR_STARTED) { printf("pubnub_subscribe() returned unexpected: %d\n", res); pubnub_free(pbp); return -1; } res = pubnub_await(pbp); if (res == PNR_STARTED) { printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); return -1; } if (PNR_OK == res) { puts("Subscribed!"); } else { printf("Subscribing failed with code: %d\n", res); } res = pubnub_subscribe(pbp, chan, NULL); if (res != PNR_STARTED) { printf("pubnub_subscribe() returned unexpected: %d\n", res); pubnub_free(pbp); return -1; } res = pubnub_await(pbp); if (res == PNR_STARTED) { printf("pubnub_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); } puts("Getting time..."); res = pubnub_time(pbp); if (res != PNR_STARTED) { printf("pubnub_time() returned unexpected: %d\n", res); pubnub_free(pbp); return -1; } res = pubnub_await(pbp); if (res == PNR_STARTED) { printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); return -1; } if (PNR_OK == res) { printf("Gotten time: %s; last time token=%s\n", pubnub_get(pbp), pubnub_last_time_token(pbp)); } else { printf("Getting time failed with code: %d\n", res); } puts("Getting history with include_token..."); res = pubnub_history(pbp, chan, 10, true); if (res != PNR_STARTED) { printf("pubnub_history() returned unexpected: %d\n", res); pubnub_free(pbp); return -1; } res = pubnub_await(pbp); if (res == PNR_STARTED) { printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); return -1; } if (PNR_OK == res) { puts("Got history! Elements:"); for (;;) { msg = pubnub_get(pbp); if (NULL == msg) { break; } puts(msg); } } else { printf("Getting history v2 with include_token failed with code: %d\n", res); } puts("Getting here_now presence..."); res = pubnub_here_now(pbp, chan, NULL); if (res != PNR_STARTED) { printf("pubnub_here_now() returned unexpected: %d\n", res); pubnub_free(pbp); return -1; } res = pubnub_await(pbp); if (res == PNR_STARTED) { printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); return -1; } if (PNR_OK == res) { puts("Got here now presence!"); for (;;) { msg = pubnub_get(pbp); if (NULL == msg) { break; } puts(msg); } } else { printf("Getting here-now presence failed with code: %d\n", res); } /** Global here_now presence for "demo" subscribe key is _very_ long, but we have it here to show that we can handle very long response if the PUBNUB_DYNAMIC_REPLY_BUFFER is "on". */ if (PUBNUB_DYNAMIC_REPLY_BUFFER) { puts("Getting global here_now presence..."); res = pubnub_global_here_now(pbp); if (res != PNR_STARTED) { printf("pubnub_global_here_now() returned unexpected: %d\n", res); pubnub_free(pbp); return -1; } res = pubnub_await(pbp); if (res == PNR_STARTED) { printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); return -1; } if (PNR_OK == res) { puts("Got global here now presence!"); for (;;) { msg = pubnub_get(pbp); if (NULL == msg) { break; } puts(msg); } } else { printf("Getting global here-now presence failed with code: %d\n", res); } } puts("Getting where_now presence..."); res = pubnub_where_now(pbp, pubnub_uuid_get(pbp)); if (res != PNR_STARTED) { printf("pubnub_where_now() returned unexpected: %d\n", res); pubnub_free(pbp); return -1; } res = pubnub_await(pbp); if (res == PNR_STARTED) { printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); return -1; } if (PNR_OK == res) { puts("Got where now presence!"); for (;;) { msg = pubnub_get(pbp); if (NULL == msg) { break; } puts(msg); } } else { printf("Getting where-now presence failed with code: %d\n", res); } puts("Setting state..."); res = pubnub_set_state(pbp, chan, NULL, pubnub_uuid_get(pbp), "{\"x\":5}"); if (res != PNR_STARTED) { printf("pubnub_set_state() returned unexpected: %d\n", res); pubnub_free(pbp); return -1; } res = pubnub_await(pbp); if (res == PNR_STARTED) { printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); return -1; } if (PNR_OK == res) { puts("Got set state response!"); for (;;) { msg = pubnub_get(pbp); if (NULL == msg) { break; } puts(msg); } } else { printf("Setting state failed with code: %d\n", res); } puts("Getting state..."); res = pubnub_state_get(pbp, chan, NULL, pubnub_uuid_get(pbp)); if (res != PNR_STARTED) { printf("pubnub_state_get() returned unexpected: %d\n", res); pubnub_free(pbp); return -1; } res = pubnub_await(pbp); if (res == PNR_STARTED) { printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); return -1; } if (PNR_OK == res) { puts("Got state!"); for (;;) { msg = pubnub_get(pbp); if (NULL == msg) { break; } puts(msg); } } else { printf("Getting state failed with code: %d\n", res); } puts("List channel group..."); res = pubnub_list_channel_group(pbp, "channel-group"); if (res != PNR_STARTED) { printf("pubnub_state_get() returned unexpected: %d\n", res); pubnub_free(pbp); return -1; } res = pubnub_await(pbp); if (res == PNR_STARTED) { printf("pubnub_await() returned unexpected: PNR_STARTED(%d)\n", res); pubnub_free(pbp); return -1; } if (PNR_OK == res) { puts("Got channel group list!"); for (;;) { msg = pubnub_get_channel(pbp); if (NULL == msg) { break; } puts(msg); } } else { printf("Getting channel group list failed with code: %d ('%s')\n", res, pubnub_res_2_string(res)); } /* We're done */ if (pubnub_free(pbp) != 0) { printf("Failed to free the Pubnub context\n"); } puts("Pubnub sync demo over."); return 0; }