blob: 2b2a1bfe49560a893408321f59f9ae572aaf3c94 [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, mainly works for app data
*/
class AppDataBuffer:DataBuffer {
public:
AppDataBuffer() {m_buffer = NULL; m_len = 0;}
AppDataBuffer(const unsigned char *buffer, size_t len);
AppDataBuffer(const DataBuffer *DataBuffer);
AppDataBuffer &operator=(const DataBuffer *DataBuffer);
/**
* @brief reset the buffer and len
*/
virtual void setBufferAndLength(const unsigned char *buffer, size_t len);
virtual ~AppDataBuffer();
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(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