blob: 4b9b3dd0d1a56c075933c3b2b4da6ea4fb9966e8 [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 Zhu7f4258e2012-12-29 19:55:13 -080013#include <boost/thread/locks.hpp>
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080014#include <boost/thread/recursive_mutex.hpp>
15#include <boost/thread/thread.hpp>
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080016
17#include "ccnx-common.h"
Zhenkai Zhu9bad2bf2012-12-28 15:31:46 -080018#include "ccnx-interest.h"
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080019#include "ccnx-closure.h"
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080020
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080021using namespace std;
22
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080023namespace Ccnx {
24
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080025struct CcnxOperationException : virtual boost::exception, virtual exception { };
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080026
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080027class CcnxWrapper
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080028{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080029public:
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080030 typedef boost::shared_mutex Lock;
31 typedef boost::unique_lock<Lock> WriteLock;
32 typedef boost::shared_lock<Lock> ReadLock;
33
34 typedef boost::recursive_mutex RecLock;
35 typedef boost::unique_lock<RecLock> UniqueRecLock;
36
Zhenkai Zhubad089c2012-12-28 10:28:27 -080037 typedef boost::function<void (const string &)> InterestCallback;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080038
39 CcnxWrapper();
40 virtual ~CcnxWrapper();
41
42 virtual int
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080043 setInterestFilter (const string &prefix, const InterestCallback &interestCallback);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080044
45 virtual void
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080046 clearInterestFilter (const string &prefix);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080047
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080048 virtual int
Zhenkai Zhu9bad2bf2012-12-28 15:31:46 -080049 sendInterest (const Interest &interest, Closure *closure);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080050
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080051 virtual int
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080052 publishData (const string &name, const unsigned char *buf, size_t len, int freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080053
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080054 int
55 publishData (const string &name, const Bytes &content, int freshness);
56
57 static string
58 getLocalPrefix ();
59
60 static string
Zhenkai Zhubad089c2012-12-28 10:28:27 -080061 extractName(const unsigned char *data, const ccn_indexbuf *comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080062
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080063 Bytes
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080064 createContentObject(const string &name, const unsigned char *buf, size_t len, int freshness = 2147/* max value for ccnx*/);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080065
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080066 int
Zhenkai Zhubad089c2012-12-28 10:28:27 -080067 putToCcnd (const Bytes &contentObject);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080068
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080069protected:
70 void
71 connectCcnd();
72
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080073 /// @cond include_hidden
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080074 void
75 createKeyLocator ();
76
77 void
78 initKeyStore ();
79
80 const ccn_pkey *
81 getPrivateKey ();
82
83 const unsigned char *
84 getPublicKeyDigest ();
85
86 ssize_t
87 getPublicKeyDigestLength ();
88
89 void
90 ccnLoop ();
91
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080092 /// @endcond
93
94protected:
95 ccn* m_handle;
96 ccn_keystore *m_keyStore;
97 ccn_charbuf *m_keyLoactor;
98 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080099 RecLock m_mutex;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800100 boost::thread m_thread;
101 bool m_running;
102 bool m_connected;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800103 map<string, InterestCallback> m_registeredInterests;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800104 // std::list< std::pair<std::string, InterestCallback> > m_registeredInterests;
105};
106
107typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
108
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800109
110} // Ccnx
111
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800112#endif