bb_smscconn.c

来自「mms client」· C语言 代码 · 共 776 行 · 第 1/2 页

C
776
字号
    list_lock(smsc_list);    for (i = 0; i < list_len(smsc_list); i++) {        conn = list_get(smsc_list, i);        if (conn != NULL && octstr_compare(conn->id, id) == 0) {            break;        }    }    list_unlock(smsc_list);    if (i >= list_len(smsc_list))        i = -1;    return i;}int smsc2_stop_smsc(Octstr *id) {    SMSCConn *conn;    int i;    /* find the specific smsc via id */    if ((i = smsc2_find(id)) == -1) {        error(0, "HTTP: Could not shutdown smsc-id `%s'", octstr_get_cstr(id));        return -1;    }    conn = list_get(smsc_list, i);    if (conn != NULL && conn->status == SMSCCONN_DEAD) {        error(0, "HTTP: Could not shutdown already deaed smsc-id `%s'",               octstr_get_cstr(id));        return -1;    }    info(0,"HTTP: Shutting down smsc-id `%s'", octstr_get_cstr(id));    smscconn_shutdown(conn, 1);   /* shutdown the smsc */    return 0;}int smsc2_restart_smsc(Octstr *id) {    CfgGroup *grp;    SMSCConn *conn;    Octstr *smscid = NULL;    int i;    /* find the specific smsc via id */    if ((i = smsc2_find(id)) == -1) {        error(0, "HTTP: Could not re-start non defined smsc-id `%s'",               octstr_get_cstr(id));        return -1;    }    /* check if smsc is online status already */    conn = list_get(smsc_list, i);    if (conn != NULL && conn->status != SMSCCONN_DEAD) {        error(0, "HTTP: Could not re-start already running smsc-id `%s'",               octstr_get_cstr(id));        return -1;    }    list_delete(smsc_list, i, 1); /* drop it from the active smsc list */    smscconn_destroy(conn);       /* destroy the connection */    /* find the group with smsc id */    grp = NULL;    list_lock(smsc_groups);    for (i = 0; i < list_len(smsc_groups) &&         (grp = list_get(smsc_groups, i)) != NULL; i++) {        smscid = cfg_get(grp, octstr_imm("smsc-id"));        if (smscid != NULL && octstr_compare(smscid, id) == 0) {            break;        }    }    list_unlock(smsc_groups);    octstr_destroy(smscid);    if (i > list_len(smsc_groups))        return -1;    info(0,"HTTP: Re-starting smsc-id `%s'", octstr_get_cstr(id));    conn = smscconn_create(grp, 1);     if (conn == NULL)        error(0, "Cannot start with SMSC connection failing");    else         modemConn = conn;    list_append(smsc_list, conn);    smscconn_start(conn);    if (router_thread >= 0)        gwthread_wakeup(router_thread);    return 0;}void smsc2_resume(void) {    SMSCConn *conn;    int i;    for (i = 0; i < list_len(smsc_list); i++) {        conn = list_get(smsc_list, i);        smscconn_start(conn);    }    if (router_thread >= 0)        gwthread_wakeup(router_thread);}void smsc2_suspend(void) {    SMSCConn *conn;    int i;    list_lock(smsc_list);    for (i = 0; i < list_len(smsc_list); i++) {        conn = list_get(smsc_list, i);        smscconn_stop(conn);    }    list_unlock(smsc_list);}int smsc2_shutdown(void) {    SMSCConn *conn;    int i;    if (!smsc_running) return -1;    /* Call shutdown for all SMSC Connections; they should     * handle that they quit, by emptying queues and then dying off     */    list_lock(smsc_list);    for (i=0; i < list_len(smsc_list); i++) {        conn = list_get(smsc_list, i);        smscconn_shutdown(conn, 1);    }    list_unlock(smsc_list);    if (router_thread >= 0)        gwthread_wakeup(router_thread);    /* start avalanche by calling shutdown */    /* XXX shouldn'w we be sure that all smsces have closed their     * receive thingies? Is this guaranteed by setting bb_status     * to shutdown before calling these?     */    list_remove_producer(incoming_sms);    list_remove_producer(incoming_wdp);        list_remove_producer(sms8bitreassembly);    return 0;}void smsc2_cleanup(void) {    SMSCConn *conn;    int i;    debug("smscconn", 0, "final clean-up for SMSCConn");    list_lock(smsc_list);    for (i = 0; i < list_len(smsc_list); i++) {        conn = list_get(smsc_list, i);        smscconn_destroy(conn);    }    list_unlock(smsc_list);    list_destroy(smsc_list, NULL);    list_destroy(smsc_groups, NULL);    octstr_destroy(unified_prefix);        numhash_destroy(white_list);    numhash_destroy(black_list);}Octstr *smsc2_status(int status_type) {    Octstr *tmp;    char tmp3[64];    char *lb;    int i, para = 0;    SMSCConn *conn;    StatusInfo info;    Octstr *conn_id = NULL;    Octstr *conn_name = NULL;    if ((lb = bb_status_linebreak(status_type)) == NULL)        return octstr_create("Un-supported format");    if (status_type == BBSTATUS_HTML || status_type == BBSTATUS_WML)        para = 1;    if (!smsc_running) {        if (status_type == BBSTATUS_XML)            return octstr_create ("<smscs>\n\t<count>0</count>\n</smscs>");        else            return octstr_format("%sNo SMSC connections%s\n\n", para ? "<p>" : "",                                 para ? "</p>" : "");    }    if (status_type != BBSTATUS_XML)        tmp = octstr_format("%sSMSC connections:%s", para ? "<p>" : "", lb);    else        tmp = octstr_format("<smscs><count>%d</count>\n\t", list_len(smsc_list));    list_lock(smsc_list);    for (i = 0; i < list_len(smsc_list); i++) {        conn = list_get(smsc_list, i);        if ((smscconn_info(conn, &info) == -1)) {            /*              * we do not delete SMSCs from the list              * this way we can show in the status which links are dead             */            continue;        }        conn_id = conn ? smscconn_id(conn) : octstr_imm("unknown");        conn_id = conn_id ? conn_id : octstr_imm("unknown");        conn_name = conn ? smscconn_name(conn) : octstr_imm("unknown");        if (status_type == BBSTATUS_HTML) {            octstr_append_cstr(tmp, "&nbsp;&nbsp;&nbsp;&nbsp;<b>");            octstr_append(tmp, conn_id);            octstr_append_cstr(tmp, "</b>&nbsp;&nbsp;&nbsp;&nbsp;");        } else if (status_type == BBSTATUS_TEXT) {            octstr_append_cstr(tmp, "    ");            octstr_append(tmp, conn_id);            octstr_append_cstr(tmp, "    ");        }        if (status_type == BBSTATUS_XML) {            octstr_append_cstr(tmp, "<smsc>\n\t\t<name>");            octstr_append(tmp, conn_name);            octstr_append_cstr(tmp, "</name>\n\t\t");            octstr_append_cstr(tmp, "<id>");            octstr_append(tmp, conn_id);            octstr_append_cstr(tmp, "</id>\n\t\t");        } else            octstr_append(tmp, conn_name);        switch (info.status) {            case SMSCCONN_ACTIVE:            case SMSCCONN_ACTIVE_RECV:                sprintf(tmp3, "online %lds", info.online);                break;            case SMSCCONN_DISCONNECTED:                sprintf(tmp3, "disconnected");                break;            case SMSCCONN_CONNECTING:                sprintf(tmp3, "connecting");                break;            case SMSCCONN_RECONNECTING:                sprintf(tmp3, "re-connecting");                break;            case SMSCCONN_DEAD:                sprintf(tmp3, "dead");                break;            default:                sprintf(tmp3, "unknown");        }        if (status_type == BBSTATUS_XML)            octstr_format_append(tmp, "<status>%s</status>\n\t\t<received>%ld</received>"                                 "\n\t\t<sent>%ld</sent>\n\t\t<failed>%ld</failed>\n\t\t"                                 "<queued>%ld</queued>\n\t</smsc>\n", tmp3,                                 info.received, info.sent, info.failed,                                 info.queued);        else            octstr_format_append(tmp, " (%s, rcvd %ld, sent %ld, failed %ld, "                                 "queued %ld msgs)%s", tmp3,                                 info.received, info.sent, info.failed,                                 info.queued, lb);    }    list_unlock(smsc_list);    if (para)        octstr_append_cstr(tmp, "</p>");    if (status_type == BBSTATUS_XML)        octstr_append_cstr(tmp, "</smscs>\n");    else        octstr_append_cstr(tmp, "\n\n");    return tmp;}/* function to route outgoing SMS'es * * If finds a good one, puts into it and returns 1 * If finds only bad ones, but acceptable, queues and *  returns 0  (like all acceptable currently disconnected) * If cannot find nothing at all, returns -1 and * message is NOT destroyed (otherwise it is) */int smsc2_rout(Msg *msg) {    StatusInfo info;    SMSCConn *conn, *best_preferred, *best_ok;    long bp_load, bo_load;    int i, s, ret, bad_found;    char *uf;    bp_load = bo_load = 0;    /* XXX handle ack here? */    if (msg_type(msg) != sms)        return -1;    if (list_len(smsc_list) == 0) {        warning(0, "No SMSCes to receive message");        return -1;    }    /* unify prefix of receiver, in case of it has not been     * already done */    uf = unified_prefix ? octstr_get_cstr(unified_prefix) : NULL;    normalize_number(uf, &(msg->sms.receiver));    /* select in which list to add this     * start - from random SMSCConn, as they are all 'equal'     */    list_lock(smsc_list);    s = gw_rand() % list_len(smsc_list);    best_preferred = best_ok = NULL;    bad_found = 0;    conn = NULL;    for (i=0; i < list_len(smsc_list); i++) {        conn = list_get(smsc_list,  (i+s) % list_len(smsc_list));        ret = smscconn_usable(conn,msg);        if (ret == -1)            continue;        /* if we already have a preferred one, skip non-preferred */        if (ret != 1 && best_preferred)            continue;        smscconn_info(conn, &info);        /* If connection is not currently answering... */        if (info.status != SMSCCONN_ACTIVE) {            bad_found = 1;            continue;        }        if (ret == 1) {          /* preferred */            if (best_preferred == NULL || info.load < bp_load) {                best_preferred = conn;                bp_load = info.load;                continue;            }        }        if (best_ok == NULL || info.load < bo_load) {            best_ok = conn;            bo_load = info.load;        }    }    list_unlock(smsc_list);    if (best_preferred)        ret = smscconn_send(best_preferred, msg);    else if (best_ok)        ret = smscconn_send(best_ok, msg);    else if (bad_found) {        if (bb_status != BB_SHUTDOWN)            list_produce(outgoing_sms, msg);        return 0;    } else {        if (bb_status == BB_SHUTDOWN)            return 0;        warning(0, "Cannot find SMSCConn for message to <%s>, rejected.",                octstr_get_cstr(msg->sms.receiver));        return -1;    }    /* check the status of sending operation */    if (ret == -1)        return(smsc2_rout(msg));   /* re-try */    msg_destroy(msg);    return 1;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?