ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/eg/chat_client
Revision: 1.2
Committed: Tue Aug 4 21:06:47 2009 UTC (14 years, 10 months ago) by root
Branch: MAIN
CVS Tags: rel-0_1, rel-0_4
Changes since 1.1: +17 -18 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #!/opt/bin/perl
2
3 use AnyEvent;
4 use AnyEvent::Handle;
5 use AnyEvent::MP;
6
7 my $server_node = "127.0.0.1:1299";
8
9 my $client_port = port;
10
11 snd $server_node, lookup => "chatter", $client_port, "resolved";
12
13 my $resolved_cv = AnyEvent->condvar;
14 my $server_port;
15
16 # setup a receiver callback for the 'resolved' message:
17 rcv $client_port, resolved => sub {
18 my ($tag, $chatter_port_id) = @_;
19
20 print "Resolved the server port 'chatter' to $chatter_port_id\n";
21 $server_port = $chatter_port_id;
22
23 $resolved_cv->send;
24 1
25 };
26
27 # lets block the client until we have resolved the server port.
28 $resolved_cv->recv;
29
30 # now setup another receiver callback for the chat messages:
31 rcv $client_port, message => sub {
32 my ($tag, $msg) = @_;
33
34 print "chat> $msg\n";
35 0
36 };
37
38 # send a 'join' message to the server:
39 snd $server_port, join => "$client_port";
40
41 sub send_message {
42 my ($msg) = @_;
43
44 snd $server_port, message => $msg;
45 }
46
47 # make an AnyEvent condition variable for the 'quit' condition
48 # (when we want to exit the client).
49 my $quit_cv = AnyEvent->condvar;
50
51 my $stdin_hdl = AnyEvent::Handle->new (
52 fh => *STDIN,
53 on_error => sub { $quit_cv->send },
54 on_read => sub {
55 my ($hdl) = @_;
56
57 $hdl->push_read (line => sub {
58 my ($hdl, $line) = @_;
59
60 if ($line =~ /^\/quit/) { # /quit will end the client
61 $quit_cv->send;
62 } else {
63 send_message ($line);
64 }
65 });
66 }
67 );
68
69 $quit_cv->recv;