Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 135 lines (125 sloc) 4.63 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2010, 2011 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "internal.h"

static void breakout_vbucket_state_listener(libcouchbase_server_t *server)
{
    if (server->instance->vbucket_state_listener_last) {
        server->instance->vbucket_state_listener =
            server->instance->vbucket_state_listener_last;
        server->instance->vbucket_state_listener_last = NULL;
    }
    server->instance->io->delete_timer(server->instance->io,
                                       server->instance->timeout.event);
    libcouchbase_maybe_breakout(server->instance);
}

static void initial_connect_timeout_handler(libcouchbase_socket_t sock,
                                            short which,
                                            void *arg)
{
    libcouchbase_t instance = arg;
    libcouchbase_error_handler(instance, LIBCOUCHBASE_CONNECT_ERROR,
                               "Could not connect to server within allotted time");

    if (instance->sock != INVALID_SOCKET) {
        /* Do we need to delete the event? */
        instance->io->delete_event(instance->io,
                                   instance->sock,
                                   instance->event);
        instance->io->close(instance->io, instance->sock);
        instance->sock = INVALID_SOCKET;
    }

    instance->io->delete_timer(instance->io, instance->timeout.event);
    instance->timeout.next = 0;
    libcouchbase_maybe_breakout(instance);

    (void)sock;
    (void)which;
    /* Notice we do not re-set the vbucket_state_listener. This is by design,
* as we are still in the same state, and are just reporting an error
* back to the user
*/
}

/**
* Returns non zero if the event loop is running now
*
* @param instance the instance to run the event loop for.
*/
LIBCOUCHBASE_API
int libcouchbase_is_waiting(libcouchbase_t instance)
{
    return instance->wait != 0;
}

/**
* Run the event loop until we've got a response for all of our spooled
* commands. You should not call this function from within your callbacks.
*
* @param instance the instance to run the event loop for.
*
* @author Trond Norbye
*/
LIBCOUCHBASE_API
void libcouchbase_wait(libcouchbase_t instance)
{
    if (instance->wait != 0) {
        return;
    }
    /*
* The API is designed for you to run your own event loop,
* but should also work if you don't do that.. In order to be
* able to know when to break out of the event loop, we're setting
* the wait flag to 1
*/
    instance->wait = 1;
    if (instance->vbucket_config == NULL) {
        /* Initial configuration. Set a timer */
        instance->vbucket_state_listener_last =
            instance->vbucket_state_listener;
        instance->vbucket_state_listener = breakout_vbucket_state_listener;

        /* Initial connection timeout */
        instance->io->update_timer(instance->io,
                                   instance->timeout.event,
                                   instance->timeout.usec,
                                   instance,
                                   initial_connect_timeout_handler);
    }
    if (instance->vbucket_config == NULL || libcouchbase_has_data_in_buffers(instance)
            || hashset_num_items(instance->timers) > 0) {
        libcouchbase_size_t idx;
        /* update timers on all servers */
        for (idx = 0; idx < instance->nservers; ++idx) {
            libcouchbase_update_server_timer(instance->servers + idx);
        }
        instance->io->run_event_loop(instance->io);
    } else {
        instance->wait = 0;
    }

    /*
* something else will call libcouchbase_maybe_breakout with a corresponding
* stop_event_loop()
*/
}

/**
* Stop event loop
*
* @param instance the instance to run the event loop for.
*/
LIBCOUCHBASE_API
void libcouchbase_breakout(libcouchbase_t instance)
{
    if (instance->wait) {
        instance->io->stop_event_loop(instance->io);
        instance->wait = 0;
    }
}
Something went wrong with that request. Please try again.