blob: 90089e7816b8e0928a7f00ebfd6d570fcf22ab74 [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
54protected:
55 Bytes
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080056 createContentObject(const string &name, const unsigned char *buf, size_t len, int freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080057
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080058 int
Zhenkai Zhubad089c2012-12-28 10:28:27 -080059 putToCcnd (const Bytes &contentObject);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080060
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080061protected:
62 void
63 connectCcnd();
64
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080065 /// @cond include_hidden
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080066 void
67 createKeyLocator ();
68
69 void
70 initKeyStore ();
71
72 const ccn_pkey *
73 getPrivateKey ();
74
75 const unsigned char *
76 getPublicKeyDigest ();
77
78 ssize_t
79 getPublicKeyDigestLength ();
80
81 void
82 ccnLoop ();
83
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080084 /// @endcond
85
86protected:
87 ccn* m_handle;
88 ccn_keystore *m_keyStore;
89 ccn_charbuf *m_keyLoactor;
90 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080091 RecLock m_mutex;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080092 boost::thread m_thread;
93 bool m_running;
94 bool m_connected;
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080095 map<string, InterestCallback> m_registeredInterests;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080096 // std::list< std::pair<std::string, InterestCallback> > m_registeredInterests;
97};
98
99typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
100
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800101
102} // Ccnx
103
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800104#endif