blob: f0f8d8ca841f4101b2cf4a5e6c863e895d27ea7e [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
Alexander Afanasyev60126e02012-10-05 16:54:42 -070062public:
Alexander Afanasyev60126e02012-10-05 16:54:42 -070063
Alexander Afanasyev2247b302012-03-14 14:11:54 -070064#ifdef _DEBUG_WRAPPER_
65 CcnxWrapper(char c='.');
66 char m_c;
67#else
Alexander Afanasyev1285b382012-03-08 16:40:27 -080068 CcnxWrapper();
Alexander Afanasyev3aaea672012-10-05 15:25:11 -070069#endif
Alexander Afanasyev19d55ec2012-10-05 16:28:31 -070070
Alexander Afanasyev1285b382012-03-08 16:40:27 -080071 ~CcnxWrapper();
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080072
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080073 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080074 * @brief send Interest; need to grab lock m_mutex first
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080075 *
76 * @param strInterest the Interest name
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080077 * @param dataCallback the callback function to deal with the returned data
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080078 * @return the return code of ccn_express_interest
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080079 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080080 int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070081 sendInterestForString (const std::string &strInterest, const StringDataCallback &strDataCallback, int retry = 0);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -070082
83 int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -070084 sendInterest (const std::string &strInterest, const RawDataCallback &rawDataCallback, int retry = 0);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080085
86 /**
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070087 * @brief set Interest filter (specify what interest you want to receive)
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080088 *
89 * @param prefix the prefix of Interest
90 * @param interestCallback the callback function to deal with the returned data
91 * @return the return code of ccn_set_interest_filter
92 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080093 int
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080094 setInterestFilter (const std::string &prefix, const InterestCallback &interestCallback);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080095
96 /**
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070097 * @brief clear Interest filter
98 * @param prefix the prefix of Interest
99 */
100 void
101 clearInterestFilter (const std::string &prefix);
102
103 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800104 * @brief publish data and put it to local ccn content store; need to grab
105 * lock m_mutex first
106 *
107 * @param name the name for the data object
108 * @param dataBuffer the data to be published
109 * @param freshness the freshness time for the data object
110 * @return code generated by ccnx library calls, >0 if success
111 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800112 int
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700113 publishStringData (const std::string &name, const std::string &dataBuffer, int freshness);
114
115 int
116 publishRawData (const std::string &name, const char *buf, size_t len, int freshness);
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800117
Alexander Afanasyev50f7e9f2012-10-08 00:20:42 -0700118 std::string
119 getLocalPrefix ();
120
Zhenkai Zhu96974e02012-10-17 13:51:31 -0700121protected:
Zhenkai Zhu26cd6e32012-10-05 13:10:58 -0700122 void
123 connectCcnd();
124
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800125 /// @cond include_hidden
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800126 void
127 createKeyLocator ();
128
129 void
130 initKeyStore ();
131
132 const ccn_pkey *
133 getPrivateKey ();
134
135 const unsigned char *
136 getPublicKeyDigest ();
137
138 ssize_t
139 getPublicKeyDigestLength ();
140
141 void
142 ccnLoop ();
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700143
144 int
145 sendInterest (const std::string &strInterest, void *dataPass);
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800146 /// @endcond
Zhenkai Zhu96974e02012-10-17 13:51:31 -0700147protected:
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800148 ccn* m_handle;
149 ccn_keystore *m_keyStore;
150 ccn_charbuf *m_keyLoactor;
151 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
152 boost::recursive_mutex m_mutex;
153 boost::thread m_thread;
154 bool m_running;
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700155 bool m_connected;
Zhenkai Zhu71bc0ba2012-10-05 14:37:44 -0700156 std::map<std::string, InterestCallback> m_registeredInterests;
Alexander Afanasyev3a6c2252012-10-05 16:10:44 -0700157 // std::list< std::pair<std::string, InterestCallback> > m_registeredInterests;
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800158};
159
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800160typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
161
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700162enum CallbackType { STRING_FORM, RAW_DATA};
163
164class ClosurePass {
165public:
Chaoyi Bian34938892012-06-13 16:47:43 -0700166 ClosurePass(CallbackType type, int retry): m_retry(retry), m_type(type) {}
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700167 int getRetry() {return m_retry;}
168 void decRetry() { m_retry--;}
169 CallbackType getCallbackType() {return m_type;}
170 virtual ~ClosurePass(){}
171 virtual void runCallback(std::string name, const char *data, size_t len) = 0;
172
173protected:
174 int m_retry;
175 CallbackType m_type;
176};
177
178class DataClosurePass: public ClosurePass {
179public:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700180 DataClosurePass(CallbackType type, int retry, const CcnxWrapper::StringDataCallback &strDataCallback);
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700181 virtual ~DataClosurePass();
182 virtual void runCallback(std::string name, const char *, size_t len);
183private:
Zhenkai Zhu1cb29292012-05-31 22:54:34 -0700184 CcnxWrapper::StringDataCallback * m_callback;
Zhenkai Zhu80af0e02012-05-31 15:49:07 -0700185};
186
187class RawDataClosurePass: public ClosurePass {
188public:
189 RawDataClosurePass(CallbackType type, int retry, const CcnxWrapper::RawDataCallback &RawDataCallback);
190 virtual ~RawDataClosurePass();
191 virtual void runCallback(std::string name, const char *, size_t len);
192private:
193 CcnxWrapper::RawDataCallback * m_callback;
194};
195
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800196} // Sync
197
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800198#endif // SYNC_CCNX_WRAPPER_H