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

Comparing Coro/eg/event (file contents):
Revision 1.6 by root, Tue Jul 17 02:21:56 2001 UTC vs.
Revision 1.9 by root, Sun Apr 14 01:18:38 2002 UTC

1#!/usr/bin/perl 1#!/usr/bin/perl
2 2
3# useless stuff 3# this crap is an asynchroneous finger client. it's rather idiotic ;)
4 4
5use Coro; 5use Coro;
6use Coro::Event; 6use Coro::Event;
7 7use Coro::Socket;
8my $stdin = Coro::Event->io(fd => \*STDIN, poll => 'r');
9 8
10# this gets started everytime a user enters a finger command 9# this gets started everytime a user enters a finger command
11sub finger { 10sub finger {
12 my $user = shift; 11 my $user = shift;
13 my $host = shift; 12 my $host = shift;
14 use IO::Socket::INET; 13
15 # is there ANY way to do non-blocking connects with IO::Socket::INET? 14 my $fh = new Coro::Socket PeerHost => $host, PeerPort => "finger"
16 # I don't see how... 15 or die "$user\@$host: $!";
17 my $io = new IO::Socket::INET PeerAddr => "$host:finger"; 16
18 $io or die;
19 syswrite $io, "$user\n"; 17 print $fh "$user\n";
20 my $r = Coro::Event->io(fd => $io, poll => 'r'); 18
21 my $buf; 19 print "$user\@$host: $_" while <$fh>;
22 $r->next while 0 != sysread $io, $buf, 8192, length $buf; 20 print "$user\@$host: done\n";
23 #do_timer(after => 5);
24 print $buf;
25} 21}
26 22
27# this reads one line from the keyboard 23my $stdin = new_from_fh Coro::Handle \*STDIN;
28sub kbdline {
29 $stdin->next;
30 my $x = <STDIN>; chomp $x; $x;
31}
32 24
33# this is the main task 25# this is the main task
34sub keyboard : Coro { 26sub keyboard : Coro {
35 $|=1; 27 $|=1;
36 while() { 28 while() {
37 print "cmd> "; my $cmd = kbdline; 29 print "cmd> "; my $cmd = <$stdin>; chomp $cmd;
38 if ($cmd eq "finger") { 30 if ($cmd eq "finger") {
39 print "user> "; my $user = kbdline; 31 print "user> "; my $user = <$stdin>; chomp $user;
40 print "host> "; my $host = kbdline; 32 print "host> "; my $host = <$stdin>; chomp $host;
41 async { finger(@_) } $user, $host; 33 async { finger($user, $host) };
42 } elsif ($cmd eq "quit") { 34 } elsif ($cmd eq "quit") {
43 unloop(777); 35 unloop(777);
44 terminate; 36 terminate;
45 } else { 37 } else {
46 print "enter command, either 'finger' or 'quit'\n"; 38 print "unknown command '$cmd', either 'finger' or 'quit'\n";
47 } 39 }
48 } 40 }
49} 41}
42
43async { finger("root", "localhost") };
50 44
51# display the time or garble the display, YMMV. 45# display the time or garble the display, YMMV.
52sub timer : Coro { 46sub timer : Coro {
53 my $w = Coro::Event->timer(interval => 0.001, hard => 1); 47 my $w = Coro::Event->timer(interval => 0.001, hard => 1);
54 use Time::HiRes qw(time); 48 use Time::HiRes qw(time);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines