【NS 2】nsに新しいプロトコルを追加する(pingプロトコル)

5789 ワード

次にpingプロトコルの追加を例にnsにプロトコルを追加する方法について説明します.
1、ヘッダファイルping.hまずデータ構造hdr_を定義するping、retは識別子で、pingのノードにパケットを送信するときは0、戻るときは1に設定されます.send_timeはタイムスタンプであり、rttを計算するためにパケットを送信する時間を記録する.
/*
 * File: Header File for a new 'Ping'Agent Class for the ns
 *       network simulator
 * Author: Marc Greis ([email protected]), 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_ping {
  char ret;
  double send_time;
};
class PingAgent : public Agent {
 public:
  PingAgent();
  int command(int argc, const char*const* argv);
  void recv(Packet*, Handler*);
 protected:
  int off_ping_;
};
#endif

2、関数の定義
/*
 * File: Code for a new 'Ping' AgentClass for the ns
 *       network simulator
 * Author: Marc Greis ([email protected]), May 1998
 *
 */
#include "ping.h"
static class PingHeaderClass : public PacketHeaderClass {
public:
  PingHeaderClass() : PacketHeaderClass("PacketHeader/Ping",
                   sizeof(hdr_ping)) {}
} class_pinghdr;
static class PingClass : public TclClass {
public:
  PingClass() : TclClass("Agent/Ping") {}
  TclObject* create(int, const char*const*) {
    return (new PingAgent());
  }
} class_ping;
PingAgent::PingAgent() : Agent(PT_PING)
{
  bind("packetSize_", &size_);
  bind("off_ping_", &off_ping_);
}
int PingAgent::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 packet:
      hdr_ping* hdr =(hdr_ping*)pkt->access(off_ping_);
      // Set the 'ret' field to 0, so the receivingnode knows
      // that it has to generate an echo packet
      hdr->ret = 0;
      // Store the current time in the 'send_time'field
      hdr->send_time =Scheduler::instance().clock();
      // Send the packet
      send(pkt, 0);
      // return TCL_OK, so the calling function knowsthat the
      // command has been processed
      return (TCL_OK);
    }
  }
  // If the command hasn't been processed by PingAgent()::command,
  // call the command() function for the base class
  return (Agent::command(argc, argv));
}
void PingAgent::recv(Packet* pkt, Handler*)
{
  // Access the IP header for the received packet:
  hdr_ip* hdrip = (hdr_ip*)pkt->access(off_ip_);
  // Access the Ping header for the received packet:
  hdr_ping* hdr = (hdr_ping*)pkt->access(off_ping_);
  // Is the 'ret' field = 0 (i.e. the receiving node is being pinged)?
  if (hdr->ret == 0) {
    // Send an 'echo'. First 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();
    // Access the Ping header for the new packet:
    hdr_ping* hdrret = (hdr_ping*)pktret->access(off_ping_);
    // Set the 'ret' field to 1, so the receiver won't sendanother echo
    hdrret->ret = 1;
    // Set the send_time field to the correct value
    hdrret->send_time = stime;
    // Send the packet
    send(pktret, 0);
  } else {
    // A packet was received. Use tcl.eval to call the Tcl
    // interpreter with the ping results.
    // Note: In the Tcl code, a procedure 'Agent/Ping recv {fromrtt}'
    // has to be defined which allows the user to react to theping
    // result.
    char out[100];
    // Prepare the output to the Tcl interpreter. Calculate theround
    // trip time
    sprintf(out, "%s recv %d %3.1f", name(),
           hdrip->src_ >> Address::instance().NodeShift_[1],
       (Scheduler::instance().clock()-hdr->send_time) * 1000);
    Tcl& tcl = Tcl::instance();
    tcl.eval_r(out);
    // Discard the packet
    Packet::free(pkt);
  }
}

3、nsファイルのいくつかの変更
packet.hに新しいpingパケットを追加
enum packet_t {
    PT_TCP,
    PT_UDP,
        ......
    // insert new packet types here
    PT_TFRC,
    PT_TFRC_ACK,
        PT_PING,    // packet protocol ID for our ping-agent
    PT_NTYPE // This MUST be the LAST one
};

また修正する
p_info()
クラス、追加#クラスツイカ#
"ping"
class p_info {
public:
    p_info() {
        name_[PT_TCP]= "tcp";
        name_[PT_UDP]= "udp";
               ...........
         name_[PT_TFRC]="tcpFriend";
        name_[PT_TFRC_ACK]="tcpFriendCtl";
               name_[PT_PING]="Ping";
       name_[PT_NTYPE]= "undefined";
    }
        .....
 }

makeを実行する前にmake dependを実行する必要があります.そうしないと、この2つのファイルはコンパイルできない可能性があります.
tcl/lib/ns-defaultも変更します.tclファイル.このファイルにはTCL objectのデフォルト値があります.次のコードを追加
Agent/Ping set packetSize_ 64

ファイルtcl/lib/ns-packet.tclのヘッダに以下のコードを追加
       { SRMEXT off_srm_ext_}
        { Ping off_ping_ }} {
set cl PacketHeader/[lindex $pair 0]

最後に、Makefileファイルを変更します.ピンをoファイルはnsのobjectファイルテーブルに追加されます.
sessionhelper.o delaymodel.o srm-ssm.o \
srm-topo.o \
ping.o \
$(LIB_DIR)int.Vec.o $(LIB_DIR)int.RVec.o \
$(LIB_DIR)dmalloc_support.o \

4、tclファイルは主にrecvプロセスであり、c++コードのrecv()関数を呼び出す.
Agent/Ping instproc recv {from rtt} {
        $self instvar node_
        puts "node [$node_ id] receivedping answer from \
       $from with round-trip-time $rtt ms."
}

より抜くhttp://endoing.spaces.live.com/blog/cns!4182DB20B664341D!181.entry?_c=BlogPart