/* 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_NMM_APPLICATION_HPP
#define NMM_NMM_APPLICATION_HPP NMM_NMM_APPLICATION_HPP

#include "nmm/nmm_prefix.hpp"

#include "nmm/services/registry/ClientRegistry.hpp"
#include "nmm/services/registry/IServerRegistryImpl.hpp"
#include "nmm/comm/netstrategy/TCPAcceptor.hpp"

// Needed for windows template instantiation
#include "nmm/services/registry/IServerRegistryImpl.hpp"
#include "nmm/comm/netstrategy/TCPAcceptor.hpp"

#include "visibility.hpp"

namespace NMM {
  
  class IRegistry;
  class IPluginManager;
  class IServerRegistryImpl;
  class TCPAcceptor;
  class UDPBroadcaster;
  
  /** The NMMApplication instantiates a ServerRegistry in the local address space to simulate a distributed ServerRegistry.
   */
  class DLLEXPORT NMMApplication {
  public:
    /** @name Constructor & Destructor
     */
    //@{
    /** Constructor takes arguments from command line.
     *
     * @param argc number of arguments from command line
     * @param argv array of arguments from command line
     * @param is_local if true ClientRegistry and ServerRegistry get instantiated locally, if false nothing is done
     * @param port local port of ClientRegistry
     * @param enable_port_listening if true NMMApplication listens for external connections
     */
    NMMApplication(int argc, char** argv, bool is_local = true, int port = ClientRegistry::default_port , bool enable_port_listening = true);
	  
    /** Destructor.
     */
    virtual ~NMMApplication();
    //@}

    /** @name ClientRegistry & PluginManager
     */
    //@{
    /** This method returns the ClientRegistry, which can be used to search and request Nodes.
     *
     * @return reference to ClientRegistry
     */
    virtual       ClientRegistry& getRegistry();

    /** This method returns the ClientRegistry, which can be used to search and request Nodes.
     *
     * @return reference to ClientRegistry
     */
    virtual const ClientRegistry& getRegistry() const;

    /** This method returns the PluginManager, which can be used to load plugins.
     *
     * @return reference to IPluginManager
     */
    virtual       IPluginManager& getPluginManager();

    /** This method returns the PluginManager, which can be used to load plugins
     *
     * @return reference to IPluginManager
     */
    virtual const IPluginManager& getPluginManager() const;

    /** This method returns the ServerRegistry.
     * 
     * This method returns the ServerRegistry, if the application uses a own ServerRegistry or returns NULL else.
     *
     * @return pointer to IServerRegistryImpl
     */
    virtual       IServerRegistryImpl* getServerRegistry();

    /** This method returns the ServerRegistry.
     * 
     * This method returns the ServerRegistry, if the application uses a own ServerRegistry or returns NULL else.
     *
     * @return pointer to IServerRegistryImpl
     */
    virtual const IServerRegistryImpl* getServerRegistry() const;

    /** Enables access to the applications server registry.     
     *
     * @param port port on which ServerRegistry listens for incoming connections
     */
    void enableServerRegistry(int port);

    /** Reads the plugin information of the server registry from given file.
     *
     * By default the file ~/.nmm/plugins.<hostname> is used.
     *
     * @param file path to file with plugin information of the server registry
     */
    void readRegistryConfig(const string& file = "");

    /** Writes the plugin information of the server registry to a given file.
     *
     * By default the file ~/.nmm/plugins.<hostname> is used.
     *
     * @param file path of file with plugin information of the server registry to be written
     */
    void writeRegistryConfig(const string& file = "");

    /** Loads all available plug-ins.
     */
    void loadPlugins();

    /** Sets UDPBroadcaster in ServerRegistry of this NMMApplication.
     *
     * @param broadcaster UDPBroadcaster used to broadcast ServerRegistry information
     * @param port port on which ServerRegistry listens for incoming connections
     */ 
    void setUDPBroadcaster(UDPBroadcaster* broadcaster, int port);
    
    /** Prints the license of NMM 
     */
    static void printLicense(const string& application);
    //@}
    
  protected:
    /** Sets ServerRegsitry.
     *
     * @param server_registry_impl ServerRegistry to set
     */
    void setServerRegistry(IServerRegistryImpl* server_registry_impl);

  private:
    auto_ptr<ClientRegistry>       __client_registry;     ///< pointer to the ClientRegistry
    auto_ptr<IServerRegistryImpl>  __server_registry;     ///< pointer to the ServerRegistry
    auto_ptr<TCPAcceptor>          __default_acceptor;    ///< The default acceptor.
    auto_ptr<TCPAcceptor>          __registry_acceptor;   ///< accpetor for the server registry
    IPluginManager_var             __plugin_manager;      ///< Pointer to the PluginManager
    
  public:
    static int                     argc;                  ///< argc from main-method
    static char**                  argv;                  ///< argv from main-method
  };


}

#include "nmm/nmm_suffix.hpp"

#endif
