⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 structure.sgml.svn-base

📁 网络模拟器
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
<p>

A duplex link encapsulates two simplex links and can be attached to a 
DuplexInterface. There is not much more to say about it but this is definitely
the kind of link you should use if you want to be sane after laying out your
network.


<sect>An Example

<p>
We will now provide some annotated example code that shows how to create a
static network structure in JNS. We will create a simple network of three
nodes: Two hosts joined by a router. The way to proceed is to show bits of
code and explain them. What all steps will have in common is that every element
that is generated has to be attached to the simulator.

<p>
<bf>Step 1:</bf> Obtain a reference to the Simulator
<code>
    Simulator sim=Simulator.getInstance();
</code>

Note that only one instance of the simulator can ever exist. That's why the
constructor is private and you have to use this method to obtain the singleton
instance.

<p>
<bf>Step 2:</bf> Create the three nodes (the code should be self-explanatory)
and attach them to the simulator:
<code>
    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);
</code>

<bf>Step 3:</bf> 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:
<itemize>
<item>We are using duplex interfaces because we want two-way communication,
like a network card
<item>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.
</itemize>
<code>
    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);
</code>


<bf>Step 4:</bf> 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).

<p>
<bf>Caveat:</bf> 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.
<code>
    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);
</code>


<bf>Step 5:</bf> 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:
<code>
    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);
</code>
Notice the parameter <tt>true</tt> that is being passed as a second argument
when the links are attached; it tells the interface to "inherit" the link's
bandwidth.


<bf>Step 6:</bf> 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.).

<p>
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. <bf>Caveat:</bf> 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.
<code>
    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);
</code>
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.

<bf>Step 7:</bf> No such step. <bf>We are finished!</bf>

<sect>The whole example code
<p>
Here is the whole code of this example without interruptions:
<code>
    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);
</code>

<sect>Additional Comments
<p>

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:
<itemize>
<item><bf>ALWAYS</bf> attach all your elements to the Simulator. Your simulation might work without doing it but you will not be able to trace anything.
<item>Use loops if you have many nodes.
<item>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.
<item>Abstract this idea further to produce whole networks shrink-wrapped.
<item>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.
</itemize>

</article>


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -