blob: 4801bc225328e739ca20c335a4bf00200034ec4c [file] [log] [blame]
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -08001#ifndef CCNX_WRAPPER_H
2#define CCNX_WRAPPER_H
3
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -08004#include <boost/thread/locks.hpp>
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -08005#include <boost/thread/recursive_mutex.hpp>
6#include <boost/thread/thread.hpp>
Zhenkai Zhu974c5a62012-12-28 14:15:30 -08007
8#include "ccnx-common.h"
Zhenkai Zhuf47109b2013-01-02 19:41:34 -08009#include "ccnx-name.h"
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080010#include "ccnx-closure.h"
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080011
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080012using namespace std;
13
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080014namespace Ccnx {
15
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080016struct CcnxOperationException : virtual boost::exception, virtual exception { };
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080017
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080018class CcnxWrapper
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080019{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080020public:
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080021 typedef boost::shared_mutex Lock;
22 typedef boost::unique_lock<Lock> WriteLock;
23 typedef boost::shared_lock<Lock> ReadLock;
24
25 typedef boost::recursive_mutex RecLock;
26 typedef boost::unique_lock<RecLock> UniqueRecLock;
27
Zhenkai Zhubad089c2012-12-28 10:28:27 -080028 typedef boost::function<void (const string &)> InterestCallback;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080029
30 CcnxWrapper();
31 virtual ~CcnxWrapper();
32
33 virtual int
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080034 setInterestFilter (const string &prefix, const InterestCallback &interestCallback);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080035
36 virtual void
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080037 clearInterestFilter (const string &prefix);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080038
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080039 virtual int
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080040 sendInterest (const string &interest, Closure *closure);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080041
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080042 virtual int
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080043 publishData (const string &name, const unsigned char *buf, size_t len, int freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080044
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080045 int
46 publishData (const string &name, const Bytes &content, int freshness);
47
48 static string
49 getLocalPrefix ();
50
51 static string
Zhenkai Zhubad089c2012-12-28 10:28:27 -080052 extractName(const unsigned char *data, const ccn_indexbuf *comps);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080053
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080054 Bytes
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080055 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 -080056
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080057 int
Zhenkai Zhubad089c2012-12-28 10:28:27 -080058 putToCcnd (const Bytes &contentObject);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080059
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080060protected:
61 void
62 connectCcnd();
63
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080064 /// @cond include_hidden
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080065 void
66 createKeyLocator ();
67
68 void
69 initKeyStore ();
70
71 const ccn_pkey *
72 getPrivateKey ();
73
74 const unsigned char *
75 getPublicKeyDigest ();
76
77 ssize_t
78 getPublicKeyDigestLength ();
79
80 void
81 ccnLoop ();
82
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080083 /// @endcond
84
85protected:
86 ccn* m_handle;
87 ccn_keystore *m_keyStore;
88 ccn_charbuf *m_keyLoactor;
89 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080090 RecLock m_mutex;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080091 boost::thread m_thread;
92 bool m_running;
93 bool m_connected;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080094 map<string, InterestCallback> m_registeredInterests;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080095 // std::list< std::pair<std::string, InterestCallback> > m_registeredInterests;
96};
97
98typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
99
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800100
101} // Ccnx
102
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800103#endif