blob: dd7d762e25c3a3148655d463c1de5003df12d9f4 [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 Zhucb2d0dd2013-01-03 14:10:48 -080028 typedef boost::function<void (const Name &)> InterestCallback;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080029
30 CcnxWrapper();
31 virtual ~CcnxWrapper();
32
33 virtual int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080034 setInterestFilter (const Name &prefix, const InterestCallback &interestCallback);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080035
36 virtual void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080037 clearInterestFilter (const Name &prefix);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080038
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080039 virtual int
Zhenkai Zhu19f81de2013-01-04 22:27:47 -080040 sendInterest (const Name &interest, const Closure *closure, const Selectors &selector = Selectors());
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080041
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080042 virtual int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080043 publishData (const Name &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
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080046 publishData (const Name &name, const Bytes &content, int freshness);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080047
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080048 static Name
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080049 getLocalPrefix ();
50
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080051 Bytes
Zhenkai Zhu82de6d22013-01-03 14:15:35 -080052 createContentObject(const Name &name, const unsigned char *buf, size_t len, int freshness = 2147/* max value for ccnx*/);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080053
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080054 int
Zhenkai Zhubad089c2012-12-28 10:28:27 -080055 putToCcnd (const Bytes &contentObject);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080056
Zhenkai Zhu90ef9df2013-01-04 22:31:59 -080057private:
58 CcnxWrapper(const CcnxWrapper &other) {}
59
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
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080066 ccnLoop ();
67
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080068 /// @endcond
69
70protected:
71 ccn* m_handle;
72 ccn_keystore *m_keyStore;
73 ccn_charbuf *m_keyLoactor;
74 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080075 RecLock m_mutex;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080076 boost::thread m_thread;
77 bool m_running;
78 bool m_connected;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080079 map<Name, InterestCallback> m_registeredInterests;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080080};
81
82typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
83
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080084
85} // Ccnx
86
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080087#endif