/* 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_TRANSPORT_STRATEGY_HPP
#define NMM_TRANSPORT_STRATEGY_HPP NMM_TRANSPORT_STRATEGY_HPP

#include "nmm/nmm_prefix.hpp"

#include "ITransportStrategyImpl.hpp"
#include "nmm/comm/messaging/IEventSender.hpp"

#include "TSDescription.hpp"

#include "visibility.hpp"

namespace NMM {
  
  class CommunicationChannel;
  class IValueStream;
  class OValueStream;
  
  /** A %TransportStrategy realizes a transport mechanism for one or more types of messages.
   * We distinguish between top-level strategies and leaf strategies. A top-level strategy can be used
   * to completely realize a binding. A leaf strategy can be used by a %CompositeStrategy to realize part of
   * a binding. The possible uses of a %TransportStrategy are defined by the interfaces that the %TransportStrategy
   * implements. These interfaces are
   * - ITSEventReceiver, IEventSender
   * - ITSMessageReceiver, IMessageSender
   * for top-level strategies and
   * - ITSOneWayEventReceiver, ITSOneWayEventSender
   * - ITSOneWayBufferReceiver, ITSOneWayBufferSender
   * - ITSOneWayCEventReceiver, ITSOneWayCEventSender
   * - ITSRequestReplyEventReceiver, ITSRequestReplyEventSender
   * for leaf strategies.
   */
  class DLLEXPORT TransportStrategy :
    virtual public ITransportStrategyImpl,
    public IEventSender
  {
  public:
    /** @name Constructor & Destructor
     */
    //@{
    /** Constructs a %TransportStrategy with default attributes.
     */
    TransportStrategy(const std::string& name = "TransportStrategy");
    
    /** Virtual destructor.
     */
    virtual ~TransportStrategy();
    
    /** Schedules destruction of this object.
     * This method is called when this %TransportStrategy is no longer needed. 
     *
     * This method must be non-blocking.
     */
    virtual void scheduleDestruction() = 0;
    //@}
    
    /** @name TransportStrategy Information
     */
    //@{
    /** Returns description of this transport strategy.
     *
     * @return TSDescription of this TransportStrategy
     */
    inline const TSDescription& getTSDescription() const throw() {
      return __description;
    }
    
    /** Sets description of this TransportStrategy.
     *
     * This method may only be called by the %TSFactory.
     *
     * @param description of this TransportStrategy to set.
     */
    inline void setTSDescription(const TSDescription& description) throw() {
      __description = description;
    }
    
    /** Returns the typename of this %TransportStrategy.
     *
     * @return typename of this %TransportStrategy
     */
    virtual std::string getStrategyType() const = 0;
    
    /** This method checks if the strategy is connected.
     *
     * @return true if the strategy is connected, false otherwise
     */
    virtual nmm_bool isConnected() const = 0;
    
    /** Returns an event prototype for the event with the given key.
     *
     * @param event_key event key
     * @return event prototype
     */
    virtual Event* getEventPrototype(const std::string& event_key) const;
    
    /** Sets the binding channel of this %TransportStrategy.
     *
     * @param com_channel binding channel to set 
     */
    void setBindingChannel(CommunicationChannel* com_channel) throw();
    
    /** Returns the binding channel of this %TransportStrategy.
     *
     * @return binding channel of this %TransportStrategy
     */
    CommunicationChannel* getBindingChannel() const throw();
    //@}
    
    /** @name Connection & Configuration
     */
    //@{
    /** Sets address and connect to peer.
     *
     * This method connects this %TransportStrategy with its peer. The given peer @c address must be of a type
     * which is supported by the type of this %TransportStrategy. 
     *
     * @param address Address of peer to connect to
     */
    virtual void setAddress(const TSAddress& address)
      throw (const BindingException&) = 0;
    
    /** Returns the partner strategy.
     *
     * This method returns an interface to the partner %TransportStrategy if this transport strategy is connected.
     *
     * @return An interface to the partner %TransportStrategy
     */
    virtual ITransportStrategy* getPartnerStrategy() const;
    
    /** Returns the partner strategy.
     *
     * This method returns an interface to the partner %TransportStrategy if this transport strategy is connected.
     *
     * @return An interface to the partner %TransportStrategy
     */
    virtual ITransportStrategy* getThisStrategy() const;
    
    /** Sends 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 result of event
     */
    virtual Result sendReliableEvent(Event& event) const = 0;
    
    /** 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 result of event
     */
    virtual Result sendReliableConfigEvent(Event& event) const = 0;
    //@}
    
  private:
    CommunicationChannel*           __com_channel;             ///< Pointer to the CommunicationChannel that manages the Strategy
    TSDescription                   __description;             ///< Description of this transport strategy.
  };
  
  
  inline void TransportStrategy::setBindingChannel(CommunicationChannel* com_channel) throw() {
    __com_channel = com_channel;
  }
  
  inline CommunicationChannel* TransportStrategy::getBindingChannel() const throw() {
    return __com_channel;
  }
  
}

#include "nmm/nmm_suffix.hpp"

#endif
