PUBNUB_API void PubNub::time(long timeout, PubNub_time_cb cb, void *cb_data) { if (cb) { time_pair *cb_info = new time_pair(std::pair<PubNub_time_cb, PubNub *>(cb, this), cb_data); pubnub_time(p, timeout, pubnub_cpp_time_cb, cb_info); } else { pubnub_time(p, timeout, NULL, NULL); } }
int srand_from_pubnub_time(pubnub_t *pbp) { pubnub_res rslt = pubnub_time(pbp); if (rslt != PNR_STARTED) { return -1; } rslt = pubnub_await(pbp); if (rslt != PNR_OK) { return -1; } char const* pbtime = pubnub_get(pbp); if (0 == pbtime) { return -1; } size_t length_of_time = strlen(pbtime); if (0 == length_of_time) { return -1; } char const *s = pbtime + length_of_time - 1; unsigned int val_for_srand = 0; for (int i = 0; (i < 10) && (s > pbtime); ++i, --s) { val_for_srand = val_for_srand * 10 + *s - '0'; } srand(val_for_srand); 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; }
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; }