# -*-Perl-*-

# instances allowed: one 

# (single instance modules would be silly to use more than one of
# anyway, so we use package local storage.  This is faster and places
# less artificial load on the machine than doing everything through
# the object hash)

package MGMmodule::netstat;
use vars qw($xpath @if @keys $active $field $prompt @prev @bytes $widget 
	    $graph $lastmod $minscale);

# class init
sub module_init{
    my$this=shift;
    my$toplevel=$this->{"toplevel"};
    my$xclass=$this->{"xclass"};
    $minscale=2048;

    # is the helper up and running?
    if(!defined(%MGMmodule::helperST::net)){
	$toplevel->optionAdd("$xclass.active",'false',21);   
    }
    $toplevel->optionAdd("$xclass.order",20,21);   
    $this;
}

# instance init
sub module_instance{
    my$this=shift;
    my$toplevel=$this->{"toplevel"};
    return undef if(defined($xpath));
    $xpath=$this->{"xpath"};

    undef@keys;
    undef@prev;
    foreach my $key (keys %MGMmodule::helperST::net){
	push @keys, $key if(defined($MGMmodule::helperST::net{$key}));
    }
    $active=$MGMmodule::helperST::active_if;
    
    # three possible different formats.  
    my@test=split ' ', $MGMmodule::helperST::net{$keys[0]};
    
    if($#test==10){
	$field=5;
	$prompt=' pkts/s';
	$minscale=16;
    }else{
	$field=($#test+1)/2;
	$prompt='B/s';
    }

    # modify defaults
    my$i=0;
    foreach my $key (@keys){
	$toplevel->optionAdd("$xpath.bar.".($i*2).".label", "$key Rx",21);
	$toplevel->optionAdd("$xpath.bar.".($i*2).".litbackground", '#787cf8',21);
	$toplevel->optionAdd("$xpath.bar.".($i*2+1).".label", "$key Tx",21);
	$toplevel->optionAdd("$xpath.bar.".($i*2+1).".litbackground","#78b0f8",21);
	$i++;
    }

    $toplevel->optionAdd("$xpath.scalewidadj", 80*$i*2,21);  # narrower
    $toplevel->optionAdd("$xpath.scalereturn", 120,21);
    
    # this relies on the above defaults

    my($minx,$miny)=MGM::Graph::calcxysize($this,1024*1024*512,
					   $prompt,$active*2);
    
    $toplevel->optionAdd("$xpath.minx",        $minx,21);      
    $toplevel->optionAdd("$xpath.miny",        $miny,21);      
    $this;
}

# instance widget build
sub module_run{
    my$this=shift;
    
    $graph=MGM::Graph->new($this,num=>$active*2,
			   prompt=>$prompt,
			   minscale=>$minscale);

    $lastmod=-1;
    $widget=$graph->{"widget"};        # must return the widget
}

sub module_update{ 
    my$this=shift;
    
    # don't update unless the helper has
    if($lastmod!=$MGMmodule::helperST::lastmod){
	my$time=$MGMmodule::helperST::lastmod;
	my@vals;

	my$i=0;
	my$reflag=0;
	foreach my $key (@keys){
	    return &reconfig if(!defined($MGMmodule::helperST::net{$key}));
	    my@temp=split ' ', $MGMmodule::helperST::net{$key};
	    $bytes[$i]=$temp[0];
	    $bytes[$i+1]=$temp[$field];
	    $i+=2;
	}
	
	if(defined(@prev)){
	    for($i=0;$i<$active*2;$i++){
		$vals[$i]=($bytes[$i]-$prev[$i])/($time-$lastmod)*100;
	    }
	    
	    $graph->set(@vals);
	    return &reconfig if($active<$MGMmodule::helperST::active_if);
	}
	
	@prev=@bytes;
	$lastmod=$time;
    }
}

sub reconfig{
    &main::reinstance() if($widget->optionGet("reconfig","") eq 'true');
}

sub destroy{
    undef $xpath;
}

bless {};

