blob: 09e15f49c0862423fe0fbbd3fca4d95a3929ff9d [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
23#ifndef SYNC_STATE_H
24#define SYNC_STATE_H
25
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;
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 */
58class AppDataBuffer:DataBuffer {
59public:
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 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 Zhu46b26a12012-03-06 13:51:16 -080069 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 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 Zhudfab1d02012-03-05 23:42:30 -080091 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 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
128#endif // SYNC_STATE_H