/* 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_LOCAL_STRATEGY_HPP
#define NMM_LOCAL_STRATEGY_HPP NMM_LOCAL_STRATEGY_HPP

#include "nmm/nmm_prefix.hpp"

#include "TransportStrategy.hpp"

#include "nmm/comm/messaging/ITSRequestReplyEventReceiver.hpp"
#include "nmm/comm/messaging/ITSRequestReplyEventSender.hpp"
#include "nmm/comm/messaging/ITSOneWayEventReceiver.hpp"
#include "nmm/comm/messaging/ITSOneWayEventSender.hpp"
#include "nmm/comm/messaging/ITSOneWayBufferReceiver.hpp"
#include "nmm/comm/messaging/ITSOneWayBufferSender.hpp"
#include "nmm/comm/messaging/ITSOneWayCEventReceiver.hpp"
#include "nmm/comm/messaging/ITSOneWayCEventSender.hpp"

#include "visibility.hpp"

namespace NMM {
  
  /** Local transport strategy.
   *
   * This %TransportStrategy can transport request-reply and one-way
   * Events, one-way CEvents and one-way Buffers by pointer-forwarding
   * to local objects.
   */
  class DLLEXPORT LocalStrategy
    : public TransportStrategy,
      public ITSRequestReplyEventReceiver,
      public ITSOneWayCEventReceiver,
      public ITSOneWayBufferReceiver,
      public ITSRequestReplyEventSender,
      public ITSOneWayCEventSender,
      public ITSOneWayBufferSender
  {
  public:
    /** @name Constructor & Destructor
     */
    //@{
    /** Default constructor.
     */
    LocalStrategy();

    /** Destructor.
     */
    virtual ~LocalStrategy();

    /** This method is called if the TS is no longer needed. 
     *
     * This method must not be blocked.
     */
    virtual void scheduleDestruction();
    //@}

    /** @name Receiver Interface
     */
    //@{
    /** This method sends an incoming one-way %CEvent over a local connection.
     *
     * @param cevent Cevent to send
     * @return event result
     */
    virtual Result receiveOneWayCEvent(CEvent* cevent);
    
    /** This method sends an incoming one-way %Buffer over a local connection.
     *
     * @param buffer Buffer to send
     * @return event result
     */
    virtual Result receiveOneWayBuffer(Buffer* buffer);
    
    /** This method sends an incoming request-reply %Event over a local connection.
     *
     * @param event Event to send
     * @return evemt result
     */
    virtual Result receiveRequestReplyEvent(Event& event);
    //@}

    /** @name Connection & Configure
     */
    //@{
    /** This mehtod connects the TransportStrategy to the given TSAddress.
     *
     * This mehtod connects the TransportStrategy to the given TSAddress and throws an exception if an error occurs.
     *
     * @param addr TSAddress to set
     */
    virtual void setAddress(const TSAddress& addr)
      throw (const BindingException&);

    /** This method sets the partner strategy which is used for the bidirectrional communication.
     *
     * @param partner_strategy partner strategy which is used for the bidirectrional communication
     */
    void setPartnerStrategy(LocalStrategy* partner_strategy);

    /** Forwards signal to partner strategy.
     *
     * @param signal Signal to forward
     */
    virtual void receiveSignal(Signal signal);

    /** This method returns the typename of this class.
     *
     * @return typename of this class
     */
    virtual string getStrategyType()          const { return getStaticStrategyType(); }
    
    /** Sends reliable event.
     *
     * @param event Event to send
     */
    virtual Result sendReliableEvent(Event& event) const;
    
    /** Sends reliable config event.
     *
     * @param event config event to send
     */
    virtual Result sendReliableConfigEvent(Event& event) const;
    
    /** Configures the %TransportStrategy for the given instream format.
     *
     * @param format The instream format
     * @exception BindingException if configuration fails or the format is not supported
     */
    virtual void configure(const Format& format) throw(const FormatNotSupportedException&);
    
    /** This method checks if the strategy is connected.
     *
     * @return true if the strategy is connected, false otherwise
     */
    virtual bool isConnected() const;
    //@}
    
    /** @name Transmission methods
     */
    //@{
    /** Forwards a %CEvent to the other object.
     *
     * @param message CEvent to forward
     */
    Result forwardOneWayCEvent(CEvent* message);

    /** Forwards a %Buffer to the other object.
     *
     * @param message Buffer to forward
     */
    Result forwardOneWayBuffer(Buffer* message);
    
    /** Forwards a request-reply %Event to the other object.
     *
     * @param event Event to forward
     */
    Result forwardRequestReplyEvent(Event& event);

    /** Forwards a reliable %Event to the other object.
     *
     * @param event Event to forward
     */
    Result forwardReliableEvent(Event& event);

    /** Forwards a reliable config %Event to the other object.
     *
     * @param event Event to forward
     */
    Result forwardReliableConfigEvent(Event& event);

    /** Forwards a signal to the parent strategy.
     *
     * Note that signals are not normally transported over the network
     * in NMM. However network strategies might have flow-control
     * which causes the strategy to return @c BLOCKED or send
     * @c FREE_QUEUE_SLOT signals. The LocalStrategy realizes
     * flow-control by forwarding NMM signals and by forwarding the
     * return values of the receiveOneWayBuffer and
     * receiveOneWayCEvent methods.
     *
     * @param signal Signal to forward
     */
    void forwardSignal(Signal signal);
    //@}

    /** @name Static type information.
     */
    //@{
    /** This method returns the type name of strategy.
     *
     * @return type name of strategy
     */
    static inline string getStaticStrategyType() { return __strategy_type; }

    /** Empty method.
     */
    virtual void flushITSReceiver() {}
    //@}
    
  private:
    mutable LocalStrategy* __partner_strategy;    ///< pointer to the other LocalStrategy

  private:
    static const char __strategy_type[];          ///< Static type name of this strategy
  };
  
}

#include "nmm/nmm_suffix.hpp"

#endif
