?? asyncmaclayer.cc.bak
字號(hào):
/* -*- mode:c++ -*- ********************************************************
* file: AsyncMacLayer.cc
*
* author: Yupeng.hu
*
* copyright: (C) 2006 HUNAN Universtiy, ChangSha China
*
***************************************************************************
* part of: Async Simulation
* description: - Async Description
*
***************************************************************************
* changelog: $Revision$
* last modified: $Date: 2006-7-11 15:36 $
* by: $Author: Yupeng.hu $
***************************************************************************/
#include "AsyncMacLayer.h"
#include<fstream>
#include <time.h>
#include <cmath>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <windows.h>
#include <iterator>
#include <mysql.h>
#include "RadioState.h"
#define EV (ev.disabled()||!debug) ? (std::ostream&)ev : ev << logName() << "::AsyncMacLayer: "
Define_Module( AsyncMacLayer );
MYSQL mysql_mac;
std::deque<std::string> x_energe;
/**
* First we have to initialize the module from which we derived ours,
* in this case BasicModule.
*
* Then we have to intialize the gates and - if necessary - some own
* variables.
*
* If you subclass your own message classes you need to overwrite the
* createCapsulePkt function.
*
**/
void AsyncMacLayer::initialize(int stage)
{
BasicMacLayer::initialize(stage);
if(stage==0){
ev << logName() << "::AsyncMacLayer:" <<" AsyncMacLayer is loaded!!!"<<endl;
energyConsumption=0;
sleepTimestamp=simTime();
// numBurstMessage=0;
ackTap=2;
WATCH_VECTOR(backupVector);
//the consumed energy of recieving msg per second
rxEnergy=par("rxEnergy");
//the consumed energy of sending msg per second
txEnergy=par("txEnergy");
//the consumed energy per second while sleeping
sleepEnergy=par("sleepEnergy");
resendCounter=0;
WATCH(resendCounter);
tempAddr=-2;
WATCH(tempAddr);
sleepEvent = new cMessage("sleepEvent");
activeEvent = new cMessage("activeEvent");
popEvent = new cMessage("popEvent");
iniEvent=new cMessage("iniEvent");
timeOut=new cMessage("timeOut");
energe=new cMessage("energe");
//
groupSize=(double)par("groupSize");
status=IS_SLEEP; //initialize node in IS_SLEEP mode
WATCH(status);
distance=-1;
scheduleAt(0.0, iniEvent);
if(findHost()->index()==0)//if the node is sink, ready for calculating the energy
{
scheduleAt(simTime()+5.0,energe);
}
/*
**初始化數(shù)據(jù)庫
*/
if(mysql_init(&mysql_mac) == NULL)
{
std::cout<<"init mysql data status fail"<<std::endl;
return;
}
else
std::cout<<"init mysql data status success"<<std::endl;
if(NULL == mysql_real_connect(&mysql_mac,"localhost","root","","wsnss",MYSQL_PORT,NULL,0))
{
std::cout<<"connect database fail"<<std::endl<<mysql_error(&mysql_mac)<<std::endl;;
return;
}
else
std::cout<<"connect database success"<<std::endl;
/*
**初始化數(shù)據(jù)庫結(jié)束
*/
}
else if(stage==1){
}
}
//send the msg in queue
void AsyncMacLayer::startPop()
{
if(status==IS_ACTIVE)
{
if(queue.length()>0)
{
MacPkt *msg=(MacPkt* )queue.pop();
ev << logName() << "::AsyncMacLayer: node-" << findHost()->index() << " currently queue length is: " << queue.length()+1 << endl;
ev << logName() << "::AsyncMacLayer: node-" << findHost()->index() << " Starting transmission of msg in queue,length:" << msg->length()<<endl;
ev<<"Make a copy!"<<endl;
MacPkt* bkMsg=(MacPkt* )(msg->dup());
bkMsg->setRetryLimit(0);//初始化重發(fā)次數(shù) backupVector.push_back(bkMsg);
//energy consumption for transmission
energyConsumption += txEnergy*msg->length()/(double)par("bitrate");
scheduleAt(simTime()+msg->length()/(double)par("bitrate")+0.001, popEvent);
sendDown((MacPkt *)msg);
}
else
{
ev << logName() << "::AsyncMacLayer::startPop():: node-" << findHost()->index() <<" queue is empty, so switch to sleep right now!"<< endl;
}
}
else
error("goint to popup but I am is sleep .....");
}
//顯示信息
/**void AsyncMacLayer::displayStatus(bool isBusy)
{
char string[50];
if(findHost()->index()!=(int)findHost()->submodule("appl")->par("destAddress"))
{
sprintf( string, "%d-%d", queue.length(), backupVector.size());
findHost()->displayString().setTagArg("t",0, string);
}}
*/
/**
* The basic handle message function.
*
* Depending on the gate a message arrives handleMessage just calls
* different handle*Msg functions to further process the message.
*
* You should not make any changes in this function but implement all
* your functionality into the handle*Msg functions called from here.
*
* @sa handleUpperMsg, handleLowerMsg, handleSelfMsg
**/
void AsyncMacLayer::handleSelfMsg(cMessage* msg)
{
if(msg==iniEvent)//parameters initializtion
{
topo = new cTopology("topo");
//the topo include the AsynHost's all modules
topo->extractByModuleType(findHost()->className(), NULL);
int dest=findHost()->submodule("appl")->par("destAddress");//topo->node(i)'s index() is i
topo->unweightedSingleShortestPathsTo(topo->node(dest));
ev<<logName()<<"AsyncMacLayer::"<<"The queue length is"<<queue.length()<<endl;
//the hops to the dest
distance=topo->nodeFor(findHost())->distanceToTarget();
ev<<logName() << "::AsyncMacLayer:" <<" distance to sink node is: "<<distance<<endl;
cycleTime=(double)par("roundTime");
activeTime=cycleTime*(double)par("dutyCycle")*((1/groupSize));
sleepTime=cycleTime-activeTime;
//孤立節(jié)點(diǎn) if(sleepTime<=0)
{
sleepTime=10000;
}
//???有必要設(shè)置節(jié)點(diǎn)部署后第一次喚醒時(shí)間呈階梯行
//??
if((((int)distance)%(int)groupSize)!=0)
wakeupTime=uniform((groupSize-((int)distance)%(int)groupSize)*cycleTime/groupSize,(groupSize-((int)distance)%(int)groupSize+1)*cycleTime/groupSize);
else
wakeupTime=uniform(0,cycleTime/groupSize);
ev<<logName()<<"the sleeptime is:"<<sleepTime<<endl;
ev<<logName()<<"the activetime is:"<<activeTime<<endl;
ev << logName() << "::AsyncMacLayer: wakeup time: "<<wakeupTime<<endl;
scheduleAt(simTime()+wakeupTime, activeEvent);
delete msg;
delete topo;
}
//************************************
//* when receiving self active event *
//************************************
else if (msg==activeEvent)//activation the node
{
char str[1024];
sprintf(str,"insert into energy_mac(ID,value)values('%d','%lf');",
findHost()->index(),energyConsumption);
std::string insert(str);
x_energe.push_back(insert);
ev << logName() << "::AsyncMacLayer: node-" << findHost()->index() << " receiving--self-- " << msg << endl;
// we begin to IS_ACTIVE and send frames in queue.
if(status==IS_SLEEP)
{
status=IS_ACTIVE;
sleepTime=cycleTime-activeTime*(tempCount.size()+1);//???tempCount可以去掉?
scheduleAt(simTime()+activeTime, sleepEvent);
if(ackTap==0)
{
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -