blob: 45cde491d8ded6a9b77f4d33ff5855a1de2a7d18 [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
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080023const std::size_t MAX_NDN_PACKET_SIZE = 8800;
24
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
Junxiao Shi8c8d2182014-01-30 22:33:00 -070039 Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070040
Junxiao Shi32bfeb32014-01-25 18:22:02 -070041 virtual
42 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070043
Junxiao Shi8c8d2182014-01-30 22:33:00 -070044 FaceId
45 getId() const;
Junxiao Shi32bfeb32014-01-25 18:22:02 -070046
47 /// fires when an Interest is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080048 EventEmitter<Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070049
Junxiao Shi32bfeb32014-01-25 18:22:02 -070050 /// fires when a Data is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080051 EventEmitter<Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080052
53 /// fires when face disconnects or fails to perform properly
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080054 EventEmitter<std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070055
Junxiao Shi32bfeb32014-01-25 18:22:02 -070056 /// send an Interest
57 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080058 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070059
Junxiao Shi32bfeb32014-01-25 18:22:02 -070060 /// send a Data
61 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080062 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080063
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080064 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080065 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080066 * This terminates all communication on the face and cause
67 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080068 */
69 virtual void
70 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070071
Junxiao Shi88884492014-02-15 15:57:43 -070072 /** \brief Get whether underlying communication is up
73 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070074 * In this base class this property is always true.
75 */
76 virtual bool
77 isUp() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070078
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080079 /** \brief Get whether face is connected to a local app
80 *
81 * False by default and can become true if a derived class, implementing
82 * one of the local face types, explicitly calls Face::setLocal(true)
83 */
84 bool
85 isLocal() const;
Junxiao Shi7860d482014-02-21 23:57:20 -070086
Junxiao Shi32bfeb32014-01-25 18:22:02 -070087 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -070088 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070089 * This is typically invoked by mgmt on set description command
90 */
91 virtual void
92 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070093
Junxiao Shi32bfeb32014-01-25 18:22:02 -070094 /// Get the description
95 virtual const std::string&
96 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -070097
Junxiao Shi32bfeb32014-01-25 18:22:02 -070098 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -070099 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700100 * In this base class this property is always false.
101 */
102 virtual bool
103 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700104
Junxiao Shi7860d482014-02-21 23:57:20 -0700105 const FaceCounters&
106 getCounters() const;
107
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800108protected:
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700109 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800110 setLocal(bool isLocal);
111
112 // this is a non-virtual method
113 bool
114 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700115
Junxiao Shi7860d482014-02-21 23:57:20 -0700116 FaceCounters&
117 getMutableCounters();
118
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700119private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800120 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700121 setId(FaceId faceId);
122
123private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700124 FaceId m_id;
125 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800126 bool m_isLocal; // for scoping purposes
Junxiao Shi7860d482014-02-21 23:57:20 -0700127 FaceCounters m_counters;
128
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700129 // allow setting FaceId
130 friend class Forwarder;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700131};
132
Junxiao Shi7860d482014-02-21 23:57:20 -0700133
134inline const FaceCounters&
135Face::getCounters() const
136{
137 return m_counters;
138}
139
140inline FaceCounters&
141Face::getMutableCounters()
142{
143 return m_counters;
144}
145
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800146} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700147
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700148#endif // NFD_FACE_FACE_HPP