Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "status-server.hpp" |
| 8 | #include "fw/forwarder.hpp" |
| 9 | #include "core/version.hpp" |
| 10 | |
| 11 | namespace nfd { |
| 12 | |
| 13 | const Name StatusServer::DATASET_PREFIX = "ndn:/localhost/nfd/status"; |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 14 | const time::milliseconds StatusServer::RESPONSE_FRESHNESS = time::milliseconds(5000); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 15 | |
| 16 | StatusServer::StatusServer(shared_ptr<AppFace> face, Forwarder& forwarder) |
| 17 | : m_face(face) |
| 18 | , m_forwarder(forwarder) |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 19 | , m_startTimestamp(time::system_clock::now()) |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 20 | { |
| 21 | m_face->setInterestFilter(DATASET_PREFIX, bind(&StatusServer::onInterest, this, _2)); |
| 22 | } |
| 23 | |
| 24 | void |
| 25 | StatusServer::onInterest(const Interest& interest) const |
| 26 | { |
| 27 | Name name(DATASET_PREFIX); |
Alexander Afanasyev | 06ae907 | 2014-03-22 23:42:08 -0700 | [diff] [blame] | 28 | name.appendVersion(); |
| 29 | name.appendSegment(0); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 30 | |
| 31 | shared_ptr<Data> data = make_shared<Data>(name); |
| 32 | data->setFreshnessPeriod(RESPONSE_FRESHNESS); |
| 33 | |
Junxiao Shi | 88d6937 | 2014-03-28 11:52:39 -0700 | [diff] [blame] | 34 | shared_ptr<ndn::nfd::ForwarderStatus> status = this->collectStatus(); |
| 35 | data->setContent(status->wireEncode()); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 36 | |
| 37 | m_face->sign(*data); |
| 38 | m_face->put(*data); |
| 39 | } |
| 40 | |
Junxiao Shi | 88d6937 | 2014-03-28 11:52:39 -0700 | [diff] [blame] | 41 | shared_ptr<ndn::nfd::ForwarderStatus> |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 42 | StatusServer::collectStatus() const |
| 43 | { |
Junxiao Shi | 88d6937 | 2014-03-28 11:52:39 -0700 | [diff] [blame] | 44 | shared_ptr<ndn::nfd::ForwarderStatus> status = make_shared<ndn::nfd::ForwarderStatus>(); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 45 | |
| 46 | status->setNfdVersion(NFD_VERSION); |
| 47 | status->setStartTimestamp(m_startTimestamp); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 48 | status->setCurrentTimestamp(time::system_clock::now()); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 49 | |
| 50 | status->setNNameTreeEntries(m_forwarder.getNameTree().size()); |
| 51 | status->setNFibEntries(m_forwarder.getFib().size()); |
| 52 | status->setNPitEntries(m_forwarder.getPit().size()); |
| 53 | status->setNMeasurementsEntries(m_forwarder.getMeasurements().size()); |
| 54 | status->setNCsEntries(m_forwarder.getCs().size()); |
| 55 | |
| 56 | const ForwarderCounters& counters = m_forwarder.getCounters(); |
| 57 | status->setNInInterests(counters.getInInterest()); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 58 | status->setNInDatas(counters.getInData()); |
Junxiao Shi | 88d6937 | 2014-03-28 11:52:39 -0700 | [diff] [blame] | 59 | status->setNOutInterests(counters.getOutInterest()); |
Junxiao Shi | ea48d8b | 2014-03-16 13:53:47 -0700 | [diff] [blame] | 60 | status->setNOutDatas(counters.getOutData()); |
| 61 | |
| 62 | return status; |
| 63 | } |
| 64 | |
| 65 | } // namespace nfd |