blob: 667d4be73cc0a49715ff4084534a7325dffd6e40 [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 Zhua6472e02013-01-06 14:33:37 -080010#include "ccnx-selectors.h"
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080011#include "ccnx-closure.h"
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080012
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080013using namespace std;
14
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080015namespace Ccnx {
16
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080017struct CcnxOperationException : virtual boost::exception, virtual exception { };
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080018
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080019class CcnxWrapper
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080020{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080021public:
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080022 typedef boost::shared_mutex Lock;
23 typedef boost::unique_lock<Lock> WriteLock;
24 typedef boost::shared_lock<Lock> ReadLock;
25
26 typedef boost::recursive_mutex RecLock;
27 typedef boost::unique_lock<RecLock> UniqueRecLock;
28
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080029 typedef boost::function<void (const Name &)> InterestCallback;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080030
31 CcnxWrapper();
32 virtual ~CcnxWrapper();
33
34 virtual int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080035 setInterestFilter (const Name &prefix, const InterestCallback &interestCallback);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080036
37 virtual void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080038 clearInterestFilter (const Name &prefix);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080039
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080040 virtual int
Zhenkai Zhu19f81de2013-01-04 22:27:47 -080041 sendInterest (const Name &interest, const Closure *closure, const Selectors &selector = Selectors());
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080042
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080043 virtual int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080044 publishData (const Name &name, const unsigned char *buf, size_t len, int freshness);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080045
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080046 int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080047 publishData (const Name &name, const Bytes &content, int freshness);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080048
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080049 static Name
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080050 getLocalPrefix ();
51
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080052 Bytes
Zhenkai Zhu82de6d22013-01-03 14:15:35 -080053 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 -080054
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080055 int
Zhenkai Zhubad089c2012-12-28 10:28:27 -080056 putToCcnd (const Bytes &contentObject);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080057
Zhenkai Zhu90ef9df2013-01-04 22:31:59 -080058private:
59 CcnxWrapper(const CcnxWrapper &other) {}
60
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
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080067 ccnLoop ();
68
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080069 /// @endcond
70
71protected:
72 ccn* m_handle;
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080073 RecLock m_mutex;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080074 boost::thread m_thread;
75 bool m_running;
76 bool m_connected;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080077 map<Name, InterestCallback> m_registeredInterests;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080078};
79
80typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
81
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080082
83} // Ccnx
84
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080085#endif