?? asyncappllayer.cc
字號:
/*********************************************************
* file: AsyncApplLayer.cc
*
* author: Fang shi
*
* copyright: (C) 2006 HUNAN Universtiy, ChangSha China
*
***************************************************************************
* part of: Async Simulation
* description: - Async Description
*
***************************************************************************
* changelog: $Revision$
* last modified: $Date: 2007-5-10 10:21 $
* by: $Author: Fang shi $
***************************************************************************/
#include "AsyncApplLayer.h"
#include <ApplPkt_m.h> //該成#include "ApplPkt_m.h"就會出錯。#include <fstream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <iterator>
//#include <direct.h> //用于創建文件夾
#define EV (ev.disabled()||!debug) ? (std::ostream&)ev : ev << logName() << "::AsyncApplLayer: "
Define_Module(AsyncApplLayer);
/**
* do some initialization
* @param stage stage number
**/
//std::vector<std::string> AsyncApplLayer::svec;
//sMap AsyncApplLayer::smap;
//int AsyncApplLayer::row = 0;
std::string int_to_filename(int i,std::string sName="shuju/data")
{
std::string s=sName;
while(i/10>0)
{
int j=i/10;
s=s+(char)(j+'0');
i=i%10;
}
s=s+(char)(i+'0');
s=s+".txt";
return s;
}
void AsyncApplLayer::initialize(int stage)
{
BasicApplLayer::initialize(stage); //DO NOT DELETE!!
srand((unsigned)time(NULL));
if(stage==0){
int fileNum=findHost()->index();
if(fileNum!=0)
{
std::string file_name=int_to_filename(fileNum);
std::string textline;
std::cout<<"file_name = "<<file_name<<endl;
std::ifstream infile( file_name.c_str(), std::ios::in );
if (!infile ) {
std::cerr << "oops! unable to open file "
<< file_name << " -- bailing out!\n";
exit( -1 );
}
while (getline(infile,textline,'\n'))
sDeque.push_back(textline);
infile.close();
row=0;
}
else
{
system("del /Q copy");
system("del /Q yasuobi.txt");
system("del /Q mse.txt");
system("del /Q result.txt");
system("del /Q delay.txt");
system("del /Q lostpkt.txt");
system("del /Q sinktime.txt");
system("del /Q clustertime.txt");
}
pkReceived = 0;
numBurstMessage=0;
maxNumBurst=(int)par("maxNumBurst"); //max number of bursting message
WATCH(numBurstMessage);
WATCH(pkReceived);
destAddress = par("destAddress"); //add sourceAddress
sourceAddress=par("sourceAddress");
clusterWaveLet=new cMessage("clusterWaveLet");
undoCode=new cMessage("undoCode");
if(parentModule()->index()==destAddress && ev.isGUI())//highlight the sink node
parentModule()->displayString().setTagArg("i",1,"yellow");
/** 4-19 添加,用來實現簇頭編碼*/
//數組清零
for(int i=0;i<32;i++)
for(int j=0;j<8;j++)
clusterHead_data[i][j]=0.0;
for(int i=0;i<32;i++)
for(int j=0;j<8;j++)
sink_data[i][j]=0.0;
for(int i=0;i<32;i++)
for(int j=0;j<8;j++)
test_data[i][j]=0.0;
for(int i=0;i<32;i++)
{
addr[i]=0;
addr_sink[i]=0;
}
//std::cout<<"aaa"<<std::endl;
dataCounter=0;
timeStamp=0.0;
adr=0;
lostCounter=0;
cTopology *topo = new cTopology("topo");
topo->extractByModuleType(parentModule()->className(), NULL);
destModuleID=topo->node(destAddress)->module()->findSubmodule("net");// find destAddress node's net module id
EV << "sink node: " << destAddress << "---net module id is: " << destModuleID <<endl;
delete topo;
if(parentModule()->index()!=destAddress)
{
generatePacket = new cMessage("generatePacket");
scheduleAt(simTime()+(double)par("iaitime"), generatePacket); ////
}
else
{
scheduleAt(simTime()+5.0,undoCode);
// scheduleAt(simTime()+200,clusterWaveLet);
}
// stopptr=-2;
EV<<" AsyncApplLayer is loaded!!!"<<endl;
}
/* else if(stage==1){
}*/
}
/**
* You got a message from the network layer (most likely)
* take care of it
* @param msg msg to handle
**/
void AsyncApplLayer::handleLowerMsg( ApplPkt* msg )
{
//對簇內節點產生的多個數據報進行兩兩小波變換 if(findHost()->index()!=(int)findHost()->submodule("appl")->par("destAddress"))//在非sink的簇頭節點操作 {
//將net層上來得數據報的8位數據讀出,放入8×32數組的相應行中
//檢查32×8數組,如果滿則馬上調用小波變換和游程編碼
if(findHost()->index()==7)
{
std::ofstream outfile( "clustertime.txt", std::ios_base::app );
outfile<<msg->timestamp()<<endl;
outfile.close(); }
if(timeStamp==0)
{
timeStamp=msg->timestamp();
}
else if(msg->timestamp()<timeStamp)
{
timeStamp=msg->timestamp();
}
//存入數組和節點地址堆棧
int address=msg->getSrcAddr();
std::deque<double> data;
for(int k=0;k<8;k++)
data.push_back(msg->getData(k));
delete msg;
cluData.insert(make_pair(address,data));
if(cluData.size()==32)
{
do_clusterWave(); //和下面第二句做同樣的工作
cancelEvent(clusterWaveLet);
scheduleAt(simTime()+200,clusterWaveLet);
}
}
else
{
std::ofstream outfile1( "delay.txt", std::ios_base::app );
outfile1<<(simTime()-msg->timestamp());
outfile1<<endl;
outfile1.close();
std::ofstream outfile2( "sinktime.txt", std::ios_base::app );
outfile2<<msg->timestamp()<<endl;
outfile2.close();
if(msg->getSeqNum2()==0)
{
pkReceived++;
double temp[8];
double temp1[8];
for(int i=0;i<8;i++)
{
temp1[i]=msg->getData(i);
}
int h=1;
while(h<=4)
{
for(int j=0;j<h;j++)
{
temp[2*j]=((temp1[j]+temp1[j+h])/2);
temp[2*j+1]=((temp1[j]-temp1[j+h])/2);
}
for(int j=0;j<(2*h);j++)
{
temp1[j]=temp[j];
}
h=h*2;
}
std::ofstream outfile( "result.txt", std::ios_base::app );
// outfile<<msg->getSrcAddr()<<"----";
for(int j=0;j<8;j++)
{
outfile<<temp1[j]<<" ";
}
outfile<<endl;
outfile.close();
delete msg;
}
/*************add by zhu tiejun 4-13*************/
else
{
std::cout<<endl;
std::cout<<"The pkt from cluster "<<msg->getSrcAddr()<<" and the num is "<<msg->getSeqNum1()<<"——"<<msg->getSeqNum2()<<endl;
int src=msg->getSrcAddr();
std::cout<<endl;
std::cout<<"Num2="<<msg->getSeqNum2()<<endl;
//std::cout<<src<<std::endl;
m_iterator m_it=dmap.find(src);
if(m_it!=dmap.end())
{
//std::deque<ApplPkt*> msgDeq = (*m_it).second;
msg->setTimestamp(simTime());
(*m_it).second.push_back(msg);
}
else
{
std::deque<ApplPkt*> de;
de.push_back(msg);
dmap.insert(make_pair(src,de));
}
/******************* end ******************/
pkReceived++;
EV << " received "<<pkReceived<< "th packet " << endl;
ev<<"msg->Data=";
for(int i=0;i<8;i++)
{
ev<<msg->getData(i)<<",";
}
ev<<"."<<endl;
//###########################################
std::ofstream outfile( "msg.out", std::ios_base::app );
if ( ! outfile ) { // 打開失敗 std::cerr << "cannot open \"msg.out\" for output\n";
//exit( -1 );
EV<<"error"<<endl;
}
else
{
for(int i=0;i<8;i++)
outfile<<msg->getData(i)<<", ";
outfile<<endl;
outfile.close();
}
if (ev.isGUI())
parentModule()->bubble("Arrived!");
// timeVector.push_back(msg);
displayStatus(true);
//}
}
}
}
void AsyncApplLayer::displayStatus(bool isBusy)
{
char string[50];
if(findHost()->index()==(int)findHost()->submodule("appl")->par("destAddress"))
{
sprintf( string, "%d", pkReceived);
findHost()->displayString().setTagArg("t",0, string);
}
}
struct Comp:public std::binary_function<ApplPkt*,ApplPkt*,bool>{
bool operator()(const ApplPkt* p1, const ApplPkt* p2)const{
return p1->getSeqNum2()<p2->getSeqNum2();
}
};
/**
刪除指針
*/
/*template<typename T>
struct DeleteObj :
public std::unary_function<const T*, void> { // 這里有這個繼承
void operator()(const T* ptr) const
{
delete ptr;
}
};*/
struct DeleteObj:public std::unary_function<ApplPkt*,void> { // 刪除這里的
void operator()(const ApplPkt* ptr) const
{
delete ptr;
}
};
/**
* You have send yourself a message -- probably a timer,
* take care of it
* @param msg msg to handle
**/
void AsyncApplLayer::handleSelfMsg(cMessage *msg){
if(msg==generatePacket && numBurstMessage<maxNumBurst)
{
ApplPkt *pk = new ApplPkt();
std::string s=sDeque[row];
/**************modify 6-12 把調用的數據拷貝到一個指定的文件夾的一個對應的文件中********/
int fileNum=findHost()->index();
std::string copyDataName=int_to_filename(fileNum,"copy/");
std::ofstream outfile( copyDataName.c_str(), std::ios_base::app );
if ( ! outfile ) { // 打開失敗 std::cerr << "cannot open "<<copyDataName<<" for output\n";
EV<<"error"<<endl;
}
outfile<<s<<endl;
outfile.close();
/**************************end***************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -