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.5 by root, Tue Jul 17 00:24:15 2001 UTC

1#!/usr/bin/perl 1#!/usr/bin/perl
2
3<<EOF;
4finger
5localhost
6root
7finger
8localhost
9root
10EOF
2 11
3use Coro; 12use Coro;
4use Coro::Event; 13use Coro::Event;
5 14
15#$Event::DebugLevel = 4;
16
6my $stdin = Coro::Event->io(fd => \*STDIN, poll => 'r'); 17my $stdin = Coro::Event->io(fd => \*STDIN, poll => 'r');
18
19sub finger {
20 my $host = shift;
21 my $user = shift;
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";
28 my $r = Coro::Event->io(fd => $io, poll => 'r');
29 my $buf;
30 $r->next while 0 != sysread $io, $buf, 8192, length $buf;
31 #do_timer(after => 5);
32 print $buf;
33}
7 34
8sub kbdline { 35sub kbdline {
9 $stdin->next; 36 $stdin->next;
10 my $x = <STDIN>; chomp $x; $x; 37 my $x = <STDIN>; chomp $x; $x;
11} 38}
12 39
13sub keyboard : Coro { 40sub keyboard : Coro {
14 $|=1; 41 $|=1;
15 while() { 42 while() {
16 print "cmd> "; my $cmd = kbdline; 43 print "cmd> "; my $cmd = kbdline;
44 if ($cmd eq "finger") {
45 print "user> "; my $user = kbdline;
17 print "data> "; my $data = kbdline; 46 print "host> "; my $host = kbdline;
18 print "cmd<$cmd> data<$data>\n"; 47 async { finger(@_) } $user, $host;
19 print "working..."; 48 } elsif ($cmd eq "quit") {
20 do_timer(after => 1); 49 unloop(777);
21 print "done\n"; 50 terminate;
51 } else {
52 print "enter command, either 'finger' or 'quit'\n";
53 }
22 } 54 }
23} 55}
24 56
25sub idle : Coro { 57sub timer : Coro {
26 my $w = Coro::Event->idle(min => 1, max => 2); 58 my $w = Coro::Event->timer(interval => 0.001, hard => 1);
59 use Time::HiRes qw(time);
27 while () { 60 while () {
28 $w->next; 61 $w->next;
29 print "."; 62 print "\e7\e[A\10\10\10 <time ",time,"> \e8";
30 }; 63 };
31} 64}
32 65
33Coro::Event->main; 66print "unlooped with value: ",loop(),"\n";
34 67

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines