blob: 9da54f65f457174791559aaa4e00b98e13c5cba3 [file] [log] [blame]
Alexander Afanasyevf278db32013-01-21 14:41:01 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070022#ifndef NDNX_WRAPPER_H
23#define NDNX_WRAPPER_H
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080024
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080025#include <boost/thread/locks.hpp>
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080026#include <boost/thread/recursive_mutex.hpp>
27#include <boost/thread/thread.hpp>
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080028
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070029#include "ndnx-common.h"
30#include "ndnx-name.h"
31#include "ndnx-selectors.h"
32#include "ndnx-closure.h"
33#include "ndnx-pco.h"
Zhenkai Zhu1888f742013-01-28 12:47:33 -080034#include "executor.h"
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080035
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070036namespace Ndnx {
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080037
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070038struct NdnxOperationException : boost::exception, std::exception { };
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080039
Zhenkai Zhu746d4442013-03-13 17:06:54 -070040class Verifier;
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070041class NdnxWrapper
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080042{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080043public:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070044 const static int MAX_FRESHNESS = 2147; // max value for ndnx
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080045 const static int DEFAULT_FRESHNESS = 60;
Zhenkai Zhuff4fa8a2013-01-28 22:02:40 -080046 typedef boost::function<void (Name, Selectors)> InterestCallback;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080047
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070048 NdnxWrapper();
49 ~NdnxWrapper();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080050
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080051 void
52 start (); // called automatically in constructor
53
54 /**
55 * @brief Because of uncertainty with executor, in some case it is necessary to call shutdown explicitly (see test-server-and-fetch.cc)
56 */
57 void
58 shutdown (); // called in destructor, but can called manually
59
Alexander Afanasyev923f2f12013-02-23 16:36:31 -080060 int
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -080061 setInterestFilter (const Name &prefix, const InterestCallback &interestCallback, bool record = true);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080062
Alexander Afanasyev923f2f12013-02-23 16:36:31 -080063 void
Alexander Afanasyev17ae9e32013-02-19 18:09:08 -080064 clearInterestFilter (const Name &prefix, bool record = true);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080065
Alexander Afanasyev923f2f12013-02-23 16:36:31 -080066 int
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080067 sendInterest (const Name &interest, const Closure &closure, const Selectors &selector = Selectors());
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080068
Alexander Afanasyev923f2f12013-02-23 16:36:31 -080069 int
Zhenkai Zhu589be8e2013-02-24 20:51:12 -080070 publishData (const Name &name, const unsigned char *buf, size_t len, int freshness = DEFAULT_FRESHNESS, const Name &keyName=Name());
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080071
Alexander Afanasyev923f2f12013-02-23 16:36:31 -080072 inline int
Zhenkai Zhu589be8e2013-02-24 20:51:12 -080073 publishData (const Name &name, const Bytes &content, int freshness = DEFAULT_FRESHNESS, const Name &keyName=Name());
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080074
Alexander Afanasyev923f2f12013-02-23 16:36:31 -080075 inline int
Zhenkai Zhu589be8e2013-02-24 20:51:12 -080076 publishData (const Name &name, const std::string &content, int freshness = DEFAULT_FRESHNESS, const Name &keyName=Name());
Zhenkai Zhu5acebd72013-02-07 19:59:25 -080077
78 int
79 publishUnsignedData(const Name &name, const unsigned char *buf, size_t len, int freshness = DEFAULT_FRESHNESS);
80
Alexander Afanasyev923f2f12013-02-23 16:36:31 -080081 inline int
82 publishUnsignedData(const Name &name, const Bytes &content, int freshness = DEFAULT_FRESHNESS);
83
84 inline int
85 publishUnsignedData(const Name &name, const std::string &content, int freshness = DEFAULT_FRESHNESS);
86
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080087 static Name
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080088 getLocalPrefix ();
89
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080090 Bytes
Zhenkai Zhu704a6582013-03-13 16:44:42 -070091 createContentObject(const Name &name, const void *buf, size_t len, int freshness = DEFAULT_FRESHNESS, const Name &keyNameParam=Name());
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080092
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080093 int
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070094 putToNdnd (const Bytes &contentObject);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080095
Zhenkai Zhu79264a42013-02-07 21:49:42 -080096 bool
Zhenkai Zhu203dfd22013-03-13 21:35:48 -070097 verify(PcoPtr &pco, double maxWait = 1 /*seconds*/);
Zhenkai Zhu746d4442013-03-13 17:06:54 -070098
Jared Lindblomf83ff942013-03-01 00:01:49 -080099 PcoPtr
100 get (const Name &interest, const Selectors &selector = Selectors(), double maxWait = 4.0/*seconds*/);
101
Zhenkai Zhu90ef9df2013-01-04 22:31:59 -0800102private:
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700103 NdnxWrapper(const NdnxWrapper &other) {}
Zhenkai Zhu90ef9df2013-01-04 22:31:59 -0800104
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800105protected:
106 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700107 connectNdnd();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800108
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800109 /// @cond include_hidden
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800110 void
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700111 ndnLoop ();
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800112
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800113 /// @endcond
114
115protected:
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800116 typedef boost::shared_mutex Lock;
117 typedef boost::unique_lock<Lock> WriteLock;
118 typedef boost::shared_lock<Lock> ReadLock;
119
120 typedef boost::recursive_mutex RecLock;
121 typedef boost::unique_lock<RecLock> UniqueRecLock;
122
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700123 ndn* m_handle;
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -0800124 RecLock m_mutex;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800125 boost::thread m_thread;
126 bool m_running;
127 bool m_connected;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800128 std::map<Name, InterestCallback> m_registeredInterests;
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800129 ExecutorPtr m_executor;
Zhenkai Zhu746d4442013-03-13 17:06:54 -0700130 Verifier *m_verifier;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800131};
132
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700133typedef boost::shared_ptr<NdnxWrapper> NdnxWrapperPtr;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800134
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800135inline int
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700136NdnxWrapper::publishData (const Name &name, const Bytes &content, int freshness, const Name &keyName)
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800137{
Zhenkai Zhu589be8e2013-02-24 20:51:12 -0800138 return publishData(name, head(content), content.size(), freshness, keyName);
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800139}
140
141inline int
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700142NdnxWrapper::publishData (const Name &name, const std::string &content, int freshness, const Name &keyName)
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800143{
Zhenkai Zhu589be8e2013-02-24 20:51:12 -0800144 return publishData(name, reinterpret_cast<const unsigned char *> (content.c_str ()), content.size (), freshness, keyName);
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800145}
146
147inline int
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700148NdnxWrapper::publishUnsignedData(const Name &name, const Bytes &content, int freshness)
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800149{
150 return publishUnsignedData(name, head(content), content.size(), freshness);
151}
152
153inline int
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700154NdnxWrapper::publishUnsignedData(const Name &name, const std::string &content, int freshness)
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800155{
156 return publishUnsignedData(name, reinterpret_cast<const unsigned char *> (content.c_str ()), content.size (), freshness);
157}
158
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800159
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700160} // Ndnx
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800161
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800162#endif