Monday, August 30, 2010

Queue Programe which stores functions with different argument

#include "stdafx.h"
#include windows.h
#include stdio.h
#include malloc.h
#include stdlib.h //put include file in < > during execution.
#using system.dll
using namespace System;
using namespace System::Timers;
#define TRUE 1
#define FALSE 0
typedef int BOOL;
HANDLE g_hISemhandler = NULL;
HANDLE g_hRSemhandler = NULL;
typedef DWORD RADRES;
typedef unsigned char BYTE;
int RemoveFromQueue (void);
int ReadQueue(void);
RADRES RdsStartCapture(BOOL, BOOL);
RADRES RdsStopCapture(BOOL, BOOL);
RADRES FMRadio_SetVolume(BYTE);
DWORD InsertThread (LPVOID lpdwThreadParam );
DWORD ReadThread (LPVOID lpdwThreadParam );

/*************Timer for inserting functions in to Queue****************/
public ref class Timer1
{
private:
static System::Timers::Timer^ aTimer;
public:
static void Demo()
{
aTimer = gcnew System::Timers::Timer(5000);
aTimer->Elapsed += gcnew ElapsedEventHandler( Timer1::OnTimedEvent );
aTimer->Interval = 5000;
aTimer->Enabled = true;
GC::KeepAlive(aTimer);
}
private:
static void OnTimedEvent( Object^ source, ElapsedEventArgs^ e )
{
ReleaseSemaphore(g_hISemhandler,1,NULL);
Console::WriteLine("insert timer event {0}", e->SignalTime);
}
};
/***********Timer for reading and removing functions from Queue**********/
public ref class Timer2
{
private:
static System::Timers::Timer^ aTimer;
public:
static void Demo()
{
aTimer = gcnew System::Timers::Timer(5000);
aTimer->Elapsed += gcnew ElapsedEventHandler( Timer2::OnTimedEvent );
aTimer->Interval = 3000;
aTimer->Enabled = true;
GC::KeepAlive(aTimer);
}
private:
static void OnTimedEvent( Object^ source, ElapsedEventArgs^ e )
{
ReleaseSemaphore(g_hRSemhandler,1,NULL);
Console::WriteLine("read timer event {0}", e->SignalTime);
}
};

/************Queue structure**************/
struct Queue
{
RADRES(*function_1arg)(BOOL, BOOL);
RADRES(*function_2arg)(BYTE);
BOOL arg1;
BOOL arg2;
BYTE arg3;
struct Queue *node;
} *front = NULL;
/************Insert Class for different InsertIntoQueue method overloading **************/
class Insert{
public:
void InsertIntoQueue (RADRES (*function)(BOOL, BOOL),BOOL a, BOOL b)
{
struct Queue *temp, *q;
temp = (struct Queue *)malloc(sizeof(struct Queue));
temp->function_1arg = *function;
temp->function_2arg = NULL;
temp->arg1 = a;
temp->arg2 = b;
temp->node=NULL;
if(front == NULL)
front = temp;
else
{
q = front;
while(q->node != NULL)
q = q->node;
q->node = temp;
}
printf("inserted into buffer\n");
}
void InsertIntoQueue (RADRES (*function)(BYTE),BYTE a)
{
struct Queue *temp, *q;
temp = (struct Queue *)malloc(sizeof(struct Queue));
temp->function_2arg = *function;
temp->function_1arg = NULL;
temp->arg3 = a;
temp->node=NULL;
if(front == NULL)
front = temp;
else
{
q = front;
while(q->node != NULL)
q = q->node;
q->node = temp;
}
printf("inserted into buffer\n");
}
};
/************main function **************/
int main()
{
g_hISemhandler = CreateSemaphore(NULL,0,1, _T("insertSem"));
g_hRSemhandler = CreateSemaphore(NULL,0,1, _T("readSem"));
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&InsertThread, (LPVOID)NULL,0,NULL);
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ReadThread, (LPVOID)NULL,0,NULL);
getchar();
}
///////////////////////////////////////////////////////////////////////////////
/******************************************************************************
* FUNCTION: RemoveFromQueue
* DESCRIPTION: removes the functions from Queue
* ARGS:
*
* RETURNS: TRUE for SUCESS and FALSE for FAILURE
******************************************************************************/
int RemoveFromQueue(void)
{
struct Queue *temp;
if(front == NULL)
return FALSE;
else
{
temp = front;
front = front->node;
free(temp);
printf("Function removed from buffer\n");
return TRUE;
}
}

RADRES RdsStartCapture(BOOL a, BOOL b)
{
printf("RdsStartCapture \n a =%d \t b=%d \n", a, b);
return TRUE;
}
RADRES RdsStopCapture(BOOL a, BOOL b)
{
printf("RdsStopCapture \n a =%d \t b=%d \n", a, b);
return TRUE;
}
RADRES FMRadio_SetVolume(BYTE a)
{
printf("RdsStopCapture \n a =%d \n", a);
return TRUE;
}

/******************************************************************************
* FUNCTION: ReadQueue
* DESCRIPTION: calles the function in Queue(FIFO) and removes from the queue
* ARGS:
*
* RETURNS: TRUE for SUCESS and FALSE for FAILURE
******************************************************************************/
int ReadQueue(void)
{
if(front != NULL && front->function_1arg!= NULL)
{
front->function_1arg(front->arg1,front->arg2);
//RETAILMSG(1, (TEXT("Calling Buffered function\n")));
RemoveFromQueue(); //removing function from the Queue.
return TRUE;
}
else if(front != NULL && front->function_2arg!= NULL)
{
front->function_2arg(front->arg3);
//RETAILMSG(1, (TEXT("Calling Buffered function\n")));
RemoveFromQueue(); //removing function from the Queue.
}
else
{
printf("Buffer Is Empty\n");
//RETAILMSG(1, (TEXT("Buffer Is Empty\n")));
}
return FALSE;
}
DWORD InsertThread (LPVOID lpdwThreadParam )
{
int i=4,j=0,k=2;
Insert ins;
printf("Insert thread begins.... \n");
Timer1::Demo();
while(1)
{
WaitForSingleObject(g_hISemhandler, 10000);
ins.InsertIntoQueue(RdsStartCapture,5,9);
ins.InsertIntoQueue(FMRadio_SetVolume,2);
printf("\n\n");
}
return TRUE;
}
DWORD ReadThread (LPVOID lpdwThreadParam )
{
printf("Read thread begins.... \n");
Timer2::Demo();
while(1)
{
WaitForSingleObject(g_hRSemhandler, 10000);
ReadQueue();
printf("\n\n");
}
return TRUE;
}
//End of PROG

No comments:

Post a Comment