Logo Search packages:      
Sourcecode: zookeeper version File versions  Download package

ZOOAPI zhandle_t* zookeeper_init ( const char *  host,
watcher_fn  watcher,
int  recv_timeout,
const clientid_t clientid,
void *  context,
int  flags 

create a handle to used communicate with zookeeper.

This method creates a new handle and a zookeeper session that corresponds to that handle. Session establishment is asynchronous, meaning that the session should not be considered established until (and unless) an event of state ZOO_CONNECTED_STATE is received.

host comma separated host:port pairs, each corresponding to a zk server. e.g. ",,"
fn the global watcher callback function. When notifications are triggered this function will be invoked.
clientid the id of a previously established session that this client will be reconnecting to. Pass 0 if not reconnecting to a previous session. Clients can access the session id of an established, valid, connection by calling zoo_client_id. If the session corresponding to the specified clientid has expired, or if the clientid is invalid for any reason, the returned zhandle_t will be invalid -- the zhandle_t state will indicate the reason for failure (typically ZOO_EXPIRED_SESSION_STATE).
context the handback object that will be associated with this instance of zhandle_t. Application can access it (for example, in the watcher callback) using zoo_get_context. The object is not used by zookeeper internally and can be null.
flags reserved for future use. Should be set to zero.
a pointer to the opaque zhandle structure. If it fails to create a new zhandle the function returns NULL and the errno variable indicates the reason.
Create a zookeeper handle associated with the given host and port.

Definition at line 599 of file zookeeper.c.

References _zhandle::active_child_watchers, _zhandle::active_exist_watchers, _zhandle::active_node_watchers, _zhandle::auth_h, _buffer_list::buffer, _zhandle::chroot, _zhandle::client_id, clientid_t::client_id, _zhandle::connect_index, _zhandle::context, _buffer_list::curr_offset, _zhandle::fd, _zhandle::hostname, _zhandle::last_zxid, _buffer_list::len, _buffer_list::next, _zhandle::next_deadline, clientid_t::passwd, _zhandle::primer_buffer, _zhandle::primer_storage_buffer, _zhandle::recv_timeout, _zhandle::socket_readable, _zhandle::state, _zhandle::watcher, and zookeeper_init().

Referenced by zk::ZooKeeperAdapter::reconnect(), and zookeeper_init().


    LOG_INFO(("Initiating client connection, host=%s sessionTimeout=%d watcher=%p"
          " sessionId=0x%llx sessionPasswd=%s context=%p flags=%d",
              (clientid == 0 ? 0 : clientid->client_id),
              ((clientid == 0) || (clientid->passwd == 0) ?
               "<null>" : "<hidden>"),

    int errnosave;
    zhandle_t *zh = calloc(1, sizeof(*zh));
    char *index_chroot;
    if (!zh) {
        return 0;
    zh->fd = -1;
    zh->state = 0;
    zh->context = context;
    zh->recv_timeout = recv_timeout;
    if (watcher) {
       zh->watcher = watcher;
    } else {
       zh->watcher = null_watcher_fn;
    if (host == 0 || *host == 0) { // what we shouldn't dup
        goto abort;
    //parse the host to get the chroot if 
    index_chroot = strchr(host, '/');
    if (index_chroot) {
        zh->chroot = strdup(index_chroot);
        // if chroot is just / set it to null
        if (strlen(zh->chroot) == 1) {
            zh->chroot = NULL;
        // cannot use strndup so allocate and strcpy
        zh->hostname = (char *) malloc(index_chroot - host + 1);
        zh->hostname = strncpy(zh->hostname, host, (index_chroot - host));
        //strncpy does not null terminate
        *(zh->hostname + (index_chroot - host)) = '\0';
    } else {
        zh->chroot = NULL;
        zh->hostname = strdup(host);
    if (zh->chroot && !isValidPath(zh->chroot, 0)) { 
        errno = EINVAL;
        goto abort;
    if (zh->hostname == 0) {
        goto abort;
    if(getaddrs(zh)!=0) {
        goto abort;
    zh->connect_index = 0;
    if (clientid) {
        memcpy(&zh->client_id, clientid, sizeof(zh->client_id));
    } else {
        memset(&zh->client_id, 0, sizeof(zh->client_id));
    zh->primer_buffer.buffer = zh->primer_storage_buffer;
    zh->primer_buffer.curr_offset = 0;
    zh->primer_buffer.len = sizeof(zh->primer_storage_buffer);
    zh->primer_buffer.next = 0;
    zh->last_zxid = 0;
    if (adaptor_init(zh) == -1) {
        goto abort;
    return zh;
    return 0;

Generated by  Doxygen 1.6.0   Back to index