コード例 #1
0
ファイル: ev_node.c プロジェクト: Alvin0113/LuoYunCloud
/*
** process instance info query reply
**
** the is the reply of instance info query through node request
*/
static int __instance_info_update(xmlDoc * doc, xmlNode * node,
                                  int ent_id, int * j_status)
{
    logdebug(_("%s called\n"), __func__);

    /* Create xpath evaluation context */
    xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
    if (xpathCtx == NULL) {
        logerror(_("unable to create new XPath context %s, %d\n"),
                 __func__, __LINE__);
        goto failed;
    }

    InstanceInfo ii;
    char *str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/id");
    if (str == NULL)
        goto failed;
    ii.id = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/status");
    if (str == NULL)
        goto failed;
    ii.status = atoi(str);
    free(str);
    ii.ip = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/ip");

    logdebug(_("update info for instance %d: status %d, ip %s\n"),
                ii.id, ii.status, ii.ip);

    /* the ent_id passed to the routine is for node, not usefult */
    ent_id = ly_entity_find_by_db(LY_ENTITY_OSM, ii.id);
    if (!ly_entity_is_registered(ent_id) &&
        db_instance_update_status(ii.id, &ii, -1) < 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        if (ii.ip)
            free(ii.ip);
        goto failed;
    }

    if (ii.ip)
        free(ii.ip);
    return 0;

failed:
    *j_status = LY_S_FINISHED_FAILURE;
    return 0;
}
コード例 #2
0
ファイル: ev_node.c プロジェクト: cloudcache/LuoYunCloud
/*
** process instance info data,
** either from internal query or from xml response
**
*/
static int __instance_info_update(xmlDoc * doc, xmlNode * node)
{
    int ret;
    logdebug(_("%s called\n"), __func__);

    /* Create xpath evaluation context */
    xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
    if (xpathCtx == NULL) {
        logerror(_("unable to create new XPath context %s, %d\n"),
                 __func__, __LINE__);
        return -1;
    }

    InstanceInfo ii;
    bzero(&ii, sizeof(InstanceInfo));
    char *str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/id");
    if (str == NULL)
        goto failed;
    ii.id = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/status");
    if (str == NULL)
        goto failed;
    ii.status = atoi(str);
    free(str);
    ii.ip = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/ip");
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/gport");
    if (str == NULL)
        goto failed;
    ii.gport = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/response/data/netstat0");
    if (str)
        sscanf(str, "%ld %ld %ld %ld", &ii.netstat[0].rx_bytes, &ii.netstat[0].rx_pkts,
                                       &ii.netstat[0].tx_bytes, &ii.netstat[0].tx_pkts);
    else
        goto failed;
    free(str);

    logdebug(_("update info for instance %d:"), ii.id);
    luoyun_instance_info_print(&ii);

    int ent_id = ly_entity_find_by_db(LY_ENTITY_OSM, ii.id);
    if (ly_entity_is_registered(ent_id))
        ii.status = DOMAIN_S_UNKNOWN; /* don't update status */
    if (db_instance_update_status(ii.id, &ii, -1) < 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        if (ii.ip)
            free(ii.ip);
        goto failed;
    }

    if (ii.ip)
        free(ii.ip);
    ret = 0;
    goto done;

failed:
    ret = -1;
done:
    xmlXPathFreeContext(xpathCtx);
    return ret;
}
コード例 #3
0
ファイル: events.c プロジェクト: alayasix/LuoYunCloud
int ly_epoll_entity_recv(int ent_id)
{
    if (ly_entity_type(ent_id) == LY_ENTITY_CLC)
        return __epoll_work_recv(ent_id);

    int fd = ly_entity_fd(ent_id);
    if (fd < 0) {
        logerror(_("fd for entity %d was closed. ignore event.\n"), ent_id);
        return 1;
    }

    LYPacketRecv *pkt = ly_entity_pkt(ent_id);
    if (pkt == NULL)
        return -255;

    int size;
    void * buf = ly_packet_buf(pkt, &size);
    if (buf == NULL) {
        logerror(_("ly_packet_buf returns NULL buffer. close socket\n"));
        return 1;
    }
    if (size == 0) {
        logerror(_("ly_packet_buf returns 0 size buffer. close socket\n"));
        return 1;
    }

    int len = recv(fd, buf, size, 0);
    if (len <= 0) {
        loginfo(_("socket %d recv returns %d. close socket\n"), fd, len);
        if (len < 0)
            loginfo(_("socket %d recv, errno %d\n"), fd, errno);

        int type = ly_entity_type(ent_id);
        int db_id = ly_entity_db_id(ent_id);
        if (type == LY_ENTITY_NODE) {
            logdebug(_("update node %d status in db to offline\n"), db_id);
            db_node_update_status(DB_NODE_FIND_BY_ID, &db_id, NODE_STATUS_OFFLINE);
            return 1;
        }

        if (ly_entity_type(ent_id) != LY_ENTITY_OSM)
            return 1;

        logdebug(_("update instance %d status in db\n"), db_id);
        InstanceInfo ii;
        ii.ip = NULL;
        ii.status = DOMAIN_S_NEED_QUERY;
        db_instance_update_status(db_id, &ii, -1);
        job_internal_query_instance(db_id);
        return 1;
    }
    logdebug(_("socket %d recv %d bytes\n"), fd, len);

    while(1) {
        int ret = ly_packet_recv(pkt, len);
        if (ret < 0) {
            logerror(_("package recv error in %s\n"), __func__);
            __print_recv_buf(buf, len);
            break;
        }

        /* currenly we only support processing a complete packet */
        if (ret == 0) {
            if (pkt->pkt_buf_received > 0) {
                loginfo(_("socket %d recv partial packet(len %d)\n"),
                           fd, pkt->pkt_buf_received);
                __print_recv_buf(buf, len);
            }
            break;
        }

        int type = ly_packet_type(pkt);
        loginfo(_("socket %d recv packet, type %d\n"), fd, type);
        /*
        if (type == PKT_TYPE_UNKNOW)
            break;
        */

        buf = ly_packet_data(pkt, &size);
        if (type == PKT_TYPE_WEB_NEW_JOB_REQUEST) {
            ly_entity_init(ent_id, LY_ENTITY_WEB);
            ret = __process_web_job(buf, size, ent_id);
            if (ret < 0)
                logerror(_("web packet process error in %s.\n"), __func__);
        }
	else if (type == PKT_TYPE_NODE_REGISTER_REQUEST) {
            ly_entity_init(ent_id, LY_ENTITY_NODE);
            ret = eh_process_node_xml(buf, ent_id);
            if (ret < 0)
                logerror(_("node packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_NODE_AUTH_REQUEST ||
                 type == PKT_TYPE_NODE_AUTH_REPLY) {
            ly_entity_init(ent_id, LY_ENTITY_NODE);
            ret = eh_process_node_auth(type == PKT_TYPE_NODE_AUTH_REPLY ?
                                       1 : 0, buf, ent_id);
            if (ret < 0)
                logerror(_("node auth packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_OSM_AUTH_REQUEST ||
                 type == PKT_TYPE_OSM_AUTH_REPLY) {
            ly_entity_init(ent_id, LY_ENTITY_OSM);
            ret = eh_process_osm_auth(type == PKT_TYPE_OSM_AUTH_REPLY ?
                                       1 : 0, buf, ent_id);
            if (ret < 0)
                logerror(_("osm auth packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_CLC_OSM_QUERY_REPLY) {
            ret = eh_process_osm_query(ly_packet_data(pkt, NULL));
            if (ret < 0)
                logerror(_("osm packet process error in %s\n"), __func__);
        }
	else if (PKT_TYPE_ENTITY_GROUP_CLC(type) ||
                 PKT_TYPE_ENTITY_GROUP_NODE(type)) {
            ret = eh_process_node_xml(buf, ent_id);
            if (ret < 0)
                logerror(_("node packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_OSM_REGISTER_REQUEST) {
            ly_entity_init(ent_id, LY_ENTITY_OSM);
            ret = eh_process_osm_register(buf, size, ent_id);
            if (ret < 0)
                logerror(_("osm packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_OSM_REPORT) {
            ret = eh_process_osm_report(buf, size, ent_id);
            if (ret < 0)
                logerror(_("osm packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_TEST_ECHO_REQUEST) {
            ret = __process_test_echo(buf, size, ent_id);
            if (ret < 0)
                logerror(_("echo packet process error in %s.\n"), __func__);
        }
        else {
            logerror(_("unrecognized packet type.\n"));
        }

        if (ly_packet_recv_done(pkt) < 0 || ret < 0) {
            logerror(_("%s return error\n"), __func__);
            return -1;
        }

        if (ret > 0)
            return ret;

        len = 0; /* continue processing data in buffer */
    }

    return 0;
}