blob: ad8ae9a201b9a1fc6228b32883efcc381e4a2be3 [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>
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080036#include <boost/shared_ptr.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 */
51class CcnxWrapper {
52private:
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;
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080058 boost::shared_ptr<boos::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();
67
68public:
69
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080070 /**
71 * @brief initialize the wrapper; a lot of things needs to be done. 1) init
72 * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run
73 *
74 */
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080075 CcnxWrapper();
76 ~CcnxWrapper();
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080077
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080078 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080079 * @brief send Interest; need to grab lock m_mutex first
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080080 *
81 * @param strInterest the Interest name
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080082 * @param dataCallback the callback function to deal with the returned data
83 * @return the return code of ccn_express_interest
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080084 */
Zhenkai Zhu41064062012-03-05 23:49:07 -080085 int sendInterest(std::string strInterest, boost::function<void
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080086 (boost::shared_ptr<DataBuffer>)> dataCallback);
87
88 /**
89 * @brief set Interest filter (specify what interest you want to receive
90 *
91 * @param prefix the prefix of Interest
92 * @param interestCallback the callback function to deal with the returned data
93 * @return the return code of ccn_set_interest_filter
94 */
Zhenkai Zhub877e3f2012-03-06 14:02:44 -080095 int setInterestFilter(std::string prefix, boost::function<void (std::string)>
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080096 interestCallback);
97
98 /**
99 * @brief publish data and put it to local ccn content store; need to grab
100 * lock m_mutex first
101 *
102 * @param name the name for the data object
103 * @param dataBuffer the data to be published
104 * @param freshness the freshness time for the data object
105 * @return code generated by ccnx library calls, >0 if success
106 */
Zhenkai Zhu8d935c82012-03-06 10:44:12 -0800107 int publishData(std::string name, boost::shared_ptr<DataBuffer> dataBuffer,
108 int freshness);
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800109
110};
111
112} // Sync
113
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800114#endif // SYNC_CCNX_WRAPPER_H