//=========================================================================== // @(#) $Name: arts++-1-1-a12 $ // @(#) $Id: ArtsProtocolTableData.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 #include #include "Arts.hh" #include "ArtsPrimitive.hh" #include "ArtsProtocolTableData.hh" using namespace std; static const std::string rcsid = "@(#) $Name: arts++-1-1-a12 $ $Id: ArtsProtocolTableData.cc,v 1.2 2004/04/21 23:51:35 kkeys Exp $"; //------------------------------------------------------------------------- // ArtsProtocolTableData::ArtsProtocolTableData() //......................................................................... // //------------------------------------------------------------------------- ArtsProtocolTableData::ArtsProtocolTableData() { this->_sampleInterval = 1; this->_totpkts = 0; this->_totbytes = 0; this->_length = 0; #ifndef NDEBUG ++this->_numObjects; #endif } //------------------------------------------------------------------------- // ArtsProtocolTableData::~ArtsProtocolTableData() //......................................................................... // //------------------------------------------------------------------------- ArtsProtocolTableData::~ArtsProtocolTableData() { #ifndef NDEBUG --this->_numObjects; #endif } //------------------------------------------------------------------------- // uint16_t ArtsProtocolTableData::SampleInterval() const //......................................................................... // //------------------------------------------------------------------------- uint16_t ArtsProtocolTableData::SampleInterval() const { return(this->_sampleInterval); } //------------------------------------------------------------------------- // uint16_t ArtsProtocolTableData::SampleInterval(uint16_t sampleInterval) //......................................................................... // //------------------------------------------------------------------------- uint16_t ArtsProtocolTableData::SampleInterval(uint16_t sampleInterval) { this->_sampleInterval = sampleInterval; return(this->_sampleInterval); } //------------------------------------------------------------------------- // uint64_t ArtsProtocolTableData::TotalPkts() const //......................................................................... // //------------------------------------------------------------------------- uint64_t ArtsProtocolTableData::TotalPkts() const { return(this->_totpkts); } //------------------------------------------------------------------------- // uint64_t ArtsProtocolTableData::TotalPkts(uint64_t totalPkts) //......................................................................... // //------------------------------------------------------------------------- uint64_t ArtsProtocolTableData::TotalPkts(uint64_t totalPkts) { this->_totpkts = totalPkts; return(this->_totpkts); } //------------------------------------------------------------------------- // uint64_t ArtsProtocolTableData::TotalBytes() const //......................................................................... // //------------------------------------------------------------------------- uint64_t ArtsProtocolTableData::TotalBytes() const { return(this->_totbytes); } //------------------------------------------------------------------------- // uint64_t ArtsProtocolTableData::TotalBytes(uint64_t totalBytes) //......................................................................... // //------------------------------------------------------------------------- uint64_t ArtsProtocolTableData::TotalBytes(uint64_t totalBytes) { this->_totbytes = totalBytes; return(this->_totbytes); } //------------------------------------------------------------------------- // vector & // ArtsProtocolTableData::ProtocolEntries() const //......................................................................... // //------------------------------------------------------------------------- vector & ArtsProtocolTableData::ProtocolEntries() const { return(this->_protocolEntries); } //------------------------------------------------------------------------- // void ArtsProtocolTableData::SortEntriesByBytes() //......................................................................... // //------------------------------------------------------------------------- void ArtsProtocolTableData::SortEntriesByBytes() { sort(this->_protocolEntries.begin(),this->_protocolEntries.end(), ArtsProtocolEntryGreaterBytes()); return; } //------------------------------------------------------------------------- // void ArtsProtocolTableData::SortEntriesByPkts() //......................................................................... // //------------------------------------------------------------------------- void ArtsProtocolTableData::SortEntriesByPkts() { sort(this->_protocolEntries.begin(),this->_protocolEntries.end(), ArtsProtocolEntryGreaterPkts()); return; } //------------------------------------------------------------------------- // uint32_t ArtsProtocolTableData::ComputeLength(uint8_t version) const //......................................................................... // //------------------------------------------------------------------------- uint32_t ArtsProtocolTableData::ComputeLength(uint8_t version) const { this->_length = 0; this->_length += sizeof(_sampleInterval); this->_length += sizeof(_totpkts); this->_length += sizeof(_totbytes); this->_length += sizeof(uint32_t); // number of protocol entries vector::const_iterator protocolEntry; for (protocolEntry = this->_protocolEntries.begin(); protocolEntry != this->_protocolEntries.end(); ++protocolEntry) { this->_length += protocolEntry->Length(version); } return(this->_length); } //------------------------------------------------------------------------- // uint32_t ArtsProtocolTableData::Length(uint8_t version) const //......................................................................... // //------------------------------------------------------------------------- uint32_t ArtsProtocolTableData::Length(uint8_t version) const { this->ComputeLength(version); return(this->_length); } //------------------------------------------------------------------------- // istream& ArtsProtocolTableData::read(istream& is, uint8_t version) //......................................................................... // //------------------------------------------------------------------------- istream& ArtsProtocolTableData::read(istream& is, uint8_t version) { uint32_t numProtocols; uint32_t protocolNum; ArtsProtocolTableEntry protocolEntry; g_ArtsLibInternal_Primitive.ReadUint16(is,this->_sampleInterval,2); g_ArtsLibInternal_Primitive.ReadUint64(is,this->_totpkts,8); g_ArtsLibInternal_Primitive.ReadUint64(is,this->_totbytes,8); g_ArtsLibInternal_Primitive.ReadUint32(is,numProtocols,4); this->_protocolEntries.reserve(numProtocols); for (protocolNum = 0; protocolNum < numProtocols; ++protocolNum) { protocolEntry.read(is,version); this->_protocolEntries.push_back(protocolEntry); } return(is); } //------------------------------------------------------------------------- // int ArtsProtocolTableData::read(int fd, uint8_t version) //......................................................................... // //------------------------------------------------------------------------- int ArtsProtocolTableData::read(int fd, uint8_t version) { uint32_t numProtocols; uint32_t protocolNum; ArtsProtocolTableEntry protocolEntry; int rc; int bytesRead = 0; rc = g_ArtsLibInternal_Primitive.ReadUint16(fd,this->_sampleInterval,2); if (rc < 2) return(-1); bytesRead += rc; rc = g_ArtsLibInternal_Primitive.ReadUint64(fd,this->_totpkts,8); if (rc < 8) return(-1); bytesRead += rc; rc = g_ArtsLibInternal_Primitive.ReadUint64(fd,this->_totbytes,8); if (rc < 8) return(-1); bytesRead += rc; rc = g_ArtsLibInternal_Primitive.ReadUint32(fd,numProtocols,4); if (rc < 4) return(-1); bytesRead += rc; for (protocolNum = 0; protocolNum < numProtocols; ++protocolNum) { rc = protocolEntry.read(fd,version); if (rc < 0) return(rc); bytesRead += rc; this->_protocolEntries.push_back(protocolEntry); } return(bytesRead); } //------------------------------------------------------------------------- // ostream& ArtsProtocolTableData::write(ostream& os, // uint8_t version) const //......................................................................... // //------------------------------------------------------------------------- ostream& ArtsProtocolTableData::write(ostream& os, uint8_t version) const { uint32_t numProtocols; g_ArtsLibInternal_Primitive.WriteUint16(os,this->_sampleInterval,2); g_ArtsLibInternal_Primitive.WriteUint64(os,this->_totpkts,8); g_ArtsLibInternal_Primitive.WriteUint64(os,this->_totbytes,8); numProtocols = this->_protocolEntries.size(); g_ArtsLibInternal_Primitive.WriteUint32(os,numProtocols,4); vector::const_iterator protocolEntry; for (protocolEntry = this->_protocolEntries.begin(); protocolEntry != this->_protocolEntries.end(); ++protocolEntry) { protocolEntry->write(os,version); } return(os); } //------------------------------------------------------------------------- // int ArtsProtocolTableData::write(int fd, uint8_t version) const //......................................................................... // //------------------------------------------------------------------------- int ArtsProtocolTableData::write(int fd, uint8_t version) const { uint32_t numProtocols; int rc; int bytesWritten = 0; rc = g_ArtsLibInternal_Primitive.WriteUint16(fd,this->_sampleInterval,2); if (rc < 2) return(-1); bytesWritten += rc; rc = g_ArtsLibInternal_Primitive.WriteUint64(fd,this->_totpkts,8); if (rc < 8) return(-1); bytesWritten += rc; rc = g_ArtsLibInternal_Primitive.WriteUint64(fd,this->_totbytes,8); if (rc < 8) return(-1); bytesWritten += rc; numProtocols = this->_protocolEntries.size(); g_ArtsLibInternal_Primitive.WriteUint32(fd,numProtocols,4); if (rc < 4) return(-1); bytesWritten += rc; vector::const_iterator protocolEntry; for (protocolEntry = this->_protocolEntries.begin(); protocolEntry != this->_protocolEntries.end(); ++protocolEntry) { rc = protocolEntry->write(fd,version); if (rc < 0) return(rc); bytesWritten += rc; } return(bytesWritten); } //------------------------------------------------------------------------- // ArtsProtocolTableData & // operator = (const ArtsProtocolTableData & artsProtocolTableData) //......................................................................... // //------------------------------------------------------------------------- ArtsProtocolTableData & ArtsProtocolTableData::operator = (const ArtsProtocolTableData & artsProtocolTableData) { this->_sampleInterval = artsProtocolTableData.SampleInterval(); this->_totpkts = artsProtocolTableData.TotalPkts(); this->_totbytes = artsProtocolTableData.TotalBytes(); this->_protocolEntries = artsProtocolTableData.ProtocolEntries(); this->_length = artsProtocolTableData.Length(); return(*this); } //------------------------------------------------------------------------- // ostream & // operator << (ostream& os, // const ArtsProtocolTableData & artsProtocolTableData) //......................................................................... // //------------------------------------------------------------------------- ostream & operator << (ostream& os, const ArtsProtocolTableData & artsProtocolTableData) { os << "PROTOCOL OBJECT DATA" << endl; os << "\tsample_interval: " << artsProtocolTableData.SampleInterval() << endl; os << "\tcount: " << artsProtocolTableData.ProtocolEntries().size() << endl; os << "\ttotpkts: " << artsProtocolTableData.TotalPkts() << endl; os << "\ttotbytes: " << artsProtocolTableData.TotalBytes() << endl; vector::const_iterator protocolEntry; for (protocolEntry = artsProtocolTableData.ProtocolEntries().begin(); protocolEntry != artsProtocolTableData.ProtocolEntries().end(); ++protocolEntry) { os << *protocolEntry; } return(os); } uint32_t ArtsProtocolTableData::_numObjects = 0;