blob: 87061885d0d5c72c5d027cb9db6cf325030488ab [file] [log] [blame]
/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2012 University of California, Los Angeles
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
* 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
* Alexander Afanasyev <alexander.afanasyev@ucla.edu>
*/
#ifndef SYNC_DATA_BUFFER_H
#define SYNC_DATA_BUFFER_H
#include <tinyxml.h>
#include <boost/shared_ptr.hpp>
#include <boost/exception/all.hpp>
#include "sync-diff-state.h"
#include "sync-full-state.h"
/**
* \defgroup sync SYNC protocol
*
* Implementation of SYNC protocol
*/
namespace Sync {
/**
* \ingroup sync
* @brief DataBuffer Interface to be used by CcnxWrapper and all other data
* publish/request/processing related functions
*/
class DataBuffer {
public:
virtual ~DataBuffer() = 0;
virtual size_t length() = 0;
virtual const unsigned char *buffer() = 0;
virtual void setBufferAndLength(const unsigned char *buffer, size_t len) =
0;
};
/**
* \ingroup sync
* @brief general Data Buffer class, implments DataBuffer interface mainly works for app data
*/
class DataBufferImpl : public DataBuffer {
public:
DataBufferImpl() {m_buffer = NULL; m_len = 0;}
DataBufferImpl(const unsigned char *buffer, size_t len);
DataBufferImpl(const DataBuffer *DataBuffer);
DataBufferImpl &operator=(const DataBuffer *DataBuffer);
/**
* @brief reset the buffer and len
*/
virtual void setBufferAndLength(const unsigned char *buffer, size_t len);
virtual ~DataBufferImpl();
virtual size_t length() {return m_len;}
virtual const unsigned char *buffer() { return const_cast<const unsigned
char *> (m_buffer); }
private:
unsigned char *m_buffer;
size_t m_len;
};
/**
* \ingroup sync
* @brief decorator class, wrapper for sync data; converts to and from states
*/
class SyncDataBuffer : DataBuffer{
public:
/**
* @brief decorates some object that implements DataBuffer interface
* primary usage here is to decorate AppDataBuffer
*/
SyncDataBuffer(boost::shared_ptr<DataBuffer> dataBuffer) { m_dataBuffer = dataBuffer;}
virtual ~SyncDataBuffer(){};
virtual size_t length() {m_dataBuffer->length();}
virtual const unsigned char *buffer() {m_dataBuffer->buffer();}
virtual void setBufferAndLength(const unsigned char *buffer, size_t len)
{m_dataBuffer->setBufferAndLength(buffer, len); }
/**
* @brief convert from FullState to SyncDataBuffer
*/
SyncDataBuffer &operator<<(FullState &fs);
/**
* @brief convert from DiffState to SyncDataBuffer
*/
SyncDataBuffer &operator<<(DiffState &ds);
/**
* @brief convert from SyncDataBuffer to FullState, throws
* SyncDataBufferConvertException if convert fails
*/
SyncDataBuffer &operator>>(FullState &fs);
/**
* @brief convert from SyncDataBuffer to DiffState, throws
* SyncDataBufferConvertException if convert fails
*/
SyncDataBuffer &operator>>(DiffState &ds);
private:
boost::shared_ptr<DataBuffer> m_dataBuffer;
};
struct SyncDataBufferConvertException : virtual boost::exception {};
} // Sync
#endif // SYNC_DATA_BUFFER_H