Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 1 | #ifndef CCNX_WRAPPER_H |
| 2 | #define CCNX_WRAPPER_H |
| 3 | |
| 4 | extern "C" { |
| 5 | #include <ccn/ccn.h> |
| 6 | #include <ccn/charbuf.h> |
| 7 | #include <ccn/keystore.h> |
| 8 | #include <ccn/uri.h> |
| 9 | #include <ccn/bloom.h> |
| 10 | #include <ccn/signing.h> |
| 11 | } |
| 12 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 13 | #include <vector> |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 14 | #include <boost/exception/all.hpp> |
| 15 | #include <boost/thread/recursive_mutex.hpp> |
| 16 | #include <boost/thread/thread.hpp> |
| 17 | #include <boost/function.hpp> |
| 18 | #include <string> |
| 19 | #include <sstream> |
| 20 | #include <map> |
| 21 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 22 | using namespace std; |
| 23 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 24 | namespace Ccnx { |
| 25 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 26 | typedef vector<unsigned char> Bytes; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 27 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 28 | void |
| 29 | readRaw(Bytes &bytes, const unsigned char *src, size_t len); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 30 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 31 | struct CcnxOperationException : virtual boost::exception, virtual exception { }; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 32 | |
| 33 | class CcnxWrapper { |
| 34 | public: |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 35 | typedef boost::function<void (string, Bytes)> DataCallback; |
| 36 | typedef boost::function<void (string)> InterestCallback; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 37 | typedef enum |
| 38 | { |
| 39 | RESULT_OK, |
| 40 | RESULT_REEXPRESS |
| 41 | } TimeoutCallbackReturnValue; |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 42 | typedef boost::function<TimeoutCallbackReturnValue (string)> TimeoutCallback; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 43 | |
| 44 | public: |
| 45 | |
| 46 | CcnxWrapper(); |
| 47 | virtual ~CcnxWrapper(); |
| 48 | |
| 49 | virtual int |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 50 | setInterestFilter (const string &prefix, const InterestCallback &interestCallback); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 51 | |
| 52 | virtual void |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 53 | clearInterestFilter (const string &prefix); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 54 | |
| 55 | virtual int |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 56 | sendInterest (const string &strInterest, const DataCallback &dataCallback, int retry = 0, const TimeoutCallback &timeoutCallback = TimeoutCallback()); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 57 | |
| 58 | virtual int |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 59 | publishData (const string &name, const char *buf, size_t len, int freshness); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 60 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 61 | int |
| 62 | publishData (const string &name, const Bytes &content, int freshness); |
| 63 | |
| 64 | static string |
| 65 | getLocalPrefix (); |
| 66 | |
| 67 | static string |
| 68 | extractName(const unsigned char *data, ccn_indexbuf *comps); |
| 69 | |
| 70 | protected: |
| 71 | Bytes |
| 72 | createContentObject(const string &name, const char *buf, size_t len, int freshness); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 73 | |
| 74 | int |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 75 | putToCcnd (Bytes &contentObject); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 76 | |
| 77 | protected: |
| 78 | void |
| 79 | connectCcnd(); |
| 80 | |
| 81 | /// @cond include_hidden |
| 82 | void |
| 83 | createKeyLocator (); |
| 84 | |
| 85 | void |
| 86 | initKeyStore (); |
| 87 | |
| 88 | const ccn_pkey * |
| 89 | getPrivateKey (); |
| 90 | |
| 91 | const unsigned char * |
| 92 | getPublicKeyDigest (); |
| 93 | |
| 94 | ssize_t |
| 95 | getPublicKeyDigestLength (); |
| 96 | |
| 97 | void |
| 98 | ccnLoop (); |
| 99 | |
| 100 | int |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 101 | sendInterest (const string &strInterest, void *dataPass); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 102 | /// @endcond |
| 103 | |
| 104 | protected: |
| 105 | ccn* m_handle; |
| 106 | ccn_keystore *m_keyStore; |
| 107 | ccn_charbuf *m_keyLoactor; |
| 108 | // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex); |
| 109 | boost::recursive_mutex m_mutex; |
| 110 | boost::thread m_thread; |
| 111 | bool m_running; |
| 112 | bool m_connected; |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 113 | map<string, InterestCallback> m_registeredInterests; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 114 | // std::list< std::pair<std::string, InterestCallback> > m_registeredInterests; |
| 115 | }; |
| 116 | |
| 117 | typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr; |
| 118 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 119 | class ClosurePass |
| 120 | { |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 121 | public: |
| 122 | ClosurePass(int retry, const CcnxWrapper::DataCallback &dataCallback, const CcnxWrapper::TimeoutCallback &timeoutCallback); |
| 123 | int getRetry() {return m_retry;} |
| 124 | void decRetry() { m_retry--;} |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame^] | 125 | virtual ~ClosurePass(); |
| 126 | virtual void runDataCallback(string name, const Bytes &content); |
| 127 | virtual CcnxWrapper::TimeoutCallbackReturnValue runTimeoutCallback(string interest); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 128 | |
| 129 | protected: |
| 130 | int m_retry; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 131 | CcnxWrapper::TimeoutCallback *m_timeoutCallback; |
| 132 | CcnxWrapper::DataCallback *m_dataCallback; |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | } // Ccnx |
| 137 | |
| 138 | #endif |