Net::Analysis::Listener::Example1 - emit/receive custom events


Net-Analysis documentation Contained in the Net-Analysis distribution.

Index


Code Index:

NAME

Top

Net::Analysis::Listener::Example1 - emit/receive custom events

SYNOPSIS

Top

 package Net::Analysis::Listener::Example1;

 use strict;
 use warnings;
 use base qw(Net::Analysis::Listener::Base);

 sub tcp_monologue {
     my ($self, $args) = @_;
     my ($mono) = $args->{monologue}; # isa Net::Analysis::TCPMonologue

     my $t = $mono->t_elapsed()->as_number();
     my $l = $mono->length();

     $self->emit(name => 'example_bandwidth_measurement_event',
                 args => { kb_sec => ($t) ? $l/($t*1024) : 0 }
                );
 }

 sub example_bandwidth_measurement_event {
     my ($self, $args) = @_;

     printf "Bandwidth: %10.2f KB/sec\n", $args->{kb_sec};
 }

 1;

You can invoke this example on a TCP capture file from the command line, as follows:

 $ perl -MNet::Analysis -e main Example1 t/t1_google.tcp

DESCRIPTION

Top

This example shows how to emit your own custom events, and also how to listen to them. This particular example has example_bandwidth_measurement_event in the same Listener.pm file, but you could easily put it in another Listener.pm if you wanted - just remember to tell the dispatcher about both of them.

SEE ALSO

Top

Net::Analysis.

AUTHOR

Top

Adam B. Worrall, <worrall@cpan.org>

COPYRIGHT AND LICENSE

Top


Net-Analysis documentation Contained in the Net-Analysis distribution.

package Net::Analysis::Listener::Example1;
# $Id: Example1.pm 140 2005-10-21 16:31:29Z abworrall $

use strict;
use warnings;
use base qw(Net::Analysis::Listener::Base);

sub tcp_monologue {
    my ($self, $args) = @_;
    my ($mono) = $args->{monologue}; # isa Net::Analysis::TCPMonologue

    my $t = $mono->t_elapsed()->as_number();
    my $l = $mono->length();

    $self->emit(name => 'example_bandwidth_measurement_event',
                args => { kb_sec => ($t) ? $l/($t*1024) : 0 }
               );
}

sub example_bandwidth_measurement_event {
    my ($self, $args) = @_;

    printf "Bandwidth: %10.2f KB/sec\n", $args->{kb_sec};
}

1;

__END__