ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/eg/event
(Generate patch)

Comparing Coro/eg/event (file contents):
Revision 1.6 by root, Tue Jul 17 02:21:56 2001 UTC vs.
Revision 1.12 by root, Sat Apr 14 15:12:30 2007 UTC

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
5use Coro; 5use Coro;
6use Coro::Socket;
6use Coro::Event; 7use Coro::Event;
8use AnyEvent;
7 9
8my $stdin = Coro::Event->io(fd => \*STDIN, poll => 'r'); 10my $quit = AnyEvent->condvar;
9 11
10# this gets started everytime a user enters a finger command 12# this gets started everytime a user enters a finger command
11sub finger { 13sub finger {
12 my $user = shift; 14 my $user = shift;
13 my $host = shift; 15 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 16
27# this reads one line from the keyboard 17 my $fh = new Coro::Socket PeerHost => $host, PeerPort => "finger"
28sub kbdline { 18 or die "$user\@$host: $!";
29 $stdin->next;
30 my $x = <STDIN>; chomp $x; $x;
31}
32 19
33# this is the main task 20 print $fh "$user\n";
34sub keyboard : Coro { 21
35 $|=1; 22 print "$user\@$host: $_" while <$fh>;
36 while() { 23 print "$user\@$host: done\n";
37 print "cmd> "; my $cmd = kbdline;
38 if ($cmd eq "finger") {
39 print "user> "; my $user = kbdline;
40 print "host> "; my $host = kbdline;
41 async { finger(@_) } $user, $host;
42 } elsif ($cmd eq "quit") {
43 unloop(777);
44 terminate;
45 } else {
46 print "enter command, either 'finger' or 'quit'\n";
47 }
48 }
49} 24}
50 25
51# display the time or garble the display, YMMV. 26# display the time or garble the display, YMMV.
52sub timer : Coro { 27sub timer : Coro {
53 my $w = Coro::Event->timer(interval => 0.001, hard => 1); 28 my $w = Coro::Event->timer (interval => 0.001, hard => 1);
54 use Time::HiRes qw(time); 29 use Time::HiRes qw(time);
55 while () { 30 while () {
56 $w->next; 31 $w->next;
57 print "\e7\e[C\e[C\e[C\e[C\e[C\e[C\e[C\e[C <time ",time,"> \e8"; 32 print "\e7\e[C\e[C\e[C\e[C\e[C\e[C\e[C\e[C <time ", time, "> \e8";
58 }; 33 };
59} 34}
60 35
61print "unlooped with value: ",loop(),"\n"; 36my $stdin = new_from_fh Coro::Handle \*STDIN;
62 37
38$SIG{PIPE} = 'IGNORE';
39
40$| = 1;
41while() {
42 print "cmd (finger|quit)> "; my $cmd = <$stdin>; chomp $cmd;
43 if ($cmd eq "finger") {
44 print "user> "; my $user = <$stdin>; chomp $user;
45 print "host> "; my $host = <$stdin>; chomp $host;
46 async { finger $user, $host };
47 } elsif ($cmd eq "quit") {
48 $quit->send;
49 } else {
50 print "unknown command '$cmd', either 'finger' or 'quit'\n";
51 }
52}
53

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines