?? structure.txt.svn-base
字號:
______________________________________________________________________
Step 3: Create an interface for the source and destination nodes and
attach them to those nodes. In this piece of code, observe the
following two things:
o We are using duplex interfaces because we want two-way
communication, like a network card
o The two nodes will be connected by a router. Therefore, by the
rules of IP, they have to be on different IP networks. We assign
two different network addresses.
___________________________________________________________________
Interface src_iface=new DuplexInterface(new IPAddr(192,168,1,10));
src.attach(src_iface);
sim.attach(src_iface);
Interface dest_iface=new DuplexInterface(new IPAddr(128,116,11,20));
dest.attach(dest_iface);
sim.attach(dest_iface);
___________________________________________________________________
Step 4: Create interfaces for the router and attach them to the
router. Note that the router will of course need two duplex interfaces
because it is attached to two different pieces of wire. In addition,
keep in mind that the router has to be on both IP networks (it is
customary to give the router the address .1 on both networks).
Caveat: Be careful not to assign the IP addresses of the router
interfaces the right way round but then attach the physical links the
wrong way round later.
______________________________________________________________________
Interface route_iface192=new DuplexInterface(new IPAddr(192,168,1,1));
Interface route_iface128=new DuplexInterface(new IPAddr(128,116,11,1));
router.attach(route_iface192);
router.attach(route_iface128);
sim.attach(route_iface192);
sim.attach(route_iface128);
______________________________________________________________________
Step 5: Connect all the interfaces via links. We need two links, one
from the source node to the router, and one from there to the
destination node. Since we are using duplex interfaces, we have to use
duplex links. We will create one 1Mbps link and one sample ISDN link:
______________________________________________________________________
Link link_src_router=new DuplexLink(1000000,0.001);
Link link_router_dest=new DuplexLink(64000,0.1);
src_iface.attach(link_src_router,true);
route_iface192.attach(link_src_router,true);
sim.attach(link_src_router);
route_iface128.attach(link_router_dest,true);
dest_iface.attach(link_router_dest,true);
sim.attach(link_router_dest);
______________________________________________________________________
Notice the parameter true that is being passed as a second argument
when the links are attached; it tells the interface to "inherit" the
link's bandwidth.
Step 6: Add the correct routing table entries. The physical structure
of the network is completely set up. However, the source host wouldn't
know how to reach the destination host yet because it has no routing
table entries. (You may argue that the routing is quite obvious in
this example, however the simulator cannot guess that you will provide
a simple network, so routing has to be set in every case.).
We will make our life simple by adding default routes at the source
and destination node that point to the router. At the router, however,
we will add exact routing entries for the two subnets. Caveat: do not
enter default routes into the routing table of a router if you can
avoid it. If you have more than one router and someone sends a packet
with an unroutable address, those two routers might play ping-pong for
a while (until the packet times out) if you set default routes.
______________________________________________________________________
src.addDefaultRoute(src_iface);
dest.addDefaultRoute(dest_iface);
router.addRoute(new IPAddr(192,168,1,0),new IPAddr(255,255,255,0),
route_iface192);
router.addRoute(new IPAddr(128,116,11,0),new IPAddr(255,255,255,0),
route_iface128);
______________________________________________________________________
In the above code, notice the following things: We did indeed simply
add a default route for the two hosts. At the router, what we did was
effectively create two class C networks by setting the netmask to
255.255.255.0.
Step 7: No such step. We are finished!
7. The whole example code
Here is the whole code of this example without interruptions:
______________________________________________________________________
Simulator sim=Simulator.getInstance();
Node src=new Node("Source node");
Node router=new Node("Router");
Node dest=new Node("Destination node");
sim.attach(src);
sim.attach(router);
sim.attach(dest);
Interface src_iface=new DuplexInterface(new IPAddr(192,168,1,10));
src.attach(src_iface);
sim.attach(src_iface);
Interface dest_iface=new DuplexInterface(new IPAddr(128,116,11,20));
dest.attach(dest_iface);
sim.attach(dest_iface);
Interface route_iface192=new DuplexInterface(new IPAddr(192,168,1,1));
Interface route_iface128=new DuplexInterface(new IPAddr(128,116,11,1));
router.attach(route_iface192);
router.attach(route_iface128);
sim.attach(route_iface192);
sim.attach(route_iface128);
Link link_src_router=new DuplexLink(1000000,0.001);
Link link_router_dest=new DuplexLink(64000,0.1);
src_iface.attach(link_src_router,true);
route_iface192.attach(link_src_router,true);
sim.attach(link_src_router);
route_iface128.attach(link_router_dest,true);
dest_iface.attach(link_router_dest,true);
sim.attach(link_router_dest);
src.addDefaultRoute(src_iface);
dest.addDefaultRoute(dest_iface);
router.addRoute(new IPAddr(192,168,1,0),new IPAddr(255,255,255,0),
route_iface192);
router.addRoute(new IPAddr(128,116,11,0),new IPAddr(255,255,255,0),
route_iface128);
______________________________________________________________________
8. Additional Comments
As mentioned before already, you should not really have to do all this
setup work, particularly if you want to simulate a rather large
network (Internet, anyone?). Here are some suggestions on how to make
your life easier:
o ALWAYS attach all your elements to the Simulator. Your simulation
might work without doing it but you will not be able to trace
anything.
o Use loops if you have many nodes.
o Write a class that will produce "shrink-wrapped" Nodes. A bit like
a mass computer retailer: Complete with an interface and routing
table. You could then produce a node with one line of code.
o Abstract this idea further to produce whole networks shrink-
wrapped.
o Alternatively, forget about all this. Download one of the many good
topology generators available on the web and either write a script
or modify its code so it directly generates JNS code.