blob: ad18b050210b8962a7e64b25fd416b71f4346280 [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>
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -070041#include <map>
Chaoyi Bian93d43102012-03-07 14:28:56 -080042
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080043/**
44 * \defgroup sync SYNC protocol
45 *
46 * Implementation of SYNC protocol
47 */
48namespace Sync {
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080049
Zhenkai Zhu009ff792012-03-09 12:37:52 -080050struct CcnxOperationException : virtual boost::exception, virtual std::exception { };
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080051/**
52 * \ingroup sync
53 * @brief A wrapper for ccnx library; clients of this code do not need to deal
54 * with ccnx library
55 */
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080056class CcnxWrapper {
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080057public:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070058 typedef boost::function<void (std::string, std::string)> StringDataCallback;
Zhenkai Zhu80af0e02012-05-31 15:49:07 -070059 typedef boost::function<void (std::string, const char *buf, size_t len)> RawDataCallback;
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080060 typedef boost::function<void (std::string)> InterestCallback;
Alexander Afanasyev3aaea672012-10-05 15:25:11 -070061
62
63 static
64 CcnxWrapper *
65 Create ()
66 {
67 static CcnxWrapper *wrapper = new CcnxWrapper ();
68 return wrapper;
69 }
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080070
Alexander Afanasyev3aaea672012-10-05 15:25:11 -070071private:
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080072 /**
73 * @brief initialize the wrapper; a lot of things needs to be done. 1) init
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080074 * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080075 *
76 */
Alexander Afanasyev2247b302012-03-14 14:11:54 -070077#ifdef _DEBUG_WRAPPER_
78 CcnxWrapper(char c='.');
79 char m_c;
80#else
Alexander Afanasyev1285b382012-03-08 16:40:27 -080081 CcnxWrapper();
Alexander Afanasyev3aaea672012-10-05 15:25:11 -070082#endif
83public:
Alexander Afanasyev1285b382012-03-08 16:40:27 -080084 ~CcnxWrapper();
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080085
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080086 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080087 * @brief send Interest; need to grab lock m_mutex first
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080088 *
89 * @param strInterest the Interest name
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080090 * @param dataCallback the callback function to deal with the returned data
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080091 * @return the return code of ccn_express_interest
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080092 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080093 int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070094 sendInterestForString (const std::string &strInterest, const StringDataCallback &strDataCallback, int retry = 0);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -070095
96 int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070097 sendInterest (const std::string &strInterest, const RawDataCallback &rawDataCallback, int retry = 0);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080098
99 /**
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700100 * @brief set Interest filter (specify what interest you want to receive)
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800101 *
102 * @param prefix the prefix of Interest
103 * @param interestCallback the callback function to deal with the returned data
104 * @return the return code of ccn_set_interest_filter
105 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800106 int
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800107 setInterestFilter (const std::string &prefix, const InterestCallback &interestCallback);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800108
109 /**
Alexander Afanasyev1b449c42012-03-13 20:24:07 -0700110 * @brief clear Interest filter
111 * @param prefix the prefix of Interest
112 */
113 void
114 clearInterestFilter (const std::string &prefix);
115
116 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800117 * @brief publish data and put it to local ccn content store; need to grab
118 * lock m_mutex first
119 *
120 * @param name the name for the data object
121 * @param dataBuffer the data to be published
122 * @param freshness the freshness time for the data object
123 * @return code generated by ccnx library calls, >0 if success
124 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800125 int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700126 publishStringData (const std::string &name, const std::string &dataBuffer, int freshness);
127
128 int
129 publishRawData (const std::string &name, const char *buf, size_t len, int freshness);
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800130
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800131private:
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700132 void
133 connectCcnd();
134
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800135 /// @cond include_hidden
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800136 void
137 createKeyLocator ();
138
139 void
140 initKeyStore ();
141
142 const ccn_pkey *
143 getPrivateKey ();
144
145 const unsigned char *
146 getPublicKeyDigest ();
147
148 ssize_t
149 getPublicKeyDigestLength ();
150
151 void
152 ccnLoop ();
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700153
154 int
155 sendInterest (const std::string &strInterest, void *dataPass);
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800156 /// @endcond
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800157private:
158 ccn* m_handle;
159 ccn_keystore *m_keyStore;
160 ccn_charbuf *m_keyLoactor;
161 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
162 boost::recursive_mutex m_mutex;
163 boost::thread m_thread;
164 bool m_running;
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700165 bool m_connected;
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700166 std::map<std::string, InterestCallback> m_registeredInterests;
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700167 // std::list< std::pair<std::string, InterestCallback> > m_registeredInterests;
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800168};
169
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800170typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
171
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700172enum CallbackType { STRING_FORM, RAW_DATA};
173
174class ClosurePass {
175public:
Chaoyi Bian34938892012-06-13 16:47:43 -0700176 ClosurePass(CallbackType type, int retry): m_retry(retry), m_type(type) {}
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700177 int getRetry() {return m_retry;}
178 void decRetry() { m_retry--;}
179 CallbackType getCallbackType() {return m_type;}
180 virtual ~ClosurePass(){}
181 virtual void runCallback(std::string name, const char *data, size_t len) = 0;
182
183protected:
184 int m_retry;
185 CallbackType m_type;
186};
187
188class DataClosurePass: public ClosurePass {
189public:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700190 DataClosurePass(CallbackType type, int retry, const CcnxWrapper::StringDataCallback &strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700191 virtual ~DataClosurePass();
192 virtual void runCallback(std::string name, const char *, size_t len);
193private:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700194 CcnxWrapper::StringDataCallback * m_callback;
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700195};
196
197class RawDataClosurePass: public ClosurePass {
198public:
199 RawDataClosurePass(CallbackType type, int retry, const CcnxWrapper::RawDataCallback &RawDataCallback);
200 virtual ~RawDataClosurePass();
201 virtual void runCallback(std::string name, const char *, size_t len);
202private:
203 CcnxWrapper::RawDataCallback * m_callback;
204};
205
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800206} // Sync
207
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800208#endif // SYNC_CCNX_WRAPPER_H