JavaGroups
Class Channel

java.lang.Object
  |
  +--JavaGroups.Channel
Direct Known Subclasses:
EnsChannel, JChannel

public abstract class Channel
extends java.lang.Object
implements Transport

A channel represents a group communication endpoint (like BSD datagram sockets). A client joins a group by connecting the channel to a group address and leaves it by disconnecting. Messages sent over the channel are received by all group members that are connected to the same group (that is, all member that have the same group address).

The FSM for a channel is roughly as follows: a channel is created (unconnected). The channel is connected to a group (connected). Messages can now be sent and received. The channel is disconnected from the group (unconnected). The channel could now be connected to a different group again. The channel is closed (closed).

Only a single sender is allowed to be connected to a channel at a time, but there can be more than one channel in an application.

Messages can be sent to the group members using the Send method and messages can be received using Receive (pull approach).

A channel instance is created using either a ChannelFactory or the public constructor. Each implementation of a channel must provide a subclass of Channel and an implementation of ChannelFactory.

Various degrees of sophistication in message exchange can be achieved using building blocks on top of channels, e.g. light-weight groups, synchronous message invocation, or remote method calls. Channels are on the same abstraction level as sockets, and should really be simple to use. Higher-level abstractions are all built on top of channels.

Author:
Bela Ban
See Also:
java.net.DatagramPacket, java.net.MulticastSocket

Field Summary
static int BLOCK
           
static int GET_STATE_EVENTS
           
static int LOCAL
           
static int SUSPECT
           
static int VIEW
           
 
Method Summary
abstract  void BlockOk()
          Called to acknowledge a Block (callback in MembershipListener or BlockEvent received from call to Receive).
abstract  void Close()
          Destroys the channel and its associated resources (e.g.
abstract  void Connect(java.lang.Object group_address)
          Connects the channel to a group.
abstract  void Disconnect()
          Disconnects the channel from the current group (if connected), leaving the group.
 void Down(Event evt)
          Access to event mechanism of channels.
abstract  boolean GetAllStates(java.util.Vector targets, long timeout)
          Retrieve all states of the group members.
abstract  java.lang.Object GetGroupAddress()
          Returns the group address of the group of which the channel is a member.
abstract  java.lang.Object GetLocalAddress()
          Returns the channel's own address.
abstract  java.lang.Object GetOpt(int option)
          Gets an option.
abstract  boolean GetState(java.lang.Object target, long timeout)
          Retrieve the state of the group.
abstract  View GetView()
          Gets the current view.
abstract  java.lang.Object Peek(long timeout)
          Returns the next message, view, block, suspect or other event without removing it from the queue.
abstract  java.lang.Object Receive(long timeout)
          Receives a message, a view change or a block event.
 void ReturnState(java.lang.Object state)
          Called by the application is response to receiving a GetState object when calling Receive.
abstract  void Send(Message msg)
          Sends a message to a destination.
 void SetChannelListener(ChannelListener channel_listener)
          Allows to be notified when a channel event such as connect, disconnect or close occurs.
abstract  void SetOpt(int option, java.lang.Object value)
          Sets an option.
 void SetUpHandler(UpHandler up_handler)
          When up_handler is set, all events will be passed to it directly.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLOCK

public static final int BLOCK

VIEW

public static final int VIEW

SUSPECT

public static final int SUSPECT

LOCAL

public static final int LOCAL

GET_STATE_EVENTS

public static final int GET_STATE_EVENTS
Method Detail

Connect

public abstract void Connect(java.lang.Object group_address)
                      throws ChannelClosed
Connects the channel to a group. The client is now able to receive group messages, views and block events (depending on the options set) and to send messages to (all or single) group members. This is a null operation if already connected.

All channels with the same group address form a group, that means all messages sent to the group will be received by all channels connected to the same group address.

The group address is opaque, and can only be interpreted by a channel implementation, i.e. the implementation decides what to do with the object.

Throws:
ChannelClosed - The channel is closed and therefore cannot be used any longer. A new channel has to be created first.
See Also:
Channel.Disconnect

Disconnect

public abstract void Disconnect()
Disconnects the channel from the current group (if connected), leaving the group. It is a null operation if not connected. It is a null operation if the channel is closed.
See Also:
Channel.Connect

Close

public abstract void Close()
Destroys the channel and its associated resources (e.g. the protocol stack). After a channel has been closed, invoking methods on it throws the ChannelClosed exception (or results in a null operation). It is a null operation if the channel is already closed.

If the channel is connected to a group, Disconnect will be called first.


Send

public abstract void Send(Message msg)
                   throws ChannelNotConnected,
                          ChannelClosed
Sends a message to a destination. The message contains
  1. a destination address (Object). A null address sends the message to all group members.
  2. a source address. Can be left empty. Will be filled in by the protocol stack.
  3. a byte buffer. The message contents.
  4. several additional fields. They can be used by application programs (or patterns). E.g. a message ID, a oneway field which determines whether a response is expected etc.
Specified by:
Send in interface Transport
Parameters:
msg - The message to be sent. Destination and buffer should be set. A null destination means to send to all group members.
Throws:
ChannelNotConnected - The channel must be connected to send messages.
ChannelClosed - The channel is closed and therefore cannot be used any longer. A new channel has to be created first.

Down

public void Down(Event evt)
Access to event mechanism of channels. Enables to send and receive events, used by building blocks to communicate with (building block) specific protocol layers. Currently useful only with JChannel.

Receive

public abstract java.lang.Object Receive(long timeout)
                                  throws ChannelNotConnected,
                                         ChannelClosed,
                                         Timeout
Receives a message, a view change or a block event. By using SetOpt, the type of objects to be received can be determined (e.g. not views and blocks, just messages). The possible types returned can be:
  1. Message. Normal message
  2. Event. All other events (used by JChannel)
  3. View. A view change.
  4. BlockEvent. A block event indicating an impending view change.
  5. SuspectEvent. A notification of a suspected member.
  6. GetStateEvent. The current state of the application should be returned using ReturnState.
  7. SetStateEvent. The state of a single/all members as requested previously by having called Channel.GetState(s).
The instanceof operator can be used to discriminate between different types returned.
Specified by:
Receive in interface Transport
Parameters:
timeout - Value in milliseconds. Value <= 0 means wait forever
Returns:
A Message, View, BlockEvent,SuspectEvent, GetStateEvent object or SetStateEvent, depending on what is on top of the internal queue.
Throws:
ChannelNotConnected - The channel must be connected to receive messages.
ChannelClosed - The channel is closed and therefore cannot be used any longer. A new channel has to be created first.
Timeout - Thrown when a timeout has occurred.

Peek

public abstract java.lang.Object Peek(long timeout)
                               throws ChannelNotConnected,
                                      ChannelClosed,
                                      Timeout
Returns the next message, view, block, suspect or other event without removing it from the queue.
Parameters:
timeout - Value in milliseconds. Value <= 0 means wait forever
Returns:
A Message, View, BlockEvent, SuspectEvent, GetStateEvent or SetStateEvent object, depending on what is on top of the internal queue.
Throws:
ChannelNotConnected - The channel must be connected to receive messages.
ChannelClosed - The channel is closed and therefore cannot be used any longer. A new channel has to be created first.
Timeout - Thrown when a timeout has occurred.
See Also:
Receive

GetView

public abstract View GetView()
Gets the current view. This does not retrieve a new view, use Receive to do so. The view may only be available after a successful Connect. The result of calling this method on an unconnected channel is implementation defined (may return null). Calling it on a channel that is not enabled to receive view events (via SetOpt) returns null. Calling this method on a closed channel returns a null view.
Returns:
The current view.

GetLocalAddress

public abstract java.lang.Object GetLocalAddress()
Returns the channel's own address. The result of calling this method on an unconnected channel is implementation defined (may return null). Calling this method on a closed channel returns null.
Returns:
The channel's address. Generated by the underlying transport, and opaque. Addresses can be used as destination in the Send operation.

GetGroupAddress

public abstract java.lang.Object GetGroupAddress()
Returns the group address of the group of which the channel is a member. This is the object that was the argument to Connect. Calling this method on a closed channel returns null.
Returns:
The group address

SetUpHandler

public void SetUpHandler(UpHandler up_handler)
When up_handler is set, all events will be passed to it directly. These will not be received by the channel (except connect/disconnect, state retrieval and the like). This can be used by building blocks on top of a channel; thus the channel is used as a pass-through medium, and the building blocks take over some of the channel's tasks. However, tasks such as connection management and state transfer is still handled by the channel.

SetChannelListener

public void SetChannelListener(ChannelListener channel_listener)
Allows to be notified when a channel event such as connect, disconnect or close occurs. E.g. a PullPushAdapter may choose to stop when the channel is closed, or to start when it is opened.

SetOpt

public abstract void SetOpt(int option,
                            java.lang.Object value)
Sets an option. The following options are currently recognized:
  1. BLOCK (0). Turn the reception of BLOCK events on/off (value is Boolean). Default is off. If set to on, receiving VIEW events will be set to on, too.
  2. VIEW (1). Turn the reception of VIEW events on/off (value is Boolean). Default is on.
  3. SUSPECT (2). Turn the reception of SUSPECT events on/off (value is Boolean). Default is on.
  4. LOCAL (3). Receive its own broadcast messages to the group (value is Boolean). Default is on.
  5. GET_STATE_EVENTS (4). Turn the reception of GetState events on/off (value is Boolean). Default is off, which means that no other members can ask this member for its state (null will be returned).
This method can be called on an unconnected channel. Calling this method on a closed channel has no effect.

GetOpt

public abstract java.lang.Object GetOpt(int option)
Gets an option. This method can be called on an unconnected channel. Calling this method on a closed channel returns null.
Parameters:
option - The option to be returned.
Returns:
The object associated with an option.

BlockOk

public abstract void BlockOk()
Called to acknowledge a Block (callback in MembershipListener or BlockEvent received from call to Receive). After sending BlockOk, no messages should be sent until a new view has been received. Calling this method on a closed channel has no effect.

GetState

public abstract boolean GetState(java.lang.Object target,
                                 long timeout)
Retrieve the state of the group. Will usually contact the oldest group member to get the state. When the method returns true, a SetStateEvent will have been added to the channel's queue, causing Receive to return the state in one of the next invocations. If false, no state will be retrieved by Receive.
Parameters:
target - The address of the member from which the state is to be retrieved. If it is null, the oldest member is contacted.
timeout - Milliseconds to wait for the response (0 = wait indefinitely).
Returns:
boolean True if the state was retrieved successfully, otherwise false.

GetAllStates

public abstract boolean GetAllStates(java.util.Vector targets,
                                     long timeout)
Retrieve all states of the group members. Will contact all group members to get the states. When the method returns true, a SetStateEvent will have been added to the channel's queue, causing Receive to return the states in one of the next invocations. If false, no states will be retrieved by Receive.
Parameters:
targets - A list of members which are contacted for states. If the list is null, all the current members of the group will be contacted.
timeout - Milliseconds to wait for the response (0 = wait indefinitely).
Returns:
boolean True if the state was retrieved successfully, otherwise false.

ReturnState

public void ReturnState(java.lang.Object state)
Called by the application is response to receiving a GetState object when calling Receive.
Parameters:
state - The state of the application as a Serializable or Externaizable (to send over the network).