blob: 61a67b141f942bf6a2467ca764a8082b4ee714fe [file] [log] [blame]
Junxiao Shiea48d8b2014-03-16 13:53:47 -07001/* -*- 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
11namespace nfd {
12
13const Name StatusServer::DATASET_PREFIX = "ndn:/localhost/nfd/status";
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070014const time::milliseconds StatusServer::RESPONSE_FRESHNESS = time::milliseconds(5000);
Junxiao Shiea48d8b2014-03-16 13:53:47 -070015
16StatusServer::StatusServer(shared_ptr<AppFace> face, Forwarder& forwarder)
17 : m_face(face)
18 , m_forwarder(forwarder)
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070019 , m_startTimestamp(time::system_clock::now())
Junxiao Shiea48d8b2014-03-16 13:53:47 -070020{
21 m_face->setInterestFilter(DATASET_PREFIX, bind(&StatusServer::onInterest, this, _2));
22}
23
24void
25StatusServer::onInterest(const Interest& interest) const
26{
27 Name name(DATASET_PREFIX);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070028 /// \todo use NumberComponent
29 name.append(Name::Component::fromNumberWithMarker(
30 time::toUnixTimestamp(time::system_clock::now()).count(), 0x00));
31 name.append(Name::Component::fromNumberWithMarker(0, 0x00));
Junxiao Shiea48d8b2014-03-16 13:53:47 -070032
33 shared_ptr<Data> data = make_shared<Data>(name);
34 data->setFreshnessPeriod(RESPONSE_FRESHNESS);
35
36 shared_ptr<ndn::nfd::Status> payload = this->collectStatus();
37 ndn::EncodingBuffer payloadBuffer;
38 payload->wireEncode(payloadBuffer);
39 data->setContent(payloadBuffer.buf(), payloadBuffer.size());
40
41 m_face->sign(*data);
42 m_face->put(*data);
43}
44
45shared_ptr<ndn::nfd::Status>
46StatusServer::collectStatus() const
47{
48 shared_ptr<ndn::nfd::Status> status = make_shared<ndn::nfd::Status>();
49
50 status->setNfdVersion(NFD_VERSION);
51 status->setStartTimestamp(m_startTimestamp);
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070052 status->setCurrentTimestamp(time::system_clock::now());
Junxiao Shiea48d8b2014-03-16 13:53:47 -070053
54 status->setNNameTreeEntries(m_forwarder.getNameTree().size());
55 status->setNFibEntries(m_forwarder.getFib().size());
56 status->setNPitEntries(m_forwarder.getPit().size());
57 status->setNMeasurementsEntries(m_forwarder.getMeasurements().size());
58 status->setNCsEntries(m_forwarder.getCs().size());
59
60 const ForwarderCounters& counters = m_forwarder.getCounters();
61 status->setNInInterests(counters.getInInterest());
62 status->setNOutInterests(counters.getOutInterest());
63 status->setNInDatas(counters.getInData());
64 status->setNOutDatas(counters.getOutData());
65
66 return status;
67}
68
69} // namespace nfd