blob: 9635488ab7132513a2293a609000ff39aeed2e66 [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";
14const ndn::Milliseconds StatusServer::RESPONSE_FRESHNESS = 5000;
15
16static inline ndn::nfd::Status::Timestamp
17now()
18{
19 // make sure boost::chrono::system_clock's epoch is UNIX epoch
20 BOOST_ASSERT(static_cast<std::time_t>(0) == boost::chrono::system_clock::to_time_t(
21 boost::chrono::system_clock::time_point(
22 boost::chrono::duration_cast<boost::chrono::system_clock::duration>(
23 ndn::nfd::Status::Timestamp::duration::zero()
24 )
25 )
26 ));
27
28 return ndn::nfd::Status::Timestamp(
29 boost::chrono::duration_cast<ndn::nfd::Status::Timestamp::duration>(
30 boost::chrono::system_clock::now().time_since_epoch()
31 )
32 );
33}
34
35StatusServer::StatusServer(shared_ptr<AppFace> face, Forwarder& forwarder)
36 : m_face(face)
37 , m_forwarder(forwarder)
38 , m_startTimestamp(now())
39{
40 m_face->setInterestFilter(DATASET_PREFIX, bind(&StatusServer::onInterest, this, _2));
41}
42
43void
44StatusServer::onInterest(const Interest& interest) const
45{
46 Name name(DATASET_PREFIX);
47 // TODO use NumberComponent
48 name.append(ndn::Name::Component::fromNumberWithMarker(ndn::ndn_getNowMilliseconds(), 0x00));
49 name.append(ndn::Name::Component::fromNumberWithMarker(0, 0x00));
50
51 shared_ptr<Data> data = make_shared<Data>(name);
52 data->setFreshnessPeriod(RESPONSE_FRESHNESS);
53
54 shared_ptr<ndn::nfd::Status> payload = this->collectStatus();
55 ndn::EncodingBuffer payloadBuffer;
56 payload->wireEncode(payloadBuffer);
57 data->setContent(payloadBuffer.buf(), payloadBuffer.size());
58
59 m_face->sign(*data);
60 m_face->put(*data);
61}
62
63shared_ptr<ndn::nfd::Status>
64StatusServer::collectStatus() const
65{
66 shared_ptr<ndn::nfd::Status> status = make_shared<ndn::nfd::Status>();
67
68 status->setNfdVersion(NFD_VERSION);
69 status->setStartTimestamp(m_startTimestamp);
70 status->setCurrentTimestamp(now());
71
72 status->setNNameTreeEntries(m_forwarder.getNameTree().size());
73 status->setNFibEntries(m_forwarder.getFib().size());
74 status->setNPitEntries(m_forwarder.getPit().size());
75 status->setNMeasurementsEntries(m_forwarder.getMeasurements().size());
76 status->setNCsEntries(m_forwarder.getCs().size());
77
78 const ForwarderCounters& counters = m_forwarder.getCounters();
79 status->setNInInterests(counters.getInInterest());
80 status->setNOutInterests(counters.getOutInterest());
81 status->setNInDatas(counters.getInData());
82 status->setNOutDatas(counters.getOutData());
83
84 return status;
85}
86
87} // namespace nfd