1 | #!/usr/bin/perl |
1 | #!/usr/bin/perl |
|
|
2 | |
|
|
3 | # this crap is an asynchroneous finger client. it's rather idiotic ;) |
2 | |
4 | |
3 | use Coro; |
5 | use Coro; |
4 | use Coro::Event; |
6 | use Coro::Event; |
|
|
7 | use Coro::Socket; |
5 | |
8 | |
6 | $Event::DebugLevel = 4; |
9 | # this gets started everytime a user enters a finger command |
|
|
10 | sub finger { |
|
|
11 | my $user = shift; |
|
|
12 | my $host = shift; |
7 | |
13 | |
8 | my $stdin = Coro::Event->io(fd => \*STDIN, poll => 'r'); |
14 | my $fh = new Coro::Socket PeerHost => $host, PeerPort => "finger" |
|
|
15 | or die "$user\@$host: $!"; |
9 | |
16 | |
10 | sub finger { |
|
|
11 | my $host = shift; |
|
|
12 | my $user = shift; |
|
|
13 | use IO::Socket::INET; |
|
|
14 | # is there ANY way to do non-blocking connects with IO::Socket::INET? |
|
|
15 | # I don't see how... |
|
|
16 | my $io = new IO::Socket::INET PeerAddr => "$host:finger"; |
|
|
17 | syswrite $io, "$user\n"; |
17 | print $fh "$user\n"; |
18 | #do_timer(after => 5); |
18 | |
19 | my $r = Coro::Event->io(fd => $io, poll => 'r'); |
19 | print "$user\@$host: $_" while <$fh>; |
20 | my $buf; |
20 | print "$user\@$host: done\n"; |
21 | $r->next while 0 != sysread $io, $buf, 8192, length $buf; |
|
|
22 | #do_timer(after => 5); |
|
|
23 | print $buf; |
|
|
24 | } |
21 | } |
25 | |
22 | |
26 | sub kbdline { |
23 | my $stdin = new_from_fh Coro::Handle \*STDIN; |
27 | $stdin->next; |
|
|
28 | my $x = <STDIN>; chomp $x; $x; |
|
|
29 | } |
|
|
30 | |
24 | |
|
|
25 | # this is the main task |
31 | sub keyboard : Coro { |
26 | sub keyboard : Coro { |
32 | $|=1; |
27 | $|=1; |
33 | while() { |
28 | while() { |
34 | print "cmd> "; my $cmd = kbdline; |
29 | print "cmd> "; my $cmd = <$stdin>; chomp $cmd; |
35 | if ($cmd eq "finger") { |
30 | if ($cmd eq "finger") { |
36 | print "user> "; my $user = kbdline; |
31 | print "user> "; my $user = <$stdin>; chomp $user; |
37 | print "host> "; my $host = kbdline; |
32 | print "host> "; my $host = <$stdin>; chomp $host; |
38 | async sub { finger($host, $user) }; |
33 | async { finger($user, $host) }; |
39 | } elsif ($cmd eq "quit") { |
34 | } elsif ($cmd eq "quit") { |
40 | unloop(777); |
35 | unloop(777); |
41 | terminate; |
36 | terminate; |
42 | } else { |
37 | } else { |
43 | print "enter command, either 'finger' or 'quit'\n"; |
38 | print "unknown command '$cmd', either 'finger' or 'quit'\n"; |
44 | } |
39 | } |
45 | } |
40 | } |
46 | } |
41 | } |
47 | |
42 | |
|
|
43 | async { finger("root", "localhost") }; |
|
|
44 | |
|
|
45 | # display the time or garble the display, YMMV. |
48 | sub timer : Coro { |
46 | sub timer : Coro { |
49 | my $w = Coro::Event->timer(interval => 0.1, hard => 1); |
47 | my $w = Coro::Event->timer(interval => 0.001, hard => 1); |
50 | use Time::HiRes qw(time); |
48 | use Time::HiRes qw(time); |
51 | while () { |
49 | while () { |
52 | $w->next; |
50 | $w->next; |
53 | print "\e7\e[A\10\10\10 <time ",time,"> \e8"; |
51 | print "\e7\e[C\e[C\e[C\e[C\e[C\e[C\e[C\e[C <time ",time,"> \e8"; |
54 | }; |
52 | }; |
55 | } |
53 | } |
56 | |
54 | |
57 | print "unlooped with value: ",loop(),"\n"; |
55 | print "unlooped with value: ",loop(),"\n"; |
58 | |
56 | |