/**
 * 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.activeblaze.group;

import org.apache.activeblaze.*;

import java.util.List;
import java.util.Set;

/**
 * <p/>
 * A <CODE>BlazeGroupChannel</CODE> enables peer-based point to point communication
 */
public interface BlazeGroupChannel extends BlazeChannel {
    /**
     * @return the name of this channel
     */
    public String getName();

    /**
     * @param newName
     */
    public void setName(String newName);

    /**
     * Send a message to an individual member
     */
    public void send(Member member, BlazeMessage message) throws Exception;

    /**
     * Send a message to an individual member and wait for a response
     *
     * @return the response
     */
    public BlazeMessage sendRequest(Member member, BlazeMessage message) throws Exception;

    /**
     * Send a message to an individual member and wait for a response
     *
     * @param timeout time in milliseconds to wait for a response
     * @return a response of null if timed out
     */
    public BlazeMessage sendRequest(Member member, BlazeMessage message, int timeout) throws Exception;

    /**
     * Send a message to the group - one Member will be picked to receive the message in a round-robin fashion
     */
    public void send(String destination, BlazeMessage message) throws Exception;

    /**
     * Send a message to the group - one Member will be picked to receive the message in a round-robin fashion
     */
    public void send(Destination destination, BlazeMessage message) throws Exception;

    /**
     * Send a message to the group - one Member will be picked to receive the message in a round-robin fashion and wait
     * for a response
     *
     * @return a response
     */
    public BlazeMessage sendRequest(String destination, BlazeMessage message) throws Exception;

    /**
     * Send a message to the group - one Member will be picked to receive the message in a round-robin fashion and wait
     * for a response
     *
     * @return a response
     */
    public BlazeMessage sendRequest(Destination destination, BlazeMessage message) throws Exception;

    /**
     * Send a message to the group - one Member will be picked to receive the message in a round-robin fashion and wait
     * for a response
     *
     * @param timeout -
     *                time in milliseconds to wait for a response
     * @return a response of null if timed out
     */
    public BlazeMessage sendRequest(String destination, BlazeMessage message, int timeout) throws Exception;


    /**
     * Send a message to the group - one Member will be picked to receive the message in a round-robin fashion and wait
     * for a response
     *
     * @param timeout -
     *                time in milliseconds to wait for a response
     * @return a response of null if timed out
     */
    public BlazeMessage sendRequest(Destination destination, BlazeMessage message, int timeout) throws Exception;

    /**
     * Send a response message to an original message - for request/response
     *
     * @param to            the Member to send a response to
     * @param response      the message to send in a response
     * @param correlationId the associated id from the original message
     */
    public void sendReply(Member to, BlazeMessage response, String correlationId) throws Exception;

    /**
     * @return the inboxListener
     */
    public BlazeMessageListener getInboxListener();

    /**
     * @param inboxListener the inboxListener to set
     */
    public void setInboxListener(BlazeMessageListener inboxListener);

    /**
     * @return the configuration
     */
    public BlazeGroupConfiguration getConfiguration();

    /**
     * @return a set of the members
     */
    public Set<Member> getMembers() throws Exception;

    /**
     * Get a member by its unique id
     *
     * @return the Member
     */
    public Member getMemberById(String id) throws Exception;

    /**
     * Return a member of the Group with the matching name
     *
     * @return the Member
     */
    public Member getMemberByName(String name) throws Exception;

    /**
     * Will wait for a member to advertise itself if not available
     *
     * @return the member or null
     */
    public Member getAndWaitForMemberByName(String name, int timeout) throws InterruptedException, Exception;

    /**
     * @return the local member that represents this <CODE>Group</CODE> instance
     */
    public Member getLocalMember();

    /**
     * Add a listener for membership changes
     */
    public void addMemberChangedListener(MemberChangedListener l) throws Exception;

    /**
     * Remove a listener for membership changes
     */
    public void removeMemberChangedListener(MemberChangedListener l) throws Exception;

    /**
     * Add a listener for messages
     */
    public void addBlazeQueueMessageListener(String destination, BlazeMessageListener l) throws Exception;

    /**
     * Add a listener for messages
     */
    public void addBlazeQueueMessageListener(Subscription subscription, BlazeMessageListener l) throws Exception;

    /**
     * Remove a listener for messages
     */
    public void removeBlazeQueueMessageListener(String destination, BlazeMessageListener l) throws Exception;

    /**
     * Remove a listener for messages
     */
    public void removeBlazeQueueMessageListener(Subscription subscription, BlazeMessageListener l) throws Exception;

    /**
     * Add member to a group
     */
    public void addToGroup(String groupName) throws Exception;

    /**
     * remove member from a group
     */
    public void removeFromGroup(String groupName) throws Exception;

    /**
     * Get an array of groups
     *
     * @return an array of groups
     */
    public List<String> getGroups() throws Exception;


}
