Changeset 357

Show
Ignore:
Timestamp:
11/11/06 15:12:07 (5 years ago)
Author:
nick
Message:

Get working

Location:
wiki-toolkit-plugin-ping
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • wiki-toolkit-plugin-ping/lib/Wiki/Toolkit/Plugin/Ping.pm

    r356 r357  
    88use Wiki::Toolkit::Plugin; 
    99use LWP; 
     10use LWP::UserAgent; 
    1011 
    1112@ISA = qw( Wiki::Toolkit::Plugin ); 
     
    1617sub new { 
    1718    my $class = shift; 
    18     my %services = @_; 
     19    my %args = @_; 
    1920 
    2021    my $self = {}; 
    2122    bless $self, $class; 
     23 
     24    # Get list of services 
     25    unless($args{services}) {  
     26        $self->{services} = {}; 
     27        return $self;  
     28    } 
     29    my %services = %{$args{services}}; 
     30 
     31    # Get node -> URL mapping 
     32    unless($args{node_to_url}) { 
     33        die("Must supply 'node_to_url;"); 
     34    } 
     35    unless($args{node_to_url} =~ /\$node/) { 
     36        die("node_to_url '$args{node_to_url}' must contain \$node"); 
     37    } 
     38    $self->{node_to_url} = $args{node_to_url}; 
     39     
    2240 
    2341    # Check the services 
     
    4260} 
    4361 
     62# Return our list of services, in case anyone's interested 
     63sub services { 
     64    my $self = shift; 
     65    return %{$self->{services}}; 
     66} 
     67 
    4468# Define our post_write plugin, which does the ping 
    4569# Happens in another thread, to stop it slowing things down 
    4670sub post_write { 
    4771    my $self = shift; 
     72    unless(keys %{$self->{services}}) { return; } 
    4873 
    4974    my %args = @_; 
     
    5277 
    5378    # Spawn a new thread 
     79    my $pid = fork(); 
     80    if($pid) { 
     81        # We're the main thread, return now 
     82        return; 
     83    } else { 
     84        # We're the child, do the work 
    5485 
    55     # Build the ping URL 
    56     # Ping 
     86        # What's the URL of the node? 
     87        my $node_url = $self->{node_to_url}; 
     88        $node_url =~ s/\$node/$node/; 
     89 
     90        # Get a LWP instance 
     91        my $ua = LWP::UserAgent->new; 
     92        $ua->agent("Wiki::Toolkit::Plugin::Ping $version");         
     93 
     94        # Ping each service 
     95        foreach my $service (keys %{$self->{services}}) { 
     96            # Build the ping URL 
     97            my $ping_url = $self->{services}->{$service}; 
     98            $ping_url =~ s/\$url/$node_url/; 
     99 
     100            # Ping 
     101            my $req = HTTP::Request->new(GET => $ping_url); 
     102            my $res = $ua->request($req); 
     103            unless($res->is_success) { 
     104                warn("Error pinging $service: $res->status_line"); 
     105            } 
     106        } 
     107 
     108        # All done, close the thread 
     109        exit; 
     110    } 
    57111} 
    58112 
  • wiki-toolkit-plugin-ping/t/01_init.t

    r354 r357  
    44use Wiki::Toolkit::Plugin::Ping; 
    55 
    6 use Test::More tests => 3; 
     6use Test::More tests => 4; 
    77 
    88# Basic create 
     
    1212# Several URls create 
    1313my $plugin2 = Wiki::Toolkit::Plugin::Ping->new( 
     14    node_to_url => 'http://localhost/\$node', 
     15    services => { 
    1416        test => 'http://hello/?$url', 
    1517        test2 => 'http://hello/?$url', 
     18    } 
    1619); 
    1720ok( !undef $plugin2, "Plugin was created OK with no URLs" ); 
     
    2124eval { 
    2225    $plugin3 = Wiki::Toolkit::Plugin::Ping->new( 
     26        node_to_url => 'http://localhost/\$node', 
     27        services => { 
    2328            test => 'http://something/' 
     29        } 
    2430    ); 
    2531}; 
    26 warn("Plugin3 is '$plugin3'\n"); 
    2732ok( ! $plugin3, "Can't create with a url missing \$url" ); 
     33 
     34# Don't give the URL builder 
     35my $plugin4 = undef; 
     36eval { 
     37    $plugin4 = Wiki::Toolkit::Plugin::Ping->new( 
     38        services => { 
     39            test => 'http://something/$url' 
     40        } 
     41    ); 
     42}; 
     43ok( ! $plugin4, "Can't create with a node_to_url missing" ); 
     44 
  • wiki-toolkit-plugin-ping/t/02_call.t

    r356 r357  
    66use Wiki::Toolkit::Plugin::Ping; 
    77 
    8 use Test::More tests => 1; 
     8use IO::Socket; 
     9 
     10use Test::More tests => 4; 
    911 
    1012 
     
    2022 
    2123# Listen on a special port, so we can check a ping happened 
     24my $sock = new IO::Socket::INET ( 
     25                    LocalPort => 112233, 
     26                    Proto => 'tcp', 
     27                    Listen => 1, 
     28); 
     29unless($sock) { 
     30    die("Can't listen on port 112233 for test"); 
     31} 
    2232 
    2333 
    2434# Create, to call localhost 
    2535my $plugin = Wiki::Toolkit::Plugin::Ping->new( 
     36    node_to_url => "http://wiki.org/\$node", 
     37    services => { 
    2638        test => "http://localhost:112233/url=\$url" 
     39    } 
    2740); 
    2841ok( $plugin, "Plugin was created OK with the local URL" ); 
     
    3952        metadata => {} 
    4053); 
     54 
     55# Check they actually sent us something 
     56my $rsock = $sock->accept(); 
     57my @req; 
     58my $going = 1; 
     59while($going && (my $line = <$rsock>)) { 
     60    $line =~ s/\r?\n$//; 
     61    unless($line) { $going = 0; } 
     62 
     63    push @req,$line; 
     64    warn "**$line**\n"; 
     65} 
     66 
     67# Check they requested the right thing 
     68like( $req[0], qr/^GET \/url=http:\/\/wiki.org\/TestNode/, "Did right get" ); 
     69like( $req[2], qr/^Host: localhost:112233/, "Correct http/1.1 host" ); 
     70 
     71# Send them an OK 
     72print $rsock "HTTP/1.0 200 OK\r\n\r\n"; 
     73 
     74# Close 
     75close($rsock); 
     76close($sock); 
     77 
     78# All happy 
     79ok( "Happy" );