spirosmastorakis | 0e2b197 | 2016-03-27 20:54:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2016 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of the nTorrent codebase. |
| 6 | * |
| 7 | * nTorrent is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with nTorrent, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS for complete list of nTorrent authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "stats-table-record.hpp" |
| 23 | |
| 24 | #include <boost/throw_exception.hpp> |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace ntorrent { |
| 28 | |
| 29 | StatsTableRecord::StatsTableRecord(const Name& recordName) |
| 30 | : m_recordName(recordName) |
| 31 | , m_sentInterests(0) |
| 32 | , m_receivedData(0) |
| 33 | , m_successRate(0) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | StatsTableRecord::StatsTableRecord(const StatsTableRecord& record) |
| 38 | : m_recordName(record.getRecordName()) |
| 39 | , m_sentInterests(record.getRecordSentInterests()) |
| 40 | , m_receivedData(record.getRecordReceivedData()) |
| 41 | , m_successRate(record.getRecordSuccessRate()) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | StatsTableRecord::incrementSentInterests() |
| 47 | { |
| 48 | ++m_sentInterests; |
| 49 | m_successRate = m_receivedData / float(m_sentInterests); |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | StatsTableRecord::incrementReceivedData() |
| 54 | { |
| 55 | if (m_sentInterests == 0) { |
| 56 | BOOST_THROW_EXCEPTION(Error("Computing success rate while having sent no Interests")); |
| 57 | } |
| 58 | ++m_receivedData; |
| 59 | m_successRate = m_receivedData / float(m_sentInterests); |
| 60 | } |
| 61 | |
| 62 | StatsTableRecord& |
| 63 | StatsTableRecord::operator=(const StatsTableRecord& other) |
| 64 | { |
| 65 | m_recordName = other.getRecordName(); |
| 66 | m_sentInterests = other.getRecordSentInterests(); |
| 67 | m_receivedData = other.getRecordReceivedData(); |
| 68 | m_successRate = other.getRecordSuccessRate(); |
| 69 | return (*this); |
| 70 | } |
| 71 | |
| 72 | bool |
| 73 | operator==(const StatsTableRecord& lhs, const StatsTableRecord& rhs) |
| 74 | { |
| 75 | return (lhs.getRecordName() == rhs.getRecordName() && |
| 76 | lhs.getRecordSentInterests() == rhs.getRecordSentInterests() && |
| 77 | lhs.getRecordReceivedData() == rhs.getRecordReceivedData()); |
| 78 | } |
| 79 | |
| 80 | bool |
| 81 | operator!=(const StatsTableRecord& lhs, const StatsTableRecord& rhs) |
| 82 | { |
| 83 | return (lhs.getRecordName() != rhs.getRecordName() || |
| 84 | lhs.getRecordSentInterests() != rhs.getRecordSentInterests() || |
| 85 | lhs.getRecordReceivedData() != rhs.getRecordReceivedData()); |
| 86 | } |
| 87 | |
| 88 | } // namespace ntorrent |
| 89 | } // namespace ndn |