NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
face.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
26 #include "face.hpp"
27 #include "core/logger.hpp"
28 
29 namespace nfd {
30 
31 Face::Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal)
32  : m_id(INVALID_FACEID)
33  , m_isLocal(isLocal)
34  , m_remoteUri(remoteUri)
35  , m_localUri(localUri)
36  , m_isOnDemand(false)
37  , m_isFailed(false)
38  , m_metric(0)
39 {
40  onReceiveInterest += [this](const ndn::Interest&) { ++m_counters.getNInInterests(); };
41  onReceiveData += [this](const ndn::Data&) { ++m_counters.getNInDatas(); };
42  onSendInterest += [this](const ndn::Interest&) { ++m_counters.getNOutInterests(); };
43  onSendData += [this](const ndn::Data&) { ++m_counters.getNOutDatas(); };
44 }
45 
47 {
48 }
49 
50 FaceId
51 Face::getId() const
52 {
53  return m_id;
54 }
55 
56 // this method is private and should be used only by the FaceTable
57 void
58 Face::setId(FaceId faceId)
59 {
60  m_id = faceId;
61 }
62 
63 void
64 Face::setDescription(const std::string& description)
65 {
66  m_description = description;
67 }
68 
69 const std::string&
71 {
72  return m_description;
73 }
74 
75 bool
77 {
78  return false;
79 }
80 
81 bool
82 Face::isUp() const
83 {
84  return true;
85 }
86 
87 bool
88 Face::decodeAndDispatchInput(const Block& element)
89 {
90  try {
92 
93  if (element.type() == tlv::Interest)
94  {
95  shared_ptr<Interest> i = make_shared<Interest>();
96  i->wireDecode(element);
97  this->onReceiveInterest(*i);
98  }
99  else if (element.type() == tlv::Data)
100  {
101  shared_ptr<Data> d = make_shared<Data>();
102  d->wireDecode(element);
103  this->onReceiveData(*d);
104  }
105  else
106  return false;
107 
108  return true;
109  }
110  catch (tlv::Error&) {
111  return false;
112  }
113 }
114 
115 void
116 Face::fail(const std::string& reason)
117 {
118  if (m_isFailed) {
119  return;
120  }
121 
122  m_isFailed = true;
123  this->onFail(reason);
124 
125  this->onFail.clear();
126 }
127 
128 template<typename FaceTraits>
129 void
130 Face::copyStatusTo(FaceTraits& traits) const
131 {
132  traits.setFaceId(getId())
133  .setRemoteUri(getRemoteUri().toString())
134  .setLocalUri(getLocalUri().toString());
135 
136  if (isLocal()) {
137  traits.setFaceScope(ndn::nfd::FACE_SCOPE_LOCAL);
138  }
139  else {
140  traits.setFaceScope(ndn::nfd::FACE_SCOPE_NON_LOCAL);
141  }
142 
143  if (isOnDemand()) {
144  traits.setFacePersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
145  }
146  else {
147  traits.setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
148  }
149 }
150 
151 template void
152 Face::copyStatusTo<ndn::nfd::FaceStatus>(ndn::nfd::FaceStatus&) const;
153 
154 template void
155 Face::copyStatusTo<ndn::nfd::FaceEventNotification>(ndn::nfd::FaceEventNotification&) const;
156 
157 ndn::nfd::FaceStatus
159 {
160  ndn::nfd::FaceStatus status;
161  copyStatusTo(status);
162 
163  this->getCounters().copyTo(status);
164 
165  return status;
166 }
167 
168 } //namespace nfd
virtual const std::string & getDescription() const
Get the description.
Definition: face.cpp:70
const FaceCounters & getCounters() const
Definition: face.hpp:222
virtual ndn::nfd::FaceStatus getFaceStatus() const
Definition: face.cpp:158
void copyStatusTo(FaceTraits &traits) const
Definition: face.cpp:130
virtual bool isUp() const
Get whether underlying communication is up.
Definition: face.cpp:82
EventEmitter< Data > onReceiveData
fires when a Data is received
Definition: face.hpp:84
bool decodeAndDispatchInput(const Block &element)
Definition: face.cpp:88
EventEmitter< Interest > onSendInterest
fires when an Interest is sent out
Definition: face.hpp:87
const PacketCounter & getNOutDatas() const
outgoing Data
const FaceUri & getRemoteUri() const
Definition: face.hpp:234
bool isOnDemand() const
Get whether face is created on demand or explicitly via FaceManagement protocol.
Definition: face.hpp:252
virtual ~Face()
Definition: face.cpp:46
const PacketCounter & getNOutInterests() const
outgoing Interest
EventEmitter< std::string > onFail
fires when face disconnects or fails to perform properly
Definition: face.hpp:93
void fail(const std::string &reason)
fail the face and raise onFail event if it's UP; otherwise do nothing
Definition: face.cpp:116
EventEmitter< Data > onSendData
fires when a Data is sent out
Definition: face.hpp:90
void copyTo(R &recipient) const
copy current obseverations to a struct
identifies a face
const PacketCounter & getNInDatas() const
incoming Data
const FaceId INVALID_FACEID
indicates an invalid FaceId
Definition: face.hpp:44
const PacketCounter & getNInInterests() const
incoming Interest
virtual bool isMultiAccess() const
Get whether packets sent this Face may reach multiple peers.
Definition: face.cpp:76
virtual void setDescription(const std::string &description)
Set the description.
Definition: face.cpp:64
FaceId getId() const
Definition: face.cpp:51
const FaceUri & getLocalUri() const
Definition: face.hpp:240
bool isLocal() const
Get whether face is connected to a local app.
Definition: face.hpp:216
Face(const FaceUri &remoteUri, const FaceUri &localUri, bool isLocal=false)
Definition: face.cpp:31
EventEmitter< Interest > onReceiveInterest
fires when an Interest is received
Definition: face.hpp:81