{ SRMEXT off_srm_ext_} { Ping off_ping_ }} { set cl PacketHeader/[lindex $pair 0] |
#foreach pair {
# { LDP off_ldp_ }
{ Myping off_ping_ }
# }{
# create-packet-header [lindex $pair 0] [lindex $pair 1]
}
将该foreach和create-packet-
其他的操作步骤按照tutorial来做基本就可以了。
将添加myping协议的整体步骤总结如下:
一、编写c++程序
(1)在ns2.34目录下创建villasy子目录,
myping.h:对ping包头结构体定义hdr_
myping.cc:
(2)对C++程序与OTcl程序互访问的原理;
二、修改ns-2相关配置
(1)ns2.34/common/packet.h,
(2)ns2.34/tcl/lib/ns-packet.
三、修改ns2.34下Makefile文件
为了编译修改过的结果,在Makefile中找到链接.
四、编写myping.tcl文件,该文件用来设定场景
如tutorial所述
最后,ns villasy/myping.tcl,得到结果,
myping.h
/*
* File: Header File for a new 'Ping' Agent Class for the ns
* network simulator
* Author: Marc Greis (greis@cs.uni-bonn.de), May 1998
*
*/
#ifndef ns_ping_h
#define ns_ping_h
#include "agent.h"
#include "tclcl.h"
#include "packet.h"
#include "address.h"
#include "ip.h"
struct hdr_myping {
char ret;
double send_time;
//header access methods;
static int offset_;
inline static hdr_myping* access(const Packet* p){
return (hdr_myping*)p->access(offset_);
}
};
class MyPingAgent : public Agent {
public:
MyPingAgent();
int command(int argc, const char*const* argv);
void recv(Packet*, Handler*);
protected:
int off_ping_;
int packetSize_;
//int off_ip_;
};
#endif
myping.cc
#include "myping.h"
static class MyPingHeaderClass : public PacketHeaderClass{
public:
MyPingHeaderClass() : PacketHeaderClass("PacketHeader/Myping",sizeof(hdr_myping)){}
}class_mypinghdr;
static class MyPingClass : public TclClass{
public:
MyPingClass() : TclClass("Agent/Myping"){}
TclObject* create(int ,const char*const*){
return (new MyPingAgent());
}
}class_myping;
//PingAgent constructor
MyPingAgent::MyPingAgent() : Agent(PT_MYPING){
bind("packetSize_",&size_);
//bind("off_ping_",&off_ping_);
//bind("off_ip_",&off_ip_);
}
int MyPingAgent::command(int argc,const char*const* argv){
if(argc ==2){
if(strcmp(argv[1],"send") == 0){
//create a new packet
Packet *pkt = allocpkt();
//access the ping header for the new header
hdr_myping *hdr = (hdr_myping*)pkt->access(off_ping_);
//set the 'ret' field to 0,so the receiving node knows
//that it has to generate an echo packet
hdr->ret = 0;
//store the current time in 'send_time' field
hdr->send_time = Scheduler::instance().clock();
//send the packet
send(pkt,0);
//return TCL_OK,so the calling function knows that
//the command has been processed
return (TCL_OK);
}
}
//if the command has not been processed by PingAgent()::command,
//call the command() function for the base class
return (Agent::command(argc,argv));
}
void MyPingAgent::recv(Packet* pkt, Handler*){
//acess the Ping header for the received packet
hdr_myping* hdr = (hdr_myping*)pkt->access(off_ping_);
//access the ip header for the received packet
hdr_ip* hdrip = (hdr_ip*)pkt->access(off_ip_);
//is the 'ret' field = 0(the receiving node is being pinged)
if(hdr->ret ==0){
//send an 'echo'. save the old packet's send_time
double stime = hdr->send_time;
//discard the packet
Packet::free(pkt);
//create a new packet
Packet* pktret = allocpkt();
//acess the Ping header for the new packet
hdr_myping* hdrret = (hdr_myping*)pktret->access(off_ping_);
//set the 'ret' field to 1,so the receiver won't send anothe
hdrret->ret = 1;
//set the send_time field to the correct value
hdrret->send_time = stime;
send(pktret,0);
}else{
//use tcl.eval to call the Tcl interpreter
char out[100];
sprintf(out,"%s recv %d %3.1f", name(), hdrip->src_.addr_ >> Address::instance().NodeShift_[1],(Scheduler::instance().clock()-hdr->send_time)*1000);
Tcl& tcl = Tcl::instance();
tcl.eval(out);
//discard the packet
Packet::free(pkt);
}
}
#Create a simulator object
set ns [new Simulator]
#Open a trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
#Create three nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
#Connect the nodes with two links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
#Define a 'recv' function for the class 'Agent/Ping'
Agent/Myping instproc recv {from rtt} {
$self instvar node_
puts "node [$node_ id] received ping answer from \
$from with round-trip-time $rtt ms."
}
#Create two ping agents and attach them to the nodes n0 and n2
set p0 [new Agent/Myping]
$ns attach-agent $n0 $p0
set p1 [new Agent/Myping]
$ns attach-agent $n2 $p1
#Connect the two agents
$ns connect $p0 $p1
#Schedule events
$ns at 0.2 "$p0 send"
$ns at 0.4 "$p1 send"
$ns at 0.6 "$p0 send"
$ns at 0.6 "$p1 send"
$ns at 1.0 "finish"
#Run the simulation
$ns run