message.c

来自「一个很好的例子-基于Linux的消息传输系统」· C语言 代码 · 共 141 行

C
141
字号
#include "message.h"
#include"LinkedList.h"

/**
* Create a Message object.
* @param type The type of this message
* @param serial Serial number of this message
* @param src The name of the source unit of the message 
* @param des The name of the destination unit of the message 
* @param priority The priority of this message
* @param length The byte-size of content
* @param content The content of the message
*/
Message* createMessage(int type, int serial, char* src, char *srcIp,int srcPort,char* des, int priority, int length, void* content)
{
	Message* message;
	if(type <1 || type >16)
	{
		return NULL;
	}
	message = (Message*)malloc(sizeof(Message));
	if(message==NULL)
	{
		return NULL;
	}
	else
	{
		message->type=type;

		message->serial=serial;

		strcpy(message->src,src);
		
		strcpy(message->srcIp,srcIp);

		message->srcPort=srcPort;
        
		strcpy(message->des,des);
		
		message->priority=priority;
		
		message->length=length;
        
		message->content=content;
        
		return message;
        }		
}

/**
* Destroy a Message object.
* @param msg The Message object being destroyed
* 
*/
void destroyMessage(Message* msg)
{
	if(msg==NULL)
	{
		return;
	}
	else
	free(msg);
}

/**
* Put a Message object to SendQueue.
* @param ci The CI object
* @param message The Message object being sent
* @param ip The ip address which the message being sent to
* @param port The Listening port on which the receiving programme is listen.
*/
void putMessage(CI *ci,Message *message,char *ip,int port)
{	
	
	Node* node;
	SendPack *sendBuf;
	sendBuf=(SendPack*)malloc(sizeof(SendPack));
	sendBuf->msg=message;
	strcpy(sendBuf->ip,ip);
	sendBuf->port=port;
	node = search(ci->sendBuffer, compareSendPack,  sendBuf);
	if(node!=NULL) 
	{
		  addBefore(ci->sendBuffer, node, sendBuf);
	}
	else
	{
		  append(ci->sendBuffer, sendBuf);
	}	
	printLinkedList(ci->sendBuffer, printASendPack);
}

/**
* Put the receiving Message object to ReceiveQueue.
* @param ci The CI object
* @param message The Message object being sent
* @param ip The ip address which the message being sent to
* @param port The Listening port on which the receiving programme is listen.
*/
void putMessageRec(CI *ci,Message *message,char *ip,int port)
{	
	
	Node* node;
	SendPack *sendBuf;
	sendBuf=(SendPack*)malloc(sizeof(SendPack));
	sendBuf->msg=message;
	strcpy(sendBuf->ip,ip);
	sendBuf->port=port;
	node = search(ci->receiveBuffer, compareSendPack,  sendBuf);
	if(node!=NULL) 
	{
		  addBefore(ci->receiveBuffer, node, sendBuf);
	}
	else
	{
		  append(ci->receiveBuffer, sendBuf);
	}	
	printLinkedList(ci->receiveBuffer, printASendPack);
}


Message * getMessage(CI *ci)
{
        SendPack * send;	
	if(ci->receiveBuffer->head==NULL||ci->receiveBuffer==NULL)
	{ 
           return NULL;
	}
	else 
	{
	send=(SendPack *)pop(ci->receiveBuffer, ci->receiveBuffer->head);
 //       printf("popmsgtype : %d\n",send->msg->type);
	
	return send->msg;
        }
}

	
	

⌨️ 快捷键说明

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