blob: 1646c649922594504407ba2ba911e27911ce5c33 [file] [log] [blame]
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080011 */
12
13#ifndef NDN_DETAIL_REGISTERED_PREFIX_HPP
14#define NDN_DETAIL_REGISTERED_PREFIX_HPP
15
16#include "../common.hpp"
Alexander Afanasyevc348f832014-02-17 16:35:17 -080017#include "../name.hpp"
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080018#include "../interest.hpp"
19
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070020#include "interest-filter-record.hpp"
21
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080022namespace ndn {
23
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070024namespace nfd {
25class ControlParameters;
26}
27
28class RegisteredPrefix : noncopyable
Alexander Afanasyev90164962014-03-06 08:29:59 +000029{
30public:
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070031 /** \brief a callback on command success
Alexander Afanasyev90164962014-03-06 08:29:59 +000032 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070033 typedef function<void(const nfd::ControlParameters&)> SuccessCallback;
Alexander Afanasyev90164962014-03-06 08:29:59 +000034
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070035 /** \brief a callback on command failure
36 */
37 typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> FailureCallback;
Alexander Afanasyev90164962014-03-06 08:29:59 +000038
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070039 /// @brief Function that should be called to unregister prefix
40 typedef function<void(const SuccessCallback& onSuccess,
41 const FailureCallback& onFailure)> Unregistrator;
Alexander Afanasyev90164962014-03-06 08:29:59 +000042
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070043 RegisteredPrefix(const Name& prefix,
44 const Unregistrator& unregistrator)
Alexander Afanasyev90164962014-03-06 08:29:59 +000045 : m_prefix(prefix)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070046 , m_unregistrator(unregistrator)
Alexander Afanasyev90164962014-03-06 08:29:59 +000047 {
48 }
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080049
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070050 RegisteredPrefix(const Name& prefix,
51 const shared_ptr<InterestFilterRecord>& filter,
52 const Unregistrator& unregistrator)
Alexander Afanasyev90164962014-03-06 08:29:59 +000053 : m_prefix(prefix)
54 , m_filter(filter)
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070055 , m_unregistrator(unregistrator)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080056 {
57 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058
59 const Name&
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080060 getPrefix() const
61 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000062 return m_prefix;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080063 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070064
Alexander Afanasyev90164962014-03-06 08:29:59 +000065 const shared_ptr<InterestFilterRecord>&
66 getFilter() const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080067 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000068 return m_filter;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080069 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070070
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070071 void
72 unregister(const SuccessCallback& onSuccess,
73 const FailureCallback& onFailure)
74 {
75 if (static_cast<bool>(m_unregistrator)) {
76 m_unregistrator(onSuccess, onFailure);
77 }
78 }
79
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080080private:
Alexander Afanasyev90164962014-03-06 08:29:59 +000081 Name m_prefix;
Alexander Afanasyev90164962014-03-06 08:29:59 +000082 shared_ptr<InterestFilterRecord> m_filter;
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070083 Unregistrator m_unregistrator;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080084};
85
Alexander Afanasyev90164962014-03-06 08:29:59 +000086/**
87 * @brief Opaque class representing ID of the registered prefix
88 */
89class RegisteredPrefixId;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080090
91/**
Alexander Afanasyev90164962014-03-06 08:29:59 +000092 * @brief Functor to match RegisteredPrefixId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080093 */
Alexander Afanasyev90164962014-03-06 08:29:59 +000094class MatchRegisteredPrefixId
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -080095{
Alexander Afanasyev90164962014-03-06 08:29:59 +000096public:
97 explicit
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098 MatchRegisteredPrefixId(const RegisteredPrefixId* registeredPrefixId)
99 : m_id(registeredPrefixId)
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800100 {
101 }
102
103 bool
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700104 operator()(const shared_ptr<RegisteredPrefix>& registeredPrefix) const
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800105 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700106 return (reinterpret_cast<const RegisteredPrefixId*>(registeredPrefix.get()) == m_id);
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800107 }
108private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700109 const RegisteredPrefixId* m_id;
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800110};
111
Alexander Afanasyev90164962014-03-06 08:29:59 +0000112
Alexander Afanasyevc8823bc2014-02-09 19:33:33 -0800113} // namespace ndn
114
115#endif // NDN_DETAIL_REGISTERED_PREFIX_HPP