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

Comparing Coro/eg/event (file contents):
Revision 1.11 by root, Sat Apr 14 15:06:06 2007 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# this crap is an asynchronous finger client. it's rather idiotic ;) 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;
7use Coro::Socket; 8use AnyEvent;
9
10my $quit = AnyEvent->condvar;
8 11
9# this gets started everytime a user enters a finger command 12# this gets started everytime a user enters a finger command
10sub finger { 13sub finger {
11 my $user = shift; 14 my $user = shift;
12 my $host = shift; 15 my $host = shift;
18 21
19 print "$user\@$host: $_" while <$fh>; 22 print "$user\@$host: $_" while <$fh>;
20 print "$user\@$host: done\n"; 23 print "$user\@$host: done\n";
21} 24}
22 25
26# display the time or garble the display, YMMV.
27sub timer : Coro {
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 };
34}
35
23my $stdin = new_from_fh Coro::Handle \*STDIN; 36my $stdin = new_from_fh Coro::Handle \*STDIN;
24 37
25# this is the main task 38$SIG{PIPE} = 'IGNORE';
26sub keyboard : Coro { 39
27 $|=1; 40$| = 1;
28 while() { 41while() {
29 print "cmd> "; my $cmd = <$stdin>; chomp $cmd; 42 print "cmd (finger|quit)> "; my $cmd = <$stdin>; chomp $cmd;
30 if ($cmd eq "finger") { 43 if ($cmd eq "finger") {
31 print "user> "; my $user = <$stdin>; chomp $user; 44 print "user> "; my $user = <$stdin>; chomp $user;
32 print "host> "; my $host = <$stdin>; chomp $host; 45 print "host> "; my $host = <$stdin>; chomp $host;
33 async { finger($user, $host) }; 46 async { finger $user, $host };
34 } elsif ($cmd eq "quit") { 47 } elsif ($cmd eq "quit") {
35 unloop(777); 48 $quit->send;
36 terminate;
37 } else { 49 } else {
38 print "unknown command '$cmd', either 'finger' or 'quit'\n"; 50 print "unknown command '$cmd', either 'finger' or 'quit'\n";
39 }
40 } 51 }
41} 52}
42 53
43async { finger("root", "localhost") };
44
45# display the time or garble the display, YMMV.
46sub timer : Coro {
47 my $w = Coro::Event->timer(interval => 0.001, hard => 1);
48 use Time::HiRes qw(time);
49 while () {
50 $w->next;
51 print "\e7\e[C\e[C\e[C\e[C\e[C\e[C\e[C\e[C <time ",time,"> \e8";
52 };
53}
54
55cede; # make sure the watchers are installed
56print "unlooped with value: ",loop(),"\n";
57

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines