next up previous contents
Next: Receiving a message Up: Channel Previous: Getting the current view   Contents

Sending a message

Once the channel is connected, messages can be sent using the send() methods:

    public void send(Message msg) throws ChannelNotConnected, ChannelClosed;
    public void send(Address dst, Address src, Object obj) throws ChannelNotConnected, ChannelClosed;

The first send() method has only one argument, which is the message to be sent. The message's destination should either be the address of the receiver (unicast) or null (multicast). When it is null, the message will be sent to all members of the group (including itself). The source address may be null; if it is, it will be set to the channel's address (so that recipients may generate a response and send it back to the sender).

The second send() method is a helper method and uses the former method internally. It requires the address of receiver and sender and an object (which has to be serializable), constructs a Message and sends it.

If the channel is not connected, or was closed, an exception will be thrown upon attempting to send a message.

Here's an example of sending a (multicast) message to all members of a group:

    Hashtable data;  // any serializable data
    try {
        channel.send(null, null, data);
    }
    catch(Exception ex) {
        // handle errors
    }

The null value as destination address means that the message will be sent to all members in th group. The sender's address will be filled in by the bottom-most protocol. The payload is a hashtable, which will be serialized into the message's buffer and unserialized at the receiver's end. Alternatively, any other means of generating a byte buffer and setting the message's buffer to it (e.g. using Message.setBuffer()) would also work.

Here's an example of sending a (unicast) message to the first member (coordinator) of a group:

    Address   receiver;
    Message   msg;
    Hashtable data;
    try {
        receiver=channel.getView().getMembers().first();
        channel.send(receiver, null, data);
    }
    catch(Exception ex) {
        // handle errors
    }

It creates a Message with a specific address for the receiver (the first member of the group). Again, the sender's address can be left null as it will be filled in by the bottom-most protocol.


next up previous contents
Next: Receiving a message Up: Channel Previous: Getting the current view   Contents
Bela Ban 2002-11-16