blob: aeb0c3ae309c2d81ea7e8069ef876b94a6b9d393 [file] [log] [blame]
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -080023#ifndef SYNC_CCNX_WRAPPER_H
24#define SYNC_CCNX_WRAPPER_H
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080025
26extern "C" {
27#include <ccn/ccn.h>
28#include <ccn/charbuf.h>
29#include <ccn/keystore.h>
30#include <ccn/uri.h>
31#include <ccn/bloom.h>
Chaoyi Bian49d46752012-03-07 10:35:21 -080032#include <ccn/signing.h>
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080033}
34
Zhenkai Zhu009ff792012-03-09 12:37:52 -080035#include <boost/exception/all.hpp>
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080036#include <boost/thread/recursive_mutex.hpp>
37#include <boost/thread/thread.hpp>
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080038#include <boost/function.hpp>
Zhenkai Zhu774aa182012-03-05 19:54:38 -080039#include <string>
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070040#include <sstream>
Chaoyi Bian93d43102012-03-07 14:28:56 -080041
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080042/**
43 * \defgroup sync SYNC protocol
44 *
45 * Implementation of SYNC protocol
46 */
47namespace Sync {
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080048
Zhenkai Zhu009ff792012-03-09 12:37:52 -080049struct CcnxOperationException : virtual boost::exception, virtual std::exception { };
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080050/**
51 * \ingroup sync
52 * @brief A wrapper for ccnx library; clients of this code do not need to deal
53 * with ccnx library
54 */
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080055class CcnxWrapper {
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080056public:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070057 typedef boost::function<void (std::string, std::string)> StringDataCallback;
Zhenkai Zhu80af0e02012-05-31 15:49:07 -070058 typedef boost::function<void (std::string, const char *buf, size_t len)> RawDataCallback;
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080059 typedef boost::function<void (std::string)> InterestCallback;
60
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080061 /**
62 * @brief initialize the wrapper; a lot of things needs to be done. 1) init
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080063 * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080064 *
65 */
Alexander Afanasyev2247b302012-03-14 14:11:54 -070066#ifdef _DEBUG_WRAPPER_
67 CcnxWrapper(char c='.');
68 char m_c;
69#else
Alexander Afanasyev1285b382012-03-08 16:40:27 -080070 CcnxWrapper();
Alexander Afanasyev2247b302012-03-14 14:11:54 -070071#endif
Alexander Afanasyev1285b382012-03-08 16:40:27 -080072 ~CcnxWrapper();
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080073
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080074 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080075 * @brief send Interest; need to grab lock m_mutex first
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080076 *
77 * @param strInterest the Interest name
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080078 * @param dataCallback the callback function to deal with the returned data
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080079 * @return the return code of ccn_express_interest
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080080 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080081 int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070082 sendInterestForString (const std::string &strInterest, const StringDataCallback &strDataCallback, int retry = 0);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -070083
84 int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070085 sendInterest (const std::string &strInterest, const RawDataCallback &rawDataCallback, int retry = 0);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080086
87 /**
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070088 * @brief set Interest filter (specify what interest you want to receive)
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080089 *
90 * @param prefix the prefix of Interest
91 * @param interestCallback the callback function to deal with the returned data
92 * @return the return code of ccn_set_interest_filter
93 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080094 int
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080095 setInterestFilter (const std::string &prefix, const InterestCallback &interestCallback);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080096
97 /**
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070098 * @brief clear Interest filter
99 * @param prefix the prefix of Interest
100 */
101 void
102 clearInterestFilter (const std::string &prefix);
103
104 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800105 * @brief publish data and put it to local ccn content store; need to grab
106 * lock m_mutex first
107 *
108 * @param name the name for the data object
109 * @param dataBuffer the data to be published
110 * @param freshness the freshness time for the data object
111 * @return code generated by ccnx library calls, >0 if success
112 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800113 int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700114 publishStringData (const std::string &name, const std::string &dataBuffer, int freshness);
115
116 int
117 publishRawData (const std::string &name, const char *buf, size_t len, int freshness);
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800118
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800119private:
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800120 /// @cond include_hidden
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800121 void
122 createKeyLocator ();
123
124 void
125 initKeyStore ();
126
127 const ccn_pkey *
128 getPrivateKey ();
129
130 const unsigned char *
131 getPublicKeyDigest ();
132
133 ssize_t
134 getPublicKeyDigestLength ();
135
136 void
137 ccnLoop ();
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700138
139 int
140 sendInterest (const std::string &strInterest, void *dataPass);
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800141 /// @endcond
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800142private:
143 ccn* m_handle;
144 ccn_keystore *m_keyStore;
145 ccn_charbuf *m_keyLoactor;
146 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
147 boost::recursive_mutex m_mutex;
148 boost::thread m_thread;
149 bool m_running;
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800150};
151
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800152typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
153
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700154enum CallbackType { STRING_FORM, RAW_DATA};
155
156class ClosurePass {
157public:
Chaoyi Bian34938892012-06-13 16:47:43 -0700158 ClosurePass(CallbackType type, int retry): m_retry(retry), m_type(type) {}
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700159 int getRetry() {return m_retry;}
160 void decRetry() { m_retry--;}
161 CallbackType getCallbackType() {return m_type;}
162 virtual ~ClosurePass(){}
163 virtual void runCallback(std::string name, const char *data, size_t len) = 0;
164
165protected:
166 int m_retry;
167 CallbackType m_type;
168};
169
170class DataClosurePass: public ClosurePass {
171public:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700172 DataClosurePass(CallbackType type, int retry, const CcnxWrapper::StringDataCallback &strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700173 virtual ~DataClosurePass();
174 virtual void runCallback(std::string name, const char *, size_t len);
175private:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700176 CcnxWrapper::StringDataCallback * m_callback;
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700177};
178
179class RawDataClosurePass: public ClosurePass {
180public:
181 RawDataClosurePass(CallbackType type, int retry, const CcnxWrapper::RawDataCallback &RawDataCallback);
182 virtual ~RawDataClosurePass();
183 virtual void runCallback(std::string name, const char *, size_t len);
184private:
185 CcnxWrapper::RawDataCallback * m_callback;
186};
187
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800188} // Sync
189
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800190#endif // SYNC_CCNX_WRAPPER_H