Exemplo n.º 1
0
void test_watcher()
{
    log_info("------------------- [ test watcher functions ] -------------------");

    std::string watcher_root = "/test_watcher2";
    do {
        int error = zk_wrapper.ExistsW(watcher_root, watcher_root_exist_cb, NULL);
        if (error) {
            log_err("check '%s' exist: %s", 
                    watcher_root.c_str(), zk_wrapper.GetErrstr().c_str());
        } else {

        }

        help_args args;

        args.cmd = CREATE_NODE;
        args.zw = &zk_wrapper;
        args.path = watcher_root;
        args.value = "root init value";
        request_theother(&args);
        check_watcher("root");

        std::string value = zk_wrapper.GetValueW(watcher_root, watcher_root_cb, NULL);
        if (value.empty()) {
            log_err("'%s' get failed: [%s]", watcher_root.c_str(), zk_wrapper.GetErrstr().c_str());
            break;
        } else {
            log_info("'%s' get succ", watcher_root.c_str());
        }                  

        args.cmd = SET_VALUE;
        args.zw = &zk_wrapper;
        args.path = watcher_root;
        args.value = "root new value";
        request_theother(&args);
        check_watcher("root");

        // test child watcher
        std::vector<std::string> children;
        error = zk_wrapper.GetChildrenW(watcher_root, watcher_child_cb, NULL, children);
        if (error) {
            log_err("get children of '%s' failed: [%s]", watcher_root.c_str(), zk_wrapper.GetErrstr().c_str());
            break;
        }

        args.cmd = CREATE_NODE;
        args.zw = &zk_wrapper;
        args.path = "/test_watcher2/watcher_child1";
        args.value = "watcher child 1";
        request_theother(&args);
        check_watcher("child of root");
    } while(0);
 
//    zk_wrapper.RemoveNode(watcher_root);
}
Exemplo n.º 2
0
/**
 * Get/set the watcher priority.  If passed a new_priority, then the
 * old_priority will be returned.  Otherwise, just returns the current
 * priority.
 *
 * Usage:
 *   old_priority = watcher:priority([new_priority])
 *
 * [+1, -0, e]
 */
static int watcher_priority(lua_State *L) {
    int has_pri = lua_gettop(L) > 1;
    ev_watcher *w = check_watcher(L, 1);
    int old_pri = ev_priority(w);

    if ( has_pri ) ev_set_priority(w, luaL_checkint(L, 2));
    lua_pushinteger(L, old_pri);
    return 1;
}
Exemplo n.º 3
0
/**
 * Get/set the watcher priority.  If passed a new_priority, then the
 * old_priority will be returned.  Otherwise, just returns the current
 * priority.
 *
 * Usage:
 *   old_priority = watcher:priority([new_priority])
 *
 * [+1, -0, e]
 */
static int watcher_priority(lua_State *L) {
    int has_pri = lua_gettop(L) > 1;
    ev_watcher *w = check_watcher(L, 1);
    int old_pri = ev_priority(w);

#if LUA_VERSION_NUM > 502
    if ( has_pri ) ev_set_priority(w, (int)luaL_checkinteger(L, 2));
#else
    if ( has_pri ) ev_set_priority(w, luaL_checkint(L, 2));
#endif
    lua_pushinteger(L, old_pri);
    return 1;
}
Exemplo n.º 4
0
/**
 * Get/set the watcher callback.  If passed a new_callback, then the
 * old_callback will be returned.  Otherwise, just returns the current
 * callback function.
 *
 * Usage:
 *   old_callback = watcher:callback([new_callback])
 *
 * [+1, -0, e]
 */
static int watcher_callback(lua_State *L) {
    int has_fn = lua_gettop(L) > 1;

    check_watcher(L, 1);
    if ( has_fn ) luaL_checktype(L, 2, LUA_TFUNCTION);

    lua_getfenv(L, 1);
    assert(lua_istable(L, -1) /* getfenv of watcher worked */);
    lua_rawgeti(L, -1, WATCHER_FN);
    if ( has_fn ) {
        lua_pushvalue(L, 2);
        lua_rawseti(L, -3, WATCHER_FN);
    }
    lua_remove(L, -2);
    return 1;
}
Exemplo n.º 5
0
/**
 * If the watcher is pending, return the revents and clear the pending
 * status (so the watcher callback won't be called).
 *
 * Usage:
 *   revents = watcher:clear_pending(loop)
 *
 * [+1, -0, e]
 */
static int watcher_clear_pending(lua_State *L) {
    lua_pushnumber(L, ev_clear_pending(*check_loop_and_init(L, 2), check_watcher(L, 1)));
    return 1;
}
Exemplo n.º 6
0
/**
 * Test if the watcher is pending.
 *
 * Usage:
 *   bool = watcher:is_pending()
 *
 * [+1, -0, e]
 */
static int watcher_is_pending(lua_State *L) {
    lua_pushboolean(L, ev_is_pending(check_watcher(L, 1)));
    return 1;
}
Exemplo n.º 7
0
/**
 * Test if the watcher is active.
 *
 * Usage:
 *   bool = watcher:is_active()
 *
 * [+1, -0, e]
 */
static int watcher_is_active(lua_State *L) {
    lua_pushboolean(L, ev_is_active(check_watcher(L, 1)));
    return 1;
}