Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 23 | #ifndef SYNC_STATE_H |
| 24 | #define SYNC_STATE_H |
| 25 | |
| 26 | extern "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 Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 34 | #include <boost/thread/recursive_mutex.hpp> |
| 35 | #include <boost/thread/thread.hpp> |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 36 | #include <boost/shared_ptr.hpp> |
Zhenkai Zhu | 774aa18 | 2012-03-05 19:54:38 -0800 | [diff] [blame] | 37 | #include <string> |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 38 | #include "sync-data-buffer.h" |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 39 | /** |
| 40 | * \defgroup sync SYNC protocol |
| 41 | * |
| 42 | * Implementation of SYNC protocol |
| 43 | */ |
| 44 | namespace Sync { |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 45 | |
| 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 | */ |
| 51 | class CcnxWrapper { |
| 52 | private: |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 53 | ccn* m_handle; |
| 54 | ccn_keystore *m_keyStore; |
| 55 | ccn_charbuf *m_keyLoactor; |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 56 | // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex); |
| 57 | boost::recursive_mutex m_mutex; |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame] | 58 | boost::shared_ptr<boos::thread> m_thread; |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | void createKeyLocator(); |
| 62 | void initKeyStore(); |
| 63 | const ccn_pkey *getPrivateKey(); |
| 64 | const unsigned char *getPublicKeyDigest(); |
| 65 | ssize_t getPublicKeyDigestLength(); |
| 66 | void ccnLoop(); |
| 67 | |
| 68 | public: |
| 69 | |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 70 | /** |
| 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 Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 75 | CcnxWrapper(); |
| 76 | ~CcnxWrapper(); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 77 | |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 78 | /** |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 79 | * @brief send Interest; need to grab lock m_mutex first |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 80 | * |
| 81 | * @param strInterest the Interest name |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 82 | * @param dataCallback the callback function to deal with the returned data |
| 83 | * @return the return code of ccn_express_interest |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 84 | */ |
Zhenkai Zhu | 4106406 | 2012-03-05 23:49:07 -0800 | [diff] [blame] | 85 | int sendInterest(std::string strInterest, boost::function<void |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 86 | (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 Zhu | 774aa18 | 2012-03-05 19:54:38 -0800 | [diff] [blame] | 95 | int sendInterestFilter(std::string prefix, boost::function<void (std::string)> |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 96 | 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 Zhu | 8d935c8 | 2012-03-06 10:44:12 -0800 | [diff] [blame] | 107 | int publishData(std::string name, boost::shared_ptr<DataBuffer> dataBuffer, |
| 108 | int freshness); |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 109 | |
| 110 | }; |
| 111 | |
| 112 | } // Sync |
| 113 | |
| 114 | #endif // SYNC_STATE_H |