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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines