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

Comparing Coro/eg/event (file contents):
Revision 1.3 by root, Sun Jul 15 15:58:16 2001 UTC vs.
Revision 1.8 by root, Fri Jul 27 02:51:33 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines