// Filename: pStatFrameData.cxx
// Created by:  drose (10Jul00)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) Carnegie Mellon University.  All rights reserved.
//
// All use of this software is subject to the terms of the revised BSD
// license.  You should have received a copy of this license along
// with this source code in a file named "LICENSE."
//
////////////////////////////////////////////////////////////////////

#include "pStatFrameData.h"
#include "pStatClientVersion.h"
#include "config_pstats.h"

#include "datagram.h"
#include "datagramIterator.h"

#include <algorithm>

////////////////////////////////////////////////////////////////////
//     Function: PStatFrameData::sort_time
//       Access: Public
//  Description: Ensures the frame data is in monotonically increasing
//               order by time.
////////////////////////////////////////////////////////////////////
void PStatFrameData::
sort_time() {
  stable_sort(_time_data.begin(), _time_data.end());
}

////////////////////////////////////////////////////////////////////
//     Function: PStatFrameData::write_datagram
//       Access: Public
//  Description: Writes the definition of the FrameData to the
//               datagram.  Returns true on success, false on failure.
////////////////////////////////////////////////////////////////////
bool PStatFrameData::
write_datagram(Datagram &destination, PStatClient *client) const {
  Data::const_iterator di;
  if (_time_data.size() >= 65536 || _level_data.size() >= 65536) {
    pstats_cat.info()
      << "Dropping frame with " << _time_data.size()
      << " time measurements and " << _level_data.size()
      << " level measurements.\n";
    return false;
  }

  destination.add_uint16(_time_data.size());
  for (di = _time_data.begin(); di != _time_data.end(); ++di) {
    destination.add_uint16((*di)._index);
    destination.add_float32((*di)._value);
  }
  destination.add_uint16(_level_data.size());
  for (di = _level_data.begin(); di != _level_data.end(); ++di) {
    destination.add_uint16((*di)._index);
    destination.add_float32((*di)._value);
  }
  
  return true;
}

////////////////////////////////////////////////////////////////////
//     Function: PStatFrameData::read_datagram
//       Access: Public
//  Description: Extracts the FrameData definition from the datagram.
////////////////////////////////////////////////////////////////////
void PStatFrameData::
read_datagram(DatagramIterator &source, PStatClientVersion *) {
  clear();

  int i;
  int time_size = source.get_uint16();
  for (i = 0; i < time_size; i++) {
    nassertv(source.get_remaining_size() > 0);
    DataPoint dp;
    dp._index = source.get_uint16();
    dp._value = source.get_float32();
    _time_data.push_back(dp);
  }
  int level_size = source.get_uint16();
  for (i = 0; i < level_size; i++) {
    nassertv(source.get_remaining_size() > 0);
    DataPoint dp;
    dp._index = source.get_uint16();
    dp._value = source.get_float32();
    _level_data.push_back(dp);
  }
  nassertv(source.get_remaining_size() == 0);
}

