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