Simple multi-to-one transmission NS2.

Source: Internet
Author: User

Lab name: simple multi-to-one transmission Simulation

Purpose:
1. Study how to implement multi-to-one transmission.

Tutorial steps:

1. Write c ++ code and register the message header.


Let's talk about the multiple-to-one transmission method. At the beginning, the receiving end sends control packets to all sending terminals, telling them how many packets they want to send.

The sender sends an ACK message to the receiver as required.

After receiving the ACK packets from all senders, the receiver resends the control packets to all senders. In this way, the cycle starts again.

In order to communicate with multiple sending terminals, the receiving end must have multiple agents, and each app can only connect to one agent. In this way, there are multiple apps on the receiving end. How can these apps communicate? How do I know the status of each other? Intuitively, only one app is running at the receiving end, and multiple apps make us feel uncomfortable. Here I think of a small trick, which is also prompted when developing P2P On-Demand videos. That is, in addition to a general control app on each node, many connections need to be maintained. Connection is responsible for connecting to other nodes. These connections are actually special apps that control the underlying data transmission agent. At the same time, they report their own situation to the app of the general control, and the app of the general control schedules them. Speaking of this, you may have understood that in this simulation, I will use two layers of apps, the top layer is a master control app, and the lower layer apps control the agents they connect.
To this end, I have defined two classes inherited from the application: simpleapp and simpleconnection. Simpleconnection is the class that controls the agent at the underlying layer. Its definition is as follows:

Class simpleconnection: Public Application {public: simpleconnection (); // receives and processes packets from the agent. If it is a control packet, the packet is sent as required. // If it is an ACK packet, it is reported to the app on the top layer. // This interface is not originally available for application. Therefore, you need to modify app. h and add this interface. Virtual void recv_msg (INT nbytes, const char * MSG = 0) end_ctrl (); R/> // sends control packets. The parameter indicates the number of packets sent by the other party. Void send_ctrl (INT); // send data packets. Each time one packet is sent, the number of packets to be sent is reduced by 1. When it is reduced to 0, an ACK message is sent to the other party. Void send_data_pkt (); // whether the task of sending packets has been completed. inline int done (); // you can specify inline void app (simpleapp *) as the pointer to the upper-layer app. protected: double next_send_time (); // calculate the time when the next data packet is sent. Void send_ack (); // send the ACK message simpleapp * app _ to the other party _; // pointer to the upper-layer app int done _; // whether the task int tosend _ has been completed; // The number of packets to be sent sendtimer timer _; // a timer, the scheduling int pktsize _; // double rate _ of the data packet size; // The connection speed, that is, the number of bits per second };

 

Simpleapp is a class for upper-layer scheduling and is a real application. Its definition is as follows:

// ================================================ ======================/// App: control Multi connections // ========================================== ========================== class simpleapp: public Application {public: simpleapp (); // check whether all senders have completed the task. // If yes, send new control packets to them. Void is_all_done (); protected: void start (); // start running, set running _ to true, and send the Control Message void stop () to all senders (); // set running _ to false // Add a new connection to the linked list void add_connection (simpleconnection *); void send_ctrl (); // send control packets to all senders // defines the commands that can be called in TCl. the add-connection command virtual int command (INT argc, const char * const * argv); Private: vector cons _; // The linked list bool running _ that stores all connection pointers; // running status };

  

In addition, we also define our own agent to facilitate control over the agent. We re-implemented the sendmsg and Recv functions to facilitate packet processing.
Initially, I also wanted to use the original udpagent. However, it is found that both sendmsg and Recv process the appdata in the message, which makes it impossible for me to process the custom message header.
By reading the definition code of appdata in the ns-process.h, it is found that appdata only has one data member, which is the data type and cannot contain the information of the packet header. In fact, the packet header information is in packet's data member bits.
"Furious", I overwrote my agent.

The following is the definition of the custom packet header:

//=============================================// new packet header//=============================================struct hdr_simple{int type; //0-normal,1-ctrl,2-ackint pkt_num; //the number of packets to sendstatic int offset_;inline static int& offset() { return offset_; }inline static hdr_simple* access(const Packet* p) {return (hdr_simple*) p->access(offset_);}};

 

Do not forget to register your own header in packet. h and ns-packet.tcl, and of course, do not forget to define a linkage in the. CC file. For more information, see ns by example.

2. Re-compile ns.
After the above work is done, put the code file in a directory and change the makefile. For example, the definitions and implementations of all my classes are written in simple. h and simple. CC, and I put them under the apps directory. I changed the makefile as follows:

...common/ivs.o common/messpass.o common/tp.o common/tpm.o apps/worm.o apps/simple.o tcp/tcp.o tcp/tcp-sink.o tcp/tcp-reno.o ...

Only the red part is added by me. It's easy. Save the makefile and re-compile it. If no error is reported, the compilation is successful.

3. Write the Tcl script.
In fact, I did not write the Tcl script after the NS2. instead, I did it before. In this way, when I write a script, I will know what functions I need and what TCL commands can call. This is equivalent to analyzing user requirements. My TCL code is as follows:

 1 # by jiqing 2007-6-6  2 set ns [new Simulator]  3 #color  4 $ns color 1 Blue  5 $ns color 2 Red 6 $ns color 3 Green 7  8 #open a nam trace file  9 set nf [open out.nam w] 10 $ns namtrace-all $nf 11 12 #open a trace file 13 set tf [open out.tr w] 14 $ns trace-all $tf15 16 #finish procedure17 18 proc finish {} {19 global ns nf tf 20 $ns flush-trace21 close $nf22 close $tf23 exec ./nam out.nam & exit 024 } 25 26 #create nodes 27 set node_(s1) [$ns node] 28 set node_(s2) [$ns node] 29 set node_(s3) [$ns node] 30 set node_(r) [$ns node]31 32 #create links33 $ns duplex-link $node_(s1) $node_(r) 1Mb 10ms DropTail 34 $ns duplex-link $node_(s2) $node_(r) 1.5Mb 10ms DropTail 35 $ns duplex-link $node_(s3) $node_(r) 1Mb 10ms DropTail36 37 #relayout 38 $ns duplex-link-op $node_(s1) $node_(r) orient right-down 39 $ns duplex-link-op $node_(s2) $node_(r) orient right 40 $ns duplex-link-op $node_(s3) $node_(r) orient right-up 41 42 #add udp agents 43 set udp_(s1) [new Agent/JiqingUDP] 44 $ns attach-agent $node_(s1) $udp_(s1) 45 set udp_(r1) [new Agent/JiqingUDP] 46 $ns attach-agent $node_(r) $udp_(r1) 47 $ns connect $udp_(s1) $udp_(r1) $udp_(s1) set fid_ 1 $udp_(r1) set fid_ 1 48 set udp_(s2) [new Agent/JiqingUDP] 49 $ns attach-agent $node_(s2) $udp_(s2) set udp_(r2) [new Agent/JiqingUDP] $ns attach-agent $node_(r) $udp_(r2) $ns connect $udp_(s2) $udp_(r2) $udp_(s2) set fid_ 2 $udp_(r2) set fid_ 2 set udp_(s3) [new Agent/JiqingUDP] $ns attach-agent $node_(s3) $udp_(s3) set udp_(r3) [new Agent/JiqingUDP] $ns attach-agent $node_(r) $udp_(r3) $ns connect $udp_(s3) $udp_(r3) $udp_(s3) set fid_ 3 $udp_(r3) set fid_ 3 #add simpleconnections set app_(s1) [new Application/SimpleCon] $app_(s1) attach-agent $udp_(s1) set app_(s2) [new Application/SimpleCon] $app_(s2) attach-agent $udp_(s2) set app_(s3) [new Application/SimpleCon] $app_(s3) attach-agent $udp_(s3) set app_(r1) [new Application/SimpleCon] $app_(r1) attach-agent $udp_(r1) set app_(r2) [new Application/SimpleCon] $app_(r2) attach-agent $udp_(r2) set app_(r3) [new Application/SimpleCon] $app_(r3) attach-agent $udp_(r3) set app_(global1) [new SIMPLE_APP] $app_(global1) add-connection $app_(s1) set app_(global2) [new SIMPLE_APP] $app_(global2) add-connection $app_(s2) set app_(global3) [new SIMPLE_APP] $app_(global3) add-connection $app_(s3) set app_(global4) [new SIMPLE_APP] $app_(global4) add-connection $app_(r1) $app_(global4) add-connection $app_(r2) $app_(global4) add-connection $app_(r3) 

#schedule...
$ns at 0.1 "$app_(global4) start"
$ns at 4.0 "$app_(global4) stop"
$ns at 4.5 "finish"
$ns run

 

This script defines three senders, one receiver, and each peer runs a simpleapp, and each agent connects to a simpleconnection, then, connect the app to the connection through add-connection.

 

4. Simulation.
There is nothing to say about this step./ns XXX. TCL.

5. Use awk to process data.
It depends on what you want to calculate. I'm bored. I just want to calculate the number of packets sent by each sender at a time.

 BEGIN{ sended1 = 0; sended2 = 0; sended3 = 0; i = 0;}{ action = $1; time = $2; sender = $3; #you may use digital or character if(action=="r" &&( sender==0 ||sender==1 || sender==2) ) {  if(sender == 0)   sended1++;  else if( sender == 1)   sended2++;  else   sended3++;  #don‘t begin with digital  _1send[i] = sended1;  _2send[i] = sended2;  _3send[i] = sended3;  time_point[i] = time;  i++; }}END{ printf("%.2f\t%.2f\t%.2f\t%.2f\n",0,0,0,0); for(j=0;j  printf("%.2f\t%.2f\t%.2f\t%.2f\n",time_point[j],_1send[j],_2send[j],_3send[j]); }}

 

Write the code and save it as measure_pkts.awk.
Run the following command line:
Awk-F measure_pkts.awk> results.txt
The result is written into results.txt.

6. Use gnuplot to draw images.
Finally, it is time to see the results. Go to gnuplot:
Plot "results.txt" using 2, "results.txt" using 3, "results.txt" using 4
You can see the result.
 

Experiment results:

There is nothing to talk about, and the goal has been achieved.

References:
1. Jae Chung, Mark Claypool. ns by example.
2. Dr. ke zhiheng's "add and modify mongo.htm ".
3. Source code of BitTorrent simulation.
4. Source code of NS2.

Simple multi-to-one transmission NS2.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.