aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/cache/driver/memcache.php
Commit message (Collapse)AuthorAgeFilesLines
* [ticket/15246] Allow use of Unix socket as memcache(d) hostrxu2017-06-171-2/+2
| | | | PHPBB3-15246
* [ticket/13930] Add missing spaces to codeMarc Alexander2015-06-071-1/+1
| | | | PHPBB3-13930
* [ticket/12594] Remove @package tags and update file headersYuriy Rusko2014-05-271-4/+7
| | | | PHPBB3-12594
* [ticket/11230] Use inheritdoc in docblocks in cache driversMatt Friedman2014-03-191-6/+2
| | | | | | http://tracker.phpbb.com/browse/PHPBB3-11230 PHPBB3-11230
* [task/code-sniffer] Remove the IN_PHPBB check side-effect from class files.Andreas Fischer2013-10-301-8/+0
| | | | PHPBB3-11980
* [ticket/11700] Move all recent code to namespacesNils Adermann2013-09-161-2/+4
| | | | PHPBB3-11700
* [ticket/11698] Moving all autoloadable files to phpbb/Nils Adermann2013-07-141-0/+129
PHPBB3-11698
id='n65' href='#n65'>65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
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, will not 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->hires; #- get ping as float
    #- default timeout is 5 seconds
    my ($ret, $ping, $address) = $p->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 not 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 Mandriva
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 Mandriva
#- 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