//===========================================================================
// @(#) $Name: arts++-1-1-a12 $
// @(#) $Id: ArtsTosTableAggregator.cc,v 1.2 2004/04/21 23:51:35 kkeys Exp $
//===========================================================================
//  Copyright Notice
//
//  By accessing this software, arts++, you are duly informed
//  of and agree to be bound by the conditions described below in this
//  notice:
//
//  This software product, arts++, is developed by Daniel W. McRobb, and
//  copyrighted(C) 1998 by the University of California, San Diego
//  (UCSD), with all rights reserved.  UCSD administers the CAIDA grant,
//  NCR-9711092, under which part of this code was developed.
//
//  There is no charge for arts++ software.  You can redistribute it
//  and/or modify it under the terms of the GNU Lesser General Public
//  License, Version 2.1, February 1999, which is incorporated by
//  reference herein.
//
//  arts++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF
//  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use
//  of it will not infringe on any third party's intellectual
//  property rights.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with arts++.  Copies can also be obtained from:
//
//    http://www.gnu.org/copyleft/lesser.html
//
//  or by writing to:
//
//  Free Software Foundation, Inc.
//  59 Temple Place, Suite 330
//  Boston, MA 02111-1307
//  USA
//
//  Or contact:
//
//    info@caida.org
//===========================================================================

#include <string>

#include "ArtsTosTableAggregator.hh"

using namespace std;

static const std::string rcsid = "@(#) $Name: arts++-1-1-a12 $ $Id: ArtsTosTableAggregator.cc,v 1.2 2004/04/21 23:51:35 kkeys Exp $";

//-------------------------------------------------------------------------
// ArtsTosTableAggregator::ArtsTosTableAggregator(const Arts & arts)
//.........................................................................
//  
//-------------------------------------------------------------------------
ArtsTosTableAggregator::ArtsTosTableAggregator(const Arts & arts)
{
  assert(arts.Header().Identifier() == artsC_OBJECT_TOS);
  
  this->_header = arts.Header();

  vector<ArtsAttribute>::const_iterator    inAttribute;

  for (inAttribute = arts.Attributes().begin();
       inAttribute != arts.Attributes().end(); inAttribute++) {
    this->_attributes.push_back(*inAttribute);
  }

  counter_t  tosCounter;
  vector<ArtsTosTableEntry>::const_iterator  inTosEntry;


  for (inTosEntry = arts.TosTableData()->TosEntries().begin();
       inTosEntry != arts.TosTableData()->TosEntries().end();
       inTosEntry++) {
    tosCounter.Pkts = inTosEntry->Pkts();
    tosCounter.Bytes = inTosEntry->Bytes();
    this->_tosCounters[inTosEntry->TosNumber()] =
      tosCounter;
  }
}

//---------------------------------------------------------------------------
//            ArtsTosTableAggregator::~ArtsTosTableAggregator()
//..........................................................................
//
//--------------------------------------------------------------------------
ArtsTosTableAggregator::~ArtsTosTableAggregator()
{
  if (this->_attributes.size() > 0) {
    this->_attributes.erase(this->_attributes.begin(),
                            this->_attributes.end());
  }
  if (this->_tosCounters.size() > 0) {
    this->_tosCounters.erase(this->_tosCounters.begin(),
                             this->_tosCounters.end());
  }
}

//-------------------------------------------------------------------------
//           void ArtsTosTableAggregator::Add(const Arts & arts)          
//.........................................................................
//  
//-------------------------------------------------------------------------
void ArtsTosTableAggregator::Add(const Arts & arts)
{
  assert(arts.Header().Identifier() == artsC_OBJECT_TOS);

  vector<ArtsAttribute>::iterator          myHostAttribute;

  for (myHostAttribute = this->_attributes.begin();
         myHostAttribute != this->_attributes.end();
         myHostAttribute++) {
    if (myHostAttribute->Identifier() == artsC_ATTR_HOST)
      break;
  }
  
  vector<ArtsAttribute>::iterator          myPeriodAttribute;
  vector<ArtsAttribute>::const_iterator    inPeriodAttribute;
  
  for (myPeriodAttribute = this->_attributes.begin();
         myPeriodAttribute != this->_attributes.end();
         myPeriodAttribute++) {
    if (myPeriodAttribute->Identifier() == artsC_ATTR_PERIOD)
      break;
  }
  
  inPeriodAttribute = arts.FindPeriodAttribute();

  const uint32_t *myPeriod = myPeriodAttribute->Period();
  const uint32_t *inPeriod = inPeriodAttribute->Period();
  
  if (inPeriod[0] < myPeriod[0]) {
    myPeriodAttribute->Period(inPeriod[0],myPeriod[1]);
  }
  if (inPeriod[1] > myPeriod[1]) {
    myPeriodAttribute->Period(myPeriod[0],inPeriod[1]);
  }
    
  counter_t  tosCounter;
  vector<ArtsTosTableEntry>::const_iterator    inTosEntry;
  map<uint8_t,counter_t,less<uint8_t> >::iterator   myTosEntry;
  
  for (inTosEntry = arts.TosTableData()->TosEntries().begin();
       inTosEntry != arts.TosTableData()->TosEntries().end();
       inTosEntry++) {
    myTosEntry =
      this->_tosCounters.find(inTosEntry->TosNumber());
    if (myTosEntry == this->_tosCounters.end()) {
      tosCounter.Pkts = inTosEntry->Pkts();
      tosCounter.Bytes = inTosEntry->Bytes();
      this->_tosCounters[inTosEntry->TosNumber()] =
        tosCounter;
    }
    else {
      (*myTosEntry).second.Pkts += inTosEntry->Pkts();
      (*myTosEntry).second.Bytes += inTosEntry->Bytes();
    }
  }
  return;
}

//-------------------------------------------------------------------------
//      ArtsTosTable *                           
//      ArtsTosTableAggregator::ConvertToArtsTosTable() const 
//.........................................................................
//  
//-------------------------------------------------------------------------
ArtsTosTable *
ArtsTosTableAggregator::ConvertToArtsTosTable() const
{
  ArtsTosTableEntry  tosEntry;
  map<uint8_t,counter_t,less<uint8_t> >::const_iterator  tosCounter;
  ArtsTosTable  *artsTosTable = new ArtsTosTable();

  artsTosTable->Header() = this->_header;
  vector<ArtsAttribute>::const_iterator   inAttribute;
  
  for (inAttribute = this->_attributes.begin();
       inAttribute != this->_attributes.end(); inAttribute++) {
    artsTosTable->Attributes().push_back(*inAttribute);
  }

  uint64_t  totalPkts = 0;
  uint64_t  totalBytes = 0;
  
  for (tosCounter = this->_tosCounters.begin();
       tosCounter != this->_tosCounters.end(); tosCounter++) {
    tosEntry.TosNumber((*tosCounter).first);
    tosEntry.Pkts((*tosCounter).second.Pkts);
    tosEntry.Bytes((*tosCounter).second.Bytes);
    artsTosTable->TosEntries().push_back(tosEntry);
    totalPkts += tosEntry.Pkts();
    totalBytes += tosEntry.Bytes();
  }

  artsTosTable->TotalPkts(totalPkts);
  artsTosTable->TotalBytes(totalPkts);
  
  return(artsTosTable);
}
