/* 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_PACKET_O_NET_STREAM_HPP
#define NMM_PACKET_O_NET_STREAM_HPP NMM_PACKET_O_NET_STREAM_HPP

#include "nmm/nmm_prefix.hpp"

#include "ONetStream.hpp"

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

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

#include "visibility.hpp"

namespace NMM {

  class PacketHeader;

  /** Packetized network output stream.
   * This class implements a network output stream which splits data into packets
   * for consistent, ordered all-or-nothing transmission on unreliable channels.
   *
   * This is the network sender part of the packetized network stream.
   * It splits messages into packets and sends the packets to the network.
   *
   * This stream also gathers statistics of the packetized transmission.
   *
   * @see PacketHeader, PacketINetStream, UDPStrategy, UDPBroadcaster
   */
  class DLLEXPORT PacketONetStream
    : public ONetStream
  {
  public:
    /** @name Constructor & Destructor
     */
    //@{
    /** Constructor.
     */
    PacketONetStream();

    /** Copy constructor.
     *
     * @param other Object to copy from
     */
    PacketONetStream(const PacketONetStream& other);

    /** Virtual destructor.
     */
    virtual ~PacketONetStream();

    /** Virtual copy constructor.
     *
     * @return A copy of this object which has the same type
     */
    virtual PacketONetStream* copy() const;
    //@}

    /** @name OStream methods
     */
    //@{
    /** Write serialized data.
     * This method must be called after all data has been serialized.
     * It flushes written data internally.
     *
     * This implementation splits the internal message buffer into packets
     * and sends all packets to the network.
     */
    virtual void writeData();
    //@}

    /** @name Protocol Options
     */
    //@{
    /** Set the maximum size of a packet.
     * This method sets the maximum size of the data in a packet.
     * This value, together with the size of the packet header, may not exceed the maximum transmission unit (MTU)
     * of the underlying channel.
     *
     * @param size Maximum packet size in bytes
     */
    virtual void setPacketSize(const unsigned int& size);
    
    /** Get the maximum size of a packet.
     * This method gets the maximum size of the data in a packet.
     *
     * @return Maximum packet size in bytes
     */
    virtual unsigned int getPacketSize() const;
    //@}

    /** @name Protocol Statistics
     */
    //@{
    /** Reset all statistics.
     * This method resets all statistics that can be queried by the other methods
     * to their initial values.
     */
    virtual void resetStatistics();

    /** Get total number of sent bytes.
     * This method returns the total number of bytes sent to the network.
     *
     * @return Total number of sent bytes
     */
    virtual unsigned int getTotalSentBytes() const;

    /** Get total number of sent packets.
     * This method returns the total number of packets which have been sent to the network.
     *
     * @return Total number of sent packets
     */
    virtual unsigned int getTotalSentPackets() const;
    //@}

  protected:
    /** Subclass constructor.
     *
     * @param ti Type information
     */
    PacketONetStream(const TypeInfo& ti);

    /** Socket write method.
     * This method performs the actual socket write operation. It is invoked by the implementation
     * of the serialization operators.
     *
     * This implementation writes data to a temporary message buffer.
     *
     * @param data Binary data to be serialized
     * @param size Size of binary data to serialize (in bytes)
     */
    virtual void writeValue(const void* data, size_t size);

    /** Update attributes after serialization.
     */
    virtual void updateAttributes();

  private:
    /** A buffer to store packet data. */
    typedef vector<char> Packet;    

    /** Write a packet to the socket.
     * This method writes a packet from the given buffer to the socket.
     *
     * @param packet Packet data, including header
     */
    void writePacket(const void* packet);

    /** Register all serialized attributes.
     */
    void registerAttributes();

  private:
    /* Begin> Packet buffers */
    ThreadMutex __packet_mutex;             ///< Mutex to protect packet buffers
    TValue<nmm_uint32> __max_packet_size;          ///< Maximum packet size
    Packet __o_packet;                      ///< Output packet buffer
    bool __stream_socket;                   ///< Socket is a stream socket
    /* End< Packet buffers */

    /* Begin> Messages */
    vector<char> __o_message;               ///< Internal message buffer
    int __o_message_pos;                    ///< Read position for output message
    int __o_message_id;                     ///< Current output message id
    /* End< */

    /* Begin> Statistics */
    unsigned int __total_sent_bytes;             ///< Total number of bytes sent
    unsigned int __total_sent_packets;      ///< Total number of packets sent
    /* End< */
  };
  
}

#include "nmm/nmm_suffix.hpp"

#endif
