Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -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 | |
Zhenkai Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame^] | 23 | #ifndef SYNC_DATA_BUFFER_H |
| 24 | #define SYNC_DATA_BUFFER_H |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 25 | |
| 26 | #include <tinyxml.h> |
| 27 | #include <boost/shared_ptr.hpp> |
Zhenkai Zhu | 2f98612 | 2012-03-06 11:56:08 -0800 | [diff] [blame] | 28 | #include <boost/exception/all.hpp> |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 29 | #include "sync-diff-state.h" |
| 30 | #include "sync-full-state.h" |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * \defgroup sync SYNC protocol |
| 35 | * |
| 36 | * Implementation of SYNC protocol |
| 37 | */ |
| 38 | namespace Sync { |
| 39 | |
| 40 | /** |
| 41 | * \ingroup sync |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 42 | * @brief DataBuffer Interface to be used by CcnxWrapper and all other data |
| 43 | * publish/request/processing related functions |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 44 | */ |
| 45 | class DataBuffer { |
Zhenkai Zhu | dfab1d0 | 2012-03-05 23:42:30 -0800 | [diff] [blame] | 46 | public: |
| 47 | virtual ~DataBuffer() = 0; |
| 48 | virtual size_t length() = 0; |
| 49 | virtual const unsigned char *buffer() = 0; |
| 50 | virtual void setBufferAndLength(const unsigned char *buffer, size_t len) = |
| 51 | 0; |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * \ingroup sync |
| 56 | * @brief general Data Buffer class, mainly works for app data |
| 57 | */ |
| 58 | class AppDataBuffer:DataBuffer { |
| 59 | public: |
| 60 | AppDataBuffer() {m_buffer = NULL; m_len = 0;} |
| 61 | AppDataBuffer(const unsigned char *buffer, size_t len); |
| 62 | AppDataBuffer(const DataBuffer *DataBuffer); |
| 63 | AppDataBuffer &operator=(const DataBuffer *DataBuffer); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * @brief reset the buffer and len |
| 67 | */ |
Zhenkai Zhu | dfab1d0 | 2012-03-05 23:42:30 -0800 | [diff] [blame] | 68 | virtual void setBufferAndLength(const unsigned char *buffer, size_t len); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 69 | virtual ~AppDataBuffer(); |
| 70 | virtual size_t length() {return m_len;} |
| 71 | virtual const unsigned char *buffer() { return const_cast<const unsigned |
| 72 | char *> (m_buffer); } |
Zhenkai Zhu | dfab1d0 | 2012-03-05 23:42:30 -0800 | [diff] [blame] | 73 | |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 74 | private: |
| 75 | unsigned char *m_buffer; |
| 76 | size_t m_len; |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | |
| 80 | |
Zhenkai Zhu | dfab1d0 | 2012-03-05 23:42:30 -0800 | [diff] [blame] | 81 | /** |
| 82 | * \ingroup sync |
| 83 | * @brief decorator class, wrapper for sync data; converts to and from states |
| 84 | */ |
| 85 | class SyncDataBuffer : DataBuffer{ |
| 86 | public: |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 87 | /** |
| 88 | * @brief decorates some object that implements DataBuffer interface |
| 89 | * primary usage here is to decorate AppDataBuffer |
| 90 | */ |
Zhenkai Zhu | dfab1d0 | 2012-03-05 23:42:30 -0800 | [diff] [blame] | 91 | SyncDataBuffer(DataBuffer *dataBuffer) { m_dataBuffer = dataBuffer;} |
| 92 | virtual ~SyncDataBuffer(){}; |
| 93 | virtual size_t length() {m_dataBuffer->length();} |
| 94 | virtual const unsigned char *buffer() {m_dataBuffer->buffer();} |
| 95 | virtual void setBufferAndLength(const unsigned char *buffer, size_t len) |
| 96 | {m_dataBuffer->setBufferAndLength(buffer, len); } |
| 97 | |
Zhenkai Zhu | 01733f0 | 2012-03-06 13:56:26 -0800 | [diff] [blame] | 98 | /** |
| 99 | * @brief convert from FullState to SyncDataBuffer |
| 100 | */ |
Zhenkai Zhu | dfab1d0 | 2012-03-05 23:42:30 -0800 | [diff] [blame] | 101 | SyncDataBuffer &operator<<(FullState &fs); |
Zhenkai Zhu | 01733f0 | 2012-03-06 13:56:26 -0800 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * @brief convert from DiffState to SyncDataBuffer |
| 105 | */ |
Zhenkai Zhu | dfab1d0 | 2012-03-05 23:42:30 -0800 | [diff] [blame] | 106 | SyncDataBuffer &operator<<(DiffState &ds); |
| 107 | |
Zhenkai Zhu | 01733f0 | 2012-03-06 13:56:26 -0800 | [diff] [blame] | 108 | /** |
| 109 | * @brief convert from SyncDataBuffer to FullState, throws |
| 110 | * SyncDataBufferConvertException if convert fails |
| 111 | */ |
Zhenkai Zhu | 2f98612 | 2012-03-06 11:56:08 -0800 | [diff] [blame] | 112 | SyncDataBuffer &operator>>(FullState &fs); |
Zhenkai Zhu | 01733f0 | 2012-03-06 13:56:26 -0800 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * @brief convert from SyncDataBuffer to DiffState, throws |
| 116 | * SyncDataBufferConvertException if convert fails |
| 117 | */ |
Zhenkai Zhu | 2f98612 | 2012-03-06 11:56:08 -0800 | [diff] [blame] | 118 | SyncDataBuffer &operator>>(DiffState &ds); |
Zhenkai Zhu | dfab1d0 | 2012-03-05 23:42:30 -0800 | [diff] [blame] | 119 | |
| 120 | private: |
| 121 | boost::shared_ptr<DataBuffer> m_dataBuffer; |
| 122 | }; |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 123 | |
Zhenkai Zhu | 01733f0 | 2012-03-06 13:56:26 -0800 | [diff] [blame] | 124 | struct SyncDataBufferConvertException : virtual boost::exception {}; |
Zhenkai Zhu | 2f98612 | 2012-03-06 11:56:08 -0800 | [diff] [blame] | 125 | |
Zhenkai Zhu | 11a0ad4 | 2012-03-05 22:28:33 -0800 | [diff] [blame] | 126 | } // Sync |
| 127 | |
Zhenkai Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame^] | 128 | #endif // SYNC_DATA_BUFFER_H |