コード例 #1
0
ファイル: pnc_ops_callback.c プロジェクト: pubnub/c-core
void pnc_ops_parse_callback(enum pubnub_trans method_name, enum pubnub_res res, pubnub_t *pn)
{
    if (res == PNR_HTTP_ERROR) {
        printf("%d HTTP error / code: %d\n",
               method_name, pubnub_last_http_code(pn));
        return;
    }
    
    if (res == PNR_STARTED) {
        printf("%d returned unexpected: PNR_STARTED(%d)\n", method_name, res);
        return;
    }
    
    if (PNR_OK == res) {
        const char *msg;
        
        puts("***************************");
        printf("Result for %d: success!\n", method_name);
        puts("***************************");
        for (;;) {
            msg = pubnub_get(pn);
            if (NULL == msg) {
                break;
            }
            puts(msg);
        }
        puts("***************************");
    } 
    else {
        printf("%d failed, error code %d: %s !\n", method_name, res, pubnub_res_2_string(res));
    }
}
コード例 #2
0
ファイル: pnc_ops_callback.c プロジェクト: pubnub/c-core
void pnc_ops_parse_response(const char *method_name, enum pubnub_res res, pubnub_t *pn)
{
    struct UserData *pUserData = pubnub_get_user_data(pn);

    if (res != PNR_STARTED) {
        printf("%s returned unexpected %d: %s\n", method_name, res, pubnub_res_2_string(res));
        return;
    }
    
    await(pUserData);
}
コード例 #3
0
ファイル: pnc_ops_sync.c プロジェクト: pubnub/c-core
void pnc_ops_parse_response(const char *method_name, enum pubnub_res res, pubnub_t *pn)
{
    const char *msg;
    
    if (res != PNR_STARTED) {
        printf("%s returned error %d: %s\n", method_name, res, pubnub_res_2_string(res));
        return;
    }
    
    res = pubnub_await(pn);
    
    if (res == PNR_HTTP_ERROR) {
        printf("%s HTTP error / code: %d\n",
               method_name, pubnub_last_http_code(pn));
        return;
    }
    
    if (res == PNR_STARTED) {
        printf("%s returned unexpected: PNR_STARTED(%d)\n", method_name, res);
        return;
    }
    
    if (PNR_OK == res) {
        puts("\n****************************************");
        printf("Result for %s: success!\n", method_name);
        puts("****************************************");
        for (;;) {
            msg = pubnub_get(pn);
            if (NULL == msg) {
                break;
            }
            puts(msg);
        }
        puts("****************************************");
    }
    else {
        printf("%s failed! error code %d: %s\n", method_name, res, pubnub_res_2_string(res));
    }
}
コード例 #4
0
ファイル: pnc_ops_callback.c プロジェクト: pubnub/c-core
static
#if defined _WIN32
    void
#else
    void* 
#endif
pnc_ops_subscribe_thr(void *pn_sub_addr)
{
    pubnub_t *pn_sub = (pubnub_t *)pn_sub_addr;
    enum pubnub_res res;
    
    char channels_string[PNC_SUBSCRIBE_CHANNELS_LIMIT * PNC_CHANNEL_NAME_SIZE];
    char channel_groups_string[PNC_SUBSCRIBE_CHANNELS_LIMIT * PNC_CHANNEL_NAME_SIZE];

    puts("\nSubscribe thread created");
    pubnub_mutex_lock(m_loop_enabled_mutex);
    m_loop_enabled = true;
    pubnub_mutex_unlock(m_loop_enabled_mutex);

    for (;;) {
        bool loop_enabled;

        pubnub_mutex_lock(m_loop_enabled_mutex);
        loop_enabled = m_loop_enabled;
        pubnub_mutex_unlock(m_loop_enabled_mutex);
        
        if (!loop_enabled) {
            break;
        }

        puts("Subscribe loop...");
        
        pnc_subscribe_list_channels(channels_string, sizeof channels_string);
        pnc_subscribe_list_channel_groups(channel_groups_string, sizeof channel_groups_string);
        
        if (strlen(channels_string) == 0 && strlen(channel_groups_string) == 0) {
            puts("You need add some channels or channel groups first. Ignoring");
            break;
        } 
        else if (strlen(channel_groups_string) == 0) {
            res = pubnub_subscribe(pn_sub, channels_string, NULL);
        } 
        else if (strlen(channels_string) == 0) {
            res = pubnub_subscribe(pn_sub, NULL, channel_groups_string);
        } 
        else {
            res = pubnub_subscribe(pn_sub, channels_string, channel_groups_string);
        }

        if (res != PNR_STARTED) {
            printf("pubnub_subscribe() returned unexpected %d: %s\n", res, pubnub_res_2_string(res));
            break;
        }
        
        await(&m_user_data_sub);
    }
    
#if !defined _WIN32
    return NULL;
#endif
}
コード例 #5
0
ファイル: pubnub_sync_sample.c プロジェクト: ramshenai/c-core
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;
}