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 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 22 | #ifndef CCNX_WRAPPER_H |
| 23 | #define CCNX_WRAPPER_H |
| 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 | |
| 29 | #include "ccnx-common.h" |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 30 | #include "ccnx-name.h" |
Zhenkai Zhu | a6472e0 | 2013-01-06 14:33:37 -0800 | [diff] [blame] | 31 | #include "ccnx-selectors.h" |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 32 | #include "ccnx-closure.h" |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 33 | #include "ccnx-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 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 36 | namespace Ccnx { |
| 37 | |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 38 | struct CcnxOperationException : virtual boost::exception, virtual std::exception { }; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 39 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 40 | class CcnxWrapper |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 41 | { |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 42 | public: |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 43 | const static int MAX_FRESHNESS = 2147; // max value for ccnx |
| 44 | const static int DEFAULT_FRESHNESS = 60; |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 45 | typedef boost::function<void (Name, Selectors)> InterestCallback; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 46 | |
| 47 | CcnxWrapper(); |
| 48 | virtual ~CcnxWrapper(); |
| 49 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 50 | void |
| 51 | start (); // called automatically in constructor |
| 52 | |
| 53 | /** |
| 54 | * @brief Because of uncertainty with executor, in some case it is necessary to call shutdown explicitly (see test-server-and-fetch.cc) |
| 55 | */ |
| 56 | void |
| 57 | shutdown (); // called in destructor, but can called manually |
| 58 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 59 | virtual int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 60 | setInterestFilter (const Name &prefix, const InterestCallback &interestCallback); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 61 | |
| 62 | virtual void |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 63 | clearInterestFilter (const Name &prefix); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 64 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 65 | virtual int |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 66 | sendInterest (const Name &interest, const Closure &closure, const Selectors &selector = Selectors()); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 67 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 68 | virtual int |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 69 | publishData (const Name &name, const unsigned char *buf, size_t len, int freshness = DEFAULT_FRESHNESS/* max value for ccnx*/); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 70 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 71 | int |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 72 | publishData (const Name &name, const Bytes &content, int freshness = DEFAULT_FRESHNESS/* max value for ccnx*/); |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 73 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 74 | int |
| 75 | publishUnsignedData(const Name &name, const Bytes &content, int freshness = DEFAULT_FRESHNESS); |
| 76 | |
| 77 | int |
| 78 | publishUnsignedData(const Name &name, const unsigned char *buf, size_t len, int freshness = DEFAULT_FRESHNESS); |
| 79 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 80 | static Name |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 81 | getLocalPrefix (); |
| 82 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 83 | Bytes |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 84 | createContentObject(const Name &name, const void *buf, size_t len, int freshness = DEFAULT_FRESHNESS/* max value for ccnx*/); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 85 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 86 | int |
Zhenkai Zhu | bad089c | 2012-12-28 10:28:27 -0800 | [diff] [blame] | 87 | putToCcnd (const Bytes &contentObject); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 88 | |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame^] | 89 | bool |
| 90 | verifyPco(PcoPtr &pco); |
| 91 | |
Zhenkai Zhu | 90ef9df | 2013-01-04 22:31:59 -0800 | [diff] [blame] | 92 | private: |
| 93 | CcnxWrapper(const CcnxWrapper &other) {} |
| 94 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 95 | protected: |
| 96 | void |
| 97 | connectCcnd(); |
| 98 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 99 | /// @cond include_hidden |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 100 | void |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 101 | ccnLoop (); |
| 102 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 103 | /// @endcond |
| 104 | |
| 105 | protected: |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 106 | typedef boost::shared_mutex Lock; |
| 107 | typedef boost::unique_lock<Lock> WriteLock; |
| 108 | typedef boost::shared_lock<Lock> ReadLock; |
| 109 | |
| 110 | typedef boost::recursive_mutex RecLock; |
| 111 | typedef boost::unique_lock<RecLock> UniqueRecLock; |
| 112 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 113 | ccn* m_handle; |
Zhenkai Zhu | 7f4258e | 2012-12-29 19:55:13 -0800 | [diff] [blame] | 114 | RecLock m_mutex; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 115 | boost::thread m_thread; |
| 116 | bool m_running; |
| 117 | bool m_connected; |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 118 | std::map<Name, InterestCallback> m_registeredInterests; |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 119 | ExecutorPtr m_executor; |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 120 | ccn_keystore *m_keystore; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr; |
| 124 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 125 | |
| 126 | } // Ccnx |
| 127 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 128 | #endif |