blob: f3c5d40cc5d14d65d23d302edc1c7ff06a9d75a3 [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 Shi2c29f3a2014-01-24 19:59:00 -070012
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080013namespace nfd {
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070014
15/** \class FaceId
16 * \brief identifies a face
17 */
18typedef int FaceId;
19
Junxiao Shi8c8d2182014-01-30 22:33:00 -070020const FaceId INVALID_FACEID = -1;
21
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080022const std::size_t MAX_NDN_PACKET_SIZE = 8800;
23
Junxiao Shi16b8bc92014-02-17 22:24:55 -070024
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070025/** \brief represents a face
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070026 */
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080027class Face : noncopyable, public enable_shared_from_this<Face>
Junxiao Shi2c29f3a2014-01-24 19:59:00 -070028{
Junxiao Shi32bfeb32014-01-25 18:22:02 -070029public:
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080030 /**
31 * \brief Face-related error
32 */
33 struct Error : public std::runtime_error
34 {
35 Error(const std::string& what) : std::runtime_error(what) {}
36 };
37
Junxiao Shi8c8d2182014-01-30 22:33:00 -070038 Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070039
Junxiao Shi32bfeb32014-01-25 18:22:02 -070040 virtual
41 ~Face();
Junxiao Shi16b8bc92014-02-17 22:24:55 -070042
Junxiao Shi8c8d2182014-01-30 22:33:00 -070043 FaceId
44 getId() const;
Junxiao Shi32bfeb32014-01-25 18:22:02 -070045
46 /// fires when an Interest is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080047 EventEmitter<Interest> onReceiveInterest;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070048
Junxiao Shi32bfeb32014-01-25 18:22:02 -070049 /// fires when a Data is received
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080050 EventEmitter<Data> onReceiveData;
Alexander Afanasyevd32cb962014-01-28 12:43:47 -080051
52 /// fires when face disconnects or fails to perform properly
Alexander Afanasyevd6cf4552014-02-11 17:15:22 -080053 EventEmitter<std::string/*reason*/> onFail;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070054
Junxiao Shi32bfeb32014-01-25 18:22:02 -070055 /// send an Interest
56 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080057 sendInterest(const Interest& interest) = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070058
Junxiao Shi32bfeb32014-01-25 18:22:02 -070059 /// send a Data
60 virtual void
Alexander Afanasyeva9034b02014-01-26 18:32:02 -080061 sendData(const Data& data) = 0;
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080062
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080063 /** \brief Close the face
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080064 *
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080065 * This terminates all communication on the face and cause
66 * onFail() method event to be invoked
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080067 */
68 virtual void
69 close() = 0;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070070
Junxiao Shi88884492014-02-15 15:57:43 -070071 /** \brief Get whether underlying communication is up
72 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070073 * In this base class this property is always true.
74 */
75 virtual bool
76 isUp() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -070077
Alexander Afanasyevbd220a02014-02-20 00:29:56 -080078 /** \brief Get whether face is connected to a local app
79 *
80 * False by default and can become true if a derived class, implementing
81 * one of the local face types, explicitly calls Face::setLocal(true)
82 */
83 bool
84 isLocal() const;
85
Junxiao Shi32bfeb32014-01-25 18:22:02 -070086 /** \brief Set the description
Junxiao Shi88884492014-02-15 15:57:43 -070087 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070088 * This is typically invoked by mgmt on set description command
89 */
90 virtual void
91 setDescription(const std::string& description);
Junxiao Shi16b8bc92014-02-17 22:24:55 -070092
Junxiao Shi32bfeb32014-01-25 18:22:02 -070093 /// Get the description
94 virtual const std::string&
95 getDescription() const;
Junxiao Shi88884492014-02-15 15:57:43 -070096
Junxiao Shi32bfeb32014-01-25 18:22:02 -070097 /** \brief Get whether packets sent this Face may reach multiple peers
Junxiao Shi88884492014-02-15 15:57:43 -070098 *
Junxiao Shi32bfeb32014-01-25 18:22:02 -070099 * In this base class this property is always false.
100 */
101 virtual bool
102 isMultiAccess() const;
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700103
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800104protected:
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700105 void
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800106 setLocal(bool isLocal);
107
108 // this is a non-virtual method
109 bool
110 decodeAndDispatchInput(const Block& element);
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700111
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700112private:
Alexander Afanasyev46f66472014-01-31 16:50:58 -0800113 void
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700114 setId(FaceId faceId);
115
116private:
Junxiao Shi32bfeb32014-01-25 18:22:02 -0700117 FaceId m_id;
118 std::string m_description;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800119 bool m_isLocal; // for scoping purposes
120
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700121 // allow setting FaceId
122 friend class Forwarder;
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700123};
124
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800125} // namespace nfd
Junxiao Shi2c29f3a2014-01-24 19:59:00 -0700126
Junxiao Shi16b8bc92014-02-17 22:24:55 -0700127#endif // NFD_FACE_FACE_HPP