/* 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_USAGE_HPP
#define NMM_USAGE_HPP NMM_USAGE_HPP

#include "nmm/nmm_prefix.hpp"

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

#include <map>

#include "visibility.hpp"

namespace NMM {

  /** Usage Manager.
   * This is an utility class which manages the usage of a node. It is the standard implementation of the
   * IUsage interface. Usage management can be used to prioritize applications sharing a node.
   */
  class DLLEXPORT Usage :
    public IUsageImpl {
  private:
    /** Usage information for a single user.
     */
    typedef pair<int, const IUsageNotify*> usermode_t;

    map<int, list<usermode_t> > usages;     ///< Usage map

    ThreadMutex usage_mutex;                ///< Mutex for protecting the usage map
  public:
    /** Default constructor.
     */
    Usage();

    /** Virtual destructor.
     */
    ~Usage();

  protected: 
    /** Set the Usage of the node.
     *
     * @param usage usage of the node
     * @param user %IUsageNotify interface of this user so it will be notified, if its status (master or slave) changes
     */
    virtual void setUsage(const nmm_int32 &usage, const IUsageNotify* user);

    /** Unset the Usage of the node.
     *
     * @param usage usage of the node
     * @param user %IUsageNotify interface of this user to identify the user
     */
    virtual void unsetUsage(const nmm_int32 &usage, const IUsageNotify* user);

    /** Get the current Usage of this node.
     *
     * @return The current usage
     */
    virtual nmm_int32 getUsage();

    /** Get the usage rank.
     * This method returns the number of users wich have requested this node before the given
     * user with the given usage.
     *
     * @param usage usage of the node
     * @param user %IUsageNotify interface of this user to identify the user
     * @return The usage rank of this node
     */
    virtual nmm_int32 getRank(const nmm_int32 &usage, const IUsageNotify* user);

    /** Checks if the given user will be master with the given usage.
     *
     * @param usage usage of the node
     * @param user %IUsageNotify interface of this user to identify the user
     * @return @c SUCCESS if the user will be master, @c FAILURE if the user will be slave
     */
    virtual Result isMaster(const nmm_int32 &mode, const IUsageNotify* user);
  };
}

#include "nmm/nmm_suffix.hpp"

#endif
