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 | |
| 34 | #include <boost/thread.hpp> |
| 35 | #include <boost/shared_ptr.hpp> |
Zhenkai Zhu | 774aa18 | 2012-03-05 19:54:38 -0800 | [diff] [blame] | 36 | #include <string> |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 37 | /** |
| 38 | * \defgroup sync SYNC protocol |
| 39 | * |
| 40 | * Implementation of SYNC protocol |
| 41 | */ |
| 42 | namespace Sync { |
| 43 | /** |
| 44 | * \ingroup sync |
| 45 | * @brief Data wrapper with size |
| 46 | */ |
| 47 | class DataBuffer { |
| 48 | private: |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame^] | 49 | unsigned char *m_buffer; |
| 50 | size_t m_len; |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 51 | public: |
| 52 | DataBuffer(const unsigned char *buffer, size_t len); |
| 53 | DataBuffer(const DataBuffer &dataBuffer); |
| 54 | DataBuffer &operator=(const DataBuffer &dataBuffer); |
| 55 | ~DataBuffer(); |
| 56 | size_t length() {return len;} |
| 57 | const unsigned char *buffer() { return const_cast<const unsigned char *> (buffer); } |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * \ingroup sync |
| 62 | * @brief A wrapper for ccnx library; clients of this code do not need to deal |
| 63 | * with ccnx library |
| 64 | */ |
| 65 | class CcnxWrapper { |
| 66 | private: |
Zhenkai Zhu | 406f37a | 2012-03-05 20:23:20 -0800 | [diff] [blame^] | 67 | ccn* m_handle; |
| 68 | ccn_keystore *m_keyStore; |
| 69 | ccn_charbuf *m_keyLoactor; |
| 70 | boost::mutex m_mutex; |
| 71 | boost::shared_ptr<boos::thread> m_thread; |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | void createKeyLocator(); |
| 75 | void initKeyStore(); |
| 76 | const ccn_pkey *getPrivateKey(); |
| 77 | const unsigned char *getPublicKeyDigest(); |
| 78 | ssize_t getPublicKeyDigestLength(); |
| 79 | void ccnLoop(); |
| 80 | |
| 81 | public: |
| 82 | |
| 83 | |
| 84 | CcnxWrapper(); |
| 85 | ~CcnxWrapper(); |
| 86 | /** |
| 87 | * @brief send Interest |
| 88 | * |
| 89 | * @param strInterest the Interest name |
| 90 | * @param callback the callback function to deal with the returned data |
| 91 | */ |
Zhenkai Zhu | 774aa18 | 2012-03-05 19:54:38 -0800 | [diff] [blame] | 92 | int sendInterest(std::string strInterest, boost::function<void (DataBuffer &)> processData); |
| 93 | int sendInterestFilter(std::string prefix, boost::function<void (std::string)> |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 94 | processInterest); |
Zhenkai Zhu | 774aa18 | 2012-03-05 19:54:38 -0800 | [diff] [blame] | 95 | int publishData(std::string name, DataBuffer &dataBuffer); |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 96 | |
| 97 | }; |
| 98 | |
| 99 | } // Sync |
| 100 | |
| 101 | #endif // SYNC_STATE_H |