blob: 65117dbaa39d9d3d80c8fe5387a7b49cf2cf1135 [file] [log] [blame]
spirosmastorakis0e2b1972016-03-27 20:54:41 -07001/* -*- 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
26namespace ndn {
27namespace ntorrent {
28
29StatsTableRecord::StatsTableRecord(const Name& recordName)
30 : m_recordName(recordName)
31 , m_sentInterests(0)
32 , m_receivedData(0)
33 , m_successRate(0)
34{
35}
36
37StatsTableRecord::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
45void
46StatsTableRecord::incrementSentInterests()
47{
48 ++m_sentInterests;
49 m_successRate = m_receivedData / float(m_sentInterests);
50}
51
52void
53StatsTableRecord::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
62StatsTableRecord&
63StatsTableRecord::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
72bool
73operator==(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
80bool
81operator!=(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