blob: 575310b4ef78e1961ac299159833db2ca677b3ea [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
13#include <boost/exception/all.hpp>
14#include <boost/thread/recursive_mutex.hpp>
15#include <boost/thread/thread.hpp>
16#include <boost/function.hpp>
17#include <string>
18#include <sstream>
19#include <map>
20
21namespace Ccnx {
22
23struct CcnxOperationException : virtual boost::exception, virtual std::exception { };
24
25class CcnxWrapper;
26
27class ContentObject
28{
29public:
30 ContentObject(const char *data, size_t len);
31 ~ContentObject();
32 void
33 dup(char **data, size_t *len);
34
35// exposed! use cautiously at your own risk.
36public:
37 char *m_data;
38 size_t m_len;
39};
40
41typedef boost::shared_ptr<ContentObject> ContentObjectPtr;
42
43class CcnxWrapper {
44public:
45 typedef boost::function<void (std::string, const char *buf, size_t len)> DataCallback;
46 typedef boost::function<void (std::string)> InterestCallback;
47 typedef boost::function<TimeoutCallbackReturnValue (std::string)> TimeoutCallback;
48
49 typedef enum
50 {
51 RESULT_OK,
52 RESULT_REEXPRESS
53 } TimeoutCallbackReturnValue;
54
55public:
56
57 CcnxWrapper();
58 virtual ~CcnxWrapper();
59
60 virtual int
61 setInterestFilter (const std::string &prefix, const InterestCallback &interestCallback);
62
63 virtual void
64 clearInterestFilter (const std::string &prefix);
65
66 virtual int
67 sendInterest (const std::string &strInterest, const RawDataCallback &rawDataCallback, int retry = 0, const TimeoutCallback &timeoutCallback = TimeoutCallback());
68
69 virtual int
70 publishData (const std::string &name, const char *buf, size_t len, int freshness);
71
72 ContentObjectPtr
73 createContentObject(const std::string &name, const char *buf, size_t len, int freshness);
74
75 int
76 putToCcnd (ContentObjectPtr co);
77
78 static std::string
79 getInterestName(ccn_upcall_info *info);
80
81 static std::string
82 getDataName(ccn_upcall_info *info);
83
84 static int
85 getContentFromContentObject(ContentObjectPtr co, char **data, size_t *len);
86
87 static std::string
88 getLocalPrefix ();
89
90protected:
91 void
92 connectCcnd();
93
94 /// @cond include_hidden
95 void
96 createKeyLocator ();
97
98 void
99 initKeyStore ();
100
101 const ccn_pkey *
102 getPrivateKey ();
103
104 const unsigned char *
105 getPublicKeyDigest ();
106
107 ssize_t
108 getPublicKeyDigestLength ();
109
110 void
111 ccnLoop ();
112
113 int
114 sendInterest (const std::string &strInterest, void *dataPass);
115 /// @endcond
116
117protected:
118 ccn* m_handle;
119 ccn_keystore *m_keyStore;
120 ccn_charbuf *m_keyLoactor;
121 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
122 boost::recursive_mutex m_mutex;
123 boost::thread m_thread;
124 bool m_running;
125 bool m_connected;
126 std::map<std::string, InterestCallback> m_registeredInterests;
127 // std::list< std::pair<std::string, InterestCallback> > m_registeredInterests;
128};
129
130typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
131
132class ClosurePass {
133public:
134 ClosurePass(int retry, const CcnxWrapper::DataCallback &dataCallback, const CcnxWrapper::TimeoutCallback &timeoutCallback);
135 int getRetry() {return m_retry;}
136 void decRetry() { m_retry--;}
137 virtual ~ClosurePass(){}
138 virtual void runCallback(std::string name, const char *data, size_t len);
139 virtual CcnxWrapper::TimeoutCallbackReturnValue runTimeoutCallback(std::string interest);
140
141protected:
142 int m_retry;
143 CallbackType m_type;
144 CcnxWrapper::TimeoutCallback *m_timeoutCallback;
145 CcnxWrapper::DataCallback *m_dataCallback;
146};
147
148
149} // Ccnx
150
151#endif