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

Comparing Coro/eg/event (file contents):
Revision 1.2 by root, Sun Jul 15 03:24:18 2001 UTC vs.
Revision 1.11 by root, Sat Apr 14 15:06:06 2007 UTC

1#!/usr/bin/perl 1#!/usr/bin/perl
2
3# this crap is an asynchronous finger client. it's rather idiotic ;)
2 4
3use Coro; 5use Coro;
4use Coro::Event; 6use Coro::Event;
7use Coro::Socket;
5 8
6my $stdin = Coro::Event->io(fd => \*STDIN, poll => 'r'); 9# this gets started everytime a user enters a finger command
10sub finger {
11 my $user = shift;
12 my $host = shift;
7 13
8sub kbdline { 14 my $fh = new Coro::Socket PeerHost => $host, PeerPort => "finger"
9 $stdin->next; 15 or die "$user\@$host: $!";
10 my $x = <STDIN>; chomp $x; $x; 16
17 print $fh "$user\n";
18
19 print "$user\@$host: $_" while <$fh>;
20 print "$user\@$host: done\n";
11} 21}
12 22
23my $stdin = new_from_fh Coro::Handle \*STDIN;
24
25# this is the main task
13sub keyboard : Coro { 26sub keyboard : Coro {
14 $|=1; 27 $|=1;
15 while() { 28 while() {
16 print "cmd> "; my $cmd = kbdline; 29 print "cmd> "; my $cmd = <$stdin>; chomp $cmd;
17 print "data> "; my $data = kbdline; 30 if ($cmd eq "finger") {
18 print "cmd<$cmd> data<$data>\n"; 31 print "user> "; my $user = <$stdin>; chomp $user;
19 print "working..."; 32 print "host> "; my $host = <$stdin>; chomp $host;
20 do_timer(after => 1); 33 async { finger($user, $host) };
21 print "done\n"; 34 } elsif ($cmd eq "quit") {
35 unloop(777);
36 terminate;
37 } else {
38 print "unknown command '$cmd', either 'finger' or 'quit'\n";
39 }
22 } 40 }
23} 41}
24 42
43async { finger("root", "localhost") };
44
45# display the time or garble the display, YMMV.
25sub idle : Coro { 46sub timer : Coro {
26 my $w = Coro::Event->idle(min => 1, max => 2); 47 my $w = Coro::Event->timer(interval => 0.001, hard => 1);
48 use Time::HiRes qw(time);
27 while () { 49 while () {
28 $w->next; 50 $w->next;
29 print "."; 51 print "\e7\e[C\e[C\e[C\e[C\e[C\e[C\e[C\e[C <time ",time,"> \e8";
30 }; 52 };
31} 53}
32 54
33Coro::Event->main; 55cede; # make sure the watchers are installed
56print "unlooped with value: ",loop(),"\n";
34 57

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines