/* 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_PROTOCOLREADER_HPP
#define NMM_PROTOCOLREADER_HPP NMM_PROTOCOLREADER_HPP

#include "nmm/comm/messaging/EventDispatcher.hpp"

#define PROTO_DEBUG_STREAM(x) NODE_DEBUG_STREAM(parent, name << "> " << x);
#define PROTO_ERROR_STREAM(x) NODE_ERROR_STREAM(parent, name << "> " << x);
#define PROTO_MESSAGE_STREAM(x) NODE_MESSAGE_STREAM(parent, name << "> " << x);
#define PROTO_WARNING_STREAM(x) NODE_WARNING_STREAM(parent, name << "> " << x);

namespace NMM {
  class Node;

  /**
   * \brief This is an abstract class for implementing a specific protocol
     for the URLNode.

     On construction, the class gets a node pointer for messaging
     through this node object instead of doing it globally. By setting
     an URL the ProtocolReader calls the virtual methods for parsing
     the URL and initializing the ProtocolReader on this URL like
     opening a socket, sending a request and parsing meta data heading
     the real content. The virtual readData() method should read
     nearly the amount of content data specified in the given data
     pointer.
   */
  class ProtocolReader {

  private:


  protected:
    //! the name of the ProtocolReader.
    const string name;
    //! member to save the target url
    string url;
    //! pointer to the calling Node for transporting Messages
    Node* parent;
    //! member to save if the read has reached EOF
    bool is_eof;

    /**
     * this method parses a  URL and initializes the members for the ProtocolReader.
       \param url: the url to parse.
       \return true iff the given url is valid for this Reader.
     */
    virtual bool parseURL(const string url);

    /**
     * this method initializes the ProtocolReader from the members, set by parseURL.
       \return true iff the Reader could be initialized.
     */
    virtual bool init();

  public:
    /**
     * The named constructor.
       \param parent: the pointer to the calling Node.
       \param name: the name of the ProtocolReader.
     */
    ProtocolReader(Node* parrent, const string name = "ProtocolReader");
    /**
     * The default destructor.
     */
    virtual ~ProtocolReader();

    /**
     * this method initialies the ProtocolReader with a specific URL.
       \param _url: the url to read data from.
       \return true iff the given url is valid for this Reader.
     */
    virtual bool setURL(const string _url);

    /**
     * this method returns the NMM type/subtype of the media format 
       \return the format
     */
    virtual string getMediaFormat();

    /**
     * this method indicates if the ProtocolReader has reached EOF.
       \return true iff the ProtocolReader has reahed EOF.
     */
    virtual bool eof();

    /**
     * this method reads the specific amount of data in a given char*.
       \param data: the pointer, where to write the read data to.
       \param amount: the amount of data in bytes.
       \return true iff the operation was successful.
     */
    virtual int readData(char* data, size_t amount);

  };
} /* namespace NMM */

#endif //NMM_PROTOCOLREADER_HPP
