/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2007 INRIA
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
 */
#ifndef NS2_MOBILITY_FILE_TOPOLOGY_H
#define NS2_MOBILITY_FILE_TOPOLOGY_H

#include <string>
#include <stdint.h>
#include "ns3/ptr.h"
#include "ns3/object.h"

namespace ns3 {

class StaticSpeedMobilityModel;

/**
 * \brief a topology object which can read ns2's movement files
 *        generated by the CMU setdest tool.
 */
class Ns2MobilityHelper
{
public:
  /**
   * \param filename filename of file which contains the
   *        ns2 movement trace.
   */
  Ns2MobilityHelper (std::string filename);

  /**
   * Read the ns2 trace file and configure the movement
   * patterns of all nodes contained in the global ns3::NodeList
   * whose nodeId is matches the nodeId of the nodes in the trace
   * file.
   */
  void Install (void) const;

  /**
   * \param begin an iterator which points to the start of the input
   *        object array.
   * \param end an iterator which points to the end of the input
   *        object array.
   *
   * Read the ns2 trace file and configure the movement
   * patterns of all input objects. Each input object
   * is identified by a unique node id which reflects
   * the index of the object in the input array.
   */
  template <typename T>
  void Install (T begin, T end) const;
private:
  class ObjectStore
  {
  public:
    virtual ~ObjectStore () {}
    virtual Ptr<Object> Get (uint32_t i) const = 0;
  };
  void LayoutObjectStore (const ObjectStore &store) const;
  Ptr<StaticSpeedMobilityModel> GetMobilityModel (std::string idString, const ObjectStore &store) const;
  double ReadDouble (std::string valueString) const;
  std::string m_filename;
};

} // namespace ns3

namespace ns3 {

template <typename T>
void 
Ns2MobilityHelper::Install (T begin, T end) const
{
  class MyObjectStore : public ObjectStore
  {
  public:
    MyObjectStore (T begin, T end)
      : m_begin (begin),
      m_end (end)
        {}
    virtual Ptr<Object> Get (uint32_t i) const {
      T iterator = m_begin;
      iterator += i;
      if (iterator >= m_end)
        {
          return 0;
        }
      return *iterator;
    }
  private:
    T m_begin;
    T m_end;
  };
  LayoutObjectStore (MyObjectStore (begin, end));
}


} // namespace ns3

#endif /* NS2_MOBILITY_FILE_TOPOLOGY_H */
