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