| 1 | package Service::Engine::Admin::Server; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | use Service::Engine; | ||
| 8 | use Service::Engine::Admin; | ||
| 9 | use base qw(Net::Server::Multiplex); | ||
| 10 | use Data::Dumper; | ||
| 11 | |||
| 12 | # IO::Multiplex callback hook | ||
| 13 | sub mux_connection { | ||
| 14 | |||
| 15 | my $self = shift; | ||
| 16 | my $mux = shift; | ||
| 17 | my $fh = shift; | ||
| 18 | my $peer = $self->{peeraddr}; | ||
| 19 | |||
| 20 | # pull in some Service::Engine globals | ||
| 21 | $self->{'Config'} = $Service::Engine::Config; | ||
| 22 | $self->{'Log'} = $Service::Engine::Log; | ||
| 23 | $self->{'Admin'} = $Service::Engine::Admin; | ||
| 24 | $self->{'EngineName'} = $Service::Engine::EngineName; | ||
| 25 | |||
| 26 | my $password = $self->{'Config'}->get_config('admin')->{'password'}; | ||
| 27 | my $timeout = $self->{'Config'}->get_config('admin')->{'timeout'}; | ||
| 28 | my $command_list = $self->{'Admin'}->command_list(); | ||
| 29 | |||
| 30 | # Net::Server stores a connection counter in the {requests} field. | ||
| 31 | $self->{id} = $self->{net_server}->{server}->{requests}; | ||
| 32 | # Keep some values that I might need while the {server} | ||
| 33 | # property hash still contains the current client info | ||
| 34 | # and stash them in my own object hash. | ||
| 35 | $self->{peerport} = $self->{net_server}->{server}->{peerport}; | ||
| 36 | |||
| 37 | $self->{'Log'}->log({msg=>"Client [$peer] (id $self->{id}) just connected...",level=>3}); | ||
| 38 | |||
| 39 | # Notify everyone that the client arrived | ||
| 40 | # $self->broadcast($mux,"JOIN: (#$self->{id}) from $peer\r\n"); | ||
| 41 | |||
| 42 | # Try out the timeout feature of IO::Multiplex | ||
| 43 | $mux->set_timeout($fh, undef); | ||
| 44 | $mux->set_timeout($fh, $timeout); | ||
| 45 | |||
| 46 | # $fh->write(`clear`); | ||
| 47 | |||
| 48 | if ($password) { | ||
| 49 | $fh->write("password: "); | ||
| 50 | $self->{'password_prompted'} = 1; | ||
| 51 | } else { | ||
| 52 | $fh->write("Welcome to \033[1m" . $self->{'EngineName'} . "\033[0m!\n"); | ||
| 53 | $fh->write("Enter '?' at any prompt for help.\n"); | ||
| 54 | $fh->write("Select an option: \n$command_list\n> "); | ||
| 55 | } | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | sub mux_input { | ||
| 60 | |||
| 61 | my $self = shift; | ||
| 62 | my $mux = shift; | ||
| 63 | my $fh = shift; | ||
| 64 | my $in_ref = shift; # Scalar reference to the input | ||
| 65 | my $peer = $self->{peeraddr}; | ||
| 66 | my $id = $self->{id}; | ||
| 67 | |||
| 68 | my $command_list = $self->{'Admin'}->command_list(); | ||
| 69 | |||
| 70 | my $password = $self->{'Config'}->get_config('admin')->{'password'}; | ||
| 71 | |||
| 72 | # Process each line in the input | ||
| 73 | while ($$in_ref =~ s/^(.*?)\r?\n//) { | ||
| 74 | |||
| 75 | if (!$1) { | ||
| 76 | $fh->write("> "); | ||
| 77 | next; | ||
| 78 | } | ||
| 79 | |||
| 80 | if ($self->{'password_prompted'}) { | ||
| 81 | |||
| 82 | if ($password eq $1) { | ||
| 83 | # $fh->write(`clear`); | ||
| 84 | $fh->write("Welcome to \033[1m" . $self->{'EngineName'} . "\033[0m!\n"); | ||
| 85 | $fh->write("Select an option: \n$command_list\n> "); | ||
| 86 | $self->{'password_prompted'} = 0; | ||
| 87 | } else { | ||
| 88 | $mux->close($fh); | ||
| 89 | } | ||
| 90 | |||
| 91 | } else { | ||
| 92 | |||
| 93 | $self->{'Log'}->log({msg=>"Processing $1",level=>3}); | ||
| 94 | $self->_process_input($1,$mux,$fh); | ||
| 95 | |||
| 96 | } | ||
| 97 | |||
| 98 | } | ||
| 99 | |||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | sub _process_input { | ||
| 104 | |||
| 105 | my $self = shift; | ||
| 106 | my $input = shift; | ||
| 107 | my $mux = shift; | ||
| 108 | my $fh = shift; | ||
| 109 | |||
| 110 | # $fh->write(`clear`); | ||
| 111 | |||
| 112 | my $command_list = $self->{'Admin'}->command_list(); | ||
| 113 | |||
| 114 | if ($input eq '?') { | ||
| 115 | |||
| 116 | my $help = qq[ | ||
| 117 | Commands | ||
| 118 | quit,close - close the program | ||
| 119 | |||
| 120 | Options | ||
| 121 | You can type the first letter of any option to select it. | ||
| 122 | Available options are: | ||
| 123 | $command_list | ||
| 124 | > ]; | ||
| 125 | $fh->write($help); | ||
| 126 | } elsif ($input eq 'close' || $input eq 'quit' || $input eq '!q') { | ||
| 127 | $mux->close($fh); | ||
| 128 | } else { | ||
| 129 | my $module = $self->{'Admin'}->$input(); | ||
| 130 | if (ref($module) eq 'HASH') { # this is a command | ||
| 131 | my $package = $module->{'module'}; | ||
| 132 | my $method = $module->{'method'}; | ||
| 133 | $package->$method($fh); | ||
| 134 | $fh->write("\n> "); | ||
| 135 | } else { | ||
| 136 | if ($module) { # this is a method | ||
| 137 | $module->run($fh); | ||
| 138 | $fh->write("\n> "); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 | } | ||
| 145 | |||
| 146 | # This callback will happen when the mux->set_timeout expires. | ||
| 147 | sub mux_timeout { | ||
| 148 | my $self = shift; | ||
| 149 | my $mux = shift; | ||
| 150 | my $fh = shift; | ||
| 151 | $fh->write("If you don't want to talk then you should leave. *BYE*\r\n"); | ||
| 152 | close(STDOUT); | ||
| 153 | $mux->set_timeout($fh, undef); | ||
| 154 | $mux->set_timeout($fh, 40); | ||
| 155 | } | ||
| 156 | |||
| 157 | # Routine to send a message to all clients in a mux. | ||
| 158 | sub broadcast { | ||
| 159 | my $self = shift; | ||
| 160 | my $mux = shift; | ||
| 161 | my $msg = shift; | ||
| 162 | foreach my $fh ($mux->handles) { | ||
| 163 | # NOTE: All the client unique objects can be found at | ||
| 164 | # $mux->{_fhs}->{$fh}->{object} | ||
| 165 | # In this example, the {id} would be | ||
| 166 | # $mux->{_fhs}->{$fh}->{object}->{id} | ||
| 167 | print $fh $msg; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | 1; |