/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.tribes;
import java.io.Serializable;
/**
* Channel interface
* A channel is a representation of a group of nodes all participating in some sort of
* communication with each other.
* The channel is the main API class for Tribes, this is essentially the only class
* that an application needs to be aware of. Through the channel the application can:
* 1. send messages
* 2. receive message (by registering a ChannelListener
* 3. get all members of the group getMembers()
* 4. receive notifications of members added and members disappeared by
* registering a MembershipListener
*
* The channel has 5 major components:
* 1. Data receiver, with a built in thread pool to receive messages from other peers
* 2. Data sender, an implementation for sending data using NIO or java.io
* 3. Membership listener,listens for membership broadcasts
* 4. Membership broadcaster, broadcasts membership pings.
* 5. Channel interceptors, the ability to manipulate messages as they are sent or arrive
* The channel layout is:
*
* ChannelListener_1..ChannelListener_N MembershipListener_1..MembershipListener_N [Application Layer]
* \ \ / /
* \ \ / /
* \ \ / /
* \ \ / /
* \ \ / /
* \ \ / /
* ---------------------------------------
* |
* |
* Channel
* |
* ChannelInterceptor_1
* | [Channel stack]
* ChannelInterceptor_N
* |
* Coordinator (implements MessageListener,MembershipListener,ChannelInterceptor)
* --------------------
* / | \
* / | \
* / | \
* / | \
* / | \
* MembershipService ChannelSender ChannelReceiver [IO layer]
*
*
* For example usage @see org.apache.catalina.tribes.group.GroupChannel
*/
public interface Channel {
/**
* Start and stop sequences can be controlled by these constants
* This allows you to start separate components of the channel int SEND_OPTIONS_DEFAULT = SEND_OPTIONS_USE_ACK;ByteMessage to
* send a pure byte array
* @param options int - sender options, see class documentation for each interceptor that is configured in order to trigger interceptors
* @return a unique Id that identifies the message that is sent
* @throws ChannelException if a serialization error happens.
* @see ByteMessage
* @see #SEND_OPTIONS_USE_ACK
* @see #SEND_OPTIONS_ASYNCHRONOUS
* @see #SEND_OPTIONS_SYNCHRONIZED_ACK
*/
public UniqueId send(Member[] destination, Serializable msg, int options) throws ChannelException;
/**
* Send a message to one or more members in the cluster
* @param destination Member[] - the destinations, null or zero length means all
* @param msg ClusterMessage - the message to send
* @param options int - sender options, see class documentation
* @param handler ErrorHandler - handle errors through a callback, rather than throw it
* @return a unique Id that identifies the message that is sent
* @exception ChannelException - if a serialization error happens.
*/
public UniqueId send(Member[] destination, Serializable msg, int options, ErrorHandler handler) throws ChannelException;
/**
* Sends a heart beat through the interceptor stacks
* Use this method to alert interceptors and other components to
* clean up garbage, timed out messages etc.heartbeat() method will be invoked when the heartbeat runs on the channel
* @param listener MembershipListener
* @see MembershipListener
*/
public void addMembershipListener(MembershipListener listener);
/**
* Add a channel listener, this is a callback object when messages are received
* heartbeat() method will be invoked when the heartbeat runs on the channel
* @param listener ChannelListener
* @see ChannelListener
* @see Heartbeat
*/
public void addChannelListener(ChannelListener listener);
/**
* remove a membership listener, listeners are removed based on Object.hashCode and Object.equals
* @param listener MembershipListener
* @see MembershipListener
*/
public void removeMembershipListener(MembershipListener listener);
/**
* remove a channel listener, listeners are removed based on Object.hashCode and Object.equals
* @param listener ChannelListener
* @see ChannelListener
*/
public void removeChannelListener(ChannelListener listener);
/**
* Returns true if there are any members in the group,
* this call is the same as getMembers().length > 0
* @return boolean - true if there are any members automatically discovered
*/
public boolean hasMembers() ;
/**
* Get all current group members
* @return all members or empty array, never null
*/
public Member[] getMembers() ;
/**
* Return the member that represents this node. This is also the data
* that gets broadcasted through the membership broadcaster component
* @param incAlive - optimization, true if you want it to calculate alive time
* since the membership service started.
* @return Member
*/
public Member getLocalMember(boolean incAlive);
/**
* Returns the member from the membership service with complete and
* recent data. Some implementations might serialize and send
* membership information along with a message, and instead of sending
* complete membership details, only send the primary identifier for the member
* but not the payload or other information. When such message is received
* the application can retrieve the cached member through this call.