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