41.8 Putting together: a simple example

We have seen all the pieces, now we present a script which provides a complete view of all pieces together. First, we build topology and other usual initializations:

        set ns [new Simulator]

        # Create topology/routing
        set node(c) [$ns node] 
        set node(e) [$ns node]
        set node(s) [$ns node]
        $ns duplex-link $node(s) $node(e) 1.5Mb 50ms DropTail
        $ns duplex-link $node(e) $node(c) 10Mb 2ms DropTail 
        $ns rtproto Session

Next we create the Http objects:

        # HTTP logs
        set log [open "http.log" w]

        # Create page pool as a central page generator. Use PagePool/Math
        set pgp [new PagePool/Math]
        set tmp [new RandomVariable/Constant] ## Page size generator;
        $tmp set val_ 1024  ## average page size;
        $pgp ranvar-size $tmp
        set tmp [new RandomVariable/Exponential] ## Page age generator;
        $tmp set avg_ 5 ## average page age;
        $pgp ranvar-age $tmp

        set server [new Http/Server $ns $node(s)] ## Create a server and link it to the central page pool;
        $server set-page-generator $pgp
        $server log $log

        set cache [new Http/Cache $ns $node(e)] ## Create a cache;
        $cache log $log

        set client [new Http/Client $ns $node(c)] ## Create a client;
        set tmp [new RandomVariable/Exponential] ## Poisson process as request sequence;
        $tmp set avg_ 5 ## average request interval;
        $client set-interval-generator $tmp
        $client set-page-generator $pgp
        $client log $log

        set startTime 1 ## simulation start time;
        set finishTime 50 ## simulation end time;
        $ns at $startTime "start-connection"
        $ns at $finishTime "finish"

Then we define a procedure which will be called after simulation starts. The procedure will setup connections among all Http objects.

        proc start-connection {} {
                global ns server cache client
                $client connect $cache
                $cache connect $server
                $client start-session $cache $server

At the end, the usual closing:

        proc finish {} {
                global ns log
                $ns flush-trace
                flush $log
                close $log
                exit 0
        $ns run

This script is also available at ns/tcl/ex/simple-webcache.tcl. Examining its output http.log, one will find that the result of the absense cache consistency algorithm results in a lot of stale hits. This can be easily remedied by replacing ``new Http/Cache'' line with: set cache [new Http/Cache/TTL $ns $node(e)]. For more complicated cache consistency algorithm examples, see ns/tcl/test/test-suite-webcache.tcl.

Tom Henderson 2011-11-05