/* 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_DISPATCHEDNETHANDLER_HPP
#define NMM_DISPATCHEDNETHANDLER_HPP NMM_DISPATCHEDNETHANDLER_HPP

#include "nmm/nmm_prefix.hpp"

#include "NetHandler.hpp"
#include "SocketHandle.hpp"

#include "visibility.hpp"

namespace NMM {

  /** \class DispatchedNetHandler 
   *
   * This class dispatches incoming events from the NetReactor and
   * maps them to a method that can be implmeneted by a sub class.
   */
  class DLLEXPORT DispatchedNetHandler : public NetHandler {
  public:
    /** @name Handler methods
     */
    //@{
    /** This method dispatches the events and maps them to one of the following method.
     * \param socket The socket on which an event occurs.
     * \param event The events which have been registered.
     * \param revents The events which were occured on the socket.
     * \return NetResult of the corresponding method that is invoked. 
     */
    virtual NetResult receiveEvent(SocketHandle socket, short event, short revents);

    /** This method is called if the event POLLIN, POLLRDNORM, POLLRDBAND, POLLPRI occurs, which means
     * that normal or priority Data can be read.
     * \param socket The corresponding socket.
     * \return VOID by default
     */
    virtual NetResult receiveData(SocketHandle)        { return VOID; };

    /** This method is called if the event POLLOUT, POLLWRNORM or POLLWRBAND occurs, which means that
     * normal or priority Data can be send.
     * \param socket The corresponding socket.
     * \return VOID by default
     */
    virtual NetResult writeData(SocketHandle)          { return VOID; };

    /** This method is called if the event POLLHUP occurs.
     * \param socket The corresponding socket.
     * \return VOID by default
     */
    virtual NetResult closeConnection(SocketHandle)    { return VOID; };

    /** This method is called if the event POLLINVAL occurs.
     * \param socket The corresponding socket.
     * \return VOID by default
     */
    virtual NetResult invalidSocket(SocketHandle )      { return VOID; };

    /** This method is called if the event POLLERR occurs.
     * \param socket The corresponding socket.
     * \return VOID by default
     */
    virtual NetResult errorOccured(SocketHandle )       { return VOID; };

    /** This method returns true, if the NetHandler requires an own thread.
     * \return true it the NetHandler requires an own thread, otherwise false
     */
    virtual bool requireThread()     const    { return false; }
    //@}
  protected:
    /** This method dispatches incoming network events and maps them to one of the following method.
     * \param socket The socket on which an event occurs.
     * \param events The events which were occured on the socket.
     * \return NetResult of the corresponding method that is invoked. 
     */
    NetResult handleEvents(SocketHandle socket, short events);
  };

}

#include "nmm/nmm_suffix.hpp"

#endif
