Merge branch 'master' of git.irl.cs.ucla.edu:ndn/sync
diff --git a/model/sync-app-data-publish.cc b/model/sync-app-data-publish.cc
new file mode 100644
index 0000000..2680859
--- /dev/null
+++ b/model/sync-app-data-publish.cc
@@ -0,0 +1,21 @@
+/* -*- 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>
+ */
diff --git a/model/sync-app-data-publish.h b/model/sync-app-data-publish.h
new file mode 100644
index 0000000..5d122dc
--- /dev/null
+++ b/model/sync-app-data-publish.h
@@ -0,0 +1,53 @@
+/* -*- 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_STATE_H
+#define SYNC_STATE_H
+#include <boost/shared_ptr.hpp>
+#include "sync-ccnx-wrapper.h"
+
+/**
+ * \defgroup sync SYNC protocol
+ *
+ * Implementation of SYNC protocol
+ */
+namespace Sync {
+
+/**
+ * \ingroup sync
+ * @brief publishes application data and keeps track of most recently published
+ * data
+ */
+class AppDataPublish
+{
+public:
+	std::pair<std::string, boost::shared_ptr<const DataBuffer> > getRecentData();
+	bool publishData(std::string name, const DataBufer &dataBuffer);
+  
+private:
+	boost::shared_ptr<CcnxWrapper> ccnxHandle;
+	std::pair<std::string, boost::shared_ptr<DataBuffer> > recentData;
+};
+
+} // Sync
+
+#endif // SYNC_STATE_H
diff --git a/model/sync-ccnx-wrapper.cc b/model/sync-ccnx-wrapper.cc
new file mode 100644
index 0000000..2680859
--- /dev/null
+++ b/model/sync-ccnx-wrapper.cc
@@ -0,0 +1,21 @@
+/* -*- 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>
+ */
diff --git a/model/sync-ccnx-wrapper.h b/model/sync-ccnx-wrapper.h
new file mode 100644
index 0000000..91f819e
--- /dev/null
+++ b/model/sync-ccnx-wrapper.h
@@ -0,0 +1,86 @@
+/* -*- 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_STATE_H
+#define SYNC_STATE_H
+
+extern "C" {
+#include <ccn/ccn.h>
+#include <ccn/charbuf.h>
+#include <ccn/keystore.h>
+#include <ccn/uri.h>
+#include <ccn/bloom.h>
+}
+
+#include <boost/thread.hpp>
+#include <boost/shared_ptr.hpp>
+#include <string>
+#include "sync-data-buffer.h"
+/**
+ * \defgroup sync SYNC protocol
+ *
+ * Implementation of SYNC protocol
+ */
+namespace Sync {
+
+/**
+ * \ingroup sync
+ * @brief A wrapper for ccnx library; clients of this code do not need to deal
+ * with ccnx library
+ */
+class CcnxWrapper { 
+private:
+	ccn* m_handle;
+	ccn_keystore *m_keyStore;
+	ccn_charbuf *m_keyLoactor;
+	boost::mutex m_mutex;
+	boost::shared_ptr<boos::thread> m_thread;
+
+private:
+	void createKeyLocator();
+	void initKeyStore();
+	const ccn_pkey *getPrivateKey();
+	const unsigned char *getPublicKeyDigest();
+	ssize_t getPublicKeyDigestLength();
+	void ccnLoop();
+
+public:
+  
+
+   CcnxWrapper();
+   ~CcnxWrapper();
+  /**
+   * @brief send Interest 
+   *
+   * @param strInterest the Interest name
+   * @param callback the callback function to deal with the returned data
+   */
+   int sendInterest(std::string strInterest, boost::function<void (DataBuffer &)> processData);
+   int sendInterestFilter(std::string prefix, boost::function<void (std::string)>
+   processInterest);
+   int publishData(std::string name, DataBuffer &dataBuffer);
+
+};
+
+} // Sync
+
+#endif // SYNC_STATE_H
diff --git a/model/sync-data-buffer.cc b/model/sync-data-buffer.cc
new file mode 100644
index 0000000..2680859
--- /dev/null
+++ b/model/sync-data-buffer.cc
@@ -0,0 +1,21 @@
+/* -*- 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>
+ */
diff --git a/model/sync-data-buffer.h b/model/sync-data-buffer.h
new file mode 100644
index 0000000..269b379
--- /dev/null
+++ b/model/sync-data-buffer.h
@@ -0,0 +1,99 @@
+/* -*- 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_STATE_H
+#define SYNC_STATE_H
+
+#include <tinyxml.h>
+#include <boost/shared_ptr.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
+ */
+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);
+	virtual void setBufferAndLength(const unsigned char *buffer, size_t len);
+	virtual ~DataBuffer();
+	virtual size_t length() {return len;}
+	virtual const unsigned char *buffer() { return const_cast<const unsigned char *> (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:
+	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); }
+
+	SyncDataBuffer &operator<<(FullState &fs);
+	SyncDataBuffer &operator<<(DiffState &ds);
+
+	FullState &operator>>(FullState &fs);
+	DiffState &operator>>(DiffState &ds);
+
+private:
+	boost::shared_ptr<DataBuffer> m_dataBuffer;
+};
+
+} // Sync
+
+#endif // SYNC_STATE_H
diff --git a/model/sync-interest-table.cc b/model/sync-interest-table.cc
new file mode 100644
index 0000000..2680859
--- /dev/null
+++ b/model/sync-interest-table.cc
@@ -0,0 +1,21 @@
+/* -*- 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>
+ */
diff --git a/model/sync-interest-table.h b/model/sync-interest-table.h
new file mode 100644
index 0000000..4993cd4
--- /dev/null
+++ b/model/sync-interest-table.h
@@ -0,0 +1,51 @@
+/* -*- 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_STATE_H
+#define SYNC_STATE_H
+#include <string>
+#include <boost/unordered_map.hpp>
+
+/**
+ * \defgroup sync SYNC protocol
+ *
+ * Implementation of SYNC protocol
+ */
+namespace Sync {
+
+/**
+ * \ingroup sync
+ * @brief A table to keep unanswered Sync Interest
+ */
+class SyncInterestTable
+{
+private:
+	boost::unordered_map<std::string, std::string> m_table;
+public:
+	bool insert(std::string interest);
+	boost::unordered_map<std::string, std::string> fetchAll();
+
+};
+
+} // Sync
+
+#endif // SYNC_STATE_H