blob: 9b50ae870358efd891fb43f045cf31d41bb991cf [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
63protected:
64 Bytes
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080065 createContentObject(const string &name, const unsigned char *buf, size_t len, int freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080066
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080067 int
Zhenkai Zhubad089c2012-12-28 10:28:27 -080068 putToCcnd (const Bytes &contentObject);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080069
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080070protected:
71 void
72 connectCcnd();
73
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080074 /// @cond include_hidden
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080075 void
76 createKeyLocator ();
77
78 void
79 initKeyStore ();
80
81 const ccn_pkey *
82 getPrivateKey ();
83
84 const unsigned char *
85 getPublicKeyDigest ();
86
87 ssize_t
88 getPublicKeyDigestLength ();
89
90 void
91 ccnLoop ();
92
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080093 /// @endcond
94
95protected:
96 ccn* m_handle;
97 ccn_keystore *m_keyStore;
98 ccn_charbuf *m_keyLoactor;
99 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -0800100 RecLock m_mutex;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800101 boost::thread m_thread;
102 bool m_running;
103 bool m_connected;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -0800104 map<string, InterestCallback> m_registeredInterests;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800105 // std::list< std::pair<std::string, InterestCallback> > m_registeredInterests;
106};
107
108typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
109
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800110
111} // Ccnx
112
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800113#endif