#! /usr/bin/perl # # traffic-w-jitter.pl generates broadcast traffic for an # ns2 simulation. The script randomly selects a broadcast # time for each node in the simulation. For each time # interval during which a node boardcasts, the transmit # time is varied by +/- the amount of jitter. The # variables start and end are the start time and end time # of the simulation. traffic_rate is the number of # times that a nodes transmits during one second. # # Nathan Balon # University of Michigan - Dearborn # use strict; use warnings; sub setValuesFromArgs($); sub getCommentBlock(); # the default values used to generate traffic our $jitter = 0.1; # the amount of jitter our $nodes = 50; # number of node our $traffic_rate = 10; # number of times a node transmits per second our $seed = 1; # seed for the random number generator our $start = 1; # start time of the simulation our $end = 61; # end time of the simulation our $file_name = 'traffic.tcl'; # file to save traffic to my $sim_length = 0; # length of the simulation my @trans_time = (); # the transmit time # read the command line arguments for(my $argnum = 0; $argnum <= $#ARGV; $argnum++){ # display help if($ARGV[$argnum] eq "-h" || $ARGV[$argnum] eq "--help" ){ printHelp(); exit; # set the values used to generate traffic from the cammand line args }elsif($argnum + 1 <= $#ARGV){ setValuesFromArgs($argnum); $argnum++; } } # determine the length of the simulation if($end > $start){ $sim_length = $end - $start; }else{ die "The end time of the simulation must be " . "greater than the start time of the simulation\n"; } srand $seed; #open the file and write the comment block to the file open(FILE, ">$file_name"); print FILE getCommentBlock; # the transmission rate to send messages my $trans_rate = sprintf("%.2f", 1/$traffic_rate); # Select the initial time a node will # transmit at based on the transmission rate. for(my $i = 0; $i < $nodes; $i++){ $trans_time[$i] = rand($trans_rate); } # the number of time each node sends a broadcast my $num_intervals = $sim_length * $traffic_rate; # the time interval to transmit during my $transmit_interval = 0; # Schedule the broadcast transmissions for the simulation. for(my $trans_int = 0; $trans_int < $num_intervals; $trans_int++){ # for each node in the simulation schedule a broadcast for(my $node = 0; $node < $nodes; $node++){ my $trans_time = 0; # the time the broadcast is sent if($trans_int == 0){ # if first transmission use the # randomly selected transmit time $trans_time = $start + $trans_time[$node]; }else{ # get the random amount of jitter for the broadcast my $jit = rand($jitter * $trans_rate); # if random number is > 0.5 add the jitter or else # subtract the jitter if(rand(1) > 0.5){ $trans_time = $start + $trans_time[$node] + $jit + $transmit_interval; }else{ $trans_time = $start + $trans_time[$node] - $jit + $transmit_interval; } } print FILE "\$ns at $trans_time \"\$bc_agent($node) send_message\"\n"; } $transmit_interval += $trans_rate; } close(FILE); ################################################################## # # functions # ################################################################## # display help containing the command line arguments to the user sub printHelp { my $space = " " x (length($0) + 6) ; print "usage $0 [--nodes \"number of nodes\"]\n" . "$space [--jitter \"amount of jitter\"]\n" . "$space [--traffic-rate \"rate of traffic per second\"]\n" . "$space [--seed \"seed for random number generator\"]\n". "$space [--start \"start time\"]\n" . "$space [--end \"end time\"]\n". "$space [--file-name \"name of the file to write to\"]\n"; } # Return a comment block for the tcl traffic file sub getCommentBlock() { # return the comment block to add to the tcl program. return "#===========================================================================\n" . "#\n" . "# Broadcast Traffic genrated by generate_traffic tool.\n" . "# Each node within the network randomly selects the time a time to\n" . "# generate broadcast message during each broadcast interval.\n" . "#\n" . "# Created by Nathan Balon, University of Michigan - Dearborn\n" . "#\n" . "# start time : $start, end time : $end\n" . "# broadcast rate: $traffic_rate\n" . "#\n" . "#===========================================================================\n\n"; } # Set the parameters used by the program from the # command line arguments. sub setValuesFromArgs($) { my $argnum = shift; my $arg = $ARGV[$argnum + 1]; if($ARGV[$argnum] eq "--jitter"){ $jitter = $arg; }elsif($ARGV[$argnum] eq "--nodes"){ $nodes = $arg; }elsif($ARGV[$argnum] eq "--traffic-rate"){ $traffic_rate = $arg; }elsif($ARGV[$argnum] eq "--seed"){ $seed = $arg; }elsif($ARGV[$argnum] eq "--start"){ $start = $arg; }elsif($ARGV[$argnum] eq "--end"){ $end = $arg; }elsif($ARGV[$argnum] eq "--file-name"){ $file_name = $arg; }else{ die "invalid command line argument"; } }