#include <stdio.h>
#include "msgqueue.h"


int create_msgqueue(key_t key)
{
	int qid;
	if((qid = msgget(key,IPC_CREAT|0777)) < 0)
	{
		perror("msgget:creat");
		return -1;
	}
	
	if(qid != msgget(key,0))
	{
		perror("msgget:open");
		return -1;		
	} 
	return qid;
}

int open_msgqueue(key_t key)
{
	int qid=msgget(key,0);
	if(qid < 0)
	{
		perror("msgget:open");
		return -1;		
	} 
	return qid;	
}


int delete_msgqueue(int qid)
{
	if(msgctl(qid,IPC_RMID,NULL) <0)
	{
		perror("msgctl");
		return -1;
	}
	return 0;
}

int send_message(int qid,UMAP_Msg* pMsg)
{
	printf("qid=%d", qid);
	if((msgsnd(qid,pMsg,sizeof(UMAP_IPCStruct),0)) < 0)
	{
		perror("msgsend");
		return -1;
	}
	return 0;
}

int recv_message(int qid,long type,UMAP_Msg* pMsg)
{
	int len;
	len = msgrcv(qid,pMsg,sizeof(UMAP_IPCStruct),type,0);
	
	if(len <= 0)
	{
		perror("msgrcv");
		return -1;
	}
	return len;
}

