| 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.9 |
my $nick = shift; |
| 11 |
elmex |
1.1 |
|
| 12 |
root |
1.9 |
initialise_node "anon/"; |
| 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 |
|