#include <sys/ipc.h>
#include "./include/iptrans.h"

static iptrans_shm *IPTRANS_shm_ptr;
static int          IPTRANS_shm_id;

/*@ignore@*/
int iptransCreate()
{
    IPTRANS_shm_ptr = (iptrans_shm *)ShmInit(IPTRANS_shm_id,"iptrans",IPTRANS_SHM_KEY,
											sizeof(iptrans_shm),IPTRANS_SHM_PERM | IPC_CREAT);
}

int GetMessage(message_list *msgGet,int port_in)
{
   int  port;
   port=port_in-PORT_BASE_NUM;
   if(IPTRANS_shm_ptr->msgPortIn[port].msgReadSub !=
          IPTRANS_shm_ptr->msgPortIn[port].msgWriteSub)
   {
      memcpy(msgGet,
	     &IPTRANS_shm_ptr->msgPortIn[port].msgList[IPTRANS_shm_ptr->msgPortIn[port].msgReadSub],
	     sizeof(struct message_list));
	  IPTRANS_shm_ptr->msgPortIn[port].msgReadSub++;
	  if(IPTRANS_shm_ptr->msgPortIn[port].msgReadSub==IN_LIST_NUM)
	     IPTRANS_shm_ptr->msgPortIn[port].msgReadSub=0;
	  return 1;
    }
    return 0;

}

int PutMessage(message_list msgPut)
{
   memcpy(&IPTRANS_shm_ptr->msgPortOut.msgList[IPTRANS_shm_ptr->msgPortOut.msgWriteSub],
          &msgPut, sizeof(struct message_list)); 
   IPTRANS_shm_ptr->msgPortOut.msgWriteSub ++;
   if(IPTRANS_shm_ptr->msgPortOut.msgWriteSub==OUT_LIST_NUM)
      IPTRANS_shm_ptr->msgPortOut.msgWriteSub=0;
   return 0;   
}

int clearPort(int port_in)
{
   int port;
   port=port_in-PORT_BASE_NUM;
   IPTRANS_shm_ptr->msgPortIn[port].msgReadSub=0;
   IPTRANS_shm_ptr->msgPortIn[port].msgWriteSub=0;  
   return 1;
} 
/*@end@*/
