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

mm_t mm_new ( sm_t  sm  ) 

allocate a module manager instance, and loads the modules

Definition at line 105 of file mm.c.

References chain_IN_ROUTER, chain_IN_SESS, chain_OUT_ROUTER, chain_OUT_SESS, chain_PKT_ROUTER, chain_PKT_SM, chain_PKT_USER, chain_SESS_END, chain_SESS_START, chain_USER_CREATE, chain_USER_DELETE, chain_USER_LOAD, sm_st::config, mm_st::in_router, mm_st::in_sess, module_st::index, module_st::init, sm_st::log, module_st::mm, mm_st::modules, config_st::nad, module_st::name, mm_st::nin_router, mm_st::nin_sess, mm_st::nindex, mm_st::nout_router, mm_st::nout_sess, mm_st::npkt_router, mm_st::npkt_sm, mm_st::npkt_user, mm_st::nsess_end, mm_st::nsess_start, mm_st::nuser_create, mm_st::nuser_delete, mm_st::nuser_load, mm_st::out_router, mm_st::out_sess, mm_st::pkt_router, mm_st::pkt_sm, mm_st::pkt_user, mm_st::sess_end, mm_st::sess_start, mm_st::sm, mm_st::user_create, mm_st::user_delete, and mm_st::user_load.

                     {
    mm_t mm;
    int celem, melem, attr, i, *nlist = NULL;
    char id[13], name[32], arg[1024];
    mod_chain_t chain = (mod_chain_t) NULL;
    mod_instance_t **list = NULL, mi;
    module_t mod;

    mm = (mm_t) malloc(sizeof(struct mm_st));
    memset(mm, 0, sizeof(struct mm_st));

    mm->sm = sm;
    mm->modules = xhash_new(101);

    if((celem = nad_find_elem(sm->config->nad, 0, -1, "modules", 1)) < 0)
        return mm;

    celem = nad_find_elem(sm->config->nad, celem, -1, "chain", 1);
    while(celem >= 0) {
        if((attr = nad_find_attr(sm->config->nad, celem, -1, "id", NULL)) < 0) {
            celem = nad_find_elem(sm->config->nad, celem, -1, "chain", 0);
            continue;
        }

        snprintf(id, 13, "%.*s", NAD_AVAL_L(sm->config->nad, attr), NAD_AVAL(sm->config->nad, attr));
        id[12] = '\0';

        log_debug(ZONE, "processing config for chain '%s'", id);

        list = NULL;
        if(strcmp(id, "sess-start") == 0) {
            chain = chain_SESS_START;
            list = &mm->sess_start;
            nlist = &mm->nsess_start;
        }
        else if(strcmp(id, "sess-end") == 0) {
            chain = chain_SESS_END;
            list = &mm->sess_end;
            nlist = &mm->nsess_end;
        }
        else if(strcmp(id, "in-sess") == 0) {
            chain = chain_IN_SESS;
            list = &mm->in_sess;
            nlist = &mm->nin_sess;
        }
        else if(strcmp(id, "in-router") == 0) {
            chain = chain_IN_ROUTER;
            list = &mm->in_router;
            nlist = &mm->nin_router;
        }
        else if(strcmp(id, "out-sess") == 0) {
            chain = chain_OUT_SESS;
            list = &mm->out_sess;
            nlist = &mm->nout_sess;
        }
        else if(strcmp(id, "out-router") == 0) {
            chain = chain_OUT_ROUTER;
            list = &mm->out_router;
            nlist = &mm->nout_router;
        }
        else if(strcmp(id, "pkt-sm") == 0) {
            chain = chain_PKT_SM;
            list = &mm->pkt_sm;
            nlist = &mm->npkt_sm;
        }
        else if(strcmp(id, "pkt-user") == 0) {
            chain = chain_PKT_USER;
            list = &mm->pkt_user;
            nlist = &mm->npkt_user;
        }
        else if(strcmp(id, "pkt-router") == 0) {
            chain = chain_PKT_ROUTER;
            list = &mm->pkt_router;
            nlist = &mm->npkt_router;
        }
        else if(strcmp(id, "user-load") == 0) {
            chain = chain_USER_LOAD;
            list = &mm->user_load;
            nlist = &mm->nuser_load;
        }
        else if(strcmp(id, "user-create") == 0) {
            chain = chain_USER_CREATE;
            list = &mm->user_create;
            nlist = &mm->nuser_create;
        }
        else if(strcmp(id, "user-delete") == 0) {
            chain = chain_USER_DELETE;
            list = &mm->user_delete;
            nlist = &mm->nuser_delete;
        }

        if(list == NULL) {
            log_write(sm->log, LOG_ERR, "unknwon chain type '%s'", id);

            celem = nad_find_elem(sm->config->nad, celem, -1, "chain", 0);
            continue;
        }

        melem = nad_find_elem(sm->config->nad, celem, -1, "module", 1);
        while(melem >= 0) {
            if(NAD_CDATA_L(sm->config->nad, melem) <= 0) {
                melem = nad_find_elem(sm->config->nad, melem, -1, "module", 0);
                continue;
            }

            snprintf(name, 32, "%.*s", NAD_CDATA_L(sm->config->nad, melem), NAD_CDATA(sm->config->nad, melem));

            for(i = 0; module_names[i] != NULL; i++) {
                if(strcmp(name, module_names[i]) == 0) {
                    log_debug(ZONE, "adding module '%s' to chain '%s'", name, id);

                    arg[0] = '\0';
                    attr = nad_find_attr(sm->config->nad, melem, -1, "arg", NULL);
                    if(attr >= 0) {
                        snprintf(arg, 1024, "%.*s", NAD_AVAL_L(sm->config->nad, attr), NAD_AVAL(sm->config->nad, attr));
                        log_debug(ZONE, "module arg: %s", arg);
                    }

                    mod = xhash_get(mm->modules, name);
                    if(mod == NULL) {
                        mod = (module_t) malloc(sizeof(struct module_st));
                        memset(mod, 0, sizeof(struct module_st));

                        mod->mm = mm;
                        mod->index = mm->nindex;
                        mod->name = strdup(name);

                        xhash_put(mm->modules, mod->name, (void *) mod);

                        mm->nindex++;
                    }

                    mi = (mod_instance_t) malloc(sizeof(struct mod_instance_st));
                    memset(mi, 0, sizeof(struct mod_instance_st));

                    mi->sm = sm;
                    mi->mod = mod;
                    mi->chain = chain;
                    mi->arg = (arg[0] == '\0') ? NULL : strdup(arg);
                    mi->seq = mod->init;

                    if((module_inits[i])(mi) != 0) {
                        log_debug(ZONE, "init for module '%s' (seq %d) failed", name, mi->seq);

                        free(mi);

                        if(mod->init == 0) {
                            xhash_zap(mm->modules, mod->name);
                            free(mod->name);
                            free(mod);

                            mm->nindex--;

                            melem = nad_find_elem(sm->config->nad, melem, -1, "module", 0);
                            continue;
                        }
                    }

                    mod->init++;

                    *list = (mod_instance_t *) realloc(*list, sizeof(mod_instance_t) * (*nlist + 1));
                    (*list)[*nlist] = mi;

                    log_debug(ZONE, "module '%s' added to chain '%s' (order %d index %d seq %d)", mod->name, id, *nlist, mod->index, mi->seq);

                    (*nlist)++;
                }
            }

            melem = nad_find_elem(sm->config->nad, melem, -1, "module", 0);
        }

        celem = nad_find_elem(sm->config->nad, celem, -1, "chain", 0);
    }

    return mm;
}


Generated by  Doxygen 1.6.0   Back to index