blob: 70e20a7587824f0447dd02b717209e177b987d67 [file] [log] [blame]
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -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_DATA_BUFFER_H
24#define SYNC_DATA_BUFFER_H
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080025
26#include <tinyxml.h>
27#include <boost/shared_ptr.hpp>
Zhenkai Zhu2f986122012-03-06 11:56:08 -080028#include <boost/exception/all.hpp>
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080029#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 */
38namespace Sync {
39
40/**
41 * \ingroup sync
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080042 * @brief DataBuffer Interface to be used by CcnxWrapper and all other data
43 * publish/request/processing related functions
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080044 */
45class DataBuffer {
Zhenkai Zhudfab1d02012-03-05 23:42:30 -080046public:
47 virtual ~DataBuffer() = 0;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080048 virtual size_t length() = 0;
Zhenkai Zhudfab1d02012-03-05 23:42:30 -080049 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
Zhenkai Zhu7f857982012-03-06 23:38:37 -080056 * @brief general Data Buffer class, implments DataBuffer interface mainly works for app data
Zhenkai Zhudfab1d02012-03-05 23:42:30 -080057 */
Zhenkai Zhu7f857982012-03-06 23:38:37 -080058class DataBufferImpl:DataBuffer {
Zhenkai Zhudfab1d02012-03-05 23:42:30 -080059public:
Zhenkai Zhu7f857982012-03-06 23:38:37 -080060 DataBufferImpl() {m_buffer = NULL; m_len = 0;}
61 DataBufferImpl(const unsigned char *buffer, size_t len);
62 DataBufferImpl(const DataBuffer *DataBuffer);
63 DataBufferImpl &operator=(const DataBuffer *DataBuffer);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080064
65 /**
66 * @brief reset the buffer and len
67 */
Zhenkai Zhudfab1d02012-03-05 23:42:30 -080068 virtual void setBufferAndLength(const unsigned char *buffer, size_t len);
Zhenkai Zhu7f857982012-03-06 23:38:37 -080069 virtual ~DataBufferImpl();
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080070 virtual size_t length() {return m_len;}
71 virtual const unsigned char *buffer() { return const_cast<const unsigned
72 char *> (m_buffer); }
Zhenkai Zhudfab1d02012-03-05 23:42:30 -080073
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080074private:
75 unsigned char *m_buffer;
76 size_t m_len;
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080077};
78
79
80
Zhenkai Zhudfab1d02012-03-05 23:42:30 -080081/**
82 * \ingroup sync
83 * @brief decorator class, wrapper for sync data; converts to and from states
84 */
85class SyncDataBuffer : DataBuffer{
86public:
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080087 /**
88 * @brief decorates some object that implements DataBuffer interface
89 * primary usage here is to decorate AppDataBuffer
90 */
Zhenkai Zhu7f857982012-03-06 23:38:37 -080091 SyncDataBuffer(boost::share_ptr<DataBuffer> dataBuffer) { m_dataBuffer = dataBuffer;}
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080092 virtual ~SyncDataBuffer(){};
Zhenkai Zhudfab1d02012-03-05 23:42:30 -080093 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 Zhu01733f02012-03-06 13:56:26 -080098 /**
99 * @brief convert from FullState to SyncDataBuffer
100 */
Zhenkai Zhudfab1d02012-03-05 23:42:30 -0800101 SyncDataBuffer &operator<<(FullState &fs);
Zhenkai Zhu01733f02012-03-06 13:56:26 -0800102
103 /**
104 * @brief convert from DiffState to SyncDataBuffer
105 */
Zhenkai Zhudfab1d02012-03-05 23:42:30 -0800106 SyncDataBuffer &operator<<(DiffState &ds);
107
Zhenkai Zhu01733f02012-03-06 13:56:26 -0800108 /**
109 * @brief convert from SyncDataBuffer to FullState, throws
110 * SyncDataBufferConvertException if convert fails
111 */
Zhenkai Zhu2f986122012-03-06 11:56:08 -0800112 SyncDataBuffer &operator>>(FullState &fs);
Zhenkai Zhu01733f02012-03-06 13:56:26 -0800113
114 /**
115 * @brief convert from SyncDataBuffer to DiffState, throws
116 * SyncDataBufferConvertException if convert fails
117 */
Zhenkai Zhu2f986122012-03-06 11:56:08 -0800118 SyncDataBuffer &operator>>(DiffState &ds);
Zhenkai Zhudfab1d02012-03-05 23:42:30 -0800119
120private:
121 boost::shared_ptr<DataBuffer> m_dataBuffer;
122};
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -0800123
Zhenkai Zhu01733f02012-03-06 13:56:26 -0800124struct SyncDataBufferConvertException : virtual boost::exception {};
Zhenkai Zhu2f986122012-03-06 11:56:08 -0800125
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -0800126} // Sync
127
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800128#endif // SYNC_DATA_BUFFER_H