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