blob: a12848de6dd6ba8c22e8a389b0284b7699360372 [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>
32}
33
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080034#include <boost/thread/recursive_mutex.hpp>
35#include <boost/thread/thread.hpp>
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080036#include <boost/function.hpp>
Zhenkai Zhu774aa182012-03-05 19:54:38 -080037#include <string>
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080038#include "sync-data-buffer.h"
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080039/**
40 * \defgroup sync SYNC protocol
41 *
42 * Implementation of SYNC protocol
43 */
44namespace Sync {
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080045
46/**
47 * \ingroup sync
48 * @brief A wrapper for ccnx library; clients of this code do not need to deal
49 * with ccnx library
50 */
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080051class CcnxWrapper {
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080052private:
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080053 ccn* m_handle;
54 ccn_keystore *m_keyStore;
55 ccn_charbuf *m_keyLoactor;
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080056 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
57 boost::recursive_mutex m_mutex;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080058 boost::thread m_thread;
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080059
60private:
61 void createKeyLocator();
62 void initKeyStore();
63 const ccn_pkey *getPrivateKey();
64 const unsigned char *getPublicKeyDigest();
65 ssize_t getPublicKeyDigestLength();
66 void ccnLoop();
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080067 bool running;
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080068
69public:
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080070
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080071 /**
72 * @brief initialize the wrapper; a lot of things needs to be done. 1) init
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080073 * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080074 *
75 */
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080076 CcnxWrapper();
77 ~CcnxWrapper();
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080078
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080079 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080080 * @brief send Interest; need to grab lock m_mutex first
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080081 *
82 * @param strInterest the Interest name
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080083 * @param dataCallback the callback function to deal with the returned data
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080084 * @return the return code of ccn_express_interest
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080085 */
Zhenkai Zhu41064062012-03-05 23:49:07 -080086 int sendInterest(std::string strInterest, boost::function<void
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080087 (boost::shared_ptr<DataBuffer>)> dataCallback);
88
89 /**
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080090 * @brief set Interest filter (specify what interest you want to receive
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080091 *
92 * @param prefix the prefix of Interest
93 * @param interestCallback the callback function to deal with the returned data
94 * @return the return code of ccn_set_interest_filter
95 */
Zhenkai Zhub877e3f2012-03-06 14:02:44 -080096 int setInterestFilter(std::string prefix, boost::function<void (std::string)>
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080097 interestCallback);
98
99 /**
100 * @brief publish data and put it to local ccn content store; need to grab
101 * lock m_mutex first
102 *
103 * @param name the name for the data object
104 * @param dataBuffer the data to be published
105 * @param freshness the freshness time for the data object
106 * @return code generated by ccnx library calls, >0 if success
107 */
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800108 int publishData(std::string name, boost::shared_ptr<DataBuffer> dataBuffer,
109 int freshness);
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800110
111};
112
113} // Sync
114
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800115#endif // SYNC_CCNX_WRAPPER_H