规则匹配优化算法:
#include "stdafx.h"
#include <afxwin.h>
#include <iostream>
using namespace std;
#define IDT_TIMER 1000
struct {
       int rule;
       int Count;
}Rule[100], mun_temp;
void main()
{
       CFile file;
       CString strPath, strData, strTime, str;
       strPath = "Rule.txt";
       file.Open(strPath, CFile::modeCreate|CFile::modeNoTruncate|CFile::modeRead);
       int i = file.GetLength();
       file.SeekToBegin();
       int len = file.Read(strData.GetBuffer(12408), 12408);
       strData.ReleaseBuffer();
       int nPos1 = 0;
       int nPos = 0;
       int nLength = 0;
       nLength = strData.GetLength();
       strData = strData.Left(nLength);
       cout <<"规则"<<"\t"<<"匹配次数"<<endl;
       for(i = 0; ; i++)
       {
              nLength = strData.GetLength();
              nPos = strData.Find(" ", 0);
              str = strData.Left(nPos);
              strData = strData.Right(nLength-nPos-1);
              nPos1 = strData.Find("\n", 0);
              strTime = strData.Left(nPos1-1);
              strData = strData.Right(nLength-nPos1-nPos-1);
              if(nPos < 0)
                     break;
              int nstr = atoi(str);
              int nstrTime = atoi(strTime);
              cout << nstr << "\t"<<nstrTime <<endl;
              Rule[i].rule = nstr;
              Rule[i].Count = 0;
       }
       file.Close();
       cout << i <<endl;
       cout <<"初始化成功!"<<endl;
       cout <<"规则"<<"\t"<<"匹配次数"<<endl;
       for(int j=0; j <= i-1; j++)
       {
              cout << Rule[j].rule << "\t"<<Rule[j].Count << endl;
       }
       cout <<"请输入数据 输入-1结束"<< endl;
       int Data;
       cin >> Data;
       while (Data!=-1) {
              for(j=0; j <= i-1; j++)
              {
                     if (Rule[j].rule == Data) {
                            Rule[j].Count++;
                     }
              }
              cin >> Data;
       }
       cout <<"规则"<<"\t"<<"匹配次数"<<endl;
       for(j=0; j <= i-1; j++)
       {
              cout << Rule[j].rule << "\t"<<Rule[j].Count << endl;
       }
       
       for(int k=0; k<i; k++)
       {
              for(int q=0; q<i; q++)
              {
                     if (Rule[k].Count > Rule[q].Count) {
                            mun_temp = Rule[k];
                            Rule[k] = Rule[q];
                            Rule[q] = mun_temp;
                     }
              }
       }
       cout <<"排序后"<< endl;
       cout <<"规则"<<"\t"<<"匹配次数"<<endl;
       for( j=0; j <= i-1; j++)
       {
              cout << Rule[j].rule << "\t"<<Rule[j].Count << endl;
       }
       CStdioFile ifile;
       ifile.Open(strPath,CFile::modeCreate|CFile::modeWrite);
       for(j=0; j <= i-1; j++)
       {
              CString strWrite;
              strWrite.Format("%d %d\n", Rule[j].rule, Rule[j].Count);
              ifile.SeekToEnd();
              ifile.WriteString(strWrite);
       }
       
       ifile.Close();
       
}
TCL代码:
# 产生一个模拟的对象
set ns [new Simulator]
#针对不同的资料流定义不同的颜色,这是要给NAM用的
$ns color 1 Blue
#开启一个NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#开启一个trace file,用来记录封包传送的过程
set nd [open out0.tr w]
$ns trace-all $nd
#定义一个结束的程序
proc finish {} {
global ns nf nd
$ns flush-trace
close $nf
close $nd
#以背景执行的方式去执行NAM
exec nam out.nam &
exit 0
}
#产生七个节点
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]
#把节点连接起来
$ns duplex-link $n0 $n5 10Mb 10ms DropTail
$ns duplex-link $n1 $n5 10Mb 10ms DropTail
$ns duplex-link $n2 $n5 10Mb 10ms DropTail
$ns duplex-link $n3 $n5 10Mb 10ms DropTail
$ns duplex-link $n4 $n5 10Mb 10ms DropTail
$ns duplex-link $n6 $n5 5Mb 20ms DropTail
#设定n5到n6之间的Queue Size为2000个封包大小
$ns queue-limit $n5 $n6 2000
#设定节点的位置,这是要给NAM用的
$ns duplex-link-op $n0 $n5 orient down
$ns duplex-link-op $n1 $n5 orient right-down
$ns duplex-link-op $n2 $n5 orient right
$ns duplex-link-op $n3 $n5 orient right-up
$ns duplex-link-op $n4 $n5 orient up
$ns duplex-link-op $n5 $n6 orient right
#观测n5到n6之间queue的变化,这是要给NAM用的
$ns duplex-link-op $n5 $n6 queuePos 0.5
#在n0节点建立一条UDP的连线
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set null0 [new Agent/Null]
$ns attach-agent $n6 $null0
$ns connect $udp0 $null0
#在UDP连线之上建立CBR应用程式
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
$cbr0 set type_ CBR
$cbr0 set packet_size_ 300
$cbr0 set rate_ 1mb
$cbr0 set random_ false
set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set null0 [new Agent/Null]
$ns attach-agent $n6 $null0
$ns connect $udp1 $null0
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
$cbr1 set type_ CBR
$cbr1 set packet_size_ 300
$cbr1 set rate_ 1mb
$cbr1 set random_ false
set udp2 [new Agent/UDP]
$ns attach-agent $n2 $udp2
set null0 [new Agent/Null]
$ns attach-agent $n6 $null0
$ns connect $udp2 $null0
set cbr2 [new Application/Traffic/CBR]
<< 上一页 [11] [12] [13] [14] [15] [16] [17] 下一页