示例#1
0
文件: cetcd.c 项目: Huang-lin/cetcd
int cetcd_add_watcher(cetcd_array *watchers, cetcd_watcher *watcher) {
    cetcd_watcher *w;
    cetcd_string url;

    url = cetcd_watcher_build_url(watcher->cli, watcher);
    curl_easy_setopt(watcher->curl,CURLOPT_URL, url);
    sdsfree(url);

    watcher->attempts = cetcd_array_size(watcher->cli->addresses);

    /* See above about CURLOPT_NOSIGNAL
     * */
    curl_easy_setopt(watcher->curl, CURLOPT_NOSIGNAL, 1L);

    curl_easy_setopt(watcher->curl, CURLOPT_CONNECTTIMEOUT, watcher->cli->settings.connect_timeout);
#if LIBCURL_VERSION_NUM >= 0x071900
    curl_easy_setopt(watcher->curl, CURLOPT_TCP_KEEPALIVE, 1L);
    curl_easy_setopt(watcher->curl, CURLOPT_TCP_KEEPINTVL, 1L); /*the same as go-etcd*/
#endif
    curl_easy_setopt(watcher->curl, CURLOPT_USERAGENT, "cetcd");
    curl_easy_setopt(watcher->curl, CURLOPT_POSTREDIR, 3L);     /*post after redirecting*/
    curl_easy_setopt(watcher->curl, CURLOPT_VERBOSE, watcher->cli->settings.verbose); 

    curl_easy_setopt(watcher->curl, CURLOPT_CUSTOMREQUEST, "GET");
    curl_easy_setopt(watcher->curl, CURLOPT_WRITEFUNCTION, cetcd_parse_response);
    curl_easy_setopt(watcher->curl, CURLOPT_WRITEDATA, watcher->parser);
    curl_easy_setopt(watcher->curl, CURLOPT_HEADER, 1L);
    curl_easy_setopt(watcher->curl, CURLOPT_FOLLOWLOCATION, 1L);
    /* We use an array to store watchers. It will cause holes when remove some watchers.
     * watcher->array_index is used to reset to the original hole if the watcher was deleted before.
     * */
    if (watcher->array_index == -1) {
        cetcd_array_append(watchers, watcher);
        watcher->array_index = cetcd_array_size(watchers) - 1;
    } else {
        w = cetcd_array_get(watchers, watcher->array_index);
        if (w) {
            cetcd_watcher_release(w);
        }
        cetcd_array_set(watchers, watcher->array_index, watcher);
    }
    return 1;
}
示例#2
0
int main(int argc, char *argv[]) {
    cetcd_client cli;
    cetcd_response *resp;
    cetcd_array addrs;

    cetcd_array_init(&addrs, 3);
    cetcd_array_append(&addrs, "http://127.0.0.1:2379");

    cetcd_client_init(&cli, &addrs);

    resp = cetcd_get(&cli, "/radar/service");
    if(resp->err) {
        printf("error :%d, %s (%s)\n", resp->err->ecode, resp->err->message, resp->err->cause);
    }
    cetcd_response_print(resp);
    cetcd_response_release(resp);

    cetcd_array_destroy(&addrs);
    cetcd_client_destroy(&cli);
    return 0;
}
示例#3
0
文件: cetcd.c 项目: Huang-lin/cetcd
void cetcd_client_init(cetcd_client *cli, cetcd_array *addresses) {
    size_t i;
    cetcd_array *addrs;
    curl_global_init(CURL_GLOBAL_ALL);
    srand(time(0));

    cli->keys_space =   "v2/keys";
    cli->stat_space =   "v2/stat";
    cli->member_space = "v2/members";
    cli->curl = curl_easy_init();
    
    addrs = cetcd_array_create(cetcd_array_size(addresses));
    for (i=0; i<cetcd_array_size(addresses); ++i) {
        cetcd_array_append(addrs, sdsnew(cetcd_array_get(addresses, i)));
    }
    
    cli->addresses = cetcd_array_shuffle(addrs);
    cli->picked = rand() % (cetcd_array_size(cli->addresses));

    cli->settings.verbose = 0;
    cli->settings.connect_timeout = 1;
    cli->settings.read_timeout = 1;  /*not used now*/
    cli->settings.write_timeout = 1; /*not used now*/

    cetcd_array_init(&cli->watchers, 10);

    /* Set CURLOPT_NOSIGNAL to 1 to work around the libcurl bug:
     *  http://stackoverflow.com/questions/9191668/error-longjmp-causes-uninitialized-stack-frame
     *  http://curl.haxx.se/mail/lib-2008-09/0197.html
     * */
    curl_easy_setopt(cli->curl, CURLOPT_NOSIGNAL, 1L);

#if LIBCURL_VERSION_NUM >= 0x071900
    curl_easy_setopt(cli->curl, CURLOPT_TCP_KEEPALIVE, 1L);
    curl_easy_setopt(cli->curl, CURLOPT_TCP_KEEPINTVL, 1L); /*the same as go-etcd*/
#endif
    curl_easy_setopt(cli->curl, CURLOPT_USERAGENT, "cetcd");
    curl_easy_setopt(cli->curl, CURLOPT_POSTREDIR, 3L);     /*post after redirecting*/
}