blob: 84893d8f0205f234ed225300e1d2fece88b4c093 [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>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * 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 Zhu46b26a12012-03-06 13:51:16 -080035#include <boost/thread/recursive_mutex.hpp>
36#include <boost/thread/thread.hpp>
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080037#include <boost/function.hpp>
Zhenkai Zhu774aa182012-03-05 19:54:38 -080038#include <string>
Chaoyi Bian93d43102012-03-07 14:28:56 -080039
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080040/**
41 * \defgroup sync SYNC protocol
42 *
43 * Implementation of SYNC protocol
44 */
45namespace Sync {
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080046
47/**
48 * \ingroup sync
49 * @brief A wrapper for ccnx library; clients of this code do not need to deal
50 * with ccnx library
51 */
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080052class CcnxWrapper {
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080053public:
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080054 typedef boost::function<void (std::string, std::string)> DataCallback;
55 typedef boost::function<void (std::string)> InterestCallback;
56
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080057 /**
58 * @brief initialize the wrapper; a lot of things needs to be done. 1) init
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080059 * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080060 *
61 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080062 CcnxWrapper();
63 ~CcnxWrapper();
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080064
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080065 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080066 * @brief send Interest; need to grab lock m_mutex first
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080067 *
68 * @param strInterest the Interest name
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080069 * @param dataCallback the callback function to deal with the returned data
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080070 * @return the return code of ccn_express_interest
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080071 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080072 int
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080073 sendInterest (const std::string &strInterest, const DataCallback &dataCallback);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080074
75 /**
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080076 * @brief set Interest filter (specify what interest you want to receive
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080077 *
78 * @param prefix the prefix of Interest
79 * @param interestCallback the callback function to deal with the returned data
80 * @return the return code of ccn_set_interest_filter
81 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080082 int
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080083 setInterestFilter (const std::string &prefix, const InterestCallback &interestCallback);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080084
85 /**
86 * @brief publish data and put it to local ccn content store; need to grab
87 * lock m_mutex first
88 *
89 * @param name the name for the data object
90 * @param dataBuffer the data to be published
91 * @param freshness the freshness time for the data object
92 * @return code generated by ccnx library calls, >0 if success
93 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080094 int
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080095 publishData (const std::string &name, const std::string &dataBuffer, int freshness);
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080096
Alexander Afanasyev1285b382012-03-08 16:40:27 -080097private:
Alexander Afanasyevc1030192012-03-08 22:21:28 -080098 /// @cond include_hidden
Alexander Afanasyev1285b382012-03-08 16:40:27 -080099 void
100 createKeyLocator ();
101
102 void
103 initKeyStore ();
104
105 const ccn_pkey *
106 getPrivateKey ();
107
108 const unsigned char *
109 getPublicKeyDigest ();
110
111 ssize_t
112 getPublicKeyDigestLength ();
113
114 void
115 ccnLoop ();
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800116 /// @endcond
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800117private:
118 ccn* m_handle;
119 ccn_keystore *m_keyStore;
120 ccn_charbuf *m_keyLoactor;
121 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
122 boost::recursive_mutex m_mutex;
123 boost::thread m_thread;
124 bool m_running;
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800125};
126
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800127typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
128
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800129} // Sync
130
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800131#endif // SYNC_CCNX_WRAPPER_H