//===========================================================================
// @(#) $Name: arts++-1-1-a12 $
// @(#) $Id: ArtsProtocolTableAggregator.cc,v 1.2 2004/04/21 23:51:34 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 "ArtsProtocolTableAggregator.hh"

using namespace std;

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

//-------------------------------------------------------------------------
// ArtsProtocolTableAggregator::ArtsProtocolTableAggregator(const Arts & arts)
//.........................................................................
//  
//-------------------------------------------------------------------------
ArtsProtocolTableAggregator::ArtsProtocolTableAggregator(const Arts & arts)
{
  assert(arts.Header().Identifier() == artsC_OBJECT_PROTO);
  
  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  protocolCounter;
  vector<ArtsProtocolTableEntry>::const_iterator  inProtocolEntry;


  for (inProtocolEntry = arts.ProtocolTableData()->ProtocolEntries().begin();
       inProtocolEntry != arts.ProtocolTableData()->ProtocolEntries().end();
       ++inProtocolEntry) {
    protocolCounter.Pkts = inProtocolEntry->Pkts();
    protocolCounter.Bytes = inProtocolEntry->Bytes();
    this->_protocolCounters[inProtocolEntry->ProtocolNumber()] =
      protocolCounter;
  }
}

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

  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  protocolCounter;
  vector<ArtsProtocolTableEntry>::const_iterator    inProtocolEntry;
  map<uint8_t,counter_t,less<uint8_t> >::iterator   myProtocolEntry;
  
  for (inProtocolEntry = arts.ProtocolTableData()->ProtocolEntries().begin();
       inProtocolEntry != arts.ProtocolTableData()->ProtocolEntries().end();
       ++inProtocolEntry) {
    myProtocolEntry =
      this->_protocolCounters.find(inProtocolEntry->ProtocolNumber());
    if (myProtocolEntry == this->_protocolCounters.end()) {
      protocolCounter.Pkts = inProtocolEntry->Pkts();
      protocolCounter.Bytes = inProtocolEntry->Bytes();
      this->_protocolCounters[inProtocolEntry->ProtocolNumber()] =
        protocolCounter;
    }
    else {
      (*myProtocolEntry).second.Pkts += inProtocolEntry->Pkts();
      (*myProtocolEntry).second.Bytes += inProtocolEntry->Bytes();
    }
  }
  return;
}

//-------------------------------------------------------------------------
//      ArtsProtocolTable *                           
//      ArtsProtocolTableAggregator::ConvertToArtsProtocolTable() const 
//.........................................................................
//  
//-------------------------------------------------------------------------
ArtsProtocolTable *
ArtsProtocolTableAggregator::ConvertToArtsProtocolTable() const
{
  ArtsProtocolTableEntry  protocolEntry;
  map<uint8_t,counter_t,less<uint8_t> >::const_iterator  protocolCounter;
  ArtsProtocolTable  *artsProtocolTable = new ArtsProtocolTable();

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

  uint64_t  totalPkts = 0;
  uint64_t  totalBytes = 0;
  for (protocolCounter = this->_protocolCounters.begin();
       protocolCounter != this->_protocolCounters.end(); ++protocolCounter) {
    protocolEntry.ProtocolNumber((*protocolCounter).first);
    protocolEntry.Pkts((*protocolCounter).second.Pkts);
    protocolEntry.Bytes((*protocolCounter).second.Bytes);
    artsProtocolTable->ProtocolEntries().push_back(protocolEntry);
    totalPkts += protocolEntry.Pkts();
    totalBytes += protocolEntry.Bytes();
  }
  artsProtocolTable->TotalPkts(totalPkts);
  artsProtocolTable->TotalBytes(totalBytes);
  
  return(artsProtocolTable);
}

