BEGIN {
#the must format is { is at the same ling with BEGIN
highest_packet_id=0;
num_of_packet=0;
send=0; #记录符合条件的成功发送数据包的数量
receive=0; #记录符合条件的成功接受数据包的数量
repeat=0; #记录重复接受的数据包数量,一般都是由多路径性能来保证的
error=0; #记录异常情况的次数,如果是0 说明一起正常
}
{
#这里必须赋值为NULL,防止上一轮循环的脏数据
mac_type=NULL;
ip_packet_type=NULL;
packet_id=NULL;
ip_source_id=-1; #NULL等于0 冲突
ip_dest_id=-1;
node_id=NULL;
node_level=NULL;
drop_reason=NULL;
event=$1;
for(i=2;i<=NF;i=i+2)
{
#find the time
if($i=="-t")
{
j=i+1;
time=$j;
}
if($i=="-Ma")
{
j=i+1;
mac_type=$j;
}
if($i=="-It")
{
j=i+1;
ip_packet_type=$j;
}
if($i=="-Ii")
{
j=i+1;
packet_id=$j;
}
if($i=="-Is")
{
j=i+1;
ip_source_id=$j;
ip_source_id=int(ip_source_id);#取整
}
if($i=="-Id")
{
j=i+1;
ip_dest_id=$j;
ip_dest_id=int(ip_dest_id);#取整
}
if($i=="-Ni")
{
j=i+1;
node_id=$j;
}
if($i=="-Nl")
{
j=i+1;
node_level=$j;
}
if($i=="-Nw")
{
j=i+1;
drop_reason=$j;
}
}
if(mac_type=="13a" && node_level=="MAC" && event=="s" && ip_packet_type=="cbr" && ip_source_id!=-1&& ip_source_id==node_id && drop_reason=="---")
{
if(packet_id > highest_packet_id)
highest_packet_id=packet_id;
if(start_time[packet_id]==0)
{
start_time[packet_id]=time;
#printf("start_time[%d]=%10.9f/n",packet_id,time);
send++;
}
else
{
#error++;
}
}
#find the end time of packets
if(mac_type=="13a" && node_level=="MAC" && event=="r" && ip_packet_type=="cbr" && ip_dest_id!=-1 && node_id==ip_dest_id && drop_reason=="---")
{
if(start_time[packet_id]!=NULL)
{
if(end_time[packet_id]==NULL)
{
end_time[packet_id]=time;
#printf("end_time[%d]=%10.9f/n",packet_id,time);
receive++;
}
else
{
repeat++;
}
}
else
{
error++;
}
}
}
END {
packet_duration=0;
for(packet_id=0;packet_id <= highest_packet_id;packet_id++)
{
start=start_time[packet_id];
end=end_time[packet_id]
if(start!=NULL && end==NULL)
{
#printf("未发送到:%d /n",packet_id);
}
else if(start==NULL && end!=NULL)
{
#printf("多发送的:%d /n",packet_id);
}
else if(start < end)
{
packet_duration+=end-start;
if(num_of_packet==0)
num_of_packet=1;
else
num_of_packet++;
}
}
packet_duration=packet_duration/num_of_packet;
#printf("average delay:%f seconds/n",packet_duration);
if(error==0)
{
#分别为:平均延时,发送包数量,接受包数量,丢报率,多路径到达总数
printf("%10.9f %d %d %10.9f %d/n",packet_duration,send,receive,(send-receive)/send,repeat);
}
else
print "errors"
}