- First Steps with AnyEvent
- Emulating POSIX Signals
- List Processes AKA ps
- Bad AnyEvent Install
Now that we have our userspace kernel, it is time to write the utilities to make it more pleasant to use. On Linux, I can simulate all of the commands using telnet, but my Windows box doesn’t have telnet installed. I will therefore need a list_processes command, a signal command and a library to help with registering new processes.
First up is list_processes which is modelled on the http_get function presented in AnyEvent::Intro. It simply needs to send:
list_processes\015\012
and then call the passed in function with each line of response. The result might be something like the following.
$ ./list_processes.pl 1: ./register.pl a b c 2: ./register.pl
list_processes.pl
use 5.010; use strict; use warnings; use constant DEBUG => $ENV{KERNEL_DEBUG}; my $cr = "\015\012"; use AnyEvent::Handle; sub list_processes { my ($host, $port, $cb) = @_; my $cv = AE::cv(); my $handle; $handle = AnyEvent::Handle->new( connect => [$host => $port], on_error => sub { say("Connection error: $!"); $handle->destroy(); }, on_eof => sub { DEBUG && say 'Connection closed'; $handle->destroy(); $cv->send(); } ); $handle->push_write('list_processes' . ${cr}); $handle->on_read(sub { my $handle = shift; my $data = $handle->rbuf(); $handle->rbuf() = ''; $data =~ tr/\r//d; foreach my $line (split /\n/, $data) { $cb->($line); } }); return $cv; } my $cv = list_processes('localhost', 12345, sub { say @_ } ); $cv->recv();