blob: f471b9a5852dc886c2d6d03cea8c0a39a9f5d8fd [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 Zhudfab1d02012-03-05 23:42:30 -080042 * @brief DataBuffer Interface
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080043 */
44class DataBuffer {
Zhenkai Zhudfab1d02012-03-05 23:42:30 -080045public:
46 virtual ~DataBuffer() = 0;
47 virtual size_t length() = 0;
48 virtual const unsigned char *buffer() = 0;
49 virtual void setBufferAndLength(const unsigned char *buffer, size_t len) =
50 0;
51};
52
53/**
54 * \ingroup sync
55 * @brief general Data Buffer class, mainly works for app data
56 */
57class AppDataBuffer:DataBuffer {
58public:
59 AppDataBuffer() {m_buffer = NULL; m_len = 0;}
60 AppDataBuffer(const unsigned char *buffer, size_t len);
61 AppDataBuffer(const DataBuffer *DataBuffer);
62 AppDataBuffer &operator=(const DataBuffer *DataBuffer);
63 virtual void setBufferAndLength(const unsigned char *buffer, size_t len);
64 virtual ~DataBuffer();
65 virtual size_t length() {return len;}
66 virtual const unsigned char *buffer() { return const_cast<const unsigned char *> (buffer); }
67
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080068private:
69 unsigned char *m_buffer;
70 size_t m_len;
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080071};
72
73
74
Zhenkai Zhudfab1d02012-03-05 23:42:30 -080075/**
76 * \ingroup sync
77 * @brief decorator class, wrapper for sync data; converts to and from states
78 */
79class SyncDataBuffer : DataBuffer{
80public:
81 SyncDataBuffer(DataBuffer *dataBuffer) { m_dataBuffer = dataBuffer;}
82 virtual ~SyncDataBuffer(){};
83 virtual size_t length() {m_dataBuffer->length();}
84 virtual const unsigned char *buffer() {m_dataBuffer->buffer();}
85 virtual void setBufferAndLength(const unsigned char *buffer, size_t len)
86 {m_dataBuffer->setBufferAndLength(buffer, len); }
87
88 SyncDataBuffer &operator<<(FullState &fs);
89 SyncDataBuffer &operator<<(DiffState &ds);
90
Zhenkai Zhu2f986122012-03-06 11:56:08 -080091 SyncDataBuffer &operator>>(FullState &fs);
92 SyncDataBuffer &operator>>(DiffState &ds);
Zhenkai Zhudfab1d02012-03-05 23:42:30 -080093
94private:
95 boost::shared_ptr<DataBuffer> m_dataBuffer;
96};
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -080097
Zhenkai Zhu2f986122012-03-06 11:56:08 -080098struct SyncDataBufferConvertError : virtual boost::exception {};
99
Zhenkai Zhu11a0ad42012-03-05 22:28:33 -0800100} // Sync
101
102#endif // SYNC_STATE_H