package Requester::solswap;

use Requester::SlowRequester;

my $oldproto_warned = {};

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

my $requester = new Requester::solswap();

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

    bless($self, $class);
    $self->command( "swap -s" );
    $self->register( "solswap" );
    return $self;
}


sub do {
    my( $self, $srq ) = @_;

    $line = $srq->readln();
    return unless( $line );
    my $used;
    my $free;
    if( $line =~ /(\d+)(k|M|G) used/ ) {
	$used = size( $1, $2 );
    }
    if( $line =~ /(\d+)(k|M|G) available/ ) {
        $free = size( $1, $2 );
    }
    my $info = $srq->{"Test.info"};
    $info->{0} = {
        "total" => $used + $free,
	"used" => $used,
	"free" => $free
    };
    $srq->status( 3, "finished OK" );
}



sub size {
    my( $value, $unit ) = @_;

    # we don't expect this to be anything but 'k', but why not caring anyway ...
    if( $unit eq "M" ) {
        $value *= 1024;
    }
    elsif( $unit eq "G" ) {
        $value *= 1024*1024;
    }
    return $value;
}


sub available {
    my $test = `swap -s`;
    return( ($test =~ /used/) && ($test =~ /available/) );
}

1;

