//*******************************************************************************
//*  Copyright (c) Federal University of Para, Brazil - 2012			*
//*  Developed at the Research Group on Computer Network and Multimedia		*
//*  Communication (GERCOM)							*
//*  All rights reserved							*
//*										*
//*  Permission to use, copy, modify, and distribute this protocol and its	*
//*  documentation for any purpose, without fee, and without written		*
//*  agreement is hereby granted, provided that the above copyright notice,	*
//*  and the author appear in all copies of this protocol.			*
//*										*
//*  Version:  0.1								*
//*  Author(s): Denis do Rosário <denis@ufpa.br>				*
//*										*
//******************************************************************************/

#include "ThroughputTest.h"
#include "WiseDebug.h"

Define_Module(ThroughputTest);

ThroughputTest::ThroughputTest() {
}

ThroughputTest::~ThroughputTest() {
}

void ThroughputTest::initialize() {
	WiseBaseApplication::initialize();
	isSink = par("isSink");
	recipientAddress = par("nextRecipient").stringValue();
	packet_rate = par("packet_rate");
	startupDelay = par("startupDelay");

	packet_spacing = packet_rate > 0 ? 1 / float (packet_rate) : -1;
	dataSN = 0;

	if (packet_spacing > 0){
		setTimer(SEND_PACKET, packet_spacing + startupDelay);
	}
	else
		trace() << "Not sending packets";
	declareOutput("Packets received per node");
}

void ThroughputTest::finish() {
    WiseBaseApplication::finish();
    cOwnedObject *Del=NULL;
    int OwnedSize = this->defaultListSize();
    for(int i=0; i<OwnedSize; i++){
	Del = this->defaultListGet(0);
	this->drop(Del);
	delete Del;
    }
}

void ThroughputTest::timerFiredCallback(int index){
	switch (index) {
		case SEND_PACKET:{
			toNetworkLayer(createGenericDataPacket(0, dataSN), par("nextRecipient"));
			dataSN++;
			setTimer(SEND_PACKET, packet_spacing);
			break;
		}
	}
}

void ThroughputTest::handleMobilityControlMessage(MobilityManagerMessage* pkt) {

}

void ThroughputTest::fromNetworkLayer(cPacket* msg, const char* src, double rssi, double lqi) {
	switch (msg->getKind()) {
		case APPLICATION_PACKET:{
			WiseApplicationPacket *rcvPacket = check_and_cast <WiseApplicationPacket*>(msg);
			int sequenceNumber = rcvPacket->getSequenceNumber();
			//trace() << "Received packet #" << sequenceNumber << " from node " << rcvPacket->getSource();
			collectOutput("Packets received per node", atoi(src));
			break;
		}
	}
}


void ThroughputTest::handleSensorReading(WiseSensorMessage* msg) {

}


void ThroughputTest::handleDirectApplicationMessage(WiseApplicationPacket* pkt) {
     //trace() << "direct app msg";
}
