/* 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_INPUT_JACK_HPP
#define NMM_INPUT_JACK_HPP

#include "nmm/nmm_prefix.hpp"

#include "Jack.hpp"

#include "nmm/comm/IInputJackImpl.hpp"
#include "nmm/comm/IOutputJack.hpp"

#include "visibility.hpp"

namespace NMM {

  class Buffer;
  class Node;
  class Message;
  class OutputJack;
  class OutputJack;
  class InstreamChannel;
  class IOutputJack;
  class InstreamCompositeStrategy;
  class IInstreamCompositeStrategy;

  /** Input jack.
   * An input jack is an instance of an input stream of a node.
   * Input jacks are connected to output jacks to exchange messages using either a transport strategy
   * or a direct local connection.
   *
   * This class is an abstract class which provides the interface and common functionality
   * and attributes of an input jack.
   */
  class DLLEXPORT InputJack :
    public Jack,
    public IInputJackImpl
  {
  private:
    mutable IOutputJack_var data_partner;                       ///< Partner jack
    InputJack  *data_jack;                                      ///< Data jack
    InstreamCompositeStrategy* __transport_strategy;            ///< Transport strategy
    mutable IInstreamCompositeStrategy* __i_transport_strategy; ///< Interface to transport strategy

  public:
    /** Create a new input jack.
     *
     * @param parent The parent node of the jack
     * @param jack_tag The name of the jack
     * @param name The class name of the jack
     */
    InputJack(Node* parent, const string& jack_tag = "default", const string& name = "InputJack");

    /** Virtual destructor.
     */
    virtual ~InputJack();

    /** Get the data jack of this jack.
     *
     * @return The data jack of this jack
     */
    virtual const InputJack* getDataJack() const {return data_jack;}

    /** Get the data jack of this jack.
     *
     * @return The data jack of this jack
     */
    virtual       InputJack* getDataJack()       {return data_jack;}

    /** Connect this jack to a local partner jack.
     * This method connects this jack to a local partner jack.
     * If the connection fails, a BindingException is thrown.
     *
     * @param jack Partner jack to connect with this jack
     */
    virtual void connectTo(const OutputJack* jack) const
      throw (const BindingException&);

    /** Check if this jack is connected.
     *
     * @return @c true if this jack is connected, otherwise @c false
     */
    virtual nmm_bool isConnected() const;
    
    /** Connect this input jack to an output jack.
     * This method creates an instream communication channel which connects this jack with a partner jack.
     * The returned InstreamChannel object must be deleted by the caller.
     *
     * @param jack Partner jack
     * @return Pointer to a new InstreamChannel object which represents the instream communication channel
     * @exception BindingException if the connection fails
     */
    virtual InstreamChannel* connectTo(const IOutputJack& jack)
      throw (const BindingException&);

    /** Disconnect this jack from its partner jack.
     */
    virtual void disconnect();

    /** Get the partner jack of this jack.
     * This method gets an interface to the jack to which this jack is connected.
     * The returned interface must be deleted by the caller.
     * If this jack is not connected, a null pointer is returned.
     *
     * @return An interface to the partner jack of this jack.
     */
    virtual IOutputJack* getDataPartner();

    /** Get the transport strategy of this jack.
     * This method returns an interface to the InstreamCompositeStrategy object which represents the endpoint
     * of this jack of the transport strategy used to connect this jack with its partner jack.
     * The returned interface must be deleted by the caller.
     * If this jack is not connected, or no transport strategy is used, this method returns a null pointer.
     *
     * @return An interface to the transport strategy of this jack
     */
    virtual IInstreamCompositeStrategy* getTransportStrategy() const;

    /** Get the transport strategy of this jack.
     * This method returns a local pointer to the InstreamCompositeStrategy object which represents the endpoint
     * of this jack of the transport strategy used to connect this jack with its partner jack.
     * The returned object must not be deleted by the caller. The pointer stays valid until the transport strategy
     * is deleted because the jack is disconnected.
     * If this jack is not connected, or no transport strategy is used, this method returns a null pointer.
     *
     * @return A pointer to the transport strategy of this jack
     */
    InstreamCompositeStrategy* getOwnTransportStrategy() const;

    /** Set the transport strategy used by this jack.
     * This method sets the transport strategy which is used by the jack to communicate with its
     * partner jack. If this method succeeded, the jack is connected to a partner jack through
     * a non-local binding using the given tarnsport strategy.
     *
     * @param strategy The transport strategy used by this jack
     */
    virtual void setTransportStrategy(InstreamCompositeStrategy* strategy)
      throw (const NMM::BindingException&);

    /** Set the transport strategy used by this jack.
     * This method sets the transport strategy which is used by the jack to communicate with its
     * partner jack. If this method succeeded, the jack is connected to a partner jack through
     * a non-local binding using the given transport strategy.
     *
     * The transport strategy is given as a transport strategy address to connect to.
     *
     * @param address The address to connect to
     */
    virtual void setTransportStrategy(const TSAddress* address)
      throw (const NMM::BindingException&);

    /** Get the default address for the given strategy type.
     * This method returns the default address which is used when connecting to this object using the given type of
     * transport strategy.
     * If it is not possible to connect to this object using the given type of transport strategy, then this method
     * returns a null pointer.
     * The caller is responsible for deleting the returned object.
     * 
     * This implementation only calls the base class method.
     *
     * @param strategy_type Type of transport strategy
     * @return The default address or 0
     */
    virtual TSAddress* getDefaultAddress(const string& strategy_type) const;

    /** Get supported transport strategies.
     * This method returns the list of transport strategies supported by this object.
     *
     * This implementation restricts the set of supported transport strategies to transport strategies which can be
     * used for instream communication and also support the format of this jack. The format of this jack is
     * queried from the parent node. The FormatStrategyMap determines which formats are supported by which transport
     * strategy.
     *
     * @param strategy_list Returns the list of supported transport strategies
     * @return SUCCESS
     */
    virtual Result getSupportedStrategies(list<TSDescription>& strategy_list) const;
  };
  
  inline InstreamCompositeStrategy* InputJack::getOwnTransportStrategy() const {
    return __transport_strategy;
  }

}

#include "nmm/nmm_suffix.hpp"

#endif
