blob: 938b890cb6086bf915b884d15807cde96cd15c52 [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 Zhucb2d0dd2013-01-03 14:10:48 -080022 typedef boost::function<void (const Name &)> InterestCallback;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080023
24 CcnxWrapper();
25 virtual ~CcnxWrapper();
26
27 virtual int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080028 setInterestFilter (const Name &prefix, const InterestCallback &interestCallback);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080029
30 virtual void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080031 clearInterestFilter (const Name &prefix);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080032
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080033 virtual int
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080034 sendInterest (const Name &interest, const Closure &closure, const Selectors &selector = Selectors());
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080035
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080036 virtual int
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080037 publishData (const Name &name, const unsigned char *buf, size_t len, int freshness = 2147/* max value for ccnx*/);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080038
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080039 int
Alexander Afanasyev21a166e2013-01-20 16:04:41 -080040 publishData (const Name &name, const Bytes &content, int freshness = 2147/* max value for ccnx*/);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080041
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080042 static Name
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080043 getLocalPrefix ();
44
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080045 Bytes
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -080046 createContentObject(const Name &name, const void *buf, size_t len, int freshness = 2147/* max value for ccnx*/);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080047
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080048 int
Zhenkai Zhubad089c2012-12-28 10:28:27 -080049 putToCcnd (const Bytes &contentObject);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080050
Zhenkai Zhu90ef9df2013-01-04 22:31:59 -080051private:
52 CcnxWrapper(const CcnxWrapper &other) {}
53
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080054protected:
55 void
56 connectCcnd();
57
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080058 /// @cond include_hidden
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080059 void
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080060 ccnLoop ();
61
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080062 /// @endcond
63
64protected:
Alexander Afanasyev66f4c492013-01-20 23:32:50 -080065 typedef boost::shared_mutex Lock;
66 typedef boost::unique_lock<Lock> WriteLock;
67 typedef boost::shared_lock<Lock> ReadLock;
68
69 typedef boost::recursive_mutex RecLock;
70 typedef boost::unique_lock<RecLock> UniqueRecLock;
71
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080072 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