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.10 by root, Mon Nov 6 19:40:02 2006 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;
13use Coro::Event; 6use Coro::Event;
7use Coro::Socket;
14 8
15#$Event::DebugLevel = 4; 9# this gets started everytime a user enters a finger command
10sub finger {
11 my $user = shift;
12 my $host = shift;
16 13
17my $stdin = Coro::Event->io(fd => \*STDIN, poll => 'r'); 14 my $fh = new Coro::Socket PeerHost => $host, PeerPort => "finger"
15 or die "$user\@$host: $!";
18 16
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"; 17 print $fh "$user\n";
28 my $r = Coro::Event->io(fd => $io, poll => 'r'); 18
29 my $buf; 19 print "$user\@$host: $_" while <$fh>;
30 $r->next while 0 != sysread $io, $buf, 8192, length $buf; 20 print "$user\@$host: done\n";
31 #do_timer(after => 5);
32 print $buf;
33} 21}
34 22
35sub kbdline { 23my $stdin = new_from_fh Coro::Handle \*STDIN;
36 $stdin->next;
37 my $x = <STDIN>; chomp $x; $x;
38}
39 24
25# this is the main task
40sub keyboard : Coro { 26sub keyboard : Coro {
41 $|=1; 27 $|=1;
42 while() { 28 while() {
43 print "cmd> "; my $cmd = kbdline; 29 print "cmd> "; my $cmd = <$stdin>; chomp $cmd;
44 if ($cmd eq "finger") { 30 if ($cmd eq "finger") {
45 print "user> "; my $user = kbdline; 31 print "user> "; my $user = <$stdin>; chomp $user;
46 print "host> "; my $host = kbdline; 32 print "host> "; my $host = <$stdin>; chomp $host;
47 async { finger(@_) } $user, $host; 33 async { finger($user, $host) };
48 } elsif ($cmd eq "quit") { 34 } elsif ($cmd eq "quit") {
49 unloop(777); 35 unloop(777);
50 terminate; 36 terminate;
51 } else { 37 } else {
52 print "enter command, either 'finger' or 'quit'\n"; 38 print "unknown command '$cmd', either 'finger' or 'quit'\n";
53 } 39 }
54 } 40 }
55} 41}
56 42
43async { finger("root", "localhost") };
44
45# display the time or garble the display, YMMV.
57sub timer : Coro { 46sub timer : Coro {
58 my $w = Coro::Event->timer(interval => 0.001, hard => 1); 47 my $w = Coro::Event->timer(interval => 0.001, hard => 1);
59 use Time::HiRes qw(time); 48 use Time::HiRes qw(time);
60 while () { 49 while () {
61 $w->next; 50 $w->next;
62 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";
63 }; 52 };
64} 53}
65 54
66print "unlooped with value: ",loop(),"\n"; 55print "unlooped with value: ",loop(),"\n";
67 56

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines