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