#!/usr/bin/perl -s 
#
#  Copyright (C) 2000, The MITRE Corporation
#
#  Use of this software is subject to the terms of the GNU General
#  Public License version 2.
#
#  Please read the file LICENSE for the exact terms.
#

# A very simple Perl/Tk Gui for displaying a GIF file. In our case, the
# GIF file contains a diagram of an ad-hoc network. In general, though, the
# GIF could be anything. The GUI periodically stat's the GIF file to see if it
# has been modified...if so, the GUI displays the new file.
#
# Usage: mmrpviz <mmrp port>
#
# $Id$ 
package mmrpviz;

use strict;
use Tk;

my $GIF;
my $object;

sub Usage {
    print STDERR "Usage: mmrpviz <mmrp port>\n";
    exit(1);
}

# Constructor
sub new {
    my $this = shift;
    my $self = {};
    
    my $class = ref($this) || $this;
    bless $self, $class;
    
    if(!defined($ARGV[0])) {
	Usage();
    }
    my $port = $ARGV[0];
    $GIF = "/var/run/mobilemesh/topology-" . $port . ".gif";
    
    $self->{mw} = new MainWindow;
    $self->MakeAppWidgets();
    $self->{modtime} = -M $GIF;

    return $self;
}

sub MakeAppWidgets {
    my $this = shift;

    # Build a frame to hold all application based widgets
    $this->{frame} = $this->{mw}->Frame()->pack(-side => 'top',-expand=>'1', -fill => 'both');
    $this->{gif} =  $this->{frame}->Photo('imggif', -file => $GIF);  
    $this->{image} = $this->{frame}->Label('-image' => $this->{gif});
    $this->{image}->pack;
}					    

sub Callback {
    $object->UpdateGif();
}

sub UpdateGif {
    my $this = shift;
    if($this->{modtime} ne -M $GIF) {
	sleep(1); # It appear that dot modifies the gif twice...wait for the second mod to complete
	$this->{modtime} = -M $GIF;
	$this->{gif} =  $this->{frame}->Photo('imggif', -file => $GIF);  
	$this->{image}->configure('-image' => $this->{gif});
	$this->{image}->pack;
    }
}

sub Run {
    my $this = shift;

    $this->{mw}->repeat(500.0,\&Callback);
    MainLoop;
}

# Build an instance and run it
$object = mmrpviz->new();
$object->Run();





