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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines