📄 mainserv.c
字号:
/********???????what is data source ?????????*****/ break; } } }/* Sort the records by ordered condition */void Sort( SORTTYPE sortType, LOCINFO **srcArray, int arryLen ){ int i,j; LOCINFO **p; switch( sortType ) { case SORT_BY_ADDRESS: /* Sort the records by source user ID */ j = arryLen-1; /*printf(" Offset value i is %d\n",j); printf(" SrcArray address is %X\n",srcArray); p= srcArray; printf(" Address of the address array head is %lX.\n",p ); for( i=0; i<GetCount( TYPE_OF_DWXX ); i++) { printf(" Address of the message pointer %d is %lX.\n",i,*p ); p++; }*/ printf("Enter QSort now.\n"); QSort( SORT_BY_ADDRESS, srcArray, srcArray+j ); for( p= srcArray; p<=srcArray+j; p++ ) printf("After sorted:Hour %d Min %d SRCID %lX\n",(*p)->dwxx.hour,(*p)->dwxx.minute,(*p)->dwxx.srcID ); break; case SORT_BY_TIMEASC: printf("Enter QSort TIMEASC now.\n"); /* Sort the records by time in the ascend order */ QSort( SORT_BY_TIMEASC, srcArray, srcArray + arryLen-1 ); break; case SORT_BY_TIMEDES: printf("Enter QSort TIMEDES now.\n"); /* Sort the records by time in the descend order */ QSort( SORT_BY_TIMEDES, srcArray, srcArray + arryLen-1 ); break; case SORT_BY_SOURCE: break; }}/* 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( SOURCETYPE sType, MESGTYPE mesgType, DELETEMODE mode, int index, void **arg ){ FILE *fp; LOCINFO **p,**srcArray; COMMINFO **q,**srcArray1;// int i,j=0,k = 0,m=0,position=0,minIndex,total=0; 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( mesgType ) { case TYPE_OF_DWXX: srcArray = (LOCINFO **)arg; Pthread_mutex_lock( &fLock.DW_FLOCK ); total = GetCount( INBOX, TYPE_OF_DWXX ); switch( mode ) { case SINGLEMODE: /* Delete one record by the given index */ p = srcArray; position = (*(p+index-1))->index; printf("Delete 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<= total; 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<= total; i++ ) Fwrite( &( (loadedMesg.locInfo)[j++].dwxx),LEN_DWXX,1,fp ); Fclose( fp ); ReduceCount( INBOX, TYPE_OF_DWXX, 1); break; case MUTIPLEMODE: /* Delete all the records in current context */ pointer = (int *)malloc( sizeof(int) * 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<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<index; i++) printf("Deleted NO is: %d\n",pointer[i]); printf("MinIndex is %d\n",minIndex); UnloadData( INBOX, TYPE_OF_DWXX ); loadedMesg.locInfo = (LOCINFO *)malloc( LEN_LOCINFO * (GetCount(INBOX, TYPE_OF_DWXX)-minIndex+1) ); /* 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(INBOX, 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(INBOX, TYPE_OF_DWXX)-minIndex+1; i++ ) { for( j=0; j<index; j++) if( (loadedMesg.locInfo)[i].index == pointer[j] ) break; if( j == 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( INBOX, TYPE_OF_DWXX, index); break; } /* After updating data,we shall unlock the file DWXX_F's lock */ //sleep( 3 ); Pthread_mutex_unlock( &fLock.DW_FLOCK ); break; case TYPE_OF_TXXX: if( sType == INBOX ) { srcArray1 = (COMMINFO **)arg; printf("arg address is %X %X\n",arg,srcArray1); Pthread_mutex_lock( &fLock.TXINDEX_FLOCK ); total = GetCount( INBOX, TYPE_OF_TXXX ); switch( mode ) { case SINGLEMODE: /* Delete one record by the given index */ q = srcArray1; position = (*(q+index-1))->index; /* Put the big data block's file index to circular queue */ if( ( *(q+index-1) )->txIndex.mainIndex != -1) { if( !ENQUEUE( INBOX, (*(q+index-1))->txIndex.mainIndex ) ) printf("Fail to enter for the full queue\n"); printf("MainIndex is %d\n",(*(q+index-1))->txIndex.mainIndex); } printf("position is %d\n",position); if( ( *(q+index-1) )->txIndex.lockSymbol== 0) { ReduceCountUnRead( 1 ); } /* Load the (n+1)th to (msgcount)th records to buffer */ offset = (long)(position * LEN_TXINDEX ); printf("offset is %ld\n",offset); fp = Fopen( "TXINDEX.data","r+"); fseek( fp, offset, SEEK_SET ); for( i= position+1; i<= total; 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<= total; i++ ) Fwrite( &( (loadedMesg.commInfo)[j++].txIndex),LEN_TXINDEX,1,fp ); Fclose( fp ); ReduceCount( INBOX, TYPE_OF_TXXX, 1); Pthread_mutex_lock(&numOverFlow_Mutex); numOverFlow--; Pthread_mutex_unlock(&numOverFlow_Mutex); break; case MUTIPLEMODE: /* Delete all the records in current context */ pointer = (int *)malloc( sizeof(int) * 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<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<index; i++) printf("Deleted NO is: %d\n",pointer[i]); printf("MinIndex is %d\n",minIndex); UnloadData( INBOX, TYPE_OF_TXXX ); loadedMesg.commInfo = (COMMINFO *)malloc( LEN_COMMINFO * (GetCount(INBOX, TYPE_OF_TXXX)-minIndex+1) ); /* 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( INBOX, 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(INBOX, TYPE_OF_TXXX)-minIndex+1; i++ ) { printf("index = %d\n",index); for( j=0; j<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); } if((loadedMesg.commInfo)[i].txIndex.lockSymbol== 0 ) m++; //the count of unread ones which to be deleted break; } printf("j= %d\n ",j); if( (j == index) || ((loadedMesg.commInfo)[i].txIndex.lockSymbol== 1 )) { Fwrite( &( (loadedMesg.commInfo)[i].txIndex),LEN_TXINDEX,1,fp ); k++; printf("Add %dth message.\n",i); printf("Add %dth message.\n",k); } } Fclose( fp ); /* Release all the deserted memory */ free( pointer ); ResetCount( INBOX, TYPE_OF_TXXX, k); ReduceCountUnRead( m ); Pthread_mutex_lock(&numOverFlow_Mutex); numOverFlow = k; Pthread_mutex_unlock(&numOverFlow_Mutex); break; } /* After updating data,we shall unlock the file TXXX_F's lock */ //sleep( 10); Pthread_mutex_unlock( &fLock.TXINDEX_FLOCK ); } else if( sType == OUTBOX ) { srcArray1 = (COMMINFO **)arg; switch( mode ) { case SINGLEMODE: q = srcArray1; position = (*(q+index-1))->index; /* Put the big data block's file index to circular queue */ if( ( *(q+index-1) )->txIndex.mainIndex != -1) { if( !ENQUEUE( OUTBOX, (*(q+index-1))->txIndex.mainIndex ) ) printf("Fail to enter for the full queue\n"); printf("MainIndex is %d\n",(*(q+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<= GetCount(OUTBOX, TYPE_OF_TXXX); 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<= GetCount(OUTBOX, TYPE_OF_TXXX); i++ ) Fwrite( &( (loadedMesg.commInfoOut)[j++].txIndex),LEN_TXINDEX,1,fp ); Fclose( fp ); ReduceCount( OUTBOX, TYPE_OF_TXXX, 1); break; case MUTIPLEMODE: k =0; truncate( "TXINDEX_OUT.data", 0); fp = Fopen( "TXINDEX_OUT.data","a+"); for( i=0; i<index ; i++ ) if( (*(loadedMesg.pCommInfoOut+i))->txIndex.lockSymbol == 1 ) { Fwrite( &( (*(loadedMesg.pCommInfoOut+i))->txIndex),LEN_TXINDEX,1,fp ); k++; } Fclose( fp ); ResetCount( OUTBOX, TYPE_OF_TXXX, k); break; } } break; }//End of switch tidCount--;}/* Submit the inquery for the feedback message by given locAddress */void QueryFeedBack( int mesgCount, QueryFilter queFilter )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -