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; }
/* process xml response */ static int __process_node_xml_response(xmlDoc * doc, xmlNode * node, int ent_id) { logdebug(_("node response for entity %d\n"), ent_id); char * str = (char *) xmlGetProp(node, (const xmlChar *) "id"); if (str) logdebug("node response id(job id) = %s\n", str); else { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } int id = atoi(str); free(str); str = (char *) xmlGetProp(node, (const xmlChar *) "status"); if (str) logdebug("node response status = %s\n", str); else { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } int status = atoi(str); free(str); int data_type = -1; node = node->children; for (; node; node = node->next) { if (node->type == XML_ELEMENT_NODE) { if (strcmp((char *)node->name, "result") == 0) { xmlNode * n = node->children; if (n && n->type == XML_TEXT_NODE && n->content) loginfo(_("response result: %s\n"), n->content); else loginfo(_("response no result message\n")); } else if (strcmp((char *)node->name, "data") == 0) { str = (char *) xmlGetProp(node, (const xmlChar *) "type"); if (str) { logdebug("node response data type = %s\n", str); data_type = atoi(str); free(str); } else logwarn(_("response data no type\n")); } } } if (id != 0) { LYJobInfo * job = job_find(id); if (job == NULL) { logwarn(_("job(%d) not found waiting for node reply\n"), id); return 0; } if (job_update_status(job, status)) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } /* hack to release entity during instance shutting down */ if (((job->j_action == LY_A_NODE_STOP_INSTANCE) || (job->j_action == LY_A_NODE_FULLREBOOT_INSTANCE) || (job->j_action == LY_A_NODE_ACPIREBOOT_INSTANCE)) && status == LY_S_RUNNING_STOPPED) { int ins_id = ly_entity_find_by_db(LY_ENTITY_OSM, job->j_target_id); if (ins_id > 0) ly_entity_release(ins_id); } /* continue processing for data */ } int ent_type = ly_entity_type(ent_id); if (ent_type == LY_ENTITY_NODE && data_type == DATA_INSTANCE_INFO) { if ( __instance_info_update(doc, node)) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } } if (ent_type == LY_ENTITY_NODE && data_type == DATA_NODE_INFO) { if ( __node_info_update(doc, node, ent_id, &status)) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } } return 0; }
/* process xml response */ static int __process_node_xml_response(xmlDoc * doc, xmlNode * node, int ent_id) { loginfo(_("node response for entity %d\n"), ent_id); char * str = (char *) xmlGetProp(node, (const xmlChar *) "id"); if (str) logdebug("node response id(job id) = %s\n", str); else { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } int id = atoi(str); free(str); str = (char *) xmlGetProp(node, (const xmlChar *) "status"); if (str) logdebug("node response status = %s\n", str); else { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } int status = atoi(str); free(str); int data_type = -1; node = node->children; for (; node; node = node->next) { if (node->type == XML_ELEMENT_NODE) { if (strcmp((char *)node->name, "result") == 0) { xmlNode * n = node->children; if (n && n->type == XML_TEXT_NODE && n->content) loginfo(_("response result: %s\n"), n->content); else loginfo(_("response no result message\n")); } else if (strcmp((char *)node->name, "data") == 0) { str = (char *) xmlGetProp(node, (const xmlChar *) "type"); if (str) { logdebug("node response data type = %s\n", str); data_type = atoi(str); free(str); } else logwarn(_("response data no type\n")); } } } if (id != 0) { LYJobInfo * job = job_find(id); if (job == NULL) { logwarn(_("job(%d) not found waiting for node reply\n"), id); return 0; } if (job_update_status(job, status)) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } if (status != LY_S_FINISHED_SUCCESS || data_type < 0) return 0; } int ent_type = ly_entity_type(ent_id); if (ent_type == LY_ENTITY_NODE && data_type == DATA_INSTANCE_INFO) { if ( __instance_info_update(doc, node, ent_id, &status)) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } } if (ent_type == LY_ENTITY_NODE && data_type == DATA_NODE_INFO) { if ( __node_info_update(doc, node, ent_id, &status)) { logerror(_("error in %s(%d)\n"), __func__, __LINE__); return -1; } } return 0; }