⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asyncappllayer.cc

📁 基于omnet++开发的Mf框架下的802.11协议仿真。
💻 CC
📖 第 1 页 / 共 3 页
字号:
/*********************************************************
 * 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 + -