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