blob: 5367fbbf83c9ff3b299fde7f147e2e67c7ef3f42 [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
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080022#ifndef CCNX_WRAPPER_H
23#define CCNX_WRAPPER_H
24
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
29#include "ccnx-common.h"
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080030#include "ccnx-name.h"
Zhenkai Zhua6472e02013-01-06 14:33:37 -080031#include "ccnx-selectors.h"
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080032#include "ccnx-closure.h"
Alexander Afanasyevf278db32013-01-21 14:41:01 -080033#include "ccnx-pco.h"
Zhenkai Zhu1888f742013-01-28 12:47:33 -080034#include "executor.h"
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080035
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080036namespace Ccnx {
37
Alexander Afanasyev923f2f12013-02-23 16:36:31 -080038struct CcnxOperationException : boost::exception, std::exception { };
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080039
Zhenkai Zhu746d4442013-03-13 17:06:54 -070040class Verifier;
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080041class CcnxWrapper
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080042{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080043public:
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080044 const static int MAX_FRESHNESS = 2147; // max value for ccnx
45 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
48 CcnxWrapper();
Alexander Afanasyev923f2f12013-02-23 16:36:31 -080049 ~CcnxWrapper();
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
Zhenkai Zhubad089c2012-12-28 10:28:27 -080094 putToCcnd (const Bytes &contentObject);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080095
Zhenkai Zhu79264a42013-02-07 21:49:42 -080096 bool
Zhenkai Zhudd1f14d2013-03-13 12:04:28 -070097 checkPcoIntegrity(PcoPtr &pco);
Zhenkai Zhu79264a42013-02-07 21:49:42 -080098
Zhenkai Zhu746d4442013-03-13 17:06:54 -070099 bool
100 verifyKey(PcoPtr &pco);
101
Jared Lindblomf83ff942013-03-01 00:01:49 -0800102 PcoPtr
103 get (const Name &interest, const Selectors &selector = Selectors(), double maxWait = 4.0/*seconds*/);
104
Zhenkai Zhu90ef9df2013-01-04 22:31:59 -0800105private:
106 CcnxWrapper(const CcnxWrapper &other) {}
107
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800108protected:
109 void
110 connectCcnd();
111
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800112 /// @cond include_hidden
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800113 void
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800114 ccnLoop ();
115
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800116 /// @endcond
117
118protected:
Alexander Afanasyev66f4c492013-01-20 23:32:50 -0800119 typedef boost::shared_mutex Lock;
120 typedef boost::unique_lock<Lock> WriteLock;
121 typedef boost::shared_lock<Lock> ReadLock;
122
123 typedef boost::recursive_mutex RecLock;
124 typedef boost::unique_lock<RecLock> UniqueRecLock;
125
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800126 ccn* m_handle;
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -0800127 RecLock m_mutex;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800128 boost::thread m_thread;
129 bool m_running;
130 bool m_connected;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800131 std::map<Name, InterestCallback> m_registeredInterests;
Zhenkai Zhu1888f742013-01-28 12:47:33 -0800132 ExecutorPtr m_executor;
Zhenkai Zhu746d4442013-03-13 17:06:54 -0700133 Verifier *m_verifier;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800134};
135
136typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
137
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800138inline int
Zhenkai Zhu589be8e2013-02-24 20:51:12 -0800139CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness, const Name &keyName)
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800140{
Zhenkai Zhu589be8e2013-02-24 20:51:12 -0800141 return publishData(name, head(content), content.size(), freshness, keyName);
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800142}
143
144inline int
Zhenkai Zhu589be8e2013-02-24 20:51:12 -0800145CcnxWrapper::publishData (const Name &name, const std::string &content, int freshness, const Name &keyName)
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800146{
Zhenkai Zhu589be8e2013-02-24 20:51:12 -0800147 return publishData(name, reinterpret_cast<const unsigned char *> (content.c_str ()), content.size (), freshness, keyName);
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800148}
149
150inline int
151CcnxWrapper::publishUnsignedData(const Name &name, const Bytes &content, int freshness)
152{
153 return publishUnsignedData(name, head(content), content.size(), freshness);
154}
155
156inline int
157CcnxWrapper::publishUnsignedData(const Name &name, const std::string &content, int freshness)
158{
159 return publishUnsignedData(name, reinterpret_cast<const unsigned char *> (content.c_str ()), content.size (), freshness);
160}
161
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800162
163} // Ccnx
164
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800165#endif