//////////////////////////////////////////////////////////////////////
// MessageQ.cpp: implementation of the MessageQ class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MessageQ.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MessageQ::MessageQ()
{
	theQ.size = 0;
	theQ.front = 1;
	theQ.rear = 0;	
	theQ.maxsize = QSIZE;	
	memset(theQ.msgArray,NULL,MAX_QMSG_SIZE*QSIZE);
	memset(theQ.msgType, OK_RESPONSE, QSIZE);
}

BOOL MessageQ::MsgQIsFull()
{
	return (theQ.size < theQ.maxsize ? FALSE:TRUE);
}

BOOL MessageQ::MsgQIsEmpty()
{
	return (theQ.size > 0 ? FALSE:TRUE);
}

BOOL MessageQ::EnqueueMsg(char* msg, int type)
{

	if(MsgQIsFull() == TRUE)
		return FALSE;
	else
	{	
		theQ.size++;
		theQ.rear = Succ(theQ.rear);
		strcpy(theQ.msgArray[theQ.rear],msg);
		theQ.msgType[theQ.rear] = type;
		return TRUE;
	}
		


}

BOOL MessageQ::DequeueMsg(char* msg, int type)
{
	if(MsgQIsEmpty() == TRUE)
		return FALSE;
	else
	{
		strcpy(msg,theQ.msgArray[theQ.front]);		
		type = theQ.msgType[theQ.front];
		theQ.size--;
		theQ.front = Succ(theQ.front);
		return TRUE;
	}
		


}

int MessageQ::Succ(int value)
{
	if(++value == theQ.maxsize)
		value = 0;
	return value;

}
