blob: f20c37be01180c9853fa59414e22656e1936d927 [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 Shi61e3cc52014-03-03 20:40:28 -070012#include "core/face-uri.hpp"
Junxiao Shi7860d482014-02-21 23:57:20 -070013#include "face-counter.hpp"
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070014
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080015namespace nfd {
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070016
17/** \class FaceId
18 * \brief identifies a face
19 */
20typedef int FaceId;
21
Junxiao Shi8c8d2182014-01-30 22:33:00 -070022const FaceId INVALID_FACEID = -1;
23
Davide Pesavento0ff10db2014-02-28 03:12:27 +010024const size_t MAX_NDN_PACKET_SIZE = 8800;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080025
Junxiao Shi16b8bc92014-02-17 22:24:55 -070026
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070027/** \brief represents a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070028 */
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080029class Face : noncopyable, public enable_shared_from_this<Face>
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070030{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070031public:
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080032 /**
33 * \brief Face-related error
34 */
35 struct Error : public std::runtime_error
36 {
37 Error(const std::string& what) : std::runtime_error(what) {}
38 };
39
Davide Pesavento0ff10db2014-02-28 03:12:27 +010040 explicit
41 Face(bool isLocal = false);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070042
Junxiao Shi32bfeb32014-01-25 18:22:02 -070043 virtual
44 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070045
Junxiao Shi8c8d2182014-01-30 22:33:00 -070046 FaceId
47 getId() const;
Junxiao Shi32bfeb32014-01-25 18:22:02 -070048
49 /// fires when an Interest is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080050 EventEmitter<Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070051
Junxiao Shi32bfeb32014-01-25 18:22:02 -070052 /// fires when a Data is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080053 EventEmitter<Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080054
55 /// fires when face disconnects or fails to perform properly
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080056 EventEmitter<std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070057
Junxiao Shi32bfeb32014-01-25 18:22:02 -070058 /// send an Interest
59 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080060 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070061
Junxiao Shi32bfeb32014-01-25 18:22:02 -070062 /// send a Data
63 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080064 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080065
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080066 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080067 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080068 * This terminates all communication on the face and cause
69 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080070 */
71 virtual void
72 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070073
Junxiao Shi32bfeb32014-01-25 18:22:02 -070074 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -070075 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070076 * This is typically invoked by mgmt on set description command
77 */
78 virtual void
79 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070080
Junxiao Shi32bfeb32014-01-25 18:22:02 -070081 /// Get the description
82 virtual const std::string&
83 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -070084
Davide Pesavento0ff10db2014-02-28 03:12:27 +010085 /** \brief Get whether face is connected to a local app
86 */
87 bool
88 isLocal() const;
89
Junxiao Shi32bfeb32014-01-25 18:22:02 -070090 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -070091 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070092 * In this base class this property is always false.
93 */
94 virtual bool
95 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070096
Davide Pesavento0ff10db2014-02-28 03:12:27 +010097 /** \brief Get whether underlying communication is up
98 *
99 * In this base class this property is always true.
100 */
101 virtual bool
102 isUp() const;
103
Junxiao Shi7860d482014-02-21 23:57:20 -0700104 const FaceCounters&
105 getCounters() const;
106
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800107protected:
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800108 // this is a non-virtual method
109 bool
110 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700111
Junxiao Shi7860d482014-02-21 23:57:20 -0700112 FaceCounters&
113 getMutableCounters();
114
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700115private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800116 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700117 setId(FaceId faceId);
118
119private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700120 FaceId m_id;
121 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800122 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700123 FaceCounters m_counters;
124
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700125 // allow setting FaceId
Junxiao Shia4f2be82014-03-02 22:56:41 -0700126 friend class FaceTable;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700127};
128
Junxiao Shi7860d482014-02-21 23:57:20 -0700129
Davide Pesavento0ff10db2014-02-28 03:12:27 +0100130inline bool
131Face::isLocal() const
132{
133 return m_isLocal;
134}
135
Junxiao Shi7860d482014-02-21 23:57:20 -0700136inline const FaceCounters&
137Face::getCounters() const
138{
139 return m_counters;
140}
141
142inline FaceCounters&
143Face::getMutableCounters()
144{
145 return m_counters;
146}
147
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800148} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700149
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700150#endif // NFD_FACE_FACE_HPP