The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ... The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

nmlab.korea.ac.kr
from nmlab.korea.ac.kr More from this publisher
12.07.2015 Views

virtual void process_data(int size, char* data) = 0;virtual void send_data(int size, char* data = 0);protected:Process* target_;};Process enables Application to link together.39.1.3 Transmitting user data over UDPCurrently there is no support in class Agent to transmit user data. There are two ways to transmit a serialized ADU throughtransport agents. First, for UDP agent (and all agents derived from there), we can derive from class UDP and add a newmethod send(int nbytes, char *userdata) to pass user data from Application to Agent. To pass data from anAgent to an Application is somewhat trickier: each agent has a pointer to its attached application, we dynamically cast thispointer to an AppConnector and then call AppConnector::process_data().As an example, we illustrate how class HttpInvalAgent is implemented. It is based on UDP, and is intended to deliver webcache invalidation messages (ns/webcache/inval-agent.h). It is defined as:class HttpInvalAgent : public Agent {public:HttpInvalAgent();virtual void recv(Packet *, Handler *);virtual void send(int realsize, AppData* data);protected:int off_inv_;};Here recv(Packet*, Handler*) overridden to extract user data, and a new send(int, AppData*) is providedto include user data in packetes. An application (HttpApp) is attached to an HttpInvalAgent using Agent::attachApp()(a dynamic cast is needed). In send(), the following code is used to write user data from AppData to the user data area in apacket:Packet *pkt = allocpkt(data->size());hdr_inval *ih = (hdr_inval *)pkt->access(off_inv_);ih->size() = data->size();char *p = (char *)pkt->accessdata();data->pack(p);In recv(), the following code is used to read user data from packet and to deliver to the attached application:hdr_inval *ih = (hdr_inval *)pkt->access(off_inv_);((HttpApp*)app_)->process_data(ih->size(), (char *)pkt->accessdata());Packet::free(pkt);343

39.1.4 Transmitting user data over TCPTransmitting user data using TCP is trickier than doing that over UDP, mainly because of TCP’s reassembly queue is onlyavailable for FullTcp. We deal with this problem by abstracting a TCP connection as a FIFO pipe.As indicated in section 38.2.4, transmission of application data can be implemented via agent upcalls. Assuming we are usingTCP agents, all data are delivered in sequence, which means we can view the TCP connection as a FIFO pipe. We emulateuser data transmission over TCP as follows. We first provide buffer for application data at the sender. Then we count the bytesreceived at the receiver. When the receiver has got all bytes of the current data transmission, it then gets the data directly fromthe sender. Class Application/TcpApp is used to implement this functionality.A TcpApp object contains a pointer to a transport agent, presumably either a FullTcp or a SimpleTcp. 1 (Currently TcpAppdoesn’t support asymmetric TCP agents, i.e., sender is separated from receiver). It provides the following OTcl interfaces:• connect: Connecting another TcpApp to this one. This connection is bi-directional, i.e., only one call to connectis needed, and data can be sent in either direction.• send: It takes two arguments: (nbytes, str). nbytes is the “nominal” size of application data. str is applicationdata in string form.In order to send application data in binary form, TcpApp provides a virtual C++ method send(int nbytes, intdsize, const char *data). In fact, this is the method used to implement the OTcl method send. Because it’sdifficult to deal with binary data in Tcl, no OTcl interface is provided to handle binary data. nbytes is the number of bytesto be transmitted, dsize is the actual size of the array data.TcpApp provides a C++ virtual method process_data(int size, char*data) to handle the received data. Thedefault handling is to treat the data as a tcl script and evaluate the script. But it’s easy to derive a class to provide other typesof handling.Here is an example of using Application/TcpApp. A similar example is Test/TcpApp-2node in ns/tcl/test/test-suitewebcache.tcl.First, we create FullTcp agents and connect them:set tcp1 [new Agent/TCP/FullTcp]set tcp2 [new Agent/TCP/FullTcp]# Set TCP parameters here, e.g., window_, iss_, . . .$ns attach-agent $n1 $tcp1$ns attach-agent $n2 $tcp2$ns connect $tcp1 $tcp2$tcp2 listenThen we create TcpApps and connect them:set app1 [new Application/TcpApp $tcp1]set app2 [new Application/TcpApp $tcp2]$app1 connect $app21 A SimpleTcp agent is used solely for web caching simulations. It is actually an UDP agent. It has neither error recovery nor flow/congestion control.It doesn’t do packet segmentation. Assuming a loss-free network and in-order packet delivery, SimpleTcp agent simplifies the trace files and hence aids thedebugging of application protocols, which, in our case, is the web cache consistency protocol.344

virtual void process_d<strong>at</strong>a(int size, char* d<strong>at</strong>a) = 0;virtual void send_d<strong>at</strong>a(int size, char* d<strong>at</strong>a = 0);protected:Process* target_;};Process enables Applic<strong>at</strong>ion to link together.39.1.3 Tra<strong>ns</strong>mitting user d<strong>at</strong>a over UDPCurrently there is no support in class Agent to tra<strong>ns</strong>mit user d<strong>at</strong>a. <strong>The</strong>re are two ways to tra<strong>ns</strong>mit a serialized ADU throughtra<strong>ns</strong>port agents. First, for UDP agent (<strong>and</strong> all agents derived from there), we can derive from class UDP <strong>and</strong> add a newmethod send(int nbytes, char *userd<strong>at</strong>a) to pass user d<strong>at</strong>a from Applic<strong>at</strong>ion to Agent. To pass d<strong>at</strong>a from anAgent to an Applic<strong>at</strong>ion is somewh<strong>at</strong> trickier: each agent has a pointer to its <strong>at</strong>tached applic<strong>at</strong>ion, we dynamically cast thispointer to an AppConnector <strong>and</strong> then call AppConnector::process_d<strong>at</strong>a().As an example, we illustr<strong>at</strong>e how class HttpInvalAgent is implemented. It is based on UDP, <strong>and</strong> is intended to deliver webcache invalid<strong>at</strong>ion messages (<strong>ns</strong>/webcache/inval-agent.h). It is defined as:class HttpInvalAgent : public Agent {public:HttpInvalAgent();virtual void recv(Packet *, H<strong>and</strong>ler *);virtual void send(int realsize, AppD<strong>at</strong>a* d<strong>at</strong>a);protected:int off_inv_;};Here recv(Packet*, H<strong>and</strong>ler*) overridden to extract user d<strong>at</strong>a, <strong>and</strong> a new send(int, AppD<strong>at</strong>a*) is providedto include user d<strong>at</strong>a in packetes. An applic<strong>at</strong>ion (HttpApp) is <strong>at</strong>tached to an HttpInvalAgent using Agent::<strong>at</strong>tachApp()(a dynamic cast is needed). In send(), the following code is used to write user d<strong>at</strong>a from AppD<strong>at</strong>a to the user d<strong>at</strong>a area in apacket:Packet *pkt = allocpkt(d<strong>at</strong>a->size());hdr_inval *ih = (hdr_inval *)pkt->access(off_inv_);ih->size() = d<strong>at</strong>a->size();char *p = (char *)pkt->accessd<strong>at</strong>a();d<strong>at</strong>a->pack(p);In recv(), the following code is used to read user d<strong>at</strong>a from packet <strong>and</strong> to deliver to the <strong>at</strong>tached applic<strong>at</strong>ion:hdr_inval *ih = (hdr_inval *)pkt->access(off_inv_);((HttpApp*)app_)->process_d<strong>at</strong>a(ih->size(), (char *)pkt->accessd<strong>at</strong>a());Packet::free(pkt);343

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!