Beispiel #1
0
int main(int argc, char *argv[])
{
    char buffer[512], prog_name[512], sTemp[256];
    int  nret, i, share_size, count;
    int  shm_id;
    int  shm_need_init;
    int  bRet;

    /*检测程序启动参数是否正确*/
    if (CheckParam(argc, argv) == -1)
    {
        return 0;
    }

    strcpy(buffer, argv[0]);
    split_str(buffer, prog_name, "/", 1, 1, 1, 1);

    /*检测程序是否已经在后台运行*/
    if (strcmp(argv[1], "start") != 0)
    {
        if ( !if_program_running(prog_name) )
        {
            WriteLog(0, 0, OUT_SCREEN, "系统尚未启动");
            return 0;
        }
    }

    WriteLog(0, 0, OUT_SCREEN, "读取配置文件 ... ");
    if (GetInitInfo(argv[0]) == -1)
    {
        return 0;
    }
    /*WriteLog(0, 0, OUT_SCREEN, "OK !");*/

    if (strcmp(G_ini.cur_version, CURRENT_SERVER_VERSION) != 0)
    {
        WriteLog(0, 0, OUT_SCREEN, "程序版本错误,禁止运行!");
        return 0;
    }

    G_shm = NULL;
    G_ctrl_data = NULL;
    G_trans_buffer = NULL;

    WriteLog(0, 0, OUT_SCREEN, "读取共享内存 ... ");
    shm_need_init = 0;
    if (strcmp(argv[1], "start") == 0 && argc == 2)
    {
        if (atoi(sTemp) > 1)
        {
            WriteLog(0, 0, OUT_SCREEN, "系统已经执行完成该命令");
            return 0;
        }

        share_size = G_ini.proc_num * ((int)sizeof(struct SHM_CONF)) + (int)sizeof(struct SHM_HEAD);
        shm_id = shmget((key_t)G_ini.shm_key, (size_t)share_size, IPC_CREAT | IPC_EXCL | 0777);
        shm_need_init = 1;

        bRet = sem_requ(G_ini.sem_key, 1, 1);
    }
    else
    {
        shm_id = shmget((key_t)G_ini.shm_key, 0, 0777);
        bRet = sem_get(G_ini.sem_key, 1);
    }
    if (shm_id < 0)
    {
        /*EEXIST 17 File exists*/
        WriteLog(0, 0, OUT_SCREEN, " shmget error(%d):%s", errno, strerror(errno));
        FreeShm(shm_id);
        return 0;
    }
    if (bRet == SEM_ERROR)
    {
        WriteLog(0, 0, OUT_SCREEN, " semget error:%s", gs_sem_err);
        FreeShm(shm_id);
        return 0;
    }

    G_shm = (char *)shmat(shm_id, 0, 0);
    if (G_shm == (void *)-1)
    {
        WriteLog(0, 0, OUT_SCREEN, " shmat error(%d):%s", errno, strerror(errno));
        FreeShm(shm_id);
        return 0;
    }

    if (shm_need_init == 1)
    {
        /*共享内存初试化*/
        InitShm();
    }
    else
    {
        if (ShmConfCheck() == -1)
        {
            WriteLog(0, 0, OUT_SCREEN, "INI文件在启动之后被修改过,禁止执行当前指令!");
            if (shm_need_init == 1)
            {
                FreeShm(shm_id);
            }
            return 0;
        }
    }

    /*WriteLog(0, 0, OUT_SCREEN, "OK !");*/

    if (strcmp(argv[1], "stop") == 0 || strcmp(argv[1], "query") == 0 || strcmp(argv[1], "refresh") == 0)
    {
        strcpy(buffer, argv[0]);
        split_str(buffer, prog_name, "/", 1, 1, 1, 1);

        if (strcmp(argv[1], "query") == 0)
        {
            QueryClient(prog_name);
        }

        if (strcmp(argv[1], "refresh") == 0)
        {
            if (argc != 3)
            {
                RefreshParam(prog_name, 0);
            }
            else
            {
                RefreshParam(prog_name, atoi(argv[2]));
            }
        }

        if (strcmp(argv[1], "stop") == 0)
        {
            WriteLog(0, 0, OUT_SCREEN, "server stop ...");
            if (argc != 3)
            {
                StopClient(prog_name, 0);
            }
            else
            {
                StopClient(prog_name, atoi(argv[2]));
            }
        }

        if (strcmp(argv[1], "refresh") != 0)
        {
            if (GetRunCount() == 0)
            {
                /*WriteLog(0, 0, OUT_SCREEN, "释放资源 ...");*/
                FreeShm(shm_id);
            }
        }
        return 0;
    }




    /****************************************************************************************/
    count = 0;

    for (i = 0 ; i < G_ini.port_num ; i++)
    {

        if (argc != 2)
        {
            if (atoi(argv[2]) != G_ini.port_list[i].lsnr_port)
            {
                continue;
            }
        }

        WriteLog(0, 0, OUT_SCREEN, "\n启动:%s接口,port=[%d]", G_ini.port_list[i].name, G_ini.port_list[i].lsnr_port);
        nret = StartPort(G_ini.port_list[i].lsnr_port, G_ini.port_list[i].lsnr_num, G_ini.port_list[i].lsnr_len, G_ini.port_list[i].is_reuse_addr);

        FreeResource();
        if (nret < 0)
        {
            continue;
        }

        count++;
    }

    if (count == 0)
    {
        WriteLog(0, 0, OUT_SCREEN, "系统启动失败,释放系统资源!");
        FreeShm(shm_id);
    }
    else
    {
        WriteLog(0, 0, OUT_SCREEN, "系统启动指令执行完成!");
        sleep(2);
        if (GetRunCount() == 0)
        {
            WriteLog(0, 0, OUT_SCREEN, "进程启动失败");
            FreeShm(shm_id);
        }
    }

    return 0;
}
Beispiel #2
0
int Classify(int *newBeat,int rr, int noiseLevel, int *beatMatch, int *fidAdj,int *QrsOn,int *QrsOff,
	int init)
	{
	int rhythmClass, beatClass, i, beatWidth, blShift ;
	static int morphType, runCount = 0 ;
	double matchIndex, domIndex, mi2 ;
	int shiftAdj ;
	int domType, domWidth, onset, offset, amp ;
	int beatBegin, beatEnd, tempClass ;
	int hfNoise, isoLevel ;
	static int lastIsoLevel=0, lastRhythmClass = UNKNOWN, lastBeatWasNew = 0 ;

	// If initializing...
	
	if(init)
		{
		ResetRhythmChk() ;
		ResetMatch() ;
		ResetPostClassify() ;
		runCount = 0 ;
		DomMonitor(0, 0, 0, 0, 1) ;
		return(0) ;
		}

	hfNoise = HFNoiseCheck(newBeat) ;	// Check for muscle noise.
	rhythmClass = RhythmChk(rr) ;			// Check the rhythm.

	// Estimate beat features.


	AnalyzeBeat(newBeat, &onset, &offset, &isoLevel,
		&beatBegin, &beatEnd, &amp) ;
	
	*QrsOn=onset;
	*QrsOff=offset;

	blShift = abs(lastIsoLevel-isoLevel) ;
	lastIsoLevel = isoLevel ;

	// Make isoelectric level 0.

	for(i = 0; i < BEATLGTH; ++i)
		newBeat[i] -= isoLevel ;

	// If there was a significant baseline shift since
	// the last beat and the last beat was a new type,
	// delete the new type because it might have resulted
	// from a baseline shift.

	if( (blShift > BL_SHIFT_LIMIT)
		&& (lastBeatWasNew == 1)
		&& (lastRhythmClass == NORMAL)
		&& (rhythmClass == NORMAL) )
		ClearLastNewType() ;

	lastBeatWasNew = 0 ;

	// Find the template that best matches this beat.

	BestMorphMatch(newBeat,&morphType,&matchIndex,&mi2,&shiftAdj) ;

	// Disregard noise if the match is good. (New)

	if(matchIndex < MATCH_NOISE_THRESHOLD)
		hfNoise = noiseLevel = blShift = 0 ;

	// Apply a stricter match limit to premature beats.

	if((matchIndex < MATCH_LIMIT) && (rhythmClass == PVC) &&
		MinimumBeatVariation(morphType) && (mi2 > PVC_MATCH_WITH_AMP_LIMIT))
		{
		morphType = NewBeatType(newBeat) ;
		lastBeatWasNew = 1 ;
		}

	// Match if within standard match limits.

	else if((matchIndex < MATCH_LIMIT) && (mi2 <= MATCH_WITH_AMP_LIMIT))
		UpdateBeatType(morphType,newBeat,mi2,shiftAdj) ;

	// If the beat isn't noisy but doesn't match, start a new beat.

	else if((blShift < BL_SHIFT_LIMIT) && (noiseLevel < NEW_TYPE_NOISE_THRESHOLD)
		&& (hfNoise < NEW_TYPE_HF_NOISE_LIMIT))
		{
		morphType = NewBeatType(newBeat) ;
		lastBeatWasNew = 1 ;
		}

	// Even if it is a noisy, start new beat if it was an irregular beat.

	else if((lastRhythmClass != NORMAL) || (rhythmClass != NORMAL))
		{
		morphType = NewBeatType(newBeat) ;
		lastBeatWasNew = 1 ;
		}

	// If its noisy and regular, don't waste space starting a new beat.

	else morphType = MAXTYPES ;

	// Update recent rr and type arrays.

	for(i = 7; i > 0; --i)
		{
		RecentRRs[i] = RecentRRs[i-1] ;
		RecentTypes[i] = RecentTypes[i-1] ;
		}
	RecentRRs[0] = rr ;
	RecentTypes[0] = morphType ;

	lastRhythmClass = rhythmClass ;
	lastIsoLevel = isoLevel ;

	// Fetch beat features needed for classification.
	// Get features from average beat if it matched.

	if(morphType != MAXTYPES)
		{
		beatClass = GetBeatClass(morphType) ;
		beatWidth = GetBeatWidth(morphType) ;
		*fidAdj = GetBeatCenter(morphType)-FIDMARK ;

		// If the width seems large and there have only been a few
		// beats of this type, use the actual beat for width
		// estimate.

		if((beatWidth > offset-onset) && (GetBeatTypeCount(morphType) <= 4))
			{
			beatWidth = offset-onset ;
			*fidAdj = ((offset+onset)/2)-FIDMARK ;
			}
		}

	// If this beat didn't match get beat features directly
	// from this beat.

	else
		{
		beatWidth = offset-onset ;
		beatClass = UNKNOWN ;
		*fidAdj = ((offset+onset)/2)-FIDMARK ;
		}

	// Fetch dominant type beat features.

	DomType = domType = DomMonitor(morphType, rhythmClass, beatWidth, rr, 0) ;
	domWidth = GetBeatWidth(domType) ;

	// Compare the beat type, or actual beat to the dominant beat.

	if((morphType != domType) && (morphType != 8))
		domIndex = DomCompare(morphType,domType) ;
	else if(morphType == 8)
		domIndex = DomCompare2(newBeat,domType) ;
	else domIndex = matchIndex ;

	// Update post classificaton of the previous beat.

	PostClassify(RecentTypes, domType, RecentRRs, beatWidth, domIndex, rhythmClass) ;

	// Classify regardless of how the morphology
	// was previously classified.

	tempClass = TempClass(rhythmClass, morphType, beatWidth, domWidth,
		domType, hfNoise, noiseLevel, blShift, domIndex) ;

	// If this morphology has not been classified yet, attempt to classify
	// it.

	if((beatClass == UNKNOWN) && (morphType < MAXTYPES))
		{

		// Classify as normal if there are 6 in a row
		// or at least two in a row that meet rhythm
		// rules for normal.

		runCount = GetRunCount() ;

		// Classify a morphology as NORMAL if it is not too wide, and there
		// are three in a row.  The width criterion prevents ventricular beats
		// from being classified as normal during VTACH (MIT/BIH 205).

		if((runCount >= 3) && (domType != -1) && (beatWidth < domWidth+BEAT_MS20))
			SetBeatClass(morphType,NORMAL) ;

		// If there is no dominant type established yet, classify any type
		// with six in a row as NORMAL.

		else if((runCount >= 6) && (domType == -1))
			SetBeatClass(morphType,NORMAL) ;

		// During bigeminy, classify the premature beats as ventricular if
		// they are not too narrow.

		else if(IsBigeminy() == 1)
			{
			if((rhythmClass == PVC) && (beatWidth > BEAT_MS100))
				SetBeatClass(morphType,PVC) ;
			else if(rhythmClass == NORMAL)
				SetBeatClass(morphType,NORMAL) ;
			}
		}

	// Save morphology type of this beat for next classification.

	*beatMatch = morphType ;

	beatClass = GetBeatClass(morphType) ;
   
	// If the morphology has been previously classified.
	// use that classification.
  //	return(rhythmClass) ;

	if(beatClass != UNKNOWN)
		return(beatClass) ;

	if(CheckPostClass(morphType) == PVC)
		return(PVC) ;

	// Otherwise use the temporary classification.

	return(tempClass) ;
	}