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.6 by root, Tue Jul 17 02:21:56 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines