Example #1
0
/******************************************************************************
 **函数名称: invtd_insert_word_req_hdl
 **功    能: 插入关键字的处理
 **输入参数:
 **     type: 消息类型
 **     orig: 源节点ID
 **     buff: 插入关键字-请求数据
 **     len: 数据长度
 **     args: 附加参数
 **输出参数: NONE
 **返    回: 0:成功 !0:失败
 **实现描述:
 **注意事项: 源节点ID(orig)将成为应答消息的目的节点ID(dest)
    **作    者: # Qifeng.zou # 2015-06-17 21:37:55 #
 ******************************************************************************/
int invtd_insert_word_req_hdl(int type, int orig, char *buff, size_t len, void *args)
{
    mesg_insert_word_rsp_t *rsp;
    invtd_cntx_t *ctx = (invtd_cntx_t *)args;
    mesg_header_t *rsp_head, *head = (mesg_header_t *)buff;
    mesg_insert_word_req_t *req = (mesg_insert_word_req_t *)(head + 1); /* 请求 */
    char addr[sizeof(mesg_header_t) + sizeof(mesg_insert_word_rsp_t)];

    /* > 转换字节序 */
    MESG_HEAD_NTOH(head, head);
    req->freq = ntohl(req->freq);

    rsp_head = (mesg_header_t *)addr;
    rsp = (mesg_insert_word_rsp_t *)(rsp_head + 1);

    /* > 插入倒排表 */
    pthread_rwlock_wrlock(&ctx->invtab_lock);
    if (invtab_insert(ctx->invtab, req->word, req->url, req->freq)) {
        pthread_rwlock_unlock(&ctx->invtab_lock);
        log_error(ctx->log, "Insert invert table failed! serial:%s word:%s url:%s freq:%d",
                head->serial, req->word, req->url, req->freq);
        /* > 设置应答信息 */
        rsp->code = MESG_INSERT_WORD_FAIL; // 失败
        snprintf(rsp->word, sizeof(rsp->word), "%s", req->word);
        goto INVTD_INSERT_WORD_RSP;
    }
    pthread_rwlock_unlock(&ctx->invtab_lock);

    /* > 设置应答信息 */
    rsp->code = MESG_INSERT_WORD_SUCC; // 成功
    snprintf(rsp->word, sizeof(rsp->word), "%s", req->word);

INVTD_INSERT_WORD_RSP:
    /* > 发送应答信息 */
    MESG_HEAD_SET(rsp_head, MSG_INSERT_WORD_RSP, head->sid,
            head->nid, head->serial, sizeof(mesg_insert_word_rsp_t));
    MESG_HEAD_HTON(rsp_head, rsp_head);
    mesg_insert_word_resp_hton(rsp);

    if (rtmq_proxy_async_send(ctx->frwder, MSG_INSERT_WORD_RSP, (void *)addr, sizeof(addr))) {
        log_error(ctx->log, "Send response failed! serial:%s word:%s url:%s freq:%d",
                head->serial, req->word, req->url, req->freq);
    }

    return INVT_OK;
}
Example #2
0
/******************************************************************************
 **函数名称: invtd_insert_word_req_hdl
 **功    能: 插入关键字的处理
 **输入参数:
 **     type: 消息类型
 **     dev_orig: 源节点ID
 **     buff: 插入关键字-请求数据
 **     len: 数据长度
 **     args: 附加参数
 **输出参数: NONE
 **返    回: 0:成功 !0:失败
 **实现描述:
 **注意事项: 源节点ID(dev_orig)将成为应答消息的目的节点ID(dest)
    **作    者: # Qifeng.zou # 2015-06-17 21:37:55 #
 ******************************************************************************/
int invtd_insert_word_req_hdl(int type, int dev_orig, char *buff, size_t len, void *args)
{
    mesg_insert_word_rsp_t rsp; /* 应答 */
    invtd_cntx_t *ctx = (invtd_cntx_t *)args;
    mesg_insert_word_req_t *req = (mesg_insert_word_req_t *)buff; /* 请求 */

    memset(&rsp, 0, sizeof(rsp));

    req->serial = ntoh64(req->serial);
    req->freq = ntohl(req->freq);

    /* > 插入倒排表 */
    pthread_rwlock_wrlock(&ctx->invtab_lock);
    if (invtab_insert(ctx->invtab, req->word, req->url, req->freq))
    {
        pthread_rwlock_unlock(&ctx->invtab_lock);
        log_error(ctx->log, "Insert invert table failed! serial:%s word:%s url:%s freq:%d",
                req->serial, req->word, req->url, req->freq);
        /* > 设置应答信息 */
        rsp.code = htonl(MESG_INSERT_WORD_FAIL); // 失败
        snprintf(rsp.word, sizeof(rsp.word), "%s", req->word);
        goto INVTD_INSERT_WORD_RSP;
    }
    pthread_rwlock_unlock(&ctx->invtab_lock);

    /* > 设置应答信息 */
    rsp.code = htonl(MESG_INSERT_WORD_SUCC); // 成功
    snprintf(rsp.word, sizeof(rsp.word), "%s", req->word);

INVTD_INSERT_WORD_RSP:
    /* > 发送应答信息 */
    rsp.serial = hton64(req->serial);
    if (rtrd_send(ctx->rtrd, MSG_INSERT_WORD_RSP, dev_orig, (void *)&rsp, sizeof(rsp))) {
        log_error(ctx->log, "Send response failed! serial:%s word:%s url:%s freq:%d",
                req->serial, req->word, req->url, req->freq);
    }

    return INVT_OK;
}