package network::test; # $Id

use strict;
use MDK::Common;
use run_program;
use Socket;

sub new {
  my ($class, $o_hostname) = @_;
  bless {
	 hostname => $o_hostname || "mandrakesoft.com"
	}, $class;
}

#- launch synchronous test, will hang until the test finishes
sub test_synchronous {
  my ($o) = @_;
  ($o->{address}, $o->{ping}) = resolve_and_ping($o->{hostname});
  $o->{done} = 1;
}

#- launch asynchronous test, won't hang
sub start {
  my ($o) = @_;
  $o->{done} = 0;
  $o->{kid} = bg_command->new(sub {
				my ($address, $ping) = resolve_and_ping($o->{hostname});
				print "$address|$ping\n";
			      });
}

#- abort asynchronous test
sub abort {
  my ($o) = @_;
  if ($o->{kid}) {
    kill -9, $o->{kid}{pid};
    undef $o->{kid};
  }
}

#- returns a true value if the test is finished, usefull for asynchronous tests
sub is_done {
  my ($o) = @_;
  $o->update_status;
  to_bool($o->{done});
}

#- return a true value if the connection works (hostname resolution and ping)
sub is_connected {
  my ($o) = @_;
  to_bool(defined($o->{hostname}) && defined($o->{ping}));
}

#- get hostname used in test for resolution and ping
sub get_hostname {
  my ($o) = @_;
  $o->{hostname};
}

#- get resolved address (if any) of given hostname
sub get_address {
  my ($o) = @_;
  $o->{address};
}

#- get ping (if any) to given hostname
sub get_ping {
  my ($o) = @_;
  $o->{ping};
}

sub resolve_and_ping {
  my ($hostname) = @_;
  require Net::Ping;
  require Time::HiRes;
  my $p;
  if ($>) {
      $p = Net::Ping->new('tcp');
      # Try connecting to the www port instead of the echo port
      $p->{port_num} = getservbyname('http', 'tcp');
  } else {
      $p = Net::Ping->new('icmp');
  }
  $p->Net::Ping::hires; #- get ping as float
  #- default timeout is 5 seconds
  my ($ret, $ping, $address) = $p->Net::Ping::ping($hostname, 5);
  if ($ret) {
      return $address, $ping;
  } elsif (defined($ret)) {
      return $address;
  }
}

sub update_status {
  my ($o) = @_;
  if ($o->{kid}) {
    my $fd = $o->{kid}{fd};
    fcntl($fd, c::F_SETFL(), c::O_NONBLOCK()) or die "can't fcntl F_SETFL: $!";
    local $| = 1;
    if (defined(my $output = <$fd>)) {
      ($o->{address}, $o->{ping}) = $output =~ /^([\d\.]+)\|([\d\.]+)*$/;
      $o->{done} = 1;
      undef $o->{kid};
    }
  }
}

1;

=head1 network::test

=head2 Test synchronously

#- resolve and get ping to hostname from command line if given, else to Mandrakesoft
use lib qw(/usr/lib/libDrakX);
use network::test;

my $net_test = network::test->new($ARGV[0]);
$net_test->test_synchronous;

my $is_connected = $net_test->is_connected;
my $hostname = $net_test->get_hostname;
my $address = $net_test->get_address;
my $ping = $net_test->get_ping;

print "connected: $is_connected
host: $hostname
resolved host: $address
ping to host: $ping
";

=head2 Test asynchronously

#- resolve and get ping to hostname from command line if given, else to Mandrakesoft
#- prints a "." every 10 miliseconds during connection test
use lib qw(/usr/lib/libDrakX);
use network::test;

my $net_test = network::test->new($ARGV[0]);
$net_test->start;

do {
  print ".\n";
  select(undef, undef, undef, 0.01);
} while !$net_test->is_done;

my $is_connected = $net_test->is_connected;
my $hostname = $net_test->get_hostname;
my $address = $net_test->get_address;
my $ping = $net_test->get_ping;

print "connected: $is_connected
host: $hostname
resolved host: $address
ping to host: $ping
";

=cut
option>
<option value='topic/switching_to_urpmi'>topic/switching_to_urpmi</option>
<option value='topic/unlabeled-1.1.1'>topic/unlabeled-1.1.1</option>
<option value='topic/v_webmin_0_87'>topic/v_webmin_0_87</option>
<option value='topic/x86_64-branch'>topic/x86_64-branch</option>
<option value='user/animtim/designWork'>user/animtim/designWork</option>
<option value='user/colin/rescue-systemd'>user/colin/rescue-systemd</option>
<option value='user/ennael/mga6'>user/ennael/mga6</option>
<option value='user/erwan/bug-13680'>user/erwan/bug-13680</option>
<option value='user/jibz/aarch64'>user/jibz/aarch64</option>
<option value='user/martinw/mga6'>user/martinw/mga6</option>
<option value='user/pterjan/arm64'>user/pterjan/arm64</option>
</select> <input type='submit' value='switch'/></form></td></tr>
<tr><td class='sub'>Mageia Installer and base platform for many utilities</td><td class='sub right'>Thierry Vignaud [tv]</td></tr></table>
<table class='tabs'><tr><td>
<a href='/software/drakx/?h=15.9'>summary</a><a href='/software/drakx/refs/?h=15.9&amp;id=259d0a6f8b4fac910e5d1a44a288786ce077ef32'>refs</a><a href='/software/drakx/log/Makefile.common?h=15.9'>log</a><a href='/software/drakx/tree/Makefile.common?h=15.9&amp;id=259d0a6f8b4fac910e5d1a44a288786ce077ef32'>tree</a><a class='active' href='/software/drakx/commit/Makefile.common?h=15.9&amp;id=259d0a6f8b4fac910e5d1a44a288786ce077ef32'>commit</a><a href='/software/drakx/diff/Makefile.common?h=15.9&amp;id=259d0a6f8b4fac910e5d1a44a288786ce077ef32'>diff</a><a href='/software/drakx/stats/Makefile.common?h=15.9'>stats</a></td><td class='form'><form class='right' method='get' action='/software/drakx/log/Makefile.common'>
<input type='hidden' name='h' value='15.9'/><input type='hidden' name='id' value='259d0a6f8b4fac910e5d1a44a288786ce077ef32'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/software/drakx/commit/?h=15.9&amp;id=259d0a6f8b4fac910e5d1a44a288786ce077ef32'>root</a>/<a href='/software/drakx/commit/Makefile.common?h=15.9&amp;id=259d0a6f8b4fac910e5d1a44a288786ce077ef32'>Makefile.common</a></div><div class='content'><div class='cgit-panel'><b>diff options</b><form method='get'><input type='hidden' name='h' value='15.9'/><input type='hidden' name='id' value='259d0a6f8b4fac910e5d1a44a288786ce077ef32'/><table><tr><td colspan='2'/></tr><tr><td class='label'>context:</td><td class='ctrl'><select name='context' onchange='this.form.submit();'><option value='1'>1</option><option value='2'>2</option><option value='3' selected='selected'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='15'>15</option><option value='20'>20</option><option value='25'>25</option><option value='30'>30</option><option value='35'>35</option><option value='40'>40</option></select></td></tr><tr><td class='label'>space:</td><td class='ctrl'><select name='ignorews' onchange='this.form.submit();'><option value='0' selected='selected'>include</option><option value='1'>ignore</option></select></td></tr><tr><td class='label'>mode:</td><td class='ctrl'><select name='dt' onchange='this.form.submit();'><option value='0' selected='selected'>unified</option><option value='1'>ssdiff</option><option value='2'>stat only</option></select></td></tr><tr><td/><td class='ctrl'><noscript><input type='submit' value='reload'/></noscript></td></tr></table></form></div><table summary='commit info' class='commit-info'>
<tr><th>author</th><td>Thierry Vignaud &lt;tvignaud@mandriva.org&gt;</td><td class='right'>2005-06-17 02:47:45 +0000</td></tr>
<tr><th>committer</th><td>Thierry Vignaud &lt;tvignaud@mandriva.org&gt;</td><td class='right'>2005-06-17 02:47:45 +0000</td></tr>
<tr><th>commit</th><td colspan='2' class='sha1'><a href='/software/drakx/commit/Makefile.common?h=15.9&amp;id=259d0a6f8b4fac910e5d1a44a288786ce077ef32'>259d0a6f8b4fac910e5d1a44a288786ce077ef32</a> (<a href='/software/drakx/patch/Makefile.common?id=259d0a6f8b4fac910e5d1a44a288786ce077ef32'>patch</a>)</td></tr>
<tr><th>tree</th><td colspan='2' class='sha1'><a href='/software/drakx/tree/?h=15.9&amp;id=259d0a6f8b4fac910e5d1a44a288786ce077ef32'>184ebc944313149fa95d76af6cc30e96355111fd</a> /<a href='/software/drakx/tree/Makefile.common?h=15.9&amp;id=259d0a6f8b4fac910e5d1a44a288786ce077ef32'>Makefile.common</a></td></tr>
<tr><th>parent</th><td colspan='2' class='sha1'><a href='/software/drakx/commit/Makefile.common?h=15.9&amp;id=71c62f751756d0fbd8c22820aac20a25ccd05e2c'>71c62f751756d0fbd8c22820aac20a25ccd05e2c</a> (<a href='/software/drakx/diff/Makefile.common?h=15.9&amp;id=259d0a6f8b4fac910e5d1a44a288786ce077ef32&amp;id2=71c62f751756d0fbd8c22820aac20a25ccd05e2c'>diff</a>)</td></tr><tr><th>download</th><td colspan='2' class='sha1'><a href='/software/drakx/snapshot/drakx-259d0a6f8b4fac910e5d1a44a288786ce077ef32.tar'>drakx-259d0a6f8b4fac910e5d1a44a288786ce077ef32.tar</a><br/><a href='/software/drakx/snapshot/drakx-259d0a6f8b4fac910e5d1a44a288786ce077ef32.tar.gz'>drakx-259d0a6f8b4fac910e5d1a44a288786ce077ef32.tar.gz</a><br/><a href='/software/drakx/snapshot/drakx-259d0a6f8b4fac910e5d1a44a288786ce077ef32.tar.bz2'>drakx-259d0a6f8b4fac910e5d1a44a288786ce077ef32.tar.bz2</a><br/><a href='/software/drakx/snapshot/drakx-259d0a6f8b4fac910e5d1a44a288786ce077ef32.tar.xz'>drakx-259d0a6f8b4fac910e5d1a44a288786ce077ef32.tar.xz</a><br/><a href='/software/drakx/snapshot/drakx-259d0a6f8b4fac910e5d1a44a288786ce077ef32.zip'>drakx-259d0a6f8b4fac910e5d1a44a288786ce077ef32.zip</a><br/></td></tr></table>
<div class='commit-subject'>(read_grub) fix reading config with new grub (thus fixing detectloader, thus</div><div class='commit-msg'>