blob: c5f8b8dac06dce9f3603be6fd1c2881b87d13086 [file] [log] [blame]
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -08001#ifndef CCNX_WRAPPER_H
2#define CCNX_WRAPPER_H
3
4extern "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 Zhu43eb2732012-12-28 00:48:26 -080013#include <vector>
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080014#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 Zhu43eb2732012-12-28 00:48:26 -080022using namespace std;
23
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080024namespace Ccnx {
25
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080026typedef vector<unsigned char> Bytes;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080027
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080028void
29readRaw(Bytes &bytes, const unsigned char *src, size_t len);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080030
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080031struct CcnxOperationException : virtual boost::exception, virtual exception { };
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080032
33class CcnxWrapper {
34public:
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080035 typedef boost::function<void (string, Bytes)> DataCallback;
36 typedef boost::function<void (string)> InterestCallback;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080037 typedef enum
38 {
39 RESULT_OK,
40 RESULT_REEXPRESS
41 } TimeoutCallbackReturnValue;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080042 typedef boost::function<TimeoutCallbackReturnValue (string)> TimeoutCallback;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080043
44public:
45
46 CcnxWrapper();
47 virtual ~CcnxWrapper();
48
49 virtual int
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080050 setInterestFilter (const string &prefix, const InterestCallback &interestCallback);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080051
52 virtual void
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080053 clearInterestFilter (const string &prefix);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080054
55 virtual int
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080056 sendInterest (const string &strInterest, const DataCallback &dataCallback, int retry = 0, const TimeoutCallback &timeoutCallback = TimeoutCallback());
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080057
58 virtual int
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080059 publishData (const string &name, const char *buf, size_t len, int freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080060
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080061 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
70protected:
71 Bytes
72 createContentObject(const string &name, const char *buf, size_t len, int freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080073
74 int
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080075 putToCcnd (Bytes &contentObject);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080076
77protected:
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 Zhu43eb2732012-12-28 00:48:26 -0800101 sendInterest (const string &strInterest, void *dataPass);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800102 /// @endcond
103
104protected:
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 Zhu43eb2732012-12-28 00:48:26 -0800113 map<string, InterestCallback> m_registeredInterests;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800114 // std::list< std::pair<std::string, InterestCallback> > m_registeredInterests;
115};
116
117typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
118
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800119class ClosurePass
120{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800121public:
122 ClosurePass(int retry, const CcnxWrapper::DataCallback &dataCallback, const CcnxWrapper::TimeoutCallback &timeoutCallback);
123 int getRetry() {return m_retry;}
124 void decRetry() { m_retry--;}
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800125 virtual ~ClosurePass();
126 virtual void runDataCallback(string name, const Bytes &content);
127 virtual CcnxWrapper::TimeoutCallbackReturnValue runTimeoutCallback(string interest);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800128
129protected:
130 int m_retry;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800131 CcnxWrapper::TimeoutCallback *m_timeoutCallback;
132 CcnxWrapper::DataCallback *m_dataCallback;
133};
134
135
136} // Ccnx
137
138#endif