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

📄 shm.c

📁 基于UNIX的中间件编程,只能运行于UNIX操作系统
💻 C
📖 第 1 页 / 共 2 页
字号:
{	if((WriteBlock(I_Index,I_Partion[PartionNumber],I_Pointer[PartionNumber],content))!=0)		return NULL;	else		return content;}int InitOutputSharedMem(){	int rc;	key_t O_Key;		O_Index=InitIndex(O_StartAddress,O_PARTIONS,O_Partion);/*	O_Key=CreatShmKey(O_MAPPINGFILENAME,'1');*/	rc=InitShareMem(&O_StartAddress,O_KEY,O_Index.TotalValue,0777);	if (rc<0)		return -1;			(void)SetIndexStartAddress(&O_Index,O_StartAddress);	if((rc=InitSuperNode(O_Partion[0].TotalBlocks,&O_SuperNode,O_StartAddress))!=0)		if((rc=GetSuperNode(O_Partion[0].TotalBlocks,&O_SuperNode,O_StartAddress))!=0)		{			printf("cannot open the shared memory's super node information\n");			return -1;		}	(void)AllocatePointer(O_StartAddress,O_Partion,O_Index,O_Pointer);	return 0;}int OpenOutputSharedMem(){	int rc;	key_t O_Key;		O_Index=InitIndex(O_StartAddress,O_PARTIONS,O_Partion);	/*	O_Key=CreatShmKey(O_MAPPINGFILENAME,'1');	rc=InitShareMem(&O_StartAddress,O_KEY,O_Index.TotalValue,0777);	*/	rc=OpenShareMem(&O_StartAddress,O_KEY,O_Index.TotalValue,0777);	if (rc<0)		return -1;	(void)SetIndexStartAddress(&O_Index,O_StartAddress);	/*	if((rc=InitSuperNode(O_Partion[0].TotalBlocks,&O_SuperNode,O_StartAddress))!=0)	*/		rc=GetSuperNode(O_Partion[0].TotalBlocks,&O_SuperNode,O_StartAddress);	if(rc<0)	{		printf("cannot get the shared memory's super node information\n");		return -1;	}	(void)AllocatePointer(O_StartAddress,O_Partion,O_Index,O_Pointer);	return 0;}char *ReadOutputSharedMem(int PartionNumber){	char temp[MAXLEN];	char *content=temp;	if((ReadBlock(O_Index,O_Partion[PartionNumber],O_Pointer[PartionNumber],&content))!=0)		return NULL;	else		return content;}char *WriteOutputSharedMem(int PartionNumber,char *content){	if((WriteBlock(O_Index,O_Partion[PartionNumber],O_Pointer[PartionNumber],content))!=0)		return NULL;	else		return content;}sm_TID *sm_OpenConn(sm_TID *tid){	int rc;	if(tid->flag ==0)	{		rc=InitInputSharedMem();		if(rc!=0)		{			tid->errorno=E_Error;			sprintf(tid->errorcause,"%s","cannot init the Input shared memory");		}		else		{			tid->errorno=E_Success;			sprintf(tid->errorcause,"%s","init the input shared memory");		}	}	else	{		rc=InitOutputSharedMem();		if(rc!=0)		{			tid->errorno=E_Error;			sprintf(tid->errorcause,"%s","cannot init the output shared memory");		}		else		{			tid->errorno=E_Success;			sprintf(tid->errorcause,"%s","init the output shared memory");		}		return tid;	}	return tid;}sm_TID *sm_CloseConn(sm_TID *tid){	if(tid->flag ==0)		ShmClose(I_StartAddress);	else		ShmClose(O_StartAddress);	return tid;}sm_TID *sm_SendMsg(int flag,char *buf,int bussize,char *filename,sm_TID *tid ){	int i=0;	char source[5];	char destination[5];	int oprcode;	S_MESSAGESTRUCTURE ms;	char message[512];	S_SeparateHostName(tid->source,".",source);	source[4]='\0';	S_SeparateHostName(tid->destination ,".",destination);	destination[4]='\0';		/*init the structure */	/*fromat the packet*/	memset(&ms,'\0',sizeof(ms));	strcpy(ms.packettype,tid->packettype);	sprintf(ms.sn,"%010s",tid->ID);	strcpy(ms.requesttype,tid->requesttype);	strcpy(ms.oprcode,tid->oprcode);	strcpy(ms.source,(char *)source);	strcpy(ms.destination,(char *)destination);	if(flag==0)/*http tramsmit file*/		strcpy(ms.message ,filename);	else       /*UDP transmit message*/		strcpy(ms.message,buf);	memset(message,'\0',sizeof(message));	S_FormatePacket(ms,message);		oprcode=atoi(tid->oprcode );/*	buf=WriteOutputSharedMem(O_PARTIONNO,message);*/	buf=WriteOutputSharedMem(tid->partionnumbers ,message);	if(buf==NULL)	{		tid->errorno =E_Error;		sprintf(tid->errorcause ,"%s","cannot write to shared memeory,maybe no room");	}	else	{		tid->errorno =E_Success;		sprintf(tid->errorcause ,"%s","write to shared memory success");	}	return tid;}sm_TID *sm_RecieveMsg(int flag,char **buf,int bufsize,char **filename,sm_TID *tid){	int oprcode=atoi(tid->oprcode );	if (flag==1)/*recive message*/	{		*buf=ReadInputSharedMem(tid->partionnumbers );		if(*buf==NULL)		{			tid->errorno =E_Error;			sprintf(tid->errorcause ,"%s","Cannot read from shared memoey,maybe error occured");			return tid;		}	}	else/*recive file*/	{		*filename=ReadInputSharedMem(tid->partionnumbers);		if(*filename==NULL)		{			tid->errorno =E_Error;			sprintf(tid->errorcause ,"%s","Cannot read from shared memoey,maybe error occured");			return tid;		}	}	tid->errorno =E_Success;	sprintf(tid->errorcause ,"%s","Read from shared memory success");	return tid;}sm_TID *sm_CallMsg(int flag,char *sbuf,int sbufsize,char *sfilename,char *rbuf,int rbufsize,char *rfilename,sm_TID *tid){	int oprcode=atoi(tid->oprcode );	if (flag==1)/*recieve message*/	{/*		sbuf=WriteOutputSharedMem(O_PARTIONNO,sbuf);*/		sbuf=WriteOutputSharedMem(tid->partionnumbers ,sbuf);		if(sbuf!=NULL)		{			rbuf=ReadInputSharedMem(tid->partionnumbers );			if(rbuf==NULL)			{				tid->errorno =E_Read;				sprintf(tid->errorcause ,"%s","cannot read from shared memory");				return tid;			}		}		else		{			tid->errorno =E_Write;			sprintf(tid->errorcause ,"%s","cannot write to shared memory");		}	}	else/*recieve file*/	{		sfilename=WriteOutputSharedMem(tid->partionnumbers ,sfilename);		if(sfilename!=NULL)		{			rfilename=ReadInputSharedMem(tid->partionnumbers +1);			if(rfilename==NULL)			{				tid->errorno =E_Read;				sprintf(tid->errorcause ,"%s","cannot read from shared memory");				return tid;			}		}		else		{			tid->errorno =E_Write;			sprintf(tid->errorcause ,"%s","cannot write to shared memory");		}	}	tid->errorno =E_Success;	sprintf(tid->errorcause ,"%s","write and read success");	return tid;}int S_FormatePacket(S_MESSAGESTRUCTURE m_struct,char *msg){	memset(msg,'\0',sizeof(msg));	strcat(msg,m_struct.packettype);	strcat(msg,m_struct.sn);	strcat(msg,m_struct.requesttype);	strcat(msg,m_struct.oprcode);	strcat(msg,m_struct.source);	strcat(msg,m_struct.destination);	strcat(msg,m_struct.message );	msg[strlen(msg)]='\0';	return 1;}/*-------------------------------------------------S_SeparateHostName():this functin translate the  host Ip address (doted) into the char array-------------------------------------------------*/char *S_SeparateHostName(char *string,char *sepstring,char returnstring[]){	char temp[30];	int i=0;	char *token=strtok(temp,sepstring);	sprintf(temp,"%s",string);	while(token!=NULL)	{		returnstring[i]=atoi(token);		token=strtok(NULL,sepstring);		i++;	}	returnstring[i]='\0';	return returnstring;}/*--------------------------------------------------------------S_SeparatePacket fucntion:separate the networking packet and set it into the data structure;--------------------------------------------------------------*/int S_SeparatePacket(char *buffer,S_MESSAGESTRUCTURE *m_struct){	if(strlen(buffer)<29)		return 1;	memset(m_struct->destination ,'\0',sizeof(m_struct->destination ));		memset(m_struct->source ,'\0',sizeof(m_struct->source));		memset(m_struct->oprcode ,'\0',sizeof(m_struct->oprcode ));		memset(m_struct->packettype ,'\0',sizeof(m_struct->packettype ));		memset(m_struct->requesttype ,'\0',sizeof(m_struct->requesttype));		memset(m_struct->sn,'\0',sizeof(m_struct->sn));		memset(m_struct->message  ,'\0',sizeof(m_struct->message  ));		memcpy(m_struct->packettype,buffer,2); 	memcpy(m_struct->sn,buffer+2,10);	memcpy(m_struct->requesttype,buffer+12,2);	memcpy(m_struct->oprcode,buffer+14,6);	memcpy(m_struct->source,buffer+20,4);	memcpy(m_struct->destination ,buffer+24,4);	memcpy(m_struct->message ,buffer+28,strlen(buffer)-28);	return 0;}char *  S_Readprofile(char *m_file,char m_section[],char m_key[]){  FILE *fp2;  char ch[2];  int i =0;  char m_tempsec[40],m_tempkey[40];  static char m_value[20]; if (strlen(m_section)<1 || strlen(m_key)<1 || strlen(m_file) < 1)  {	  printf(" the variable is invalidate\n");	  return "failed";  }  if((fp2=fopen(m_file,"r"))==NULL)	{		printf(" File opened failed!");		return "failed";	}/*step 1 :loop int the file to get the section name 	  */  while(!feof(fp2))  {	 	  if((ch[0]=fgetc(fp2))=='[')/*the start of the section flag*/		{			strcpy(m_tempsec,"");			ch[0]=fgetc(fp2);			i =0 ;	    	while(ch[0] !=']' && ch[0] !='\n' && !feof(fp2))/*the end of the sextion flag*/			{  				m_tempsec[i++] = ch[0];				ch[0]=fgetc(fp2);			}			m_tempsec[i++] = '\0';	  			if((strcmp(m_section,m_tempsec))==0)/*compare the section values with the get values*/			{				ch[0]=fgetc(fp2);/*step 2:loop in the section to get the key and kkey values*/				while(ch[0] !='[' && !feof(fp2))/*the end of the section flag of the end flag of file*/				{					if((ch[0]>='0' && ch[0] <= '9')||(ch[0]>='A' && ch[0]<='z'))					{	i = 0 ;						strcpy(m_tempkey,"");						m_tempkey[i++] = ch[0];						ch[0]=fgetc(fp2);/*step 3:loop in the line to get the key*/						while((ch[0]>='0' && ch[0] <= '9')||(ch[0]>='A' && ch[0]<='z'))						{							m_tempkey[i++] = ch[0];							ch[0]=fgetc(fp2);						}						m_tempkey[i++] = '\0';												if((strcmp(m_tempkey,m_key))==0)/*compare the key values*/						{							while(ch[0] != '=' && ch[0] !='\n')/*step over the unnessary word*/								ch[0]=fgetc(fp2);														if(ch[0] == '\n')							{								printf(" cannot get the key values :%s  in the profile:%s !",m_key,m_file);								fclose(fp2);								return "failed";							}							else							{								strcpy(m_value,"");/*step 4:loop int the line to get the key values*/								while(ch[0] !='\n' && !feof(fp2))								{									i = 0;									ch[0]=fgetc(fp2);									if((ch[0]>='0' && ch[0] <= '9')||(ch[0]>='A' && ch[0]<='z'))									{										m_value[i++] = ch[0];										ch[0]=fgetc(fp2);										while((ch[0]>='0' && ch[0] <= '9')||(ch[0]>='A' && ch[0]<='z')||(ch[0]=='.'))										{											m_value[i++] = ch[0];											ch[0]=fgetc(fp2);										}										m_value[i++] ='\0';										fclose(fp2);										return m_value;									}																	}								fclose(fp2);								return "failed";							}						}					}					ch[0]=fgetc(fp2);				}			}				}		}	fclose(fp2);	printf("File opened failed!");	return "failed";	}/*----------------------------------------------------------------------writelog():write the log information into the log file----------------------------------------------------------------------*/int WriteLog(LOG *m_log){	FILE *fp1;	struct tm *m_curr;	time_t *tp;	long m_logsize=ftell(fp1);	char m_last512[751];	if((fp1=fopen(LOGFILE,"a+"))==NULL)	{	    printf("open log file %s failed!\n",LOGFILE);		return 2;	}	if(fseek(fp1,0L,SEEK_END))		printf("seek failed!\n");		if(m_logsize>10000000)	{			printf("the filesize is %d\n",m_logsize);		fseek(fp1,m_logsize-750,SEEK_SET);		fread(m_last512,sizeof(char),750 ,fp1);		fclose(fp1);		if((fp1=fopen(LOGFILE,"w+t"))==NULL)		{			printf(">>open log file failed!\n");			return 2;		}			m_last512[738]='\n';                m_last512[739]='\0';		printf("the 512 bytes are %d",strlen(m_last512));		fputs(m_last512,fp1);		fclose(fp1);	}	if((fp1=fopen(LOGFILE,"a"))==NULL)	{			printf("open log file failed!\n");			return 2;	}		time(tp);	m_curr=localtime(tp);		sprintf(m_log->log_date,"%4d/%02d/%02d",m_curr->tm_year,m_curr->tm_mon,m_curr->tm_mday);	if(!fprintf(fp1,"Log level:%-4d%-12s%-10s%s   %s\n",m_log->log_level,m_log->log_date,m_log->log_time,m_log->log_event,m_log->log_cause))		printf("write logfile failed!");	fclose(fp1);	return 0;}/* --------------------------------------------------------------------SetLogContent:this functin set the content of the log data structure--------------------------------------------------------------------*/LOG SetLogContents(int Log_Level,char *Log_Envent,char *Log_Cause){	LOG m_log;	m_log.log_level=Log_Level;	strcpy(m_log.log_event,Log_Envent);	strcpy(m_log.log_cause,Log_Cause);	return m_log;}/*--------------------------------------------InitSemaphore():init the input and output semaphore--------------------------------------------*/int InitSemaphore(){  key_t  in_key,out_key;/*  if((in_key=CreatShmKey(IN_SEMAPHORE_FILE,IN_SEMAPHORE_ID))<0)  {     printf("cannot create the input semaphore key>>\n");     return -1;   }  else     printf("create the input sempahore key success\n");     if((out_key=CreatShmKey(OUT_SEMAPHORE_FILE,OUT_SEMAPHORE_ID))<0)   {     printf("cannot create the output semaphore key\n");     return -1;   }  else     printf("create the output sempahore key success\n");*/    InitSemaphoreValue();       if((InputSemaphoreID=SemCreat(IN_SEMAPHORE_KEY,INPUT_SEMAPHORE_NUMBERS,InputSemaphoreValue,0777))<0)	{   	 printf("cannot create the input semaphore\n");   	 return -1;   	 }    else	printf("create the input semaphore success\n");      if((OutputSemaphoreID=SemCreat(OUT_SEMAPHORE_KEY,OUTPUT_SEMAPHORE_NUMBERS,OutputSemaphoreValue,0777))<0)	{   	 printf("cannot create the output semaphore\n");   	 return -1;   	 }    else	printf("create the output semaphore success\n");   	return 0;}/*--------------------------------------------OpenSemaphore():init the input and output semaphore--------------------------------------------*/int OpenSemaphore(){  key_t  in_key,out_key;/*  if((in_key=CreatShmKey(IN_SEMAPHORE_FILE,IN_SEMAPHORE_ID))<0)  {     printf("cannot create the input semaphore key\n");     return -1;   }  else     printf("create the input sempahore key success\n");  if((out_key=CreatShmKey(OUT_SEMAPHORE_FILE,OUT_SEMAPHORE_ID))<0)   {     printf("cannot create the output semaphore key\n");     return -1;   }  else     printf("create the output sempahore key success\n");*/    if((InputSemaphoreID=SemOpen(IN_SEMAPHORE_KEY,INPUT_SEMAPHORE_NUMBERS))<0)	{   	 printf("cannot open the input semaphore\n");   	 return -1;   	 }    else	printf("open the input semaphore success\n");      if((OutputSemaphoreID=SemOpen(OUT_SEMAPHORE_KEY,OUTPUT_SEMAPHORE_NUMBERS))<0)	{   	 printf("cannot open the output semaphore\n");   	 return -1;   	 }    else	printf("open the output semaphore success\n");   	return 0;}int InitSemaphoreValue(){  int i;  for(i=0;i<INPUT_SEMAPHORE_NUMBERS;i++)    InputSemaphoreValue[i]=0;  for(i=0;i<OUTPUT_SEMAPHORE_NUMBERS;i++)    OutputSemaphoreValue[i]=0;  return 0;}int SemaphoreP(int SemaphoreID,int No){   return SemP(SemaphoreID,No,1);}int SemaphoreV(int SemaphoreID,int No){   return SemV(SemaphoreID,No);}int DelAllSemAndShm(){SemDel(IN_SEMAPHORE_KEY,INPUT_SEMAPHORE_NUMBERS);SemDel(OUT_SEMAPHORE_KEY,OUTPUT_SEMAPHORE_NUMBERS);ShmDel(I_KEY);ShmDel(O_KEY);return 1;}

⌨️ 快捷键说明

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