/* 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_UDP_BROADCASTER_HPP
#define NMM_UDP_BROADCASTER_HPP NMM_UDP_BROADCASTER_HPP

#include "nmm/nmm_prefix.hpp"

#include "INetStream.hpp"

#include "nmm/comm/serialize/TValue.hpp"

#include "nmm/utils/thread/ThreadedObject.hpp"
#include "nmm/comm/messaging/IEventSender.hpp"
#include "nmm/comm/messaging/Event.hpp"


#include "nmm/utils/net/SockConnector.hpp"
#include "nmm/utils/net/SockAcceptor.hpp"
#include "nmm/utils/net/SockDGram.hpp"
#include "nmm/utils/net/INetAddr.hpp"
#include "nmm/utils/net/ThreadedNetHandler.hpp"
#include "nmm/comm/netstrategy/PacketINetStream.hpp"
#include "nmm/comm/serialize/MagicNumberIValueStream.hpp"
#include "nmm/comm/netstrategy/PacketONetStream.hpp"
#include "nmm/comm/serialize/MagicNumberOValueStream.hpp"


#include "visibility.hpp"

namespace NMM {

  /** UDP event broadcaster.
   * This class allows to send and receive NMM messages using a broadcast address
   * and the User Datagram Protocol (UDP/IP).
   * To receive messages, the receiver must implement the IEventReceiver interface
   * and register itself at an instance of this object.
   */
  class DLLEXPORT UDPBroadcaster
    : public IEventSender, public ThreadedNetHandler
  {
  public:
    /** @name Constructor & Destructor
     */
    //@{
    /** Create a new instance of UDPBroadcaster.
     *
     * @param sending_port port number that is used to send messages
     * @param receiving_port port number that is used to receive messages
     */
    UDPBroadcaster(int sending_port, int receiving_port);

    /** Destroy an instance of UDPBroadcaster.
     */
    virtual ~UDPBroadcaster();
    //@}

    /** @name Sending methods
     */
    //@{
    /** Broadcast an event.
     * This method sends an NMM event as broadcast message to the network.
     *
     * @param event event that is sent as broadcast message
     * @return SUCCESS if the event could be sent, FAILURE otherwise
     */
    virtual Result sendEvent(const Event& event);
    //@}

    /** @name Get Methods
     */
    //@{
    /** Get the sender port.
     * @return Sender port
     */
    int getSendingPort()    { return __sending_port; }

    /** Get the receiver port.
     * @return Receiver port
     */
    int getReceivingPort()  { return __receiving_port; }
    //@}

  protected:
    /** Incoming data notification.
     * 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
     */
    NetHandler::NetResult receiveData(SocketHandle socket);

  private:
    int __sending_port;                ///< Sending port number
    int __receiving_port;              ///< Receiving port number

    PacketONetStream        __pos;     ///< Packetized network stream for writing messages to the socket
    MagicNumberOValueStream __movs;    ///< Value stream for serializing outgoing messages

    PacketINetStream        __pis;     ///< Packetized network stream for reading messages from the socket
    MagicNumberIValueStream __mivs;    ///< Value stream for serializing incoming messages

    INetAddr __sending_addr;           ///< Sender address
    INetAddr __receiving_addr;         ///< Receiver address

    SockDGram __sending_socket;        ///< Socket for sending messages
    SockDGram __receiving_socket;      ///< Socket for receiving messages

    SockConnector __connector;         ///< Connector to connect to broadcast address
    SockAcceptor  __acceptor;          ///< Acceptor  to accept messages from broadcast address

    ThreadMutex __internal_send_mutex; ///< Mutex to lock sending process
    ThreadMutex __internal_recv_mutex; ///< Mutex to lock receiving process

    bool          __is_running;        ///< This broadcaster is running
  };
}

#include "nmm/nmm_suffix.hpp"

#endif
