/* 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_INSTREAM_DEMULTIPLEXER_HPP
#define NMM_INSTREAM_DEMULTIPLEXER_HPP NMM_INSTREAM_DEMULTIPLEXER_HPP

#include "nmm/nmm_prefix.hpp"

#include "nmm/comm/messaging/ICSSignalReceiver.hpp"
#include "nmm/Result.hpp"

#include "nmm/utils/thread/ThreadMutex.hpp"

namespace NMM {
  
  class InstreamCompositeStrategy;
  class Message;
  
  /** Instream message demultiplexer. 
   *
   * This class is an adapter between
   * an %InstreamCompositeStrategy and its child strategies.
   * Subclasses implement strategies for demultiplexing instream
   * messages.
   */
  class InstreamDemultiplexer
    : public ICSSignalReceiver
  {
  public:
    /** Default constructor.
     */
    InstreamDemultiplexer() throw();
    
    /** Virtual destructor.
     */
    virtual ~InstreamDemultiplexer() throw() {}
    
    /** @name Access parent strategy.
     */
    //@{
    /** Sets the parent strategy.
     *
     * @param parent parent strategy
     */
    inline void setParent(InstreamCompositeStrategy* parent) throw() { __parent = parent; }
    
    /** Returns the parent strategy.
     *
     * @return parent strategy
     */
    inline InstreamCompositeStrategy* getParent() const throw() { return __parent; }
    //@}
    
    /** @name Receiver Interface
     */
    //@{
    /** This method passes a message to a top-level %TransportStrategy and returns the queue status.
     *
     * @param message The message
     * @return The queue status of the %TransportStrategy
     */
    virtual Result receiveCSMessage(Message* message) = 0;
    
    /** Receives a signal from a child strategy.
     *
     * @param signal signal to be received
     * @param sender sender of signal
     */
    virtual void receiveSignal(Signal signal, ISignalSender* sender) = 0;
    //@}

    /** This method flushes the buffer and event receiver. 
     */
    virtual void flushITSReceiver() = 0;      

  protected:
    /** Returns signal mutex.
     *
     * @return signal mutex
     */
    const ThreadMutex& getSignalMutex() const throw() {
      return __signal_mutex;
    }
    
    /** Tests  if %Buffer strategy is blocked.
     *
     * @return true if buffer strategy is blocked, false otherwise
     */
    bool isBufferStrategyBlocked() const throw() {
      return __buffer_blocked;
    }

    /** Tests if %CEvent strategy is blocked.
     *
     * @return true %CEvent strategy is blocked, false otherwise
     */    
    bool isCEventStrategyBlocked() const throw() {
      return __cevent_blocked;
    }

    /** Sets flag which specifies if %Buffer strategy is blocked.
     *
     * @param blocked flag value to set
     */
    void setBufferStrategyBlocked(bool blocked) throw() {
      __buffer_blocked = blocked;
    }

    /** Sets flag which specifies if %CEvent strategy is blocked.
     *
     * @param blocked flag value to set
     */    
    void setCEventStrategyBlocked(bool blocked) throw() {
      __cevent_blocked = blocked;
    }
    
  private:
    InstreamCompositeStrategy* __parent;         ///< The parent strategy.
    ThreadMutex                __signal_mutex;   ///< Signal mutex.
    bool                       __buffer_blocked; ///< %Buffer child strategy is blocked
    bool                       __cevent_blocked; ///< %CEvent child strategy is blocked
  };
  
}

#include "nmm/nmm_suffix.hpp"

#endif
