//
// Copyright (C) 2008 Institut fuer Telematik, Universitaet Karlsruhe (TH)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//

/**
 * @file MyGlobalKQ.cc
 * @author Ingmar Baumgart, Dilum Bandara
 * @version 0.1.1
 * @date 07/31/2010
 */

#include <stdlib.h>
#include <string.h>
#include <omnetpp.h>
#include "MyGlobalKQ.h"

using namespace std;

Define_Module(MyGlobalKQ);


MyGlobalKQ::MyGlobalKQ()
{
}


MyGlobalKQ::~MyGlobalKQ()
{
}


void MyGlobalKQ::initialize()
{
	keyCount = 0;

	keyFile = par("keyFile").stringValue();
	pKeyFile = fopen(keyFile, "r");
	if (pKeyFile == NULL) 
		throw cRuntimeError("MyGlobalKQ::initialize(): Key file not found!");

	useMultipleQueryFiles = par("useMultipleQueryFiles");
	if(!useMultipleQueryFiles)	//Use only 1 file
	{
		queryFile1 = par("queryFile").stringValue();
		pQueryFile1 = fopen(queryFile1, "r");
		if (pQueryFile1 == NULL) 
			throw cRuntimeError("MyGlobalKQ::initialize(): Query file not found!");
	}
	else
	{
		queryFile1 = par("queryFile1").stringValue();
		queryFile2 = par("queryFile2").stringValue();
		queryFile3 = par("queryFile3").stringValue();
		queryFile4 = par("queryFile4").stringValue();
		queryFile5 = par("queryFile5").stringValue();
		queryFile6 = par("queryFile6").stringValue();
		queryFile7 = par("queryFile7").stringValue();
		queryFile8 = par("queryFile8").stringValue();
		queryFile9 = par("queryFile9").stringValue();
		queryFile10 = par("queryFile10").stringValue();

		pQueryFile1 = fopen(queryFile1, "r");
		pQueryFile2 = fopen(queryFile2, "r");
		pQueryFile3 = fopen(queryFile3, "r");
		pQueryFile4 = fopen(queryFile4, "r");
		pQueryFile5 = fopen(queryFile5, "r");
		pQueryFile6 = fopen(queryFile6, "r");
		pQueryFile7 = fopen(queryFile7, "r");
		pQueryFile8 = fopen(queryFile8, "r");
		pQueryFile9 = fopen(queryFile9, "r");
		pQueryFile10 = fopen(queryFile10, "r");

		if (pQueryFile1 == NULL) 
			throw cRuntimeError("MyGlobalKQ::initialize(): Query file 1 not found!");
		if (pQueryFile2 == NULL) 
			throw cRuntimeError("MyGlobalKQ::initialize(): Query file 2 not found!");
		if (pQueryFile3 == NULL) 
			throw cRuntimeError("MyGlobalKQ::initialize(): Query file 3 not found!");
		if (pQueryFile4 == NULL) 
			throw cRuntimeError("MyGlobalKQ::initialize(): Query file 4 not found!");
		if (pQueryFile5 == NULL) 
			throw cRuntimeError("MyGlobalKQ::initialize(): Query file 5 not found!");
		if (pQueryFile6 == NULL) 
			throw cRuntimeError("MyGlobalKQ::initialize(): Query file 6 not found!");
		if (pQueryFile7 == NULL) 
			throw cRuntimeError("MyGlobalKQ::initialize(): Query file 7 not found!");
		if (pQueryFile8 == NULL) 
			throw cRuntimeError("MyGlobalKQ::initialize(): Query file 8 not found!");
		if (pQueryFile9 == NULL) 
			throw cRuntimeError("MyGlobalKQ::initialize(): Query file 9 not found!");
		if (pQueryFile10 == NULL) 
			throw cRuntimeError("MyGlobalKQ::initialize(): Query file 10 not found!");
	}
}


void MyGlobalKQ::finish()
{
    fclose(pKeyFile);
    fclose(pQueryFile1);
	if(useMultipleQueryFiles)	//Close other files
	{
	    fclose(pQueryFile2);
	    fclose(pQueryFile3);
    	fclose(pQueryFile4);
    	fclose(pQueryFile5);
    	fclose(pQueryFile6);
	    fclose(pQueryFile7);
    	fclose(pQueryFile8);
    	fclose(pQueryFile9);
    	fclose(pQueryFile10);
	}
}


void MyGlobalKQ::handleMessage(cMessage* msg)
{
	throw cRuntimeError("MyGlobalKQ::handleMessage: Module is not suppose to receive messages!");
}


const OverlayKey& MyGlobalKQ::getKey()
{
	if(keyCount == 0)		//Need a new key
	{
		fgets(buffer, 50, pKeyFile);
		if(feof(pKeyFile))	//End of file reached
        	return OverlayKey::UNSPECIFIED_KEY;
			
		char *tokens;
		tokens = strtok(buffer," \t");
		char *tmpKey = tokens;		//Get key
		key = OverlayKey(tmpKey);	//New Key
		tokens = strtok(NULL,"");
		keyCount = atoi(tokens); 	//Get no of copies
	}

	keyCount--;
	return key;
}


const OverlayKey& MyGlobalKQ::getQuery(int groupId)
{
	if(useMultipleQueryFiles)
	{
		switch(groupId)
		{
			case 2:
				fgets(buffer, 50, pQueryFile2);
				if (feof(pQueryFile2))				//End of file reached
	    		   	return OverlayKey::UNSPECIFIED_KEY;
				break;
			case 3:
				fgets(buffer, 50, pQueryFile3);
				if (feof(pQueryFile3))				//End of file reached
		    	   	return OverlayKey::UNSPECIFIED_KEY;
				break;
			case 4:
				fgets(buffer, 50, pQueryFile4);
				if (feof(pQueryFile4))				//End of file reached
	   		   		return OverlayKey::UNSPECIFIED_KEY;
				break;
			case 5:
				fgets(buffer, 50, pQueryFile5);
				if (feof(pQueryFile5))				//End of file reached
	    		   	return OverlayKey::UNSPECIFIED_KEY;
				break;
			case 6:
				fgets(buffer, 50, pQueryFile6);
				if (feof(pQueryFile6))				//End of file reached
	    		   	return OverlayKey::UNSPECIFIED_KEY;
				break;
			case 7:
				fgets(buffer, 50, pQueryFile7);
				if (feof(pQueryFile7))				//End of file reached
	    		   	return OverlayKey::UNSPECIFIED_KEY;
				break;
			case 8:
				fgets(buffer, 50, pQueryFile8);
				if (feof(pQueryFile8))				//End of file reached
	    		   	return OverlayKey::UNSPECIFIED_KEY;
				break;
			case 9:
				fgets(buffer, 50, pQueryFile9);
				if (feof(pQueryFile9))				//End of file reached
	    		   	return OverlayKey::UNSPECIFIED_KEY;
				break;
			case 10:
				fgets(buffer, 50, pQueryFile10);
				if (feof(pQueryFile10))				//End of file reached
	    		   	return OverlayKey::UNSPECIFIED_KEY;
				break;
			default:
				fgets(buffer, 50, pQueryFile1);
				if (feof(pQueryFile1))				//End of file reached
	   	   			return OverlayKey::UNSPECIFIED_KEY;
				break;
		}
	}
	else
	{
		fgets(buffer, 50, pQueryFile1);
		if (feof(pQueryFile1))				//End of file reached
	   		return OverlayKey::UNSPECIFIED_KEY;
	}

	char *tokens;
	tokens = strtok(buffer," \n");
	char *tmpKey = tokens;		//Get key
	key = OverlayKey(tmpKey);	//New Key
	return key;
}

