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