/* 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_SESSION_HPP
#define NMM_SESSION_HPP

#include "nmm/nmm_prefix.hpp"

#include "ISessionImpl.hpp"
#include "GraphDescription.hpp"

namespace NMM {
  /** Each running flowgraph that was requested by a ClientRegistry using a GraphDescription is represented as a 
   *  Session within the ServerRegistry. The corresponding Session object stores all important information about 
   *  a running Session (e.g. used Synchronizer). Furthermore it provides a reference counter for nodes to determine 
   *  if a node is reused by another Session or can be released. 
   */
  class Session : public ISessionImpl {
  public:
    Session(const GraphDescription& gd);
    virtual ~Session();

  protected:
    /**
     * Returns the graph description of this sesssion.
     * @return pointer to the GraphDescription of this session. The caller of this method is responsible to delete the returned object.
     */
    virtual GraphDescription* getGraphDescription() const;

    /**
     * Returns interface to the source synchronizer of this sesssion.
     * @return interface to the source synchronizer of this sesssion.
     */
    virtual ISourceSynchronizer* getSourceSynchronizer() const;

    /**
     * Returns interface to the sink synchronizer of this sesssion.
     * @return interface to the sink synchronizer of this sesssion.
     */
    virtual ISinkSynchronizer* getSinkSynchronizer() const;

    /**
     * Sets the sink synchronizer for the session.
     * @param synchronizer interface to the sink synchronizer that should be used. 
     */
    virtual void setSinkSynchronizer(const ISinkSynchronizer& synchronizer);

    /**
     * Sets the source synchronizer for the session.
     * @param synchronizer interface to the source synchronizer that should be used. 
     */
    virtual void setSourceSynchronizer(const ISourceSynchronizer& synchronizer);

    /**
     * Sets the graph description for the session.
     * @param gd GraphDescription that is used in this session 
     */
    virtual void setGraphDescription(const GraphDescription& gd);

    /** 
     * Returns the MasterGraph of this Session. 
     * If a Session reuses nodes that has been allocated by another GraphDescription 
     * (which is called MasterGraph) this GraphDescription can be accessed using this method. If no nodes are reused a NULL pointer is returned.
     * @return pointer to the MasterGraph of this session or NULL-
     */
    virtual GraphDescription* getMasterGraph();


    /**
     * Sets the MasterGraph for this Session. 
     * If a Session reuses nodes that has been allocated by another GraphDescription 
     * (which is called MasterGraph) this GraphDescription is set using this method. 
     * @param master_graph reference to the MasterGraph for this Session
     */
    virtual void setMasterGraph(const GraphDescription& master_graph);

    /** This method invokes realizeGraph on the internal GraphDescription.
     */
    virtual void realizeGraph();

    /** This method invokes activateGraph on the internal GraphDescription.
     */
    virtual void activateGraph();

    /** This method invokes startGraph on the internal GraphDescription.
     */
    virtual void startGraph();

    /** This method invokes stopGraph on the internal GraphDescription.
     */
    virtual void stopGraph();
  private:
    /** This mehtod increases the reference count of each Node of the GraphDescription by one. 
     * @param gd reference to the GraphDescription. 
     */
    void increaseCount(const GraphDescription& gd);

    /** This mehtod decreases the reference count of each Node of the GraphDescription by one. This metod is 
     * automatically invoked if a Session object is destoryed.
     * @param gd reference to the GraphDescription. 
     */
    void decreaseCount(const GraphDescription& gd);

    /** This method releases all nodes whose reference count is equal to zero. 
     */
    void releaseNodes();
  private:
    GraphDescription_var    __gd;                       ///< GraphDescription of the Session 
    GraphDescription_var    __master_graph;             ///< GraphDescription of the MasterGraph
    ISourceSynchronizer_var __source_synchronizer;      ///< Used SourceSynchronizer of this Session
    ISinkSynchronizer_var   __sink_synchronizer;        ///< Used SinkSynchronizer of this Session
  };

  typedef auto_ptr<Session> Session_var;

}

#include "nmm/nmm_suffix.hpp"

#endif
