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

Leigh Stoller's avatar
Leigh Stoller committed
3
#
4
# Copyright (c) 2000-2013 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
24 25
#

26 27 28 29
use English;
use Getopt::Std;
use lib '@prefix@/lib';
use libdb;
Leigh Stoller's avatar
Leigh Stoller committed
30

31
#
32 33
# Render experiment from the database.
# Spits PNG image out to stdout.
34 35
#

36 37 38
# TODO: delay nodes
# TODO: display text in image if there are no nodes.

39
use GD;
Chad Barb's avatar
Chad Barb committed
40
use Getopt::Std;
41 42 43 44 45
#
# Configure variables
#
my $TB		= "@prefix@";
my $ICONDIR     = "$TB/www";
46

47 48 49
sub dprint($);

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

53 54 55 56 57 58 59 60 61
#
# Look for -x option, and chain to SVG rendering.
#
if (grep {$_ eq "-x"} @ARGV) {
    exec "$TB/libexec/vis/svgrender", @ARGV;
    die("render: Could not exec svgrender: $!");
}

my  $optlist = "z:d:vt:x";
62
if (! getopts($optlist, \%options)) { usage; }
63

64 65 66 67
if (@ARGV != 2) { usage; }

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

Chad Barb's avatar
Chad Barb committed
69 70 71 72 73 74
my $zoom = 1;
if ( defined($options{"z"} ) ) {
    my $zf = $options{"z"};
    if ($zf =~ /^([\.0-9]+)/) {
	$zoom = $1;
    } else {
75
	die("Bad argument to -z; must be float.");
Chad Barb's avatar
Chad Barb committed
76 77 78
    }
}

79 80 81 82 83
my $detail = 0;
if (defined($options{"d"})) {
    my $df = $options{"d"};
    if ($df =~ /^([0-9]+)/) {
	$detail = $1;
84
    } else {
85
	die("Bad argument to -d; must be non-negative integer.");
Chad Barb's avatar
Chad Barb committed
86
    }
87 88
}

89 90 91 92 93 94 95 96 97 98
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.");
    }
}

99 100
my %nodes = ();
my %links = ();
101

102 103
my $pid = $ARGV[0];
my $eid = $ARGV[1];
104

105 106 107 108 109 110
my $exptidx;
if (!TBExptIDX($pid, $eid, \$exptidx)) {
    die("*** $0:\n".
	"    No such experiment $pid/$eid!");
}

111 112
# Get node list.
# (left join info from vis_nodes; could also left join virt_lans,
113 114
#  but there's no reason to since lan info shows up on the link,
#  not the LAN node.)
115

116
my $result = DBQueryFatal("SELECT vis.vname, vis.vis_type, vis.x, vis.y, ".
117
			  "       virt.type " .
118 119
			  "FROM vis_nodes as vis " .
			  "LEFT JOIN virt_nodes as virt on ".
120 121
			  "     vis.vname=virt.vname and vis.pid=virt.pid and".
			  "     vis.eid=virt.eid ".
122
			  "WHERE vis.pid='$pid' AND vis.eid='$eid'");
123

124
while (my ($name, $vis_type, $vis_x, $vis_y, $virt_type) = $result->fetchrow) {
125
    dprint "NODE $name $vis_type $vis_x $vis_y\n";
126

127 128 129
    $nodes{$name}{"type"} = $vis_type; # vis type, not hwtype.
    $nodes{$name}{"x"} = $vis_x;
    $nodes{$name}{"y"} = $vis_y;
130

131 132 133
    # what appears in label depends on the detail level.
    my $label = $name;
    if ($detail > 0) {
134
	if (defined($virt_type)) {
135
	    $label .= "(" . $virt_type . ")";
136 137
	}
    }
138
    $nodes{$name}{"label"} = $label;
139

140 141 142 143 144
    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; }
}
145

146 147
if (!(defined $min_x)) {
    # no nodes.
148 149 150 151 152 153 154 155 156 157 158 159
    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) {
160 161
	$nodes{$i}{"x"} = (($nodes{$i}{"x"} - $min_x) * $zoom) + 60;
	$nodes{$i}{"y"} = (($nodes{$i}{"y"} - $min_y) * $zoom) + 60;
162 163 164
    }
    
    # adjust max x,y appropriately.
165 166
    $max_x = (($max_x - $min_x) * $zoom) + 120;
    $max_y = (($max_y - $min_y) * $zoom) + 120;
167 168
}

169 170
# get vlan list.

171 172
$result = DBQueryFatal("SELECT vname, vnode, vport, ip, ".
		          "delay, bandwidth, lossrate, " .
173 174 175 176
			  "rdelay, rbandwidth, rlossrate, " . 
			  "member FROM virt_lans " .
			  "WHERE pid='$pid' AND eid='$eid'");

177
while (my ($vname, $vnode, $vport, $ip, $delay, $bandwidth, 
178 179 180 181 182 183 184 185 186
           $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;
187 188 189 190 191 192 193

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

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
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";
	}
    }
}    

237

238 239
# if $embiggen == 1, node fonts will be rendered bigger.
# if $embiggen == 2, node and link fonts will be rendered bigger.
240

241
my $embiggen = 0; 
242

243 244
if ($zoom >= 1.5) { $embiggen = 1; }
if ($zoom >= 1.75) { $embiggen = 2; }
Chad Barb's avatar
Chad Barb committed
245

246 247
# start constructing the image

248
dprint "Image size = $max_x x $max_y\n";
249 250 251 252 253 254

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

256 257
$nodeicon = GD::Image->newFromPng("$ICONDIR/nodeicon.png") || warn "nodeicon.png not found";
$lanicon  = GD::Image->newFromPng("$ICONDIR/lanicon.png") || warn "lanicon.png not found";
258 259 260

%colors = ();

261 262 263 264 265 266 267
$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);
268 269 270 271
$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);
272 273 274 275 276 277 278 279 280
$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);
281 282

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

285
$im->transparent($bgcolor);
286 287
$im->fill( 1, 1, $bgcolor );

288
#$im->interlaced('true');
289 290
#$im->rectangle( 0,0,99,99, $gray50 );

291 292 293
if ($thumbnail != 0) {    
    # Thumbnails are drawn similarly to full views,
    # but there are enough differences to warrant separate code.
294

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    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"} );
    }
312

313 314 315 316 317 318 319 320 321 322 323 324 325
    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);

326 327
	$size = ceil(min( min( 16 * $thumbnail / $max_x, 16 * $thumbnail / $max_y ), $thumbnail / 16));
	
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

	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.
344 345


346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
# 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"});
	}
    } 
366

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 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
# 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 );
	}
    }
420

421
# render nodes.
422

423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    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)
459 460
    
    
461 462
# render link text.
# (this is done in a second pass so no text is obscured by boxes)
463
    
464 465 466
    foreach $i (keys %links) {
	# only render label if there _is_ a label.
	if (!exists $links{$i}{"label"}) { next; }
467
	
468 469 470 471 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
	# 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)
    
501 502
# render node text.
# (this is done in a second pass so no text is obscured by boxes)
Chad Barb's avatar
Chad Barb committed
503
    
504 505 506 507 508 509 510 511 512 513 514 515 516 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
    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
547
#     warn "$j $x $y!";
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 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	    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 );
    }
}
614

Leigh Stoller's avatar
Leigh Stoller committed
615 616 617
# Stash in database if not a thumb.
if (! $thumbnail) {
    DBQueryFatal("replace into vis_graphs set ".
618 619
		 "  exptidx='$exptidx', pid='$pid', eid='$eid', ".
                 "  zoom='$zoom', detail='$detail', ".
Leigh Stoller's avatar
Leigh Stoller committed
620 621 622
		 "  image=" . DBQuoteSpecial($im->png));
}

623 624 625 626 627
#write it to stdout

binmode STDOUT;
print $im->png;

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
# 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"; }
650

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    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";
    }
}
673

674 675 676 677 678 679 680 681 682
sub gendesc {
    my ($delay0, $delay1, $bw0, $bw1, $loss0, $loss1) = @_;
    my $desc = "";
    
    if ($bw0 == $bw1) {
	$desc .= reportbw( $bw0 ) . " ";
    } else {
	$desc .= reportbw( $bw0 ) . "/" . reportbw( $bw1 ) . " ";
    }
683

684 685 686 687 688 689 690
    if ($delay0 == $delay1) {
	if ($delay0 != 0) {
	    $desc .= reportdelay( $delay0 ) . " ";
	}
    } else {
	$desc .= reportdelay( $delay0 ) . "/" . reportdelay( $delay1 ) . " ";
    }
691

692 693 694 695 696 697 698
    if ($loss0 == $loss1) {
	if ($loss0 != 0.0) {
	    $desc .= reportloss( $loss0 ) . " ";
	}
    } else {
	$desc .= reportloss( $loss0 ) . "/" . reportloss( $loss1 ) . " ";
    }
699

700
    $desc =~ s/\s$//g;
701

702 703
    return $desc;
}
704

705 706 707 708
sub combineloss {
    my ($a, $b) = @_;
    return (1.0 - ((1.0 - $a) * (1.0 - $b)));
}
709

710 711 712 713 714 715 716
sub ceil {
    my ($a) = @_;
    my $b = sprintf("%i",$a);
    if ($a == $b) { return $b; }
    return $b + 1;
}

717 718 719 720
sub dprint($) {
    my $n = shift;
    if ($debug > 0) { print STDERR $n; }
}
721