#!d:\perli\bin\perl -w # # UDP example # # Traffic generator that sends DDP to 127.0.0.1 ports 2346 and 2347 # using a catch up calculation # # # A call generator starts a timer with a delay of $interArrivalTime # $interArrivalTime is calculated to have a negative exp distribution. # # Alternatively, if you have a constant time tick and a probability # of a call in this interval, you can work out if this time tick is # to make a call. # # Alternatively, if you have a constant time tick You can catch up real # time by adding the interarrival times to $nowTime and sending calls. # You can then start a timer and knock back $nowTime by the duration of the # timer. This is how this script works. # # # #!/usr/bin/perl -w use strict; use Socket; use Sys::Hostname; my ( $count, $hisiaddr, $hispaddr, $histime, $host, $iaddr, $paddr, $port, $proto, $rin, $rout, $rtime, $SECS_of_70_YEARS); $SECS_of_70_YEARS = 2208988800; # $iaddr = gethostbyname(hostname()); $proto = getprotobyname('udp'); $paddr = sockaddr_in(2345,inet_aton("127.0.0.1")); socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) || die "socket: $!"; bind(SOCKET, $paddr) || die "bind: $!"; $| = 1; printf "%-12s %8s %s\n", "localhost", 0, scalar localtime time; my $message = "Message"; for( $count = 0; $count < 100 ; $count++ ){ $hispaddr = sockaddr_in(2345, inet_aton("127.0.0.1") ); send(SOCKET, "$message $count \r\n", 0, $hispaddr) ; print $count." " ; } # Call Generator simulator code. my $ip; my $now; my $CallsPerSecond = 4; my $timerTickTime = 1; my $sum=0; my $interArrivalTime = -log( 1 - rand() ) / $CallsPerSecond; for( $count = 0; $count<1000 ; $count++ ){ $interArrivalTime = -log( 1 - rand() ) / $CallsPerSecond; $sum = $sum + $interArrivalTime; print "$sum $interArrivalTime \r\n" } my $sum = 0; my $StartedAt = time; my $running = 0; my $callType; my $destination; # Catch Up calculation method. # A fixed timer is used, and on each tick, # calls are sent to catch up simulated time. my $nowTime = -5.0; while( 1 ) { if ( $nowTime > 0.0 ) { # start timer for a fixed period $count = select( undef, undef, undef, $timerTickTime ) ; $nowTime = $nowTime - $timerTickTime; } else { # Catch up with calls # generate a time interval modulated by Neg Exp distribution $interArrivalTime = -log( 1 - rand() ) / $CallsPerSecond; $sum = $sum + $interArrivalTime; $running = time - $StartedAt; # Now pick a call type $callType = int( rand()*10 ); # Now pick a destination $destination = int( rand()*10 ); #print "Call Generator: $running,$sum,$callType,$destination,$interArrivalTime \r\n"; #$ip = ; # Prepare Message # TS,Protocol,OrigID,DestID,Operation,P1,,,CRLF $message = "$sum,0,0,$destination,MAKECAll,,,END\r\n"; if ( rand() < 0.5 ) { $hispaddr = sockaddr_in(2346, inet_aton("127.0.0.1") ); } else { $hispaddr = sockaddr_in(2347, inet_aton("127.0.0.1") ); } send(SOCKET, "CLG:,$message", 0, $hispaddr) ; print "Call Generator: $running,$sum, $message"; $nowTime = $nowTime + $interArrivalTime; } }