Multiple connections to a single agent
From Nsnam
When writing a new Agent, it may be desirable (or necessary) to have an agent that can be connected to more than one other agent. Consider the following topolgy:
A1 A2 A3 | | | N1 - N2 - N3
We have three nodes (N1, N2, N3) to which we want to attach three possibly different agents (A1, A2, A3). In Tcl, we can connect the agents to the nodes using the commands:
$ns attach-agent $N1 $A1 $ns attach-agent $N1 $A2 $ns attach-agent $N1 $A3
We can then connect two agents using the command:
$ns connect $A1 $A2
However, there may be some cases where using the command $ns connect $A2 $A3 will not produce the desired result. In this case, one must take care when writing code in A2 that should send a packet to A3. In particular, A2 must specifically set the destination address AND the destination port.
A simple example might look like this:
void A2::sendPacketToA3(){
Packet* pkt = allocpkt(); // create a packet
hdr_ip* hdrip = hdr_ip::access(pkt); // Access the IP header
hdrip->saddr() = addr(); // IP address of N2
hdrip->sport() = port(); // Port at which A2 is attached
hdrip->daddr() = n3_addr; // IP address of N3
hdrip->dport() = a3_port; // Port at which A3 is attached
send(pkt, 0); //send the packet
}
Note that the tricky part about this code is obtaining the address and port of N3 and A3. In this case, one might wish to use a different method of attaching an agent to a node. In Tcl, one could use the attach command:
# <node> attach <agent> <port> $N3 attach $A3 0
In this case I know that agent A3 is attached to the node N3 at port 0.
Obtaining a Node's Address
In Tcl, one could use the following command to discover a node's address:
# $node node-addr $N3 node-addr
This would return the address of N3 in the topology above.
Obtaining an Agent's Port
Likewise, one could use the following command to discover an agent's port at a node:
# $agent port $A3 port
This would return the port of A3 in the topology above.
