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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines