blob: 77246ced4a8f74943fa8bdedf0707e0e0044b495 [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 Zhucb2d0dd2013-01-03 14:10:48 -080040 sendInterest (const Name &interest, 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 -080051protected:
52 Bytes
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080053 createContentObject(const Name &name, const unsigned char *buf, size_t len, int freshness);
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 Zhu1ddeb6f2012-12-27 14:04:18 -080058protected:
59 void
60 connectCcnd();
61
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080062 /// @cond include_hidden
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080063 void
64 createKeyLocator ();
65
66 void
67 initKeyStore ();
68
69 const ccn_pkey *
70 getPrivateKey ();
71
72 const unsigned char *
73 getPublicKeyDigest ();
74
75 ssize_t
76 getPublicKeyDigestLength ();
77
78 void
79 ccnLoop ();
80
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080081 /// @endcond
82
83protected:
84 ccn* m_handle;
85 ccn_keystore *m_keyStore;
86 ccn_charbuf *m_keyLoactor;
87 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080088 RecLock m_mutex;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080089 boost::thread m_thread;
90 bool m_running;
91 bool m_connected;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080092 map<Name, InterestCallback> m_registeredInterests;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080093};
94
95typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
96
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080097
98} // Ccnx
99
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800100#endif