package Requester::lxNetwork;

use strict;

our( @ISA );
@ISA = qw(Requester::Requester);

my $requester = new Requester::lxNetwork;

sub new
{
    my ($this, $args ) = @_;
    my  $class = ref($this) || $this;
    my  $self = {};

    bless($self, $class);
    $self->register( "lxNetwork" );
    return $self;
}




# the default "request()" method is good enough for our purposes


sub getinfo {
    my( $self ) = @_;

    my $info = {};
    open( INFO, "</proc/net/dev" ) || return {};
    my $line = <INFO>;
	return( {} ) unless( $line =~ /transmit/i );

	my $firstline = <INFO>;
	chomp $firstline;
    $firstline =~ s/\|/ |/g;
	$firstline =~ s/^[\s\t]+//;
	my( @names ) = split( /[\s\t]+/, $firstline );
	my $prefix = "";
	my $i = 0;
	$names[0] = "description";
	foreach my $name (@names) {
	    if( $name =~ /^\|/ ) {
		    $prefix = $prefix ? "out":"in";
			$name = $';
	    }
		$name = $prefix.$name;
	    $i++;
    }

	my $id = 0;
    while( <INFO> ) {
        chomp;
		s/^[\s\t]+//;
		my @values = split( /[\s\t:]+/ );
		my $iface = $info->{$id++} = {};
	    for( my $i=0; $i<=$#names; $i++ ) {
		    $iface->{$names[$i]} = $values[$i];
		}
		$iface->{"ifIndex"} = $id;
		$iface->{"ifDescr"} = $iface->{"description"};
		$iface->{"ifInOctets"} = $iface->{"inbytes"};
		$iface->{"ifOutOctets"} = $iface->{"outbytes"};
		$iface->{"ifInErrors"} = $iface->{"inerrs"};
		$iface->{"ifOutErrors"} = $iface->{"outerrs"};
		$iface->{"ifInUcastPkts"} = $iface->{"inpackets"};
		$iface->{"ifOutUcastPkts"} = $iface->{"outpackets"};
		$iface->{"ifOutDiscards"} = $iface->{"outdrop"};

		# we are lying about these things
		$iface->{"ifSpeed"} = 10000000;
		$iface->{"ifAdminStatus"} = 1;
		$iface->{"ifOperStatus"} = 1;
    }
    close INFO;

    return $info;
}


sub available {
    return( -f "/proc/net/dev" );
}


1;

