ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MP/eg/chat_client
Revision: 1.8
Committed: Wed Aug 19 05:57:14 2009 UTC (14 years, 9 months ago) by root
Branch: MAIN
CVS Tags: rel-0_8
Changes since 1.7: +8 -2 lines
Log Message:
0.8

File Contents

# User Rev Content
1 root 1.2 #!/opt/bin/perl
2    
3 root 1.8 # Usage: ./chat_client nickname optional-servername
4     # implement a chat client using "traditional message passing"
5    
6 root 1.4 use common::sense;
7 elmex 1.1 use AnyEvent::MP;
8 root 1.6 use AnyEvent::MP::Global;
9 elmex 1.1
10 root 1.4 my ($nick, $servernode) = @ARGV;
11 elmex 1.1
12 root 1.4 initialise_node "slave/", $servernode;
13 elmex 1.3
14 root 1.4 $| = 1;
15 elmex 1.3
16 root 1.4 my $port = port;
17 elmex 1.1
18 root 1.4 my ($client, $server);
19 elmex 1.1
20 root 1.4 sub server_connect {
21 root 1.7 my $servernodes = AnyEvent::MP::Global::find "eg_chat_server"
22     or return after 1, \&server_connect;
23 elmex 1.1
24 root 1.7 print "\rconnecting...\n";
25    
26 root 1.8 $server = $servernodes->[0];
27    
28 root 1.7 $client = port { print "\r \r@_\n> " };
29     mon $client, sub {
30     print "\rdisconnected @_\n";
31     &server_connect;
32     };
33    
34 root 1.8 snd $server, join => $client, $nick;
35 root 1.7 mon $server, $client;
36 root 1.4 }
37 elmex 1.1
38 root 1.4 server_connect;
39 elmex 1.1
40 root 1.4 my $w = AE::io 0, 0, sub {
41     chomp (my $line = <STDIN>);
42     print "> ";
43 root 1.8 snd $server, privmsg => $nick, $line
44     if $server;
45 root 1.2 };
46 elmex 1.1
47 root 1.4 print "> ";
48     AE::cv->recv;
49 elmex 1.1