blob: ea08f5b129ca21a32843508aad5dd3b10ed90d5c [file] [log] [blame]
Junxiao Shi32bfeb32014-01-25 18:22:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi08d07a72014-06-09 23:17:57 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 **/
Junxiao Shi32bfeb32014-01-25 18:22:02 -070025
26#include "face.hpp"
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -070027#include "face-flags.hpp"
Junxiao Shi16b8bc92014-02-17 22:24:55 -070028#include "core/logger.hpp"
Junxiao Shi32bfeb32014-01-25 18:22:02 -070029
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080030namespace nfd {
Junxiao Shi32bfeb32014-01-25 18:22:02 -070031
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000032static inline void
Junxiao Shi79494162014-04-02 18:25:11 -070033increaseCounter(FaceCounter& counter)
Alexander Afanasyev7e698e62014-03-07 16:48:35 +000034{
35 ++counter;
36}
37
Junxiao Shi79494162014-04-02 18:25:11 -070038Face::Face(const FaceUri& remoteUri, const FaceUri& localUri, bool isLocal)
Junxiao Shi8c8d2182014-01-30 22:33:00 -070039 : m_id(INVALID_FACEID)
Davide Pesavento0ff10db2014-02-28 03:12:27 +010040 , m_isLocal(isLocal)
Junxiao Shi79494162014-04-02 18:25:11 -070041 , m_remoteUri(remoteUri)
42 , m_localUri(localUri)
Alexander Afanasyev355c0662014-03-20 18:08:17 -070043 , m_isOnDemand(false)
Junxiao Shi08d07a72014-06-09 23:17:57 -070044 , m_isFailed(false)
Junxiao Shi32bfeb32014-01-25 18:22:02 -070045{
Alexander Afanasyevf6980282014-05-13 18:28:40 -070046 onReceiveInterest += bind(&increaseCounter, ref(m_counters.getNInInterests()));
47 onReceiveData += bind(&increaseCounter, ref(m_counters.getNInDatas()));
48 onSendInterest += bind(&increaseCounter, ref(m_counters.getNOutInterests()));
49 onSendData += bind(&increaseCounter, ref(m_counters.getNOutDatas()));
Junxiao Shi32bfeb32014-01-25 18:22:02 -070050}
51
52Face::~Face()
53{
54}
55
Junxiao Shi8c8d2182014-01-30 22:33:00 -070056FaceId
57Face::getId() const
58{
59 return m_id;
60}
61
Alexander Afanasyev46f66472014-01-31 16:50:58 -080062// this method is private and should be used only by the Forwarder
63void
Junxiao Shi8c8d2182014-01-30 22:33:00 -070064Face::setId(FaceId faceId)
65{
66 m_id = faceId;
67}
68
Junxiao Shi32bfeb32014-01-25 18:22:02 -070069void
70Face::setDescription(const std::string& description)
71{
72 m_description = description;
73}
74
75const std::string&
76Face::getDescription() const
77{
78 return m_description;
79}
80
81bool
82Face::isMultiAccess() const
83{
84 return false;
85}
86
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080087bool
Davide Pesavento0ff10db2014-02-28 03:12:27 +010088Face::isUp() const
89{
90 return true;
91}
92
93bool
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080094Face::decodeAndDispatchInput(const Block& element)
Junxiao Shi32bfeb32014-01-25 18:22:02 -070095{
Alexander Afanasyev650028d2014-04-25 18:39:10 -070096 try {
97 /// \todo Ensure lazy field decoding process
Alexander Afanasyev355c0662014-03-20 18:08:17 -070098
Alexander Afanasyev650028d2014-04-25 18:39:10 -070099 if (element.type() == tlv::Interest)
100 {
101 shared_ptr<Interest> i = make_shared<Interest>();
102 i->wireDecode(element);
103 this->onReceiveInterest(*i);
104 }
105 else if (element.type() == tlv::Data)
106 {
107 shared_ptr<Data> d = make_shared<Data>();
108 d->wireDecode(element);
109 this->onReceiveData(*d);
110 }
111 else
112 return false;
113
114 return true;
115 }
116 catch (tlv::Error&) {
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800117 return false;
Alexander Afanasyev650028d2014-04-25 18:39:10 -0700118 }
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700119}
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700120
Junxiao Shi08d07a72014-06-09 23:17:57 -0700121void
122Face::fail(const std::string& reason)
123{
124 if (m_isFailed) {
125 return;
126 }
127
128 m_isFailed = true;
129 this->onFail(reason);
130}
131
Alexander Afanasyeve515f0a2014-06-30 15:28:10 -0700132ndn::nfd::FaceStatus
133Face::getFaceStatus() const
134{
135 const FaceCounters& counters = getCounters();
136
137 ndn::nfd::FaceStatus status;
138 status.setFaceId(getId())
139 .setRemoteUri(getRemoteUri().toString())
140 .setLocalUri(getLocalUri().toString())
141 .setFlags(getFaceFlags(*this))
142 .setNInInterests(counters.getNInInterests())
143 .setNInDatas(counters.getNInDatas())
144 .setNOutInterests(counters.getNOutInterests())
145 .setNOutDatas(counters.getNOutDatas());
146
147 return status;
148}
149
150
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800151} //namespace nfd