⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainserv.c

📁 这是一个进程通信和手机数据处理程序, 与以前发的手机界面一起构成一个简单手机系统,是我们做的第一个版本,供大家一起学习
💻 C
📖 第 1 页 / 共 5 页
字号:
/* DELETEMODE_SINGLEMODE : Delete one record by the given index *//* DELETEMODE_MUTIPLEMODE: Delete  all the records in current context *//* when we choose SINGLEMODE,we shall input the record's key to index *//* when we choose MUTIPLEMODE,we can input any integer value  to index */void *Delete( void *arg ){	FILE *fp;	LOCINFO **p,**srcArray;	COMMINFO **q,**srcArray1;//	int i,j=0,position=0,minIndex;	int *pointer;	long offset;	/************************************Notice*********************************************/	/* Before we delete any records in the file,we shall apply the writer_lock of the file */	/* For it may conflict with the new comming message's file writting action in the file*/	/************************************Notice*********************************************/	printf("Enter Delete now.\n");	switch( deleteArg.mesgType )	{	case 	TYPE_OF_DWXX:				srcArray = (LOCINFO **)deleteArg.arg;		Pthread_mutex_lock( &fLock.DW_FLOCK );		switch( deleteArg.mode )		{		case	SINGLEMODE:			/* Delete one record by the given index */			p = srcArray;			position = (*(p+deleteArg.index-1))->index;			printf("position is %d\n",position);			/* Load the (n+1)th to (msgcount)th records to buffer */			fp = Fopen( "DWXX.data","r+");			offset = (long)(position * LEN_DWXX );			printf("offset is %ld\n",offset);			fseek( fp, offset, SEEK_SET );			for( i= position+1; i<= GetCount( TYPE_OF_DWXX ); i++)				/******Problem here,can't use the wrapper function Fread()*******/
     				if( fread( &( (loadedMesg.locInfo)[j].dwxx), LEN_DWXX, 1, fp) != 1)
     				{
       				if( feof(fp))
       					{
         					Fclose(fp);
         					break;
       					}
       				perror("File read error!");
     				}				else				{					(loadedMesg.locInfo)[j].index = i;					printf("Index is %d.\n",(loadedMesg.locInfo)[j].index);					printf("File content:Hour %d Min %d SRCID %lX\n",loadedMesg.locInfo[j].dwxx.hour,loadedMesg.locInfo[j].dwxx.minute,loadedMesg.locInfo[j].dwxx.srcID );					j++;									}				Fclose( fp );			j=0;			/* Add the previous the (n+1)th to (msgcount)th records to file */			/* After deleting,we async the updated data to the local file   */			fp = Fopen( "DWXX.data","a+");				/* we shall use the truncate() during the time of writting to file */			truncate( "DWXX.data", (position-1)*LEN_DWXX );						offset = (long) ((position-1) *  LEN_DWXX);			fseek( fp, offset, SEEK_SET );			for(  i= position+1; i<= GetCount( TYPE_OF_DWXX ); i++ )				Fwrite( &( (loadedMesg.locInfo)[j++].dwxx),LEN_DWXX,1,fp );			Fclose( fp );				ReduceCount( TYPE_OF_DWXX, 1);						/* Release the current memeory */			UnloadData( INBOX, TYPE_OF_DWXX );			sleep( 5 );			break;			case	MUTIPLEMODE:			/* Delete  all the records in current context */			pointer = (int *)malloc( sizeof(int) * deleteArg.index );			p = srcArray;			minIndex = (*p)->index;			printf("MinIndex is %d\n",minIndex);			/* Record the index of the deleted records in local file */			for( i=0; i<deleteArg.index; i++)			{				if( ( (*p)->index ) < minIndex )				{					minIndex = (*p)->index;				}				pointer[i] = (*p)->index;				printf("pointer[%d] = %d\n",i,pointer[i]);				p++;							}			/*For debug*/for( i=0; i<deleteArg.index; i++)				printf("Deleted NO is: %d\n",pointer[i]);			printf("MinIndex is %d\n",minIndex);				/* Load the minIndex to (msgcount)th records to buffer */			fp = Fopen( "DWXX.data","r+");			offset = (long)((minIndex-1) * LEN_DWXX );			printf("offset is %ld\n",offset);			fseek( fp, offset, SEEK_SET );			for( i= minIndex; i<= GetCount( TYPE_OF_DWXX ); i++)				/******Problem here,can't use the wrapper function Fread()*******/
     				if( fread( &( (loadedMesg.locInfo)[j].dwxx), LEN_DWXX, 1, fp) != 1)
     				{
       				if( feof(fp))
       					{
         					Fclose(fp);
         					break;
       					}
       				perror("File read error!");
     				}				else				{					(loadedMesg.locInfo)[j].index = i;					printf("Index is %d.\n",(loadedMesg.locInfo)[j].index);					printf("File content:Hour %d Min %d SRCID %lX\n",loadedMesg.locInfo[j].dwxx.hour,loadedMesg.locInfo[j].dwxx.minute,loadedMesg.locInfo[j].dwxx.srcID );					j++;									}				Fclose( fp );							/* Add the non_deleted records to file */			fp = Fopen( "DWXX.data","a+");			/* we shall use the truncate() during the time of writting to file */			truncate( "DWXX.data", (minIndex-1)*LEN_DWXX );						offset = (long) ((minIndex-1) *  LEN_DWXX);			fseek( fp, offset, SEEK_SET );			for(  i=0; i<GetCount(TYPE_OF_DWXX)-minIndex+1; i++ )			{				for( j=0; j<deleteArg.index; j++)					if( (loadedMesg.locInfo)[i].index == pointer[j] )						break;				if( j == deleteArg.index )				{					Fwrite( &( (loadedMesg.locInfo)[i].dwxx),LEN_DWXX,1,fp );					printf("Add %dth message.\n",i);				}			}			Fclose( fp );			/* Release all the deserted memory */			free( pointer );			ReduceCount( TYPE_OF_DWXX, deleteArg.index);			UnloadData( INBOX, TYPE_OF_DWXX );			sleep( 5 );			break;			}		/* After updating data,we shall unlock the file DWXX_F's lock */		Pthread_mutex_unlock( &fLock.DW_FLOCK );		break;	case 	TYPE_OF_TXXX:		if( deleteArg.sType == INBOX )		{			srcArray1 = (COMMINFO **)deleteArg.arg;			printf("arg address is  %X %X\n",deleteArg.arg,srcArray1);			Pthread_mutex_lock( &fLock.TXINDEX_FLOCK );			switch( deleteArg.mode )			{			case	SINGLEMODE:				/* Delete one record by the given index */				q = srcArray1;				position = (*(q+deleteArg.index-1))->index;				/* Put the big data block's file index to circular queue */				if( (*(q+deleteArg.index-1))->txIndex.mainIndex != -1)				{					if( !ENQUEUE( INBOX, (*(q+deleteArg.index-1))->txIndex.mainIndex ) )						printf("Fail to enter for the full queue\n");					printf("MainIndex is %d\n",(*(q+deleteArg.index-1))->txIndex.mainIndex);				}					printf("position is %d\n",position);				/* Load the (n+1)th to (msgcount)th records to buffer */				fp = Fopen( "TXINDEX.data","r+");				offset = (long)(position * LEN_TXINDEX );				printf("offset is %ld\n",offset);				fseek( fp, offset, SEEK_SET );				for( i= position+1; i<= GetCount( TYPE_OF_TXXX ); i++)					/******Problem here,can't use the wrapper function Fread()*******/
     					if( fread( &( (loadedMesg.commInfo)[j].txIndex), LEN_TXINDEX, 1, fp) != 1)	
     					{
       					if( feof(fp))
       						{
         						Fclose(fp);
         						break;
       						}
       					perror("File read error!");
     					}					else					{						(loadedMesg.commInfo)[j].index = i;						printf("Index is %d.\n",(loadedMesg.commInfo)[j].index);						printf("File content:Hour %d Min %d SRCID %lX\n",loadedMesg.commInfo[j].txIndex.hour,loadedMesg.commInfo[j].txIndex.minute,loadedMesg.commInfo[j].txIndex.srcID );						j++;										}					Fclose( fp );				j=0;				/* Add the previous the (n+1)th to (msgcount)th records to file */				/* After deleting,we async the updated data to the local file   */				fp = Fopen( "TXINDEX.data","a+");						/* we shall use the truncate() during the time of writting to file */				truncate( "TXINDEX.data", (position-1)*LEN_TXINDEX );							offset = (long) ((position-1) *  LEN_TXINDEX);				fseek( fp, offset, SEEK_SET );				for(  i= position+1; i<= GetCount( TYPE_OF_TXXX ); i++ )					Fwrite( &( (loadedMesg.commInfo)[j++].txIndex),LEN_TXINDEX,1,fp );				Fclose( fp );					ReduceCount( TYPE_OF_TXXX, 1);							/* Release the current memeory */				UnloadData( INBOX, TYPE_OF_TXXX );				sleep( 5 );				break;				case	MUTIPLEMODE:				/* Delete  all the records in current context */					pointer = (int *)malloc( sizeof(int) * deleteArg.index );				q = srcArray1;				minIndex = (*q)->index;				printf("MinIndex is %d\n",minIndex);				/* Record the index of the deleted records in local file */				for( i=0; i<deleteArg.index; i++)				{					if( ( (*q)->index ) < minIndex )					{						minIndex = (*q)->index;					}					pointer[i] = (*q)->index;					printf("pointer[%d] = %d\n",i,pointer[i]);						q++;									}				/*For debug*/for( i=0; i<deleteArg.index; i++)					printf("Deleted NO is: %d\n",pointer[i]);				printf("MinIndex is %d\n",minIndex);						/* Load the minIndex to (msgcount)th records to buffer */				fp = Fopen( "TXINDEX.data","r+");				offset = (long)((minIndex-1) * LEN_TXINDEX );				printf("offset is %ld\n",offset);				fseek( fp, offset, SEEK_SET );				for( i= minIndex; i<= GetCount( TYPE_OF_TXXX ); i++)					/******Problem here,can't use the wrapper function Fread()*******/
     					if( fread( &( (loadedMesg.commInfo)[j].txIndex), LEN_TXINDEX, 1, fp) != 1)
     					{
       					if( feof(fp))
       						{
         						Fclose(fp);
         						break;
       						}
       					perror("File read error!");
     					}					else					{						(loadedMesg.commInfo)[j].index = i;						printf("Index is %d.\n",(loadedMesg.commInfo)[j].index);						printf("File content:Hour %d Min %d SRCID %lX\n",loadedMesg.commInfo[j].txIndex.hour,loadedMesg.commInfo[j].txIndex.minute,loadedMesg.commInfo[j].txIndex.srcID );						j++;										}					Fclose( fp );								/* Add the non_deleted records to file */				fp = Fopen( "TXINDEX.data","a+");				/* we shall use the truncate() during the time of writting to file */				truncate( "TXINDEX.data", (minIndex-1)*LEN_TXINDEX );								offset = (long) ((minIndex-1) *  LEN_TXINDEX);				fseek( fp, offset, SEEK_SET );				for(  i=0; i<GetCount(TYPE_OF_TXXX)-minIndex+1; i++ )				{					for( j=0; j<deleteArg.index; j++)						if( (loadedMesg.commInfo)[i].index == pointer[j] )						{							/* Put the big data block's file index to circular queue */							if( (loadedMesg.commInfo)[i].txIndex.mainIndex != -1)							{								if( !ENQUEUE( INBOX, (loadedMesg.commInfo)[i].txIndex.mainIndex ))									printf("Fail to enter for the full queue\n");								printf("MainIndex is %d\n",(loadedMesg.commInfo)[i].txIndex.mainIndex);							}												break;						}					if( j == deleteArg.index )					{						Fwrite( &( (loadedMesg.commInfo)[i].txIndex),LEN_TXINDEX,1,fp );						printf("Add %dth message.\n",i);					}				}				Fclose( fp );				/* Release all the deserted memory */				free( pointer );				ReduceCount( TYPE_OF_TXXX, deleteArg.index);				UnloadData( INBOX, TYPE_OF_TXXX );				sleep( 5 );				break;				}			/* After updating data,we shall unlock the file DWXX_F's lock */			Pthread_mutex_unlock( &fLock.TXINDEX_FLOCK );		}		else if( deleteArg.sType == OUTBOX )		{			srcArray1 = (COMMINFO **)deleteArg.arg;			switch( deleteArg.mode )			{			case	SINGLEMODE:								q = srcArray1;				position = (*(q+deleteArg.index-1))->index;				/* Put the big data block's file index to circular queue */				if( (*(q+deleteArg.index-1))->txIndex.mainIndex != -1)				{					if( !ENQUEUE( OUTBOX, (*(q+deleteArg.index-1))->txIndex.mainIndex ) )						printf("Fail to enter for the full queue\n");					printf("MainIndex is %d\n",(*(q+deleteArg.index-1))->txIndex.mainIndex);				}					printf("position is %d\n",position);				/* Load the (n+1)th to (msgcount)th records to buffer */				fp = Fopen( "TXINDEX_OUT.data","r+");				offset = (long)(position * LEN_TXINDEX );				printf("offset is %ld\n",offset);				fseek( fp, offset, SEEK_SET );				for( i= position+1; i<= countTXIDXOut; i++)					/******Problem here,can't use the wrapper function Fread()*******/
     					if( fread( &( (loadedMesg.commInfoOut)[j].txIndex), LEN_TXINDEX, 1, fp) != 1)	
     					{
       					if( feof(fp))
       						{
         						Fclose(fp);
         						break;
       						}
       					perror("File read error!");
     					}					else					{						(loadedMesg.commInfoOut)[j].index = i;						printf("Index is %d.\n",(loadedMesg.commInfoOut)[j].index);						printf("File content:Hour %d Min %d SRCID %lX\n",loadedMesg.commInfoOut[j].txIndex.hour,loadedMesg.commInfoOut[j].txIndex.minute,loadedMesg.commInfoOut[j].txIndex.srcID );						j++;										}					Fclose( fp );				j=0;				/* Add the previous the (n+1)th to (msgcount)th records to file */				/* After deleting,we async the updated data to the local file   */				fp = Fopen( "TXINDEX_OUT.data","a+");						/* we shall use the truncate() during the time of writting to file */				truncate( "TXINDEX_OUT.data", (position-1)*LEN_TXINDEX );							offset = (long) ((position-1) *  LEN_TXINDEX);				fseek( fp, offset, SEEK_SET );				for(  i= position+1; i<= countTXIDXOut; i++ )					Fwrite( &( (loadedMesg.commInfoOut)[j++].txIndex),LEN_TXINDEX,1,fp );				Fclose( fp );					countTXIDXOut--;							/* Release the current memeory */				UnloadData( OUTBOX, TYPE_OF_TXXX );				sleep( 3 );				break;			case   MUTIPLEMODE:								/* Delete all current existed data records in the file */				fp = Fopen( "TXINDEX_OUT.data","a+");				/* we shall use the truncate() during the time of writting to file */				truncate( "TXINDEX_OUT.data", 0);				Fclose( fp );					countTXIDXOut = 0;;				UnloadData( OUTBOX, TYPE_OF_TXXX );											break;			}		}		break;		}//End of switch 		tidCount--;	return(NULL); }void **Filtrate( MESGTYPE mesgType, int megCount, FILTER filter, QueryFilter queFilter, void **arg ){	int i;	LOCINFO 	**p,**q,**t,**srcArray;	COMMINFO	**x,**y,**z,**srcArray1;		printf("Enter Filtrate now.\n");	if( arg == NULL )		err_sys("Error occurred-> Empty records in memory!");		switch( mesgType )	{	case 	TYPE_OF_DWXX:		srcArray = (LOCINFO **)arg;		switch( filter )		{		case	BY_LOCADDRSS: 			/* Search the records by the given source ID */			/* Mark the non-fixed records' pointer to NULL*/			for( p=srcArray; p< srcArray+megCount; p++)				if( (*p)->dwxx.srcID != queFilter.srcID )				{					*p = NULL;									}				else				{					fRecords++;					printf("Found records' address is %X\n",*p);				}				printf("Found records is %d\n",fRecords);			if( fRecords != 0 )/* Fixed records are found */			{				t = srcArray;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -