/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 * 
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM is free software; you can redistribute it and/or modify it    
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; only version 2 of the License.      
 * 
 * NMM is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 
 * License for more details.  
 * 
 * You should have received a copy of the GNU General Public License 
 * along with NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#ifndef NMM_COMPOSITE_STRATEGY_HPP
#define NMM_COMPOSITE_STRATEGY_HPP NMM_COMPOSITE_STRATEGY_HPP

#include "nmm/nmm_prefix.hpp"

#include "TransportStrategy.hpp"
#include "ICompositeStrategyImpl.hpp"

#include "visibility.hpp"

namespace NMM {
  
  class IConnectionManager;
  
  /** A %TransportStrategy which is composed of other TransportStrategies.
   * This %TransportStrategy combines multiple leaf strategies using the composite design pattern.
   * @see CompositeAddress
   */
  class DLLEXPORT CompositeStrategy
    : public TransportStrategy,
      virtual public ICompositeStrategyImpl
  {
  public:
    /** @name Constructor & Destructor
     */
    //@{
    /** Construct a %CompositeStrategy with default attributes.
     *
     * @param name name of this CompositeStrategy object
     */
    CompositeStrategy(const string& name = "CompositeStrategy");
    
    /** Virtual destructor.
     */
    virtual ~CompositeStrategy();
    
    /** Schedules destruction of this object.
     *
     * This method is called when this %TransportStrategy is no longer need. This method must be non-blocking.
     */
    virtual void scheduleDestruction();
    //@}
    
    /** @name %TransportStrategy Information
     */
    //@{
    /** Returns the typename of this %TransportStrategy.
     * 
     * @return typname of this TranspoertStrategy
     */
    virtual string getStrategyType() const;
    
    /** This method checks if the strategy is connected.
     *
     * A composite strategy is connected if the binding is complete and all
     * child strategies are connected.
     *
     * @see CompositeStrategy::isBindingComplete
     * @return true if the TransportStrategy is connected
     */
    virtual bool isConnected() const;
      
    /** This method checks if the binding is complete.
     *
     * A binding is complete if to each communication role a transport
     * strategy is assigned. The transport strategy need not be connected.
     * Each subclass must implement this method to define when a binding is
     * complete.
     *
     * @see CompositeStrategy::isConnected
     * @return true if the binding is complete, otherwise false
     */
    virtual bool isBindingComplete() const throw() = 0;
    //@}
    
    /** @name Connection & Configuration
     */
    //@{
    /** Send a reliable event to the partner %NMMObject.
     *
     * This method reliably sends an event to the partner %NMMObject. It is used for sending events
     * that configure a binding. The event is sent through the %TransportStrategy itself (if supported)
     * or through a reliable middleware-internal channel (%IConnectionManager interface)
     *
     * @param event The event that should be sent
     * @return SUCCESS if the Event could be sent, otherwise FAILURE
     */
    virtual Result sendReliableEvent(Event& event) const;
    
    /** Send a reliable event to the partner %TransportStrategy.
     * 
     * This method reliably sends an event to the partner %TransportStrategy. It is used for sending events
     * that configure a binding. The event is sent through the %TransportStrategy itself (if supported)
     * or through a reliable middleware-internal channel (%IConnectionManager interface)
     * 
     * @param event The event that should be sent
     * @return SUCCESS if the Event could be sent, otherwise FAILURE
     */
    virtual Result sendReliableConfigEvent(Event& event) const;
    
    /** Returns number of child strategies.
     *
     * @return The number of child strategies in the composite
     */
    virtual nmm_uint32 getChildStrategyCount() const;
    
    /** Returns child strategy.
     *
     * @param index Index of child strategy
     * @return Interface to the child strategy with the given index
     */
    virtual ITransportStrategy* getChildStrategy(const nmm_uint32& index) const;
    
    /** Returns child strategy.
     *
     * @param index Index of child strategy
     * @return Child strategy with the given index
     */
    TransportStrategy* getOwnChildStrategy(unsigned int i) const;
    
    /** Sets the connection manager interface and the NMMReference of
     * the partner strategy.
     *
     * This method should not be called directly by normal application code
     *
     * @param partner_icm Interface to connection manager local to partner strategy
     * @param partner_reference NMMReference of partner strategy
     */
    void setConnectionManager(IConnectionManager* partner_icm, const NMMReference& partner_reference);
    //@}
    
    /** @name Child Strategies
     */
    //@{
    /** This method adds the given strategy to this composite strategy
     * and returns its index.
     *
     * This method is called by subclasses and should not be called by
     * normal application code.  It is public for testing purposes
     * only.
     *
     * @param strategy Transport strategy to add
     * @return Index of transport strategy
     */
    unsigned int addChildStrategy(TransportStrategy* strategy);
    
    /** This methods removes the given child strategy from the
     * composite strategy.  The indices of other child strategies may
     * change. The child strategy is scheduled for destruction.
     *
     * This method is called by subclasses and should not be called by
     * normal application code.  It is public for testing purposes
     * only.
     *
     * @param index Index of transport strategy to remove
     */
    void removeChildStrategy(unsigned int index);
    
    /** Returns the number of child strategies in this transport strategy.
     *
     * @return The number of child strategies in this transport strategy
     */
    size_t getNumberOfChildStrategies() const throw() {
      return __child_strategies.size();
    }
    
    /** This method returns a child strategy of this transport strategy.
     *
     * @param index Index of child strategy
     * @return Pointer to the child strategy or 0 if index is out of range
     */
    TransportStrategy* getChildStrategy(unsigned int index) const throw() {
      return (index < __child_strategies.size()) ? __child_strategies[index] : 0;
    }
    
    /** This method removes all child strategies.
     */
    void removeAllChildStrategies();
    //@}
      
  private:
    vector<TransportStrategy*> __child_strategies;  ///< Child strategies
    ThreadMutex __child_strategy_mutex;             ///< Mutex to protect child strategies
    
    IConnectionManager* __partner_icm;              ///< Partner connection manager interface.
    NMMReference        __partner_reference;        ///< Partner NMM reference.    
  };
  
}

#include "nmm/nmm_suffix.hpp"

#endif
