Esempio n. 1
0
static int timer_lua_create(lua_State *L)
{

    mrp_context_t *ctx    = mrp_lua_get_murphy_context();
    char           e[128] = "";
    timer_lua_t   *t;
    int            narg;

    if (ctx == NULL)
        luaL_error(L, "failed to get murphy context");

    narg = lua_gettop(L);

    t = (timer_lua_t *)mrp_lua_create_object(L, TIMER_LUA_CLASS, NULL, 0);

    t->L        = L;
    t->ctx      = ctx;
    t->callback = LUA_NOREF;
    t->data     = LUA_NOREF;

    switch (narg) {
    case 1:
        break;
    case 2:
        if (mrp_lua_init_members(t, L, -2, e, sizeof(e)) != 1)
            return luaL_error(L, "failed to initialize timer members (%s)", e);
        break;
    default:
        return luaL_error(L, "expecting 0 or 1 constructor arguments, "
                          "got %d", narg);
    }

    if (t->callback != LUA_NOREF && t->t == NULL) {
        t->t = mrp_add_timer(t->ctx->ml, t->msecs, timer_lua_cb, t);

        if (t->t == NULL) {
            mrp_lua_destroy_object(L, NULL, 0, t);
            return luaL_error(L, "failed to create Murphy timer");
        }
    }

    mrp_lua_push_object(L, t);

    return 1;
}
Esempio n. 2
0
static int resource_set_lua_create(lua_State *L)
{
    char e[128] = "";
    resource_set_lua_t *rset;
    int narg;
    mrp_htbl_config_t conf;

    mrp_debug("create");

    narg = lua_gettop(L);

    rset = (resource_set_lua_t *) mrp_lua_create_object(L,
            RESOURCE_SET_LUA_CLASS, NULL, 0);

    if (!rset)
        return luaL_error(L, "could not create Lua object");

    rset->L = L;

    /* user can affect these values */
    rset->zone = mrp_strdup("default");
    rset->application_class = NULL;
    rset->autorelease = FALSE;
    rset->dont_wait = FALSE;
    rset->priority = 0;
    rset->committed = FALSE;
    rset->initialized = FALSE;

    switch (narg) {
    case 2:
        /* argument table */
        if (mrp_lua_init_members(rset, L, -2, e, sizeof(e)) != 1)
            return luaL_error(L, "failed to initialize resource members (%s)",
                    e);
        break;
    default:
        return luaL_error(L, "expecting a constructor argument, "
                          "got %d", narg);
    }

    if (rset->application_class == NULL)
        return luaL_error(L, "application_class is a mandatory parameter");

    if (rset->priority < 0)
        rset->priority = 0;

    /* initial state, these cannot be set by user */
    rset->available = FALSE;
    rset->acquired = FALSE;

    /* initialize resource map */
    conf.nbucket = 0;
    conf.nentry = 10;
    conf.comp = mrp_string_comp;
    conf.hash = mrp_string_hash;
    conf.free = htbl_free_resource;

    rset->resources = mrp_htbl_create(&conf);
    if (!rset->resources)
        goto error;

    /* do the actual resource work */

    if (!client) {
        /* create the resource client */

        client = mrp_resource_client_create("lua", NULL);

        if (!client)
            goto error;
    }

    rset->resource_set = mrp_resource_set_create(client, rset->autorelease,
            rset->dont_wait, rset->priority, event_cb, rset);

    if (rset->resource_set)
        n_sets++;
    else
        goto error;

    rset->initialized = TRUE;

    mrp_lua_push_object(L, rset);

    return 1;

error:
    return luaL_error(L, "internal resource library error");
}