# NtpTraceReqWorker is a helper class for the Requester::ntptrace class
# below. NtptraceReqWorker runs ntptrace in the background (since it may
# take some time)
package NtpTraceReqWorker;

use CmdWorker;

our(@ISA);
@ISA = qw( CmdWorker );

# create a new Worker executing command $comm and putting
# the results into $info hash
sub new {
    my( $this, $info, $comm ) = @_;
    print "  creating NtpTraceReqWorker for $comm\n" if( $BigSister::common::dl > 2 );
    my $self = CmdWorker::new( $this, $comm );
    $self->{"ntptrace.info"} = $info;
    $info->{0}->{"text"} = "";
    $info->{0}->{"status"} = 0;
    return $self;
}


sub process {
    my( $self, $line ) = @_;

    print "      processing ntptrace:$line\n" if( $BigSister::common::dl > 6 );
    my $info = $self->{"ntptrace.info"}->{0};
    unless( $info->{"text"} ) {
        if( $line =~ /stratum (\d+).*offset ([0-9-.]+).*distance ([0-9-.]+)/ ) {
	    $info->{"stratum"} = $1;
	    $info->{"offset"} = $2;
	    $info->{"distance"} = $3;
	    $info->{"status"} = ($1>=16) ? 1 : 2;
	    print "        ntptrace: got info\n" if( $BigSister::common::dl > 7 );
	}
	elsif( $line =~ /\*Timeout\*/ ) {
	    $info->{"status"} = 0;
	    print "        ntptrace: timeout\n" if( $BigSister::common::dl > 7 );
	}
    }
    if( $line =~ /\*Not Synchronized\*/ ) {
        $info->{"status"} = 1;
    }
    $info->{"text"} .= "$line\n";
}



package Requester::ntptrace;

use strict;
use Requester::Requester;

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

my $requester = new Requester::ntptrace;
my $comm = "ntptrace -n -r 2 -t 5";


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

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




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


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

    my $info = { 0 => {} };
    my $hostcomm = $comm." ".($self->{"env"}->{"nodeinstance"}->{"host"});
    my $worker = new NtpTraceReqWorker( $info, $hostcomm );
    $worker->{"Test.info"} = $info;
    return $worker;
}



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

	return( $self->command_available( "ntptrace", "invalid", "options" ) );
}

1;

