如何在 Wireless broadcast 封包?
2007/12/19
2007/12/17
NS2 快速入門: DV-distance 模擬 (5)
存取實體層
Reference: http://www.baisi.net/viewthread.php?tid=13936
節錄片段程式碼:
dv-distance/dv-distance.cc
[cpp]
int DV_DistanceAgent::command(int argc, const char*const* argv) {
Node* self_ = Node::get_node_by_address(addr());
printf("S: self->nodeid(): %d\n", self_->nodeid());
}
void DV_DistanceAgent::recv(Packet* pkt, Handler*) {
printf("Recv OK\n");
printf("R: pkt->txinfo_.getNode()->nodeid(): %d\n", pkt->txinfo_.getNode()->nodeid());
printf("R: txinfo_.getTxPr(): %.20lf\n", pkt->txinfo_.getTxPr());
printf("R: txinfo_.getLambda(): %.20lf\n", pkt->txinfo_.getLambda());
printf("R: txinfo_.RxPr: %.20lf\n", pkt->txinfo_.RxPr);
printf("R: distance: %.20lf\n", this->getDist(pkt));
}
double DV_DistanceAgent::getDist(Packet* pkt) const {
Antenna* ant = pkt->txinfo_.getAntenna();
WirelessPhy* phy = (WirelessPhy*)(pkt->txinfo_.getNode()->ifhead().lh_first);
double lambda = pkt->txinfo_.getLambda();
double Pr = pkt->txinfo_.RxPr;
double Pt = pkt->txinfo_.getTxPr();
double Gt = ant->getTxGain(ant->getX(), ant->getY(), ant->getZ(), lambda);
double Gr = ant->getRxGain(ant->getX(), ant->getY(), ant->getZ(), lambda);
double ht = ant->getZ();
double hr = ant->getZ();
double L = phy->getL();
return phy->getDist(Pr, Pt, Gt, Gr, ht, hr, L, lambda);
}
[/cpp]
Reference: http://www.baisi.net/viewthread.php?tid=13936
節錄片段程式碼:
dv-distance/dv-distance.cc
[cpp]
int DV_DistanceAgent::command(int argc, const char*const* argv) {
Node* self_ = Node::get_node_by_address(addr());
printf("S: self->nodeid(): %d\n", self_->nodeid());
}
void DV_DistanceAgent::recv(Packet* pkt, Handler*) {
printf("Recv OK\n");
printf("R: pkt->txinfo_.getNode()->nodeid(): %d\n", pkt->txinfo_.getNode()->nodeid());
printf("R: txinfo_.getTxPr(): %.20lf\n", pkt->txinfo_.getTxPr());
printf("R: txinfo_.getLambda(): %.20lf\n", pkt->txinfo_.getLambda());
printf("R: txinfo_.RxPr: %.20lf\n", pkt->txinfo_.RxPr);
printf("R: distance: %.20lf\n", this->getDist(pkt));
}
double DV_DistanceAgent::getDist(Packet* pkt) const {
Antenna* ant = pkt->txinfo_.getAntenna();
WirelessPhy* phy = (WirelessPhy*)(pkt->txinfo_.getNode()->ifhead().lh_first);
double lambda = pkt->txinfo_.getLambda();
double Pr = pkt->txinfo_.RxPr;
double Pt = pkt->txinfo_.getTxPr();
double Gt = ant->getTxGain(ant->getX(), ant->getY(), ant->getZ(), lambda);
double Gr = ant->getRxGain(ant->getX(), ant->getY(), ant->getZ(), lambda);
double ht = ant->getZ();
double hr = ant->getZ();
double L = phy->getL();
return phy->getDist(Pr, Pt, Gt, Gr, ht, hr, L, lambda);
}
[/cpp]
2007/12/15
NS2 快速入門: DV-distance 模擬 (4)
先新增一個自訂的 Agent
之後在修改執行演算法的部份來跑自訂的 protocol
新增 Agent 的方法如下:
(附註: 這裡的 ns2 目錄是指 ns2 所在的目錄,每個人不一定一樣,
ex: ~/ns-allinone-2.33/ns-2.33)
ns2/dv-distance/dv-distance.h
[cpp]
#ifndef ns_dv_distance_h
#define ns_dv_distance_h
#include "agent.h"
#include "tclcl.h"
#include "packet.h"
#include "address.h"
#include "ip.h"
struct hdr_dv_distance {
// Header access method
static int offset_; // required by PacketHeaderManager
inline static int& offset() { return offset_; }
inline static hdr_dv_distance* access(const Packet* p) {
return (hdr_dv_distance*) p->access(offset_);
}
};
class DV_DistanceAgent : public Agent {
public:
DV_DistanceAgent();
virtual int command(int argc, const char*const* argv);
virtual void recv(Packet*, Handler*);
};
#endif // ns_dv_distance_h
[/cpp]
ns2/dv-distance/dv-distance.cc
[cpp]
#include "dv-distance.h"
int hdr_dv_distance::offset_;
static class DV_DistanceHeaderClass : public PacketHeaderClass {
public:
DV_DistanceHeaderClass() : PacketHeaderClass("PacketHeader/DV_Distance",
sizeof(hdr_dv_distance)) {
bind_offset(&hdr_dv_distance::offset_);
}
} class_dv_distancehdr;
static class DV_DistanceClass : public TclClass {
public:
DV_DistanceClass() : TclClass("Agent/DV_Distance") {}
TclObject* create(int, const char*const*) {
return (new DV_DistanceAgent());
}
} class_dv_distance;
DV_DistanceAgent::DV_DistanceAgent() : Agent(PT_DV_DISTANCE) {
//bind("packetSize_", &size_);
}
int DV_DistanceAgent::command(int argc, const char*const* argv) {
if(argc == 2) {
if(strcmp(argv[1], "start") == 0) {
// Create a new packet
Packet* pkt = allocpkt();
// Access the DV_Distance header for the new packet
hdr_dv_distance* hdr = hdr_dv_distance::access(pkt);
// Send the packet
send(pkt, 0);
// return TCL_OK, so the calling function knows that
// the command has been processed
return (TCL_OK);
}
}
return (Agent::command(argc, argv));
}
void DV_DistanceAgent::recv(Packet* pkt, Handler*) {
// Access the IP header for the received packet
hdr_ip* hdrip = hdr_ip::access(pkt);
// Access the DV_distance header for the received packet
hdr_dv_distance* hdr = hdr_dv_distance::access(pkt);
printf("Recv OK");
Packet::free(pkt);
}
[/cpp]
接著要把自訂的 Agent 加到 NS2 中
才能在 NS2 中使用
1. 修改 ns2/Makefile.in
在 OBJ_CC = 的地方加上 dv-distance/dv-distance.o
(注意:修改 Makefile 無效,Makefile 會在每次 configure 後依據 Makefile.in 產生)
2. 修改 ns2/common/packet.h 加上新的 packet type
ns 2.32 以前:
在 enum packet_t 加上 PT_DV_DISTANCE
(不能加在最後一個,PT_NTYPE 必須最後)
在 class p_info 的 public: p_info() { } 內
加上 name_[PT_DV_DISTANCE]="DV_Distance";
ns 2.33 以後:
在 static packet_t PT_NTYPE 前加上
static const packet_t PT_DV_DISTANCE = XX;
XX 為其他 packet type 沒用過的數字 (ex: 62)
(注意:一定要加在 PT_NTYPE 之前,且 PT_NTYPE 的 value 一定要最大,
否則發送 packet 時,會出現 Segmentation fault (core dumped) 無法正常運作)
在 class p_info 的 public: static void initName() { } 內
name_[PT_NTYPE]= "undefined"; 附近加上
name_[PT_DV_DISTANCE]="DV_Distance";
建議和加 PT_DV_DISTANCE 一樣
加在 name_[PT_NTYPE]= "undefined"; 之前
3. 修改 trace/cmu-trace.cc (非必要)
在 void CMUTrace::format(Packet* p, const char *why) {} 加上 case PT_DV_DISTANCE:
4. 修改 tcl/lib/ns-packet.tcl (非必要)
在 foreach prot { } 加上 DV_Distance
加這個是為了方便在模擬時
可以用下列方式寫在 tcl 內
只顯示想要觀察的 packet type
[code]remove-packet-header AODV ARP DV_Distance
# ...
set ns [new Simulator][/code]
[code]remove-all-packet-headers
add-packet-header AODV ARP DV_Distance
# ...
set ns [new Simulator][/code]
大功告成
開始編譯
1. 有改 Makefile.in 或 XXX.h 檔時
在 ns2 目錄下執行 ./configure; make clean; make depend; make
2. 只改 source code (XXX.cc 檔) 時
在 ns2 目錄下直接執行 make 就可以了
3. 只改 XXX.h 檔
如果不想等太久,可以 touch XXX.cc 後再 make
ex: touch dv-distance/dv-distance.cc; make
但不保證能正常運作
附註:若編譯時出現關於 proxytrace2any.cc 的錯誤,請參考這篇文章,以我的經驗如果是第一次編譯,2.32和2.33都會出現這個問題,可以先修改後再編譯,才不用花很久的時間重新編譯
之後在修改執行演算法的部份來跑自訂的 protocol
新增 Agent 的方法如下:
(附註: 這裡的 ns2 目錄是指 ns2 所在的目錄,每個人不一定一樣,
ex: ~/ns-allinone-2.33/ns-2.33)
ns2/dv-distance/dv-distance.h
[cpp]
#ifndef ns_dv_distance_h
#define ns_dv_distance_h
#include "agent.h"
#include "tclcl.h"
#include "packet.h"
#include "address.h"
#include "ip.h"
struct hdr_dv_distance {
// Header access method
static int offset_; // required by PacketHeaderManager
inline static int& offset() { return offset_; }
inline static hdr_dv_distance* access(const Packet* p) {
return (hdr_dv_distance*) p->access(offset_);
}
};
class DV_DistanceAgent : public Agent {
public:
DV_DistanceAgent();
virtual int command(int argc, const char*const* argv);
virtual void recv(Packet*, Handler*);
};
#endif // ns_dv_distance_h
[/cpp]
ns2/dv-distance/dv-distance.cc
[cpp]
#include "dv-distance.h"
int hdr_dv_distance::offset_;
static class DV_DistanceHeaderClass : public PacketHeaderClass {
public:
DV_DistanceHeaderClass() : PacketHeaderClass("PacketHeader/DV_Distance",
sizeof(hdr_dv_distance)) {
bind_offset(&hdr_dv_distance::offset_);
}
} class_dv_distancehdr;
static class DV_DistanceClass : public TclClass {
public:
DV_DistanceClass() : TclClass("Agent/DV_Distance") {}
TclObject* create(int, const char*const*) {
return (new DV_DistanceAgent());
}
} class_dv_distance;
DV_DistanceAgent::DV_DistanceAgent() : Agent(PT_DV_DISTANCE) {
//bind("packetSize_", &size_);
}
int DV_DistanceAgent::command(int argc, const char*const* argv) {
if(argc == 2) {
if(strcmp(argv[1], "start") == 0) {
// Create a new packet
Packet* pkt = allocpkt();
// Access the DV_Distance header for the new packet
hdr_dv_distance* hdr = hdr_dv_distance::access(pkt);
// Send the packet
send(pkt, 0);
// return TCL_OK, so the calling function knows that
// the command has been processed
return (TCL_OK);
}
}
return (Agent::command(argc, argv));
}
void DV_DistanceAgent::recv(Packet* pkt, Handler*) {
// Access the IP header for the received packet
hdr_ip* hdrip = hdr_ip::access(pkt);
// Access the DV_distance header for the received packet
hdr_dv_distance* hdr = hdr_dv_distance::access(pkt);
printf("Recv OK");
Packet::free(pkt);
}
[/cpp]
接著要把自訂的 Agent 加到 NS2 中
才能在 NS2 中使用
1. 修改 ns2/Makefile.in
在 OBJ_CC = 的地方加上 dv-distance/dv-distance.o
(注意:修改 Makefile 無效,Makefile 會在每次 configure 後依據 Makefile.in 產生)
2. 修改 ns2/common/packet.h 加上新的 packet type
ns 2.32 以前:
在 enum packet_t 加上 PT_DV_DISTANCE
(不能加在最後一個,PT_NTYPE 必須最後)
在 class p_info 的 public: p_info() { } 內
加上 name_[PT_DV_DISTANCE]="DV_Distance";
ns 2.33 以後:
在 static packet_t PT_NTYPE 前加上
static const packet_t PT_DV_DISTANCE = XX;
XX 為其他 packet type 沒用過的數字 (ex: 62)
(注意:一定要加在 PT_NTYPE 之前,且 PT_NTYPE 的 value 一定要最大,
否則發送 packet 時,會出現 Segmentation fault (core dumped) 無法正常運作)
在 class p_info 的 public: static void initName() { } 內
name_[PT_NTYPE]= "undefined"; 附近加上
name_[PT_DV_DISTANCE]="DV_Distance";
建議和加 PT_DV_DISTANCE 一樣
加在 name_[PT_NTYPE]= "undefined"; 之前
3. 修改 trace/cmu-trace.cc (非必要)
在 void CMUTrace::format(Packet* p, const char *why) {} 加上 case PT_DV_DISTANCE:
4. 修改 tcl/lib/ns-packet.tcl (非必要)
在 foreach prot { } 加上 DV_Distance
加這個是為了方便在模擬時
可以用下列方式寫在 tcl 內
只顯示想要觀察的 packet type
[code]remove-packet-header AODV ARP DV_Distance
# ...
set ns [new Simulator][/code]
[code]remove-all-packet-headers
add-packet-header AODV ARP DV_Distance
# ...
set ns [new Simulator][/code]
大功告成
開始編譯
1. 有改 Makefile.in 或 XXX.h 檔時
在 ns2 目錄下執行 ./configure; make clean; make depend; make
2. 只改 source code (XXX.cc 檔) 時
在 ns2 目錄下直接執行 make 就可以了
3. 只改 XXX.h 檔
如果不想等太久,可以 touch XXX.cc 後再 make
ex: touch dv-distance/dv-distance.cc; make
但不保證能正常運作
附註:若編譯時出現關於 proxytrace2any.cc 的錯誤,請參考這篇文章,以我的經驗如果是第一次編譯,2.32和2.33都會出現這個問題,可以先修改後再編譯,才不用花很久的時間重新編譯
2007/12/14
NS2 2.32 make error with "proxytrace2any.cc"
Problem: NS2 2.32 make error with "proxytrace2any.cc"
Solution: Modify file "ns-allinone-2.32/ns-2.32/indep-utils/webtrace-conv/dec/my-endian.h"
...
proxytrace2any.cc: In function `int main(int, char**)':
proxytrace2any.cc:112: error: `IsLittleEndian' undeclared (first use this function)
proxytrace2any.cc:112: error: (Each undeclared identifier is reported only once for each function it appears in.)
proxytrace2any.cc:120: error: `ToOtherEndian' undeclared (first use this function)
make[1]: *** [proxytrace2any.o] Error 1
make[1]: Leaving directory `/home/sokoyo/ns-allinone-2.32/ns-2.32/indep-utils/webtrace-conv/dec'
make[1]: Entering directory `/home/sokoyo/ns-allinone-2.32/ns-2.32/indep-utils/webtrace-conv/epa'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/home/sokoyo/ns-allinone-2.32/ns-2.32/indep-utils/webtrace-conv/epa'
make[1]: Entering directory `/home/sokoyo/ns-allinone-2.32/ns-2.32/indep-utils/webtrace-conv/nlanr'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/home/sokoyo/ns-allinone-2.32/ns-2.32/indep-utils/webtrace-conv/nlanr'
make[1]: Entering directory `/home/sokoyo/ns-allinone-2.32/ns-2.32/indep-utils/webtrace-conv/ucb'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/home/sokoyo/ns-allinone-2.32/ns-2.32/indep-utils/webtrace-conv/ucb'
Solution: Modify file "ns-allinone-2.32/ns-2.32/indep-utils/webtrace-conv/dec/my-endian.h"
-#ifndef _ENDIAN_H_
-#define _ENDIAN_H_
+#ifndef _MY_ENDIAN_H_
+#define _MY_ENDIAN_H_
...
2007/12/13
NS2 快速入門: DV-distance 模擬 (3)
節錄使用 Hierarchical Addressing 片段
參考
ns-2/tcl/test/test-suite-WLtutorial.tcl
ns-2/tcl/test/test-suite-wireless-lan-aodv.tcl
ns-2/tcl/test/test-suite-wireless-lan-tora.tcl
case 1
[code]
$ns node-config -wiredRouting OFF
$ns node-config -addressType hierarchical
AddrParams set domain_num_ 2
lappend cluster_num 1 2
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel 1 2 2
AddrParams set nodes_num_ $eilastlevel
set n0 [$ns node 0.0.0]
$n0 random-motion 0
$n0 set X_ 301
$n0 set Y_ 399
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node 1.0.0]
$n1 random-motion 0
$n1 base-station [AddrParams addr2id [$n0 node-addr]]
$n1 set X_ 480
$n1 set Y_ 544
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
[/code]
case 2
[code]
puts "Hierarchical Routing..."
set naddr {0.0.0 \
0.0.1 \
0.0.2 \
0.0.3 \
0.0.4 \
0.0.5 \
0.0.6 \
0.0.7 \
0.0.8 \
0.0.9 \
0.0.10 \
0.0.11 \
0.0.12 \
0.0.13 \
0.0.14 \
0.0.15 \
0.0.16 \
0.0.17 \
0.0.18 \
0.0.19}
# Create 20 nodes
for {set i 0} {$i < $opt(nn)} {incr i} {
set node($i) [$ns node [lindex $naddr $i]]
$node($i) random-motion 0
$node($i) base-station [AddrParams addr2id [$node(0) node-addr]]
}
[/code]
參考
ns-2/tcl/test/test-suite-WLtutorial.tcl
ns-2/tcl/test/test-suite-wireless-lan-aodv.tcl
ns-2/tcl/test/test-suite-wireless-lan-tora.tcl
case 1
[code]
$ns node-config -wiredRouting OFF
$ns node-config -addressType hierarchical
AddrParams set domain_num_ 2
lappend cluster_num 1 2
AddrParams set cluster_num_ $cluster_num
lappend eilastlevel 1 2 2
AddrParams set nodes_num_ $eilastlevel
set n0 [$ns node 0.0.0]
$n0 random-motion 0
$n0 set X_ 301
$n0 set Y_ 399
$n0 set Z_ 0.0
$ns initial_node_pos $n0 20
set n1 [$ns node 1.0.0]
$n1 random-motion 0
$n1 base-station [AddrParams addr2id [$n0 node-addr]]
$n1 set X_ 480
$n1 set Y_ 544
$n1 set Z_ 0.0
$ns initial_node_pos $n1 20
[/code]
case 2
[code]
puts "Hierarchical Routing..."
set naddr {0.0.0 \
0.0.1 \
0.0.2 \
0.0.3 \
0.0.4 \
0.0.5 \
0.0.6 \
0.0.7 \
0.0.8 \
0.0.9 \
0.0.10 \
0.0.11 \
0.0.12 \
0.0.13 \
0.0.14 \
0.0.15 \
0.0.16 \
0.0.17 \
0.0.18 \
0.0.19}
# Create 20 nodes
for {set i 0} {$i < $opt(nn)} {incr i} {
set node($i) [$ns node [lindex $naddr $i]]
$node($i) random-motion 0
$node($i) base-station [AddrParams addr2id [$node(0) node-addr]]
}
[/code]
Subscribe to:
Posts (Atom)