| 1 | package Sprocket::Plugin::Manager; | ||
| 2 | |||
| 3 | use Sprocket qw( Plugin ); | ||
| 4 | use base 'Sprocket::Plugin'; | ||
| 5 | |||
| 6 | use POE::Filter::Line; | ||
| 7 | use Data::Dumper; | ||
| 8 | |||
| 9 | BEGIN { | ||
| 10 | eval "use Devel::Gladiator"; | ||
| 11 | # You can get it here: | ||
| 12 | # http://code.sixapart.com/svn/Devel-Gladiator/trunk/ | ||
| 13 | if ( $@ ) { | ||
| 14 | eval 'sub HAS_GLADIATOR() { 0 }'; | ||
| 15 | } else { | ||
| 16 | eval 'sub HAS_GLADIATOR() { 1 }'; | ||
| 17 | } | ||
| 18 | }; | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | my $class = shift; | ||
| 25 | $class->SUPER::new( | ||
| 26 | name => 'Manager', | ||
| 27 | @_ | ||
| 28 | ); | ||
| 29 | } | ||
| 30 | |||
| 31 | # --------------------------------------------------------- | ||
| 32 | # server | ||
| 33 | |||
| 34 | sub local_connected { | ||
| 35 | my ( $self, $server, $con, $socket ) = @_; | ||
| 36 | |||
| 37 | $self->take_connection( $con ); | ||
| 38 | |||
| 39 | # POE::Filter::Stackable object: | ||
| 40 | $con->filter->push( POE::Filter::Line->new() ); | ||
| 41 | |||
| 42 | $con->filter->shift(); # POE::Filter::Stream | ||
| 43 | |||
| 44 | $con->send( "Sprocket Manager" ); | ||
| 45 | $con->call( cmd_help => [] ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub local_receive { | ||
| 49 | my ( $self, $server, $con, $data ) = @_; | ||
| 50 | |||
| 51 | #$self->_log( v => 4, msg => "manager:".Data::Dumper->Dump([ $data ])); | ||
| 52 | my ( $cmd, @args ) = split( /\s+/, $data ); | ||
| 53 | |||
| 54 | if ( $self->can( "cmd_$cmd" ) ) { | ||
| 55 | $con->call( "cmd_$cmd" => \@args ); | ||
| 56 | $con->send( "command finished." ); | ||
| 57 | } else { | ||
| 58 | $con->send( "unknown command. Need 'help'?" ); | ||
| 59 | } | ||
| 60 | |||
| 61 | return 1; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub cmd_help { | ||
| 65 | my ( $self, $server, $con, $args ) = @_; | ||
| 66 | |||
| 67 | $con->send( "commands: dump [val], list_conn, con_dump [cid], find_leaks, find_refs, quit" ); | ||
| 68 | if ( $args->[0] ) { | ||
| 69 | $con->send( "no detailed info for $args->[0]" ); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | sub cmd_dump { | ||
| 74 | my ( $self, $server, $con, $args ) = @_; | ||
| 75 | |||
| 76 | $con->send( eval "Data::Dumper->Dump([$args->[0]])" ); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub cmd_x { | ||
| 80 | my ( $self, $server, $con, $args ) = @_; | ||
| 81 | |||
| 82 | if ( $args->[0] =~ m/^0x(\S+)/i ) { | ||
| 83 | my $c = $server->get_connection( $1 ); | ||
| 84 | my $res = eval "$args->[1]"; | ||
| 85 | $con->send( $res ); | ||
| 86 | $con->send( $@ ) if ( $@ ); | ||
| 87 | } else { | ||
| 88 | my $res = eval "$args->[0]"; | ||
| 89 | $con->send( $res ); | ||
| 90 | $con->send( $@ ) if ( $@ ); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | sub cmd_list_conn { | ||
| 95 | my ( $self, $server, $con, $args ) = @_; | ||
| 96 | |||
| 97 | foreach my $p ( @{ $sprocket->get_components } ) { | ||
| 98 | next unless ( $p ); | ||
| 99 | foreach my $c ( values %{$p->{heaps}} ) { | ||
| 100 | $con->send( $p->name." - $c - ".$c->peer_addr ); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | sub cmd_con_dump { | ||
| 106 | my ( $self, $server, $con, $args ) = @_; | ||
| 107 | |||
| 108 | return $con->send( "con_dump <id>" ) | ||
| 109 | unless ( @$args ); | ||
| 110 | |||
| 111 | $con->send('looking for '.$args->[0]); | ||
| 112 | LOOP: foreach my $p ( @{ $sprocket->get_components } ) { | ||
| 113 | next unless ( $p ); | ||
| 114 | foreach my $c ( values %{$p->{heaps}} ) { | ||
| 115 | next unless ( lc( $c->ID ) eq $args->[0] ); | ||
| 116 | $con->send( $p->name." - $c - ".Data::Dumper->Dump([ $c ]) ); | ||
| 117 | last LOOP; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | sub cmd_find_leaks { | ||
| 123 | my ( $self, $server, $con, $args ) = @_; | ||
| 124 | |||
| 125 | return $con->send( "Devel::Gladiator not installed: http://code.sixapart.com/svn/Devel-Gladiator/trunk/" ) | ||
| 126 | unless ( HAS_GLADIATOR ); | ||
| 127 | |||
| 128 | my $array = Devel::Gladiator::walk_arena(); | ||
| 129 | for my $i ( 0 .. $#{$array} ) { | ||
| 130 | next unless ( ref($array->[$i]) =~ m/^Sprocket\:\:Connection/ ); | ||
| 131 | my $found = undef; | ||
| 132 | foreach my $c ( @{ $sprocket->get_components } ) { | ||
| 133 | next unless ( $c ); | ||
| 134 | $found = $c | ||
| 135 | if ( exists( $c->{heaps}->{$array->[$i]->ID} ) ); | ||
| 136 | } | ||
| 137 | if ($found) { | ||
| 138 | #$con->send( "cometd connection: ".$array->[$i]->ID." with plugin ".$array->[$i]->plugin()." found in ".$found->name ); | ||
| 139 | } else { | ||
| 140 | $con->send( "cometd connection: ".$array->[$i]->ID." with plugin ".$array->[$i]->plugin()." not found --- leaked!" ); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | sub cmd_find_refs { | ||
| 146 | my ( $self, $server, $con, $args ) = @_; | ||
| 147 | |||
| 148 | return $con->send( "Devel::Gladiator not installed http://code.sixapart.com/svn/Devel-Gladiator/trunk/" ) | ||
| 149 | unless ( HAS_GLADIATOR ); | ||
| 150 | |||
| 151 | my $array = Devel::Gladiator::walk_arena(); | ||
| 152 | for my $i ( 0 .. $#{$array} ) { | ||
| 153 | if ( ref($array->[$i]) =~ m/^Sprocket/ && ref($array->[$i]) !~ m/^Sprocket::Session/ ) { | ||
| 154 | $con->send( "obj: $array->[$i] ".( $array->[$i]->can( "name" ) ? $array->[$i]->name : '' )); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | sub cmd_quit { | ||
| 160 | my ( $self, $server, $con, $args ) = @_; | ||
| 161 | |||
| 162 | $con->send( "goodbye." ); | ||
| 163 | $con->close(); | ||
| 164 | } | ||
| 165 | |||
| 166 | 1; |