render.in 20.4 KB
Newer Older
1 2
#!/usr/bin/perl -w

Leigh Stoller's avatar
Leigh Stoller committed
3 4
#
# EMULAB-COPYRIGHT
Leigh Stoller's avatar
Leigh Stoller committed
5
# Copyright (c) 2000-2006 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
6 7 8
# All rights reserved.
#

9 10 11 12
use English;
use Getopt::Std;
use lib '@prefix@/lib';
use libdb;
Leigh Stoller's avatar
Leigh Stoller committed
13

14
#
15 16
# Render experiment from the database.
# Spits PNG image out to stdout.
17 18
#

19 20 21
# TODO: delay nodes
# TODO: display text in image if there are no nodes.

22
use GD;
Chad Barb's avatar
Chad Barb committed
23
use Getopt::Std;
24 25 26 27 28
#
# Configure variables
#
my $TB		= "@prefix@";
my $ICONDIR     = "$TB/www";
29

30 31 32
sub dprint($);

sub usage {
33
    die "Usage:\nrender [-v] [-t <thumbsize>] [-z <zoomfactor>] [-d <detaillevel>] <pid> <eid>\n";
Chad Barb's avatar
Chad Barb committed
34 35
}

36
my  $optlist = "z:d:vt:";
37
if (! getopts($optlist, \%options)) { usage; }
38

39 40 41 42
if (@ARGV != 2) { usage; }

my $debug = 0;
if ( defined($options{"v"}) ) { $debug++; }
43

Chad Barb's avatar
Chad Barb committed
44 45 46 47 48 49
my $zoom = 1;
if ( defined($options{"z"} ) ) {
    my $zf = $options{"z"};
    if ($zf =~ /^([\.0-9]+)/) {
	$zoom = $1;
    } else {
50
	die("Bad argument to -z; must be float.");
Chad Barb's avatar
Chad Barb committed
51 52 53
    }
}

54 55 56 57 58
my $detail = 0;
if (defined($options{"d"})) {
    my $df = $options{"d"};
    if ($df =~ /^([0-9]+)/) {
	$detail = $1;
59
    } else {
60
	die("Bad argument to -d; must be non-negative integer.");
Chad Barb's avatar
Chad Barb committed
61
    }
62 63
}

64 65 66 67 68 69 70 71 72 73
my $thumbnail = 0;
if (defined($options{"t"})) {
    my $tf = $options{"t"};
    if ($tf =~ /^([0-9]+)/) {
	$thumbnail = $1;
    } else {
	die("Bad argument to -t; must be non-negative integer.");
    }
}

74 75
my %nodes = ();
my %links = ();
76

77 78
my $pid = $ARGV[0];
my $eid = $ARGV[1];
79

80 81
# Get node list.
# (left join info from vis_nodes; could also left join virt_lans,
82 83
#  but there's no reason to since lan info shows up on the link,
#  not the LAN node.)
84

85
my $result = DBQueryFatal("SELECT vis.vname, vis.vis_type, vis.x, vis.y, ".
86
			  "       virt.type " .
87 88
			  "FROM vis_nodes as vis " .
			  "LEFT JOIN virt_nodes as virt on ".
89 90
			  "     vis.vname=virt.vname and vis.pid=virt.pid and".
			  "     vis.eid=virt.eid ".
91
			  "WHERE vis.pid='$pid' AND vis.eid='$eid'");
92

93
while (my ($name, $vis_type, $vis_x, $vis_y, $virt_type) = $result->fetchrow) {
94
    dprint "NODE $name $vis_type $vis_x $vis_y\n";
95

96 97 98
    $nodes{$name}{"type"} = $vis_type; # vis type, not hwtype.
    $nodes{$name}{"x"} = $vis_x;
    $nodes{$name}{"y"} = $vis_y;
99

100 101 102
    # what appears in label depends on the detail level.
    my $label = $name;
    if ($detail > 0) {
103
	if (defined($virt_type)) {
104
	    $label .= "(" . $virt_type . ")";
105 106
	}
    }
107
    $nodes{$name}{"label"} = $label;
108

109 110 111 112 113
    if (!(defined $min_x) || $vis_x < $min_x) { $min_x = $vis_x; }
    if (!(defined $min_y) || $vis_y < $min_y) { $min_y = $vis_y; }
    if (!(defined $max_x) || $vis_x > $max_x) { $max_x = $vis_x; }
    if (!(defined $max_y) || $vis_y > $max_y) { $max_y = $vis_y; }
}
114

115 116
if (!(defined $min_x)) {
    # no nodes.
117 118 119 120 121 122 123 124 125 126 127 128
    if ($thumbnail != 0) {
	$max_x = 64;
	$max_y = 64;
    }
    $noNodes = 1;
    # die "No visible nodes in '$pid/$eid', or experiment does not exist.\n";
} else {
    dprint "min x,y = $min_x, $min_y\n" .
	   "max x,y = $max_x, $max_y\n";
    
    # adjust each node's position so topleftmost node is at (60,60) * $zoom.
    foreach $i (keys %nodes) {
129 130
	$nodes{$i}{"x"} = (($nodes{$i}{"x"} - $min_x) * $zoom) + 60;
	$nodes{$i}{"y"} = (($nodes{$i}{"y"} - $min_y) * $zoom) + 60;
131 132 133
    }
    
    # adjust max x,y appropriately.
134 135
    $max_x = (($max_x - $min_x) * $zoom) + 120;
    $max_y = (($max_y - $min_y) * $zoom) + 120;
136 137
}

138 139
# get vlan list.

140 141
$result = DBQueryFatal("SELECT vname, vnode, vport, ip, ".
		          "delay, bandwidth, lossrate, " .
142 143 144 145
			  "rdelay, rbandwidth, rlossrate, " . 
			  "member FROM virt_lans " .
			  "WHERE pid='$pid' AND eid='$eid'");

146
while (my ($vname, $vnode, $vport, $ip, $delay, $bandwidth, 
147 148 149 150 151 152 153 154 155
           $lossrate, $rdelay, $rbandwidth, 
           $rlossrate, $member) = $result->fetchrow) {
    $member =~ s/\:.*//;
    $lans{$vname}{$member}{"delay"}  = $delay;
    $lans{$vname}{$member}{"bw"}     = $bandwidth;
    $lans{$vname}{$member}{"loss"}   = $lossrate;
    $lans{$vname}{$member}{"rdelay"} = $rdelay;
    $lans{$vname}{$member}{"rbw"}    = $rbandwidth;
    $lans{$vname}{$member}{"rloss"}  = $rlossrate;
156 157 158 159 160 161 162

    # what appears in label depends on the detail level.
    if ($detail > 0) {
	if (defined($ip)) {
	    $nodes{$vnode}{"label"} .= " " . $ip;
	}
    }
163 164
}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
foreach my $lan (keys %lans) {
    if ((keys %{$lans{$lan}}) == 2) {
	# amalgamate into 2 member link.
	my $a = (keys %{$lans{$lan}})[0];
	my $b = (keys %{$lans{$lan}})[1];
	my $delaya2b = $lans{$lan}{$a}{"delay"} + $lans{$lan}{$b}{"rdelay"};
	my $delayb2a = $lans{$lan}{$b}{"delay"} + $lans{$lan}{$a}{"rdelay"};
	my $bwa2b = min( $lans{$lan}{$a}{"bw"}, $lans{$lan}{$b}{"rbw"} );
	my $bwb2a = min( $lans{$lan}{$b}{"bw"}, $lans{$lan}{$a}{"rbw"} );
	my $lossa2b = combineloss( $lans{$lan}{$a}{"loss"}, $lans{$lan}{$b}{"rloss"} );
	my $lossb2a = combineloss( $lans{$lan}{$b}{"loss"}, $lans{$lan}{$a}{"rloss"} );
	my $desc = "";
	if ($detail > 0) {
	    $desc = gendesc( $delaya2b, $delayb2a, $bwa2b, $bwb2a, $lossa2b, $lossb2a );
	}
	# create the link
	dprint "LINK $a $b '$desc'\n";
	$links{"$a $b"}{"label"} = $desc; 
    } else {
	# add links from node to LAN.
	unless (exists $nodes{$lan} && ($nodes{$lan}{"type"} eq "lan")) {
	    warn "No LAN $lan!";
	}
	foreach my $node (keys %{$lans{$lan}}) {
	    my $delayin  = $lans{$lan}{$node}{"delay"};
	    my $delayout = $lans{$lan}{$node}{"rdelay"};
	    my $bwin     = $lans{$lan}{$node}{"bw"};
	    my $bwout    = $lans{$lan}{$node}{"rbw"};
	    my $lossin   = $lans{$lan}{$node}{"loss"};
	    my $lossout  = $lans{$lan}{$node}{"rloss"};
	    my $desc = "";
	    if ($detail > 0) {
		$desc = gendesc( $delayin, $delayout, $bwin, $bwout, $lossin, $lossout );
	    }
	    # create a link from node to lan.
	    $links{"$node $lan"}{"label"} = $desc;
	    dprint "LINK $node $lan '$desc'\n";
	}
    }
}    

206

207 208
# if $embiggen == 1, node fonts will be rendered bigger.
# if $embiggen == 2, node and link fonts will be rendered bigger.
209

210
my $embiggen = 0; 
211

212 213
if ($zoom >= 1.5) { $embiggen = 1; }
if ($zoom >= 1.75) { $embiggen = 2; }
Chad Barb's avatar
Chad Barb committed
214

215 216
# start constructing the image

217
dprint "Image size = $max_x x $max_y\n";
218 219 220 221 222 223

if ($thumbnail == 0) {
    $im = new GD::Image($max_x, $max_y);
} else {
    $im = new GD::Image($thumbnail, $thumbnail);
}
224

225 226
$nodeicon = GD::Image->newFromPng("$ICONDIR/nodeicon.png") || warn "nodeicon.png not found";
$lanicon  = GD::Image->newFromPng("$ICONDIR/lanicon.png") || warn "lanicon.png not found";
227 228 229

%colors = ();

230 231 232 233 234 235 236
$colors{"black"}     =  $im->colorAllocate(0,0,0);
$colors{"darkblue"}  =  $im->colorAllocate(0,0,96);
$colors{"darkred"}   =  $im->colorAllocate(128,0,0);
$colors{"red"}       =  $im->colorAllocate(192,0,0);
$colors{"blue"}      =  $im->colorAllocate(0,0,192);
$colors{"paleyellow"}      =  $im->colorAllocate(192,192,127);
$colors{"paleblue"}  =  $im->colorAllocate(127,127,192);
237 238 239 240
$colors{"palered"}  =  $im->colorAllocate(210,200,180);
#$colors{"green"}     =  $im->colorAllocate(0,96,0);
$colors{"green"}     =  $im->colorAllocate(0,192,0);
$colors{"palegreen"}     =  $im->colorAllocate(96,192,96);
241 242 243 244 245 246 247 248 249
$colors{"lightgreen"} =  $im->colorAllocate(0, 160, 0);
$colors{"orange"}    =  $im->colorAllocate(255, 128, 0);
$colors{"yellow"}    =  $im->colorAllocate(255, 255, 0);
$colors{"cyan"}    =  $im->colorAllocate(0, 191, 191);
$colors{"white"}     =  $im->colorAllocate(255,255,255);
$colors{"gray80"}    =  $im->colorAllocate(210,210,210);
$colors{"gray75"}    =  $im->colorAllocate(191,191,191);
$colors{"gray50"}    =  $im->colorAllocate(127,127,127);
$colors{"gray25"}    =  $im->colorAllocate(63,63,63);
250 251

# set clear background
252
$bgcolor = $im->colorAllocate(254, 254, 254);
253

254
$im->transparent($bgcolor);
255 256
$im->fill( 1, 1, $bgcolor );

257
#$im->interlaced('true');
258 259
#$im->rectangle( 0,0,99,99, $gray50 );

260 261 262
if ($thumbnail != 0) {    
    # Thumbnails are drawn similarly to full views,
    # but there are enough differences to warrant separate code.
263

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    foreach $i (keys %links) {
	# get endpoint names from link name
	($a, $b) = ($i =~ /(\S+)\s(\S+)/);
	
	# get endpoint node location
	($x1, $y1) = ($nodes{ $a }{"x"}, $nodes{ $a }{"y"});
	($x2, $y2) = ($nodes{ $b }{"x"}, $nodes{ $b }{"y"});

	# scale down to thumbnail size; 'ceil' prevents subpixel errors,
	# though it is probably not needed for lines.
	$x1 = ceil(($x1 * $thumbnail) / $max_x);
	$y1 = ceil(($y1 * $thumbnail) / $max_y);
	$x2 = ceil(($x2 * $thumbnail) / $max_x);
	$y2 = ceil(($y2 * $thumbnail) / $max_y);

	$im->line( $x1, $y1, $x2, $y2, $colors{"paleblue"} );
    }
281

282 283 284 285 286 287 288 289 290 291 292 293 294
    foreach $i (keys %nodes) {
	# get node position and type.
	my ($x, $y) = ($nodes{$i}{"x"}, $nodes{$i}{"y"});
	my $type = $nodes{$i}{"type"};    

	# scale down to thumbnail size; 'ceil' prevents subpixel errors.
	# 'ceil' is important, since if $x has a fractional part as well as $size,
	# when they're added together, they may produce an additional pixel of
	# width or height on some of the nodes; such an error is surprisingly noticable.

	$x = ceil(($x * $thumbnail) / $max_x);
	$y = ceil(($y * $thumbnail) / $max_y);

295 296
	$size = ceil(min( min( 16 * $thumbnail / $max_x, 16 * $thumbnail / $max_y ), $thumbnail / 16));
	
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

	if ($type eq "special") {
	    $im->filledRectangle( $x - $size, $y - $size, $x + $size, $y + $size, $colors{"darkred"} );	
	    $im->rectangle( $x - $size, $y - $size, $x + $size, $y + $size, $colors{"black"} );	
	} elsif ($type eq "lan") {
	    for ($i = 1; $i < $size; $i++) {
		$im->arc( $x, $y, $i * 2, $i * 2, 0, 360, $colors{"blue"} );
	    }	    
	    $im->arc( $x, $y, $size * 2, $size * 2, 0, 360, $colors{"black"} );
	} elsif ($type eq "node") {
	    $im->filledRectangle( $x - $size, $y - $size, $x + $size, $y + $size, $colors{"palegreen"} );	
	    $im->rectangle( $x - $size, $y - $size, $x + $size, $y + $size, $colors{"black"} );	
	}
    }
} else { 
# not a thumbnail, so we do the full rendering path.
313 314


315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
# render shadows
    foreach $i (keys %nodes) {
	# get node location
	($x, $y) = ($nodes{$i}{"x"}, $nodes{$i}{"y"});
	if ($nodes{$i}{"type"} eq "lan") {
	    # render filled circle for LAN shadow.
	    # there doesn't seem to be a filledArc (!),
	    # so we render 18 concentric circles.
	    for ($i = 1; $i < 18; $i++) {
		$im->arc( $x + 4, $y + 4, 
			  $i * 2, $i * 2, 
			  0, 360, 
			  $colors{"gray80"} );
	    }
	} else {
	    # not a LAN, so render solid square for shadow.
	    $im->filledRectangle( $x - 12, $y - 12, 
				  $x + 20, $y + 20, $colors{"gray80"});
	}
    } 
335

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
# render links
    foreach $i (keys %links) {
	# get endpoint names from link name
	($a, $b) = ($i =~ /(\S+)\s(\S+)/);
	
	# get endpoint node location
	($x1, $y1) = ($nodes{ $a }{"x"}, $nodes{ $a }{"y"});
	($x2, $y2) = ($nodes{ $b }{"x"}, $nodes{ $b }{"y"});  
	
	# get near-endpoints
	($xv, $yv) = ($x2 - $x1, $y2 - $y1);
	$vmag = sqrt( $xv * $xv + $yv * $yv );
	
	if ($vmag > (26 * 2)) {
	    $xv = ($xv / $vmag) * 26;
	    $yv = ($yv / $vmag) * 26;
	    
	    ($x1n, $y1n) = ($x1 + $xv, $y1 + $yv);
	    ($x2n, $y2n) = ($x2 - $xv, $y2 - $yv);
	    
	    # set link color
	    # $im->setStyle($colors{"paleblue"}); 
	    # $im->setStyle($red, $red, gdTransparent);   
	    
	    # actual rendering
	    # $im->line( $x1, $y1, $x2, $y2, gdStyled );
	    
	    $im->setStyle( $colors{"yellow"} );
	    $im->line( $x1 + 1, $y1, $x1n + 1, $y1n, gdStyled );
	    $im->line( $x2n + 1, $y2n, $x2 + 1, $y2, gdStyled );
	    
	    $im->line( $x1, $y1 + 1, $x1n, $y1n + 1, gdStyled );
	    $im->line( $x2n, $y2n + 1, $x2, $y2 + 1, gdStyled );
	    
	    $im->line( $x1 - 1, $y1, $x1n - 1, $y1n, gdStyled );
	    $im->line( $x2n - 1, $y2n, $x2 - 1, $y2, gdStyled );
	    
	    $im->line( $x1, $y1 - 1, $x1n, $y1n - 1, gdStyled );
	    $im->line( $x2n, $y2n - 1, $x2, $y2 - 1, gdStyled );
	    
	    $im->setStyle( $colors{"paleyellow"} );
	    
	    $im->line( $x1, $y1, $x1n, $y1n, gdStyled );
	    $im->line( $x2n, $y2n, $x2, $y2, gdStyled );
	    
	    $im->setStyle( $colors{"paleblue"} );
	    $im->line( $x1n, $y1n, $x2n, $y2n, gdStyled );
	    
	} else {
	    $im->setStyle( $colors{"black"}, gdTransparent );
	    $im->line( $x1, $y1, $x2, $y2, gdStyled );
	}
    }
389

390
# render nodes.
391

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
    foreach $i (keys %nodes) {
	# get node position and type.
	my ($x, $y) = ($nodes{$i}{"x"}, $nodes{$i}{"y"});
	my $type = $nodes{$i}{"type"};
	
	if ($type eq "special") {
	    # 'special' is not used for anything right now.
	    $im->rectangle( $x - 16, $y - 16, $x + 16, $y + 16, $colors{"darkred"} );
	    $im->rectangle( $x - 15, $y - 15, $x + 15, $y + 15, $colors{"darkred"} );
	    $im->filledRectangle( $x - 14, $y - 14, 
				  $x + 14, $y + 14, $colors{"white"} );
	    # render icon
	    $im->copy($nodeicon, $x-16, $y-16, 0, 0, 32, 32);
	} elsif ($type eq "lan") {
	    # render multiple concentric circles (again, no filledArc) for LAN.
	    # render outermost circle at 50% grey to provide
	    # fake antialiasing.
	    $im->arc( $x, $y, 36, 36, 0, 360, $colors{"gray50"} );
	    $im->arc( $x, $y, 34, 34, 0, 360, $colors{"gray25"} );
	    $im->arc( $x, $y, 32, 32, 0, 360, $colors{"gray75"} );
	    for ($i = 1; $i < 16; $i++) {
		$im->arc( $x, $y, $i * 2, $i * 2, 0, 360, $colors{"white"} );
	    }
	    # render icon
	    
	    $im->copy($lanicon, $x-16, $y-16, 0, 0, 32, 32);
	} else {
	    # anything that isn't a LAN (in other words, a node.)
	    $im->rectangle( $x - 16, $y - 16, $x + 16, $y + 16, $colors{"gray25"} );
	    $im->rectangle( $x - 15, $y - 15, $x + 15, $y + 15, $colors{"gray25"} );
	    $im->filledRectangle( $x - 14, $y - 14, $x + 14, $y + 14, $colors{"white"} );
	    # render icon
	    
	    $im->copy($nodeicon, $x-16, $y-16, 0, 0, 32, 32);
	}
    } # foreach $i (keys %nodes)
428 429
    
    
430 431
# render link text.
# (this is done in a second pass so no text is obscured by boxes)
432
    
433 434 435
    foreach $i (keys %links) {
	# only render label if there _is_ a label.
	if (!exists $links{$i}{"label"}) { next; }
436
	
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
	# get endpoint names and positions
	($a, $b) = ($i =~ /(\S+)\s(\S+)/);
	($x1, $y1) = ($nodes{$a}{"x"}, $nodes{$a}{"y"});
	($x2, $y2) = ($nodes{$b}{"x"}, $nodes{$b}{"y"});  
	# calculate midpoint of link line
	($x, $y) = ( ($x1 + $x2) / 2, ($y1 + $y2) / 2 );
	
	# $links{$i}{"label"} =~ s/^\!..//;
	
	# split lines by space
	my @lines = split " ", $links{$i}{"label"};
	
	# center vertically
	$y -= (0.5 * (@lines * gdTinyFont->height)); 
	
	my $linenum = 0;
	foreach $j (@lines) {
	    $xpos = $x - ((length($j) - 0.5) * (($embiggen == 2) ? gdSmallFont->width : gdTinyFont->width) / 2); 
	    
	    $im->string(($embiggen == 2) ? gdSmallFont : gdTinyFont, $xpos + 1, $y, 
			$j, $bgcolor);
	    $im->string(($embiggen == 2) ? gdSmallFont : gdTinyFont, $xpos - 1, $y, 
			$j, $bgcolor);
	    $im->string(($embiggen == 2) ? gdSmallFont : gdTinyFont, $xpos, $y - 1, 
			$j, $bgcolor);
	    $im->string(($embiggen == 2) ? gdSmallFont : gdTinyFont, $xpos, $y + 1, 
			$j, $bgcolor);
	    $im->string(($embiggen == 2) ? gdSmallFont : gdTinyFont, $xpos, $y, 
			$j, $colors{"darkblue"}); 
	    $y += ($embiggen == 2) ? gdSmallFont->height : gdTinyFont->height; 
	}
    } # foreach $i (keys %links)
    
470 471
# render node text.
# (this is done in a second pass so no text is obscured by boxes)
Chad Barb's avatar
Chad Barb committed
472
    
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    foreach $i (keys %nodes) {
	# only render label if there _is_ a label.  
	if (!exists $nodes{$i}{"label"}) { next; }
	
	# get node position
	($x, $y) = ($nodes{$i}{"x"}, $nodes{$i}{"y"});
	
	# my $nm = $i;
	my $nm = $nodes{$i}{"label"};
	
	@lines = ();
	
	# append space, so same patterns work on the last word.
	$nm .= " ";
	
	# first word (i.e., node name) 
	# always gets its own line.
	$nm =~ s/^(\S+)\s+//;
	push @lines, $1;
	
	# greedy line breaking (split works for links, but isn't quite sexy enough for nodes.):
	while ($nm ne "") {
	    if ($nm =~ s/^(.{1,12})\s+//) {
		# if the next n words (plus the space between them) 
		# total less than 13 characters, use that as a line. 
		push @lines, $1;
	    } elsif ($nm =~ s/^(\S+)\s+//) {
		# if the next word is longer than 12, we fall through to this,
		# which uses that word as a line.
		push @lines, $1;
	    } else {
		# if neither of the above applies,
		# we abort the loop, and add a complaint to the string list.
		push @lines, "ERROR";
		last;
	    }
	}
	
	# now that @lines contains each line of the node caption,
	# render it.
	
	my $linenum = 0;
	foreach $j (@lines) {
Chad Barb's avatar
Chad Barb committed
516
#     warn "$j $x $y!";
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
	    if ($linenum++ == 0) {
		# The first line, so we render it bigger.
		$xpos = $x - ((length($j) - 0.5) * ($embiggen ? gdMediumBoldFont->width : gdSmallFont->width) / 2);
		
		$im->string($embiggen ? gdMediumBoldFont : gdSmallFont, 
			    $xpos + 1, $y + 20, $j, $bgcolor);
		$im->string($embiggen ? gdMediumBoldFont : gdSmallFont, 
			    $xpos - 1, $y + 20, $j, $bgcolor);
		$im->string($embiggen ? gdMediumBoldFont : gdSmallFont, 
			    $xpos, $y + 19, 
			    $j, $bgcolor);
		$im->string($embiggen ? gdMediumBoldFont : gdSmallFont, 
			    $xpos, $y + 21, 
			    $j, $bgcolor);
		$im->string($embiggen ? gdMediumBoldFont : gdSmallFont, 
			    $xpos, $y + 20, 
			    $j, $colors{"black"}); 
		$y += $embiggen ? gdMediumBoldFont->height : gdSmallFont->height; 
	    } else {
		# Not the first line, so we render it smaller.
		$xpos = $x - ((length($j) - 0.5) * ($embiggen ? gdSmallFont->width : gdTinyFont->width) / 2); 
		
		$im->string(($embiggen) ? gdSmallFont : gdTinyFont, 
			    $xpos + 1, $y + 20, 
			    $j, $bgcolor);
		$im->string(($embiggen) ? gdSmallFont : gdTinyFont, 
			    $xpos - 1, $y + 20, 
			    $j, $bgcolor);
		$im->string(($embiggen) ? gdSmallFont : gdTinyFont, 
			    $xpos, $y + 19, 
			    $j, $bgcolor);
		$im->string(($embiggen) ? gdSmallFont : gdTinyFont, 
			    $xpos, $y + 21, 
			    $j, $bgcolor);
		$im->string(($embiggen) ? gdSmallFont : gdTinyFont, 
			    $xpos, $y + 20, 
			    $j, $colors{"black"}); 
		$y += ($embiggen) ? gdSmallFont->height : gdTinyFont->height; 
	    }
       } # foreach $j (@lines) 
    } # foreach $i (keys %nodes)
} # if ($thumbnail == 0)


# if there were no nodes, provide visual clue as to what's up.
if ($noNodes) {
    if ($thumbnail) {
	$center_x = ceil( $thumbnail / 2 );
	$center_y = $center_x;
	$radius = $center_x;
	($brx,$bry) = ($thumbnail, $thumbnail);
    } else {
	$center_x = ceil( $max_x / 2 );
	$center_y = ceil( $max_y / 2 );
	$radius = min( $center_x, $center_y );
	($brx,$bry) = ($max_x, $max_y);
    }

    for ($i = 0; $i < 4; $i++) {
	$im->arc( $center_x, $center_y, $radius - $i, $radius - $i, 0, 360, $colors{"palered"} );    
    }

    for ($i = -3; $i < 4; $i++) {
	$im->line( 0, 0 + $i,$brx,$bry + $i, $bgcolor );
    }
}
583

Leigh Stoller's avatar
Leigh Stoller committed
584 585 586 587 588 589 590
# Stash in database if not a thumb.
if (! $thumbnail) {
    DBQueryFatal("replace into vis_graphs set ".
		 "  pid='$pid', eid='$eid', zoom='$zoom', detail='$detail', ".
		 "  image=" . DBQuoteSpecial($im->png));
}

591 592 593 594 595
#write it to stdout

binmode STDOUT;
print $im->png;

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
# functions to generate labels for links on $detail > 0

sub reportbw {
    my $bandwidth = shift;
    if ($bandwidth >= 5000000) {
	return sprintf( "%.0f", ($bandwidth / 1000000) ) . "Gb";
    } elsif ($bandwidth >= 1000000) {
	return sprintf( "%.1f", ($bandwidth / 1000000) ) . "Gb";
    } elsif ($bandwidth >= 5000) {
	return sprintf( "%.0f", ($bandwidth / 1000) ) . "Mb";
    } elsif ($bandwidth >= 1000) {
	return sprintf( "%.1f", ($bandwidth / 1000) ) . "Mb";
    } elsif ($bandwidth >= 5) {
	return sprintf( "%.0f", $bandwidth ) . "kb";
    } else {
	return sprintf( "%.1f", $bandwidth ) . "kb";
    }
}

sub reportdelay {
    my $delay = shift;
    if ($delay == 0) { return "0msec"; }
618

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
    if ($delay >= 10) {
	return sprintf( "%.0f", $delay ) . "msec";
    } else {
	return sprintf( "%.1f", $delay ) . "msec";
    }
}

sub reportloss {
    my $losspct = shift;
    $losspct *= 100;
    if ($losspct < 0.0001) { return "0\%loss"; }

    if ($losspct > 5) {
	return sprintf( "%.0f", $losspct ) . "\%loss";
    } elsif ($losspct > 1) {
	return sprintf( "%.1f", $losspct ) . "\%loss";
    } elsif ($losspct > 0.1) {
	return sprintf( "%.2f", $losspct ) . "\%loss";
    } else {
	return sprintf( "%.3f", $losspct ) . "\%loss";
    }
}
641

642 643 644 645 646 647 648 649 650
sub gendesc {
    my ($delay0, $delay1, $bw0, $bw1, $loss0, $loss1) = @_;
    my $desc = "";
    
    if ($bw0 == $bw1) {
	$desc .= reportbw( $bw0 ) . " ";
    } else {
	$desc .= reportbw( $bw0 ) . "/" . reportbw( $bw1 ) . " ";
    }
651

652 653 654 655 656 657 658
    if ($delay0 == $delay1) {
	if ($delay0 != 0) {
	    $desc .= reportdelay( $delay0 ) . " ";
	}
    } else {
	$desc .= reportdelay( $delay0 ) . "/" . reportdelay( $delay1 ) . " ";
    }
659

660 661 662 663 664 665 666
    if ($loss0 == $loss1) {
	if ($loss0 != 0.0) {
	    $desc .= reportloss( $loss0 ) . " ";
	}
    } else {
	$desc .= reportloss( $loss0 ) . "/" . reportloss( $loss1 ) . " ";
    }
667

668
    $desc =~ s/\s$//g;
669

670 671
    return $desc;
}
672

673 674 675 676
sub combineloss {
    my ($a, $b) = @_;
    return (1.0 - ((1.0 - $a) * (1.0 - $b)));
}
677

678 679 680 681 682 683 684
sub ceil {
    my ($a) = @_;
    my $b = sprintf("%i",$a);
    if ($a == $b) { return $b; }
    return $b + 1;
}

685 686 687 688
sub dprint($) {
    my $n = shift;
    if ($debug > 0) { print STDERR $n; }
}
689