Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 22 | #ifndef NDNX_WRAPPER_H |
| 23 | #define NDNX_WRAPPER_H |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 24 | |
Zhenkai Zhu | 7f4258e | 2012-12-29 19:55:13 -0800 | [diff] [blame] | 25 | #include <boost/thread/locks.hpp> |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 26 | #include <boost/thread/recursive_mutex.hpp> |
| 27 | #include <boost/thread/thread.hpp> |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 29 | #include "ndnx-common.h" |
| 30 | #include "ndnx-name.h" |
| 31 | #include "ndnx-selectors.h" |
| 32 | #include "ndnx-closure.h" |
| 33 | #include "ndnx-pco.h" |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 34 | #include "executor.h" |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 36 | namespace Ndnx { |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 38 | struct NdnxOperationException : boost::exception, std::exception { }; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 39 | |
Zhenkai Zhu | 746d444 | 2013-03-13 17:06:54 -0700 | [diff] [blame] | 40 | class Verifier; |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 41 | class NdnxWrapper |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 42 | { |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 43 | public: |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 44 | const static int MAX_FRESHNESS = 2147; // max value for ndnx |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 45 | const static int DEFAULT_FRESHNESS = 60; |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 46 | typedef boost::function<void (Name, Selectors)> InterestCallback; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 47 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 48 | NdnxWrapper(); |
| 49 | ~NdnxWrapper(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 51 | void |
| 52 | start (); // called automatically in constructor |
| 53 | |
| 54 | /** |
| 55 | * @brief Because of uncertainty with executor, in some case it is necessary to call shutdown explicitly (see test-server-and-fetch.cc) |
| 56 | */ |
| 57 | void |
| 58 | shutdown (); // called in destructor, but can called manually |
| 59 | |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 60 | int |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 61 | setInterestFilter (const Name &prefix, const InterestCallback &interestCallback, bool record = true); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 63 | void |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 64 | clearInterestFilter (const Name &prefix, bool record = true); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 66 | int |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 67 | sendInterest (const Name &interest, const Closure &closure, const Selectors &selector = Selectors()); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 69 | int |
Zhenkai Zhu | 589be8e | 2013-02-24 20:51:12 -0800 | [diff] [blame] | 70 | publishData (const Name &name, const unsigned char *buf, size_t len, int freshness = DEFAULT_FRESHNESS, const Name &keyName=Name()); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 71 | |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 72 | inline int |
Zhenkai Zhu | 589be8e | 2013-02-24 20:51:12 -0800 | [diff] [blame] | 73 | publishData (const Name &name, const Bytes &content, int freshness = DEFAULT_FRESHNESS, const Name &keyName=Name()); |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 75 | inline int |
Zhenkai Zhu | 589be8e | 2013-02-24 20:51:12 -0800 | [diff] [blame] | 76 | publishData (const Name &name, const std::string &content, int freshness = DEFAULT_FRESHNESS, const Name &keyName=Name()); |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 77 | |
| 78 | int |
| 79 | publishUnsignedData(const Name &name, const unsigned char *buf, size_t len, int freshness = DEFAULT_FRESHNESS); |
| 80 | |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 81 | inline int |
| 82 | publishUnsignedData(const Name &name, const Bytes &content, int freshness = DEFAULT_FRESHNESS); |
| 83 | |
| 84 | inline int |
| 85 | publishUnsignedData(const Name &name, const std::string &content, int freshness = DEFAULT_FRESHNESS); |
| 86 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 87 | static Name |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 88 | getLocalPrefix (); |
| 89 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 90 | Bytes |
Zhenkai Zhu | 704a658 | 2013-03-13 16:44:42 -0700 | [diff] [blame] | 91 | createContentObject(const Name &name, const void *buf, size_t len, int freshness = DEFAULT_FRESHNESS, const Name &keyNameParam=Name()); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 92 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 93 | int |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 94 | putToNdnd (const Bytes &contentObject); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 95 | |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame] | 96 | bool |
Zhenkai Zhu | 203dfd2 | 2013-03-13 21:35:48 -0700 | [diff] [blame] | 97 | verify(PcoPtr &pco, double maxWait = 1 /*seconds*/); |
Zhenkai Zhu | 746d444 | 2013-03-13 17:06:54 -0700 | [diff] [blame] | 98 | |
Jared Lindblom | f83ff94 | 2013-03-01 00:01:49 -0800 | [diff] [blame] | 99 | PcoPtr |
| 100 | get (const Name &interest, const Selectors &selector = Selectors(), double maxWait = 4.0/*seconds*/); |
| 101 | |
Zhenkai Zhu | 90ef9df | 2013-01-04 22:31:59 -0800 | [diff] [blame] | 102 | private: |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 103 | NdnxWrapper(const NdnxWrapper &other) {} |
Zhenkai Zhu | 90ef9df | 2013-01-04 22:31:59 -0800 | [diff] [blame] | 104 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 105 | protected: |
| 106 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 107 | connectNdnd(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 108 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 109 | /// @cond include_hidden |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 110 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 111 | ndnLoop (); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 112 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 113 | /// @endcond |
| 114 | |
| 115 | protected: |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 116 | typedef boost::shared_mutex Lock; |
| 117 | typedef boost::unique_lock<Lock> WriteLock; |
| 118 | typedef boost::shared_lock<Lock> ReadLock; |
| 119 | |
| 120 | typedef boost::recursive_mutex RecLock; |
| 121 | typedef boost::unique_lock<RecLock> UniqueRecLock; |
| 122 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 123 | ndn* m_handle; |
Zhenkai Zhu | 7f4258e | 2012-12-29 19:55:13 -0800 | [diff] [blame] | 124 | RecLock m_mutex; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 125 | boost::thread m_thread; |
| 126 | bool m_running; |
| 127 | bool m_connected; |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 128 | std::map<Name, InterestCallback> m_registeredInterests; |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 129 | ExecutorPtr m_executor; |
Zhenkai Zhu | 746d444 | 2013-03-13 17:06:54 -0700 | [diff] [blame] | 130 | Verifier *m_verifier; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 133 | typedef boost::shared_ptr<NdnxWrapper> NdnxWrapperPtr; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 135 | inline int |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 136 | NdnxWrapper::publishData (const Name &name, const Bytes &content, int freshness, const Name &keyName) |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 137 | { |
Zhenkai Zhu | 589be8e | 2013-02-24 20:51:12 -0800 | [diff] [blame] | 138 | return publishData(name, head(content), content.size(), freshness, keyName); |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | inline int |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 142 | NdnxWrapper::publishData (const Name &name, const std::string &content, int freshness, const Name &keyName) |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 143 | { |
Zhenkai Zhu | 589be8e | 2013-02-24 20:51:12 -0800 | [diff] [blame] | 144 | return publishData(name, reinterpret_cast<const unsigned char *> (content.c_str ()), content.size (), freshness, keyName); |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | inline int |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 148 | NdnxWrapper::publishUnsignedData(const Name &name, const Bytes &content, int freshness) |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 149 | { |
| 150 | return publishUnsignedData(name, head(content), content.size(), freshness); |
| 151 | } |
| 152 | |
| 153 | inline int |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 154 | NdnxWrapper::publishUnsignedData(const Name &name, const std::string &content, int freshness) |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 155 | { |
| 156 | return publishUnsignedData(name, reinterpret_cast<const unsigned char *> (content.c_str ()), content.size (), freshness); |
| 157 | } |
| 158 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 159 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 160 | } // Ndnx |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 161 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 162 | #endif |