Пример #1
0
void plcb_ctor_init_common(PLCB_t *object, libcouchbase_t instance,
                           AV *options)
{
    NV timeout_value;
    SV **tmpsv;
    
    object->instance = instance;
    object->errors = newAV();
    if(! (object->ret_stash = gv_stashpv(PLCB_RET_CLASSNAME, 0)) ) {
        die("Could not load '%s'", PLCB_RET_CLASSNAME);
    }
    
    /*gather instance-related options from the constructor*/
    if( (tmpsv = av_fetch(options, PLCB_CTORIDX_TIMEOUT, 0))  && 
            (SvIOK(*tmpsv) || SvNOK(*tmpsv))) {
        timeout_value = SvNV(*tmpsv);
        if(!timeout_value) {
            warn("Cannot use 0 for timeout");
        } else {
            libcouchbase_set_timeout(instance,
                timeout_value * (1000*1000));
        }
    }
    
    if((tmpsv = av_fetch(options, PLCB_CTORIDX_NO_CONNECT, 0)) &&
       SvTRUE(*tmpsv)) {
        object->my_flags |= PLCBf_NO_CONNECT;
    }
    /*maybe more stuff here?*/
}
Пример #2
0
LIBCOUCHBASE_API
libcouchbase_t libcouchbase_create(const char *host,
                                   const char *user,
                                   const char *passwd,
                                   const char *bucket,
                                   struct libcouchbase_io_opt_st *io)
{
    char buffer[1024];
    libcouchbase_ssize_t offset;
    libcouchbase_t ret;

    if (io == NULL) {
        io = libcouchbase_create_io_ops(LIBCOUCHBASE_IO_OPS_DEFAULT,
                                        NULL, NULL);
        if (io == NULL) {
            /* You can't initialize the library without a io-handler! */
            return NULL;
        }
    }

    if (host == NULL) {
        host = "localhost";
    }

    if (bucket == NULL || strlen(bucket) == 0) {
        bucket = "default";
    }

    if (sasl_client_init(NULL) != SASL_OK) {
        return NULL;
    }

    if ((ret = calloc(1, sizeof(*ret))) == NULL) {
        return NULL;
    }
    libcouchbase_initialize_packet_handlers(ret);
    libcouchbase_behavior_set_syncmode(ret, LIBCOUCHBASE_ASYNCHRONOUS);

    if (setup_boostrap_hosts(ret, host) == -1) {
        free(ret);
        return NULL;
    }

    offset = snprintf(buffer, sizeof(buffer),
                      "GET /pools/default/bucketsStreaming/%s HTTP/1.1\r\n",
                      bucket);

    if (user && passwd) {
        char cred[256];
        char base64[256];
        snprintf(cred, sizeof(cred), "%s:%s", user, passwd);
        if (libcouchbase_base64_encode(cred, base64, sizeof(base64)) == -1) {
            libcouchbase_destroy(ret);
            return NULL;
        }

        offset += snprintf(buffer + offset, sizeof(buffer) - (libcouchbase_size_t)offset,
                           "Authorization: Basic %s\r\n", base64);
    }
    offset += snprintf(buffer + offset, sizeof(buffer) - (libcouchbase_size_t)offset, "\r\n");
    ret->http_uri = strdup(buffer);

    if (ret->http_uri == NULL) {
        libcouchbase_destroy(ret);
        return NULL;
    }

    ret->sock = INVALID_SOCKET;

    /* No error has occurred yet. */
    ret->last_error = LIBCOUCHBASE_SUCCESS;

    /* setup io iops! */
    ret->io = io;
    ret->timeout.event = ret->io->create_timer(ret->io);
    assert(ret->timeout.event);

    libcouchbase_set_timeout(ret, LIBCOUCHBASE_DEFAULT_TIMEOUT);

    return ret;
}