//===========================================================================
//  @(#) $Name: arts++-1-1-a12 $
//  @(#) $Id: ArtsProtocolTableAggregatorMap.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
//===========================================================================

extern "C" {
#include "artslocal.h"
}

#include <string>
#include <iterator>
#ifdef HAVE_FSTREAM
  #include <fstream>
#else
  #include <fstream.h>
#endif

#include "ArtsProtocolTableAggregatorMap.hh"

using namespace std;

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

//-------------------------------------------------------------------------
//          int ArtsProtocolTableAggregatorMap::Add(const Arts & arts)         
//.........................................................................
//  
//-------------------------------------------------------------------------
int ArtsProtocolTableAggregatorMap::Add(const Arts & arts)
{
  vector<ArtsAttribute>::const_iterator   hostAttribute;
  vector<ArtsAttribute>::const_iterator   ifIndexAttribute;
  ArtsAggregatorMapKey                    protoKey;

  if (arts.Header().Identifier() != artsC_OBJECT_PROTO)
    return(-1);
  
  hostAttribute = arts.FindHostAttribute();
  if (hostAttribute != arts.Attributes().end()) {
    protoKey.Router(hostAttribute->Host());
  }
  else {
    protoKey.Router(0);
  }

  ifIndexAttribute = arts.FindIfIndexAttribute();
  if (ifIndexAttribute != arts.Attributes().end()) {
    protoKey.IfIndex(ifIndexAttribute->IfIndex());
  }
  else {
    protoKey.IfIndex(0);
  }

  if ((*this).find(protoKey) == (*this).end()) {
    (*this)[protoKey] = new ArtsProtocolTableAggregator(arts);
  }
  else {
    ((*this)[protoKey])->Add(arts);
  }
  
  return(0);
}

//-------------------------------------------------------------------------
// int
// ArtsProtocolTableAggregatorMap::AddAllFromFile(const string & fileName, 
//                                                bool quiet)              
//.........................................................................
//  
//-------------------------------------------------------------------------
int ArtsProtocolTableAggregatorMap::AddAllFromFile(const string & fileName,
                                              bool quiet)
{
  ifstream    artsStream(fileName.c_str());
  int         numAdded = 0;
  
  if (!artsStream) {
    return(-1);
  }

  istream_iterator<ArtsProtocolTable>  inStreamIter(artsStream);
  istream_iterator<ArtsProtocolTable>  inStreamEnd;

  for (; inStreamIter != inStreamEnd; inStreamIter++) {
    //  Add the data to the map.
    if (this->Add(*inStreamIter) == 0)
      numAdded++;
    if (!quiet) {
      cout << ".";
      cout.flush();
    }
  }
  artsStream.close();
  return(numAdded);
}
