blob: cfa3983b19e7da2d8df052539cd50e3cd0ea07a3 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080020 */
21
22#ifndef NDN_DETAIL_REGISTERED_PREFIX_HPP
23#define NDN_DETAIL_REGISTERED_PREFIX_HPP
24
25#include "../common.hpp"
Alexander Afanasyevc348f832014-02-17 16:35:17 -080026#include "../name.hpp"
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080027#include "../interest.hpp"
28
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070029#include "interest-filter-record.hpp"
30
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080031namespace ndn {
32
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070033namespace nfd {
34class ControlParameters;
35}
36
37class RegisteredPrefix : noncopyable
Alexander Afanasyev90164962014-03-06 08:29:59 +000038{
39public:
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070040 /** \brief a callback on command success
Alexander Afanasyev90164962014-03-06 08:29:59 +000041 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070042 typedef function<void(const nfd::ControlParameters&)> SuccessCallback;
Alexander Afanasyev90164962014-03-06 08:29:59 +000043
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070044 /** \brief a callback on command failure
45 */
46 typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> FailureCallback;
Alexander Afanasyev90164962014-03-06 08:29:59 +000047
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070048 /// @brief Function that should be called to unregister prefix
49 typedef function<void(const SuccessCallback& onSuccess,
50 const FailureCallback& onFailure)> Unregistrator;
Alexander Afanasyev90164962014-03-06 08:29:59 +000051
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070052 RegisteredPrefix(const Name& prefix,
53 const Unregistrator& unregistrator)
Alexander Afanasyev90164962014-03-06 08:29:59 +000054 : m_prefix(prefix)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070055 , m_unregistrator(unregistrator)
Alexander Afanasyev90164962014-03-06 08:29:59 +000056 {
57 }
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080058
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070059 RegisteredPrefix(const Name& prefix,
60 const shared_ptr<InterestFilterRecord>& filter,
61 const Unregistrator& unregistrator)
Alexander Afanasyev90164962014-03-06 08:29:59 +000062 : m_prefix(prefix)
63 , m_filter(filter)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070064 , m_unregistrator(unregistrator)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080065 {
66 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067
68 const Name&
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080069 getPrefix() const
70 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000071 return m_prefix;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080072 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070073
Alexander Afanasyev90164962014-03-06 08:29:59 +000074 const shared_ptr<InterestFilterRecord>&
75 getFilter() const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080076 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000077 return m_filter;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080078 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070079
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070080 void
81 unregister(const SuccessCallback& onSuccess,
82 const FailureCallback& onFailure)
83 {
84 if (static_cast<bool>(m_unregistrator)) {
85 m_unregistrator(onSuccess, onFailure);
86 }
87 }
88
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080089private:
Alexander Afanasyev90164962014-03-06 08:29:59 +000090 Name m_prefix;
Alexander Afanasyev90164962014-03-06 08:29:59 +000091 shared_ptr<InterestFilterRecord> m_filter;
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070092 Unregistrator m_unregistrator;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080093};
94
Alexander Afanasyev90164962014-03-06 08:29:59 +000095/**
96 * @brief Opaque class representing ID of the registered prefix
97 */
98class RegisteredPrefixId;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080099
100/**
Alexander Afanasyev90164962014-03-06 08:29:59 +0000101 * @brief Functor to match RegisteredPrefixId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800102 */
Alexander Afanasyev90164962014-03-06 08:29:59 +0000103class MatchRegisteredPrefixId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800104{
Alexander Afanasyev90164962014-03-06 08:29:59 +0000105public:
106 explicit
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700107 MatchRegisteredPrefixId(const RegisteredPrefixId* registeredPrefixId)
108 : m_id(registeredPrefixId)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800109 {
110 }
111
112 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700113 operator()(const shared_ptr<RegisteredPrefix>& registeredPrefix) const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800114 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700115 return (reinterpret_cast<const RegisteredPrefixId*>(registeredPrefix.get()) == m_id);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800116 }
117private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700118 const RegisteredPrefixId* m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800119};
120
Alexander Afanasyev90164962014-03-06 08:29:59 +0000121
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800122} // namespace ndn
123
124#endif // NDN_DETAIL_REGISTERED_PREFIX_HPP