int Switch_Manager::CheckOneSwitch(char* strUrl,char* outPutUrl,char* strSessionID) { LOG_DEBUG("DEBUG - [SWM]: - CheckOneSwitch\n"); printf("--CheckOneSwitch\n"); int ret = -1; //查找会话 // printf("---find sessionid %s map size=%d \n",strSessionID,m_mapSwitchTaskInfo.size()); SwitchSeverInfo** ppSeverSwitch = (SwitchSeverInfo**)malloc(sizeof(SwitchSeverInfo*)); memset(ppSeverSwitch,0,sizeof(ppSeverSwitch)); int iRet = FindOneSwitchServer(strSessionID,ppSeverSwitch); //找到对应服务器 SwitchSeverInfo *pFindSeverInfo = *ppSeverSwitch; if(pFindSeverInfo ==NULL) { printf("---no find sever info or no sessiond \n"); ReplyException(strUrl,outPutUrl,Error_NO_Stream); return -2; } //连接server //报文转发 Stream ptmpRequest; int iret = ptmpRequest.ConnectServer(pFindSeverInfo->strServerIPAddr,pFindSeverInfo->iListenPort); if(iret != 0) { //连接失败 ReplyException(strUrl,outPutUrl,Error_Connect_Sever); return -1; } iret = ptmpRequest.Send_str(strUrl); if(iret != 0) { ReplyException(strUrl,outPutUrl,Error_Connect_Sever); return -1; } char Rcv_buf[1024]={0}; int iLen = sizeof(Rcv_buf); iret = ptmpRequest.Recv_str(Rcv_buf,&iLen); if(iret !=0) { ReplyException(strUrl,outPutUrl,Error_Recv_Sever); return -1; } memcpy(outPutUrl,Rcv_buf,iLen); //LOG_INFO_FORMAT("check one switch status :%s"); //cJSON *pcmd = NULL; //解析报文数据 //replace(Rcv_buf, "XXEE", ""); //cJSON* pRoot = cJSON_Parse(Rcv_buf); return 0; }
int Switch_Manager::DeleteOneSwitch(char* strUrl,char* outPutUrl,char* strSessionID) { printf("----DeleteOneSwitch fun \n"); LOG_DEBUG("DEBUG - [SWM]: - DeleteOneSwitch fun \n"); //查找会话 // printf("---find sessionid %s map size=%d \n",strSessionID,m_mapSwitchTaskInfo.size()); SwitchSeverInfo** ppSeverSwitch = (SwitchSeverInfo**)malloc(sizeof(SwitchSeverInfo*)); memset(ppSeverSwitch,0,sizeof(ppSeverSwitch)); int iRet = FindOneSwitchServer(strSessionID,ppSeverSwitch); //找到对应服务器 SwitchSeverInfo *pFindSeverInfo = *ppSeverSwitch; if(pFindSeverInfo == NULL) { printf("---no find sever info or no sessiond \n"); ReplyException(strUrl,outPutUrl,0); return -2; } //连接server //报文转发 Stream ptmpRequest; int iret = ptmpRequest.ConnectServer(pFindSeverInfo->strServerIPAddr,pFindSeverInfo->iListenPort); if(iret != 0) { //连接失败 ReplyException(strUrl,outPutUrl,Error_Connect_Sever); return -1; } iret = ptmpRequest.Send_str(strUrl); if(iret != 0) { ReplyException(strUrl,outPutUrl,Error_Connect_Sever); return -1; } char Rcv_buf[1024]={0}; int iLen = sizeof(Rcv_buf); iret = ptmpRequest.Recv_str(Rcv_buf,&iLen); if(iret !=0) { ReplyException(strUrl,outPutUrl,Error_Recv_Sever); return -1; } printf("recv Del strem\n"); fflush(stdout); memcpy(outPutUrl,Rcv_buf,iLen); cJSON *pcmd = NULL; //解析报文数据 replace(Rcv_buf, "XXEE", ""); cJSON* pRoot = cJSON_Parse(Rcv_buf); if (pRoot) { pcmd = cJSON_GetObjectItem(pRoot, "cmd"); if (pcmd) { //判断请求类型 if(strcmp(pcmd->valuestring, "del_ads_stream") == 0) { cJSON* pSeesid = cJSON_GetObjectItem(pRoot, "sessionid"); char strSeesionID[128] ={0}; int iSeessionID = 0; if(pSeesid){ iSeessionID = atoi(pSeesid->valuestring); memcpy(strSeesionID,pSeesid->valuestring,strlen(pSeesid->valuestring)+1); } cJSON* pRetCOde = cJSON_GetObjectItem(pRoot,"ret_code"); int iRetCode = -1; if(pRetCOde) iRetCode = atoi(pRetCOde->valuestring); //printf("parse--%s\n",pRetCOde->valuestring); cJSON* pSerialNo = cJSON_GetObjectItem(pRoot,"serialno"); char strSerialNo[128] ={0}; //printf("parse--%s\n",pSerialNo->valuestring); if(pSerialNo) memcpy(strSerialNo,pSerialNo->valuestring,strlen(pSerialNo->valuestring)+1); printf("--del_ads_stream return \n"); if(iRetCode >= 0) { //添加成功增加关联sessionID 和severID 键值对 pthread_mutex_lock(&m_lockerttTaskInfo); MapSeverInfo::iterator itlook = m_mapSwitchTaskInfo.find(strSeesionID); if(itlook != m_mapSwitchTaskInfo.end()) { //出现两个一样会话ID 先删除 yyd m_mapSwitchTaskInfo.erase(itlook); } //存放sever的map不会在运行中做增删操作 pFindSeverInfo->iCurrentSwitchNumber--; printf("----erase to map %s ,size=%d \n",strSeesionID,m_mapSwitchTaskInfo.size()); pthread_mutex_unlock(&m_lockerttTaskInfo); LOG_INFO_FORMAT("----erase to map %s ,size=%d \n",strSeesionID,m_mapSwitchTaskInfo.size()); } } } } return 0; }
//返回的是关联的会话 int Switch_Manager::AddOneSwitch(char* strUrl,char* outPutUrl,char* strSessionID,char* inputUrl) { printf("-----AddOneSwitch\n"); LOG_DEBUG("DEBUG - [SWM]: - AddOneSwitch\n"); //查找可用的sever 找使用率最小的 //遍历切流器 printf("-----add switch %s \n",strSessionID); SwitchSeverInfo **ppFindSeverInfo = (SwitchSeverInfo **)malloc(sizeof(SwitchSeverInfo *)); memset(ppFindSeverInfo,0,sizeof(ppFindSeverInfo)); int iRet = FindOneSwitchServer(strSessionID,ppFindSeverInfo); //找到对应服务器 SwitchSeverInfo *pFindSeverInfo = NULL; if(iRet == 0 && *ppFindSeverInfo!= NULL) { //找到正在转发的流 pFindSeverInfo = *ppFindSeverInfo; } else { //新增的转发流 //需要根据inputip来确定使用哪个sever //解析出ip 端口 int iSrcPort = 0; char strSrcIP[128]={0}; char strSrcPort[32]={0}; FindDataFromString(inputUrl,strSrcIP,strSrcPort); iSrcPort = atoi(strSrcPort); //根据IP查找sever iRet = FindSwitchServerFromIP(strSrcIP,ppFindSeverInfo); if(*ppFindSeverInfo ==NULL) { //如果没有指定ip则由调度分配 iRet = GetOneSwitchServer(ppFindSeverInfo); if(*ppFindSeverInfo ==NULL) { printf("---no can used sever -----\n"); ReplyException(strUrl,outPutUrl,Error_NO_Sever); return -1; //没有可以用的Switch_Sever; } } pFindSeverInfo = *ppFindSeverInfo; } //连接server //报文转发 Stream ptmpRequest; int iret = ptmpRequest.ConnectServer(pFindSeverInfo->strServerIPAddr,pFindSeverInfo->iListenPort); if(iret != 0) { //连接失败 ReplyException(strUrl,outPutUrl,Error_Connect_Sever); return -1; } iret = ptmpRequest.Send_str(strUrl); if(iret != 0) { ReplyException(strUrl,outPutUrl,Error_Connect_Sever); return -1; } char Rcv_buf[1024]={0}; int iLen = sizeof(Rcv_buf); iret = ptmpRequest.Recv_str(Rcv_buf,&iLen); if(iret !=0) { ReplyException(strUrl,outPutUrl,Error_Recv_Sever); return -1; } memcpy(outPutUrl,Rcv_buf,iLen); cJSON *pcmd = NULL; //解析报文数据 replace(Rcv_buf, "XXEE", ""); cJSON* pRoot = cJSON_Parse(Rcv_buf); if (pRoot) { pcmd = cJSON_GetObjectItem(pRoot, "cmd"); if (pcmd) { //判断请求类型 if (strcmp(pcmd->valuestring, "add_ads_stream") == 0) { printf("--add_ads_stream return \n"); //通过 cJSON* pSeesid = cJSON_GetObjectItem(pRoot, "sessionid"); char strSeesionID[128] ={0}; int iSeessionID = 0; if(pSeesid){ iSeessionID = atoi(pSeesid->valuestring); memcpy(strSeesionID,pSeesid->valuestring,strlen(pSeesid->valuestring)+1); } cJSON* pTaskID = cJSON_GetObjectItem(pRoot, "task_id"); int iTaskID = -1; if(pTaskID) iTaskID = pTaskID->valueint; cJSON* pRetCOde = cJSON_GetObjectItem(pRoot,"ret_code"); int iRetCode = -1; if(pRetCOde) iRetCode = atoi(pRetCOde->valuestring); cJSON* pSerialNo = cJSON_GetObjectItem(pRoot,"serialno"); char strSerialNo[128] ={0}; if(pSerialNo) memcpy(strSerialNo,pSerialNo->valuestring,strlen(pSerialNo->valuestring)+1); if(iRetCode >= 0) { //添加成功增加关联sessionID 和severID 键值对 pthread_mutex_lock(&m_lockerttTaskInfo); MapSeverInfo::iterator itlook = m_mapSwitchTaskInfo.find(strSeesionID); if(itlook != m_mapSwitchTaskInfo.end()) { //出现两个一样会话ID 先删除 yyd m_mapSwitchTaskInfo.erase(itlook); } pFindSeverInfo->iCurrentSwitchNumber++; m_mapSwitchTaskInfo.insert(MapSeverInfo::value_type(strSeesionID,pFindSeverInfo->iServerID)); printf("----insert to map %s ,size=%d \n",strSeesionID,m_mapSwitchTaskInfo.size()); pthread_mutex_unlock(&m_lockerttTaskInfo); LOG_INFO_FORMAT("----insert to map %s ,size=%d \n",strSeesionID,m_mapSwitchTaskInfo.size()); } } } } return 0; }
int Switch_Manager::ReqOneSwitchForPort(char* strUrl,char* outPutUrl,char* strSessionID) { //查找可用的sever 找使用率最小的 //遍历切流器 printf("-----ReqOneSwitchForPort \n"); LOG_DEBUG("DEBUG - [SWM]: - ReqOneSwitchForPort \n"); printf("-----add switch %s \n",strSessionID); SwitchSeverInfo **ppFindSeverInfo = (SwitchSeverInfo **)malloc(sizeof(SwitchSeverInfo *)); memset(ppFindSeverInfo,0,sizeof(ppFindSeverInfo)); int iRet = FindOneSwitchServer(strSessionID,ppFindSeverInfo); //找到对应服务器 SwitchSeverInfo *pFindSeverInfo = NULL; if(iRet == 0 && *ppFindSeverInfo!= NULL) { //找到正在转发的流 pFindSeverInfo = *ppFindSeverInfo; } else { iRet = GetOneSwitchServer(ppFindSeverInfo); if(*ppFindSeverInfo ==NULL) { printf("---no can used sever -----\n"); ReplyException(strUrl,outPutUrl,Error_NO_Sever); return -1; //没有可以用的Switch_Sever; } pFindSeverInfo = *ppFindSeverInfo; } //连接server //报文转发 Stream ptmpRequest; int iret = ptmpRequest.ConnectServer(pFindSeverInfo->strServerIPAddr,pFindSeverInfo->iListenPort); if(iret != 0) { //连接失败 ReplyException(strUrl,outPutUrl,Error_Connect_Sever); return -1; } iret = ptmpRequest.Send_str(strUrl); if(iret != 0) { ReplyException(strUrl,outPutUrl,Error_Connect_Sever); return -1; } char Rcv_buf[1024]={0}; int iLen = sizeof(Rcv_buf); iret = ptmpRequest.Recv_str(Rcv_buf,&iLen); if(iret !=0) { ReplyException(strUrl,outPutUrl,Error_Recv_Sever); return -1; } memcpy(outPutUrl,Rcv_buf,iLen); return 0; }
int Switch_Manager::DeleteAllWsitch(char* strUrl,char* outPutUrl,char* strSessionID) { LOG_DEBUG("DEBUG - [SWM]: - DeleteAllWsitch fun \n"); printf("---DeleteAllWsitch fun \n"); bool bIsReplyflag = false; //遍历切流器 时间过长,不加锁 pthread_mutex_lock(&m_mutexServerList); MapSwitchSeverInfo::iterator iterLook = m_mapSwitchServerInfo.begin(); while(iterLook != m_mapSwitchServerInfo.end()) { // SwitchSeverInfo *pFindSeverInfo = iterLook->second; if(pFindSeverInfo && pFindSeverInfo->iRunStatus == 0) //在线的切流器 { //连接server //报文转发 Stream ptmpRequest; int iret = ptmpRequest.ConnectServer(pFindSeverInfo->strServerIPAddr,pFindSeverInfo->iListenPort); if(iret != 0) { //连接失败 continue; } iret = ptmpRequest.Send_str(strUrl); if(iret != 0) { continue; } char Rcv_buf[1024]={0}; int iLen = sizeof(Rcv_buf); iret = ptmpRequest.Recv_str(Rcv_buf,&iLen); if(iret !=0) continue; memcpy(outPutUrl,Rcv_buf,iLen); bIsReplyflag = true; outPutUrl[iLen] ='\0'; cJSON *pcmd = NULL; //解析报文数据 replace(Rcv_buf, "XXEE", ""); cJSON* pRoot = cJSON_Parse(Rcv_buf); int iRet = -1; if (pRoot) { pcmd = cJSON_GetObjectItem(pRoot, "cmd"); if (pcmd) { //判断请求类型 if(strcmp(pcmd->valuestring, "reset_device") == 0) { iRet = 0; } } } if(iRet == 0) { LOG_INFO_FORMAT("INFO - [SWM]:切流器[%s] 重置流成功 \n",pFindSeverInfo->strServerIPAddr); pFindSeverInfo->iStreamStatus = 0; pFindSeverInfo->iCurrentSwitchNumber = 0; } else { LOG_INFO_FORMAT("INFO - [SWM]:切流器[%s] 重置流失败 \n",pFindSeverInfo->strServerIPAddr); pFindSeverInfo->iStreamStatus = 1; //流状态异常 } } else printf("---切流器 不在线 %s \n",pFindSeverInfo->strServerIPAddr); ++iterLook; } pthread_mutex_unlock(&ManagerFactory::instance()->m_mutexServerList); if(!bIsReplyflag) { ReplyException(strUrl,outPutUrl,Error_ReSet_Device); LOG_INFO("INFO - [SWM]: Delete ALL stream exception \n"); } return 0; }