blob: 325ad670c2076c6f2c45021b390c440e448a6086 [file] [log] [blame]
Junxiao Shi2c29f3a2014-01-24 19:59:00 -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
Junxiao Shi16b8bc92014-02-17 22:24:55 -07007#ifndef NFD_FACE_FACE_HPP
8#define NFD_FACE_FACE_HPP
Junxiao Shi2c29f3a2014-01-24 19:59:00 -07009
10#include "common.hpp"
Junxiao Shi7f02c1e2014-01-29 22:57:01 -070011#include "core/event-emitter.hpp"
Junxiao Shi7860d482014-02-21 23:57:20 -070012#include "face-counter.hpp"
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070013
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080014namespace nfd {
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070015
16/** \class FaceId
17 * \brief identifies a face
18 */
19typedef int FaceId;
20
Junxiao Shi8c8d2182014-01-30 22:33:00 -070021const FaceId INVALID_FACEID = -1;
22
Davide Pesavento0ff10db2014-02-28 03:12:27 +010023const size_t MAX_NDN_PACKET_SIZE = 8800;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080024
Junxiao Shi16b8bc92014-02-17 22:24:55 -070025
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070026/** \brief represents a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070027 */
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080028class Face : noncopyable, public enable_shared_from_this<Face>
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070029{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070030public:
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080031 /**
32 * \brief Face-related error
33 */
34 struct Error : public std::runtime_error
35 {
36 Error(const std::string& what) : std::runtime_error(what) {}
37 };
38
Davide Pesavento0ff10db2014-02-28 03:12:27 +010039 explicit
40 Face(bool isLocal = false);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070041
Junxiao Shi32bfeb32014-01-25 18:22:02 -070042 virtual
43 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070044
Junxiao Shi8c8d2182014-01-30 22:33:00 -070045 FaceId
46 getId() const;
Junxiao Shi32bfeb32014-01-25 18:22:02 -070047
48 /// fires when an Interest is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080049 EventEmitter<Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070050
Junxiao Shi32bfeb32014-01-25 18:22:02 -070051 /// fires when a Data is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080052 EventEmitter<Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080053
54 /// fires when face disconnects or fails to perform properly
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080055 EventEmitter<std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070056
Junxiao Shi32bfeb32014-01-25 18:22:02 -070057 /// send an Interest
58 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080059 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070060
Junxiao Shi32bfeb32014-01-25 18:22:02 -070061 /// send a Data
62 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080063 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080064
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080065 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080066 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080067 * This terminates all communication on the face and cause
68 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080069 */
70 virtual void
71 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070072
Junxiao Shi32bfeb32014-01-25 18:22:02 -070073 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -070074 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070075 * This is typically invoked by mgmt on set description command
76 */
77 virtual void
78 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070079
Junxiao Shi32bfeb32014-01-25 18:22:02 -070080 /// Get the description
81 virtual const std::string&
82 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -070083
Davide Pesavento0ff10db2014-02-28 03:12:27 +010084 /** \brief Get whether face is connected to a local app
85 */
86 bool
87 isLocal() const;
88
Junxiao Shi32bfeb32014-01-25 18:22:02 -070089 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -070090 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070091 * In this base class this property is always false.
92 */
93 virtual bool
94 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070095
Davide Pesavento0ff10db2014-02-28 03:12:27 +010096 /** \brief Get whether underlying communication is up
97 *
98 * In this base class this property is always true.
99 */
100 virtual bool
101 isUp() const;
102
Junxiao Shi7860d482014-02-21 23:57:20 -0700103 const FaceCounters&
104 getCounters() const;
105
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800106protected:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800107 // this is a non-virtual method
108 bool
109 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700110
Junxiao Shi7860d482014-02-21 23:57:20 -0700111 FaceCounters&
112 getMutableCounters();
113
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700114private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800115 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700116 setId(FaceId faceId);
117
118private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700119 FaceId m_id;
120 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800121 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700122 FaceCounters m_counters;
123
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700124 // allow setting FaceId
125 friend class Forwarder;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700126};
127
Junxiao Shi7860d482014-02-21 23:57:20 -0700128
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100129inline bool
130Face::isLocal() const
131{
132 return m_isLocal;
133}
134
Junxiao Shi7860d482014-02-21 23:57:20 -0700135inline const FaceCounters&
136Face::getCounters() const
137{
138 return m_counters;
139}
140
141inline FaceCounters&
142Face::getMutableCounters()
143{
144 return m_counters;
145}
146
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800147} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700148
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700149#endif // NFD_FACE_FACE_HPP